Blog

 
1:   using System;
2:   using System.IO;
3:  
4:   using iTextSharp.text;
5:   using iTextSharp.text.pdf;
6:  
7:   namespace iTextSharp.tutorial.Chap10
8:   {
9:       /// <summary>
10:       /// Templates
11:       /// </summary>
12:       public class Chap1003 
13:       {
14:       
15:           public Chap1003() 
16:           {
17:           
18:               Console.WriteLine("Chapter 10 example 3: Templates");
19:           
20:               // step 1: creation of a document-object
21:               Document document new Document();
22:           
23:               try 
24:               {
25:               
26:                   // step 2:
27:                   // we create a writer that listens to the document
28:                   // and directs a PDF-stream to a file
29:                   PdfWriter writer PdfWriter.GetInstance(documentnew FileStream("Chap1003.pdf"FileMode.Create));
30:               
31:                   // step 3: we open the document
32:                   document.Open();
33:               
34:                   // step 4: we grab the ContentByte and do some stuff with it
35:                   PdfContentByte cb writer.DirectContent;
36:               
37:                   // we create a PdfTemplate
38:                   PdfTemplate template cb.CreateTemplate(500200);
39:               
40:                   // we add some graphics
41:                   template.MoveTo(0200);
42:                   template.LineTo(5000);
43:                   template.Stroke();
44:                   template.SetRGBColorStrokeF(255f0f0f);
45:                   template.Circle(250f100f80f);
46:                   template.Stroke();
47:               
48:                   // we add some text
49:                   template.BeginText();
50:                   BaseFont bf BaseFont.CreateFont(BaseFont.HELVETICABaseFont.CP1252BaseFont.NOT_EMBEDDED);
51:                   template.SetFontAndSize(bf12);
52:                   template.SetTextMatrix(100100);
53:                   template.ShowText("Text at the position 100,100 (relative to the template!)");
54:                   template.EndText();
55:               
56:                   // we add the template on different positions
57:                   cb.AddTemplate(template00);
58:                   cb.AddTemplate(template01, -10500200);
59:                   cb.AddTemplate(template.5f00.5f100400);
60:               
61:                   // we go to a new page
62:                   document.NewPage();
63:                   cb.AddTemplate(template0400);
64:                   cb.AddTemplate(template2002, -200400);
65:               }
66:               catch(DocumentException de
67:               {
68:                   Console.Error.WriteLine(de.Message);
69:               }
70:               catch(IOException ioe
71:               {
72:                   Console.Error.WriteLine(ioe.Message);
73:               }
74:           
75:               // step 5: we close the document
76:               document.Close();
77:           }
78:       }
79:   }