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:       public class Chap1011 
10:       {
11:       
12:           public Chap1011() 
13:           {
14:           
15:               Console.WriteLine("Chapter 10 example 11: a PdfPTable in a template");
16:               
17:               // step 1: creation of a document-object
18:               Rectangle rect new Rectangle(PageSize.A4);
19:               rect.BackgroundColor new Color(23822188);
20:               Document document new Document(rect50505050);
21:               try 
22:               {
23:                   // step 2: we create a writer that listens to the document
24:                   PdfWriter writer PdfWriter.GetInstance(documentnew FileStream("Chap1011.pdf"FileMode.Create));
25:                   // step 3: we open the document
26:                   document.Open();
27:                   // step 4:
28:                   PdfTemplate template writer.DirectContent.CreateTemplate(2020);
29:                   BaseFont bf BaseFont.CreateFont(BaseFont.HELVETICABaseFont.WINANSIBaseFont.NOT_EMBEDDED);
30:                   String text "Vertical";
31:                   float size 16;
32:                   float width bf.GetWidthPoint(textsize);
33:                   template.BeginText();
34:                   template.SetRGBColorFillF(111);
35:                   template.SetFontAndSize(bfsize);
36:                   template.SetTextMatrix(02);
37:                   template.ShowText(text);
38:                   template.EndText();
39:                   template.Width width;
40:                   template.Height size 2;
41:                   Image img Image.GetInstance(template);
42:                   img.RotationDegrees=90;
43:                   Chunk ck new Chunk(img00);
44:                   PdfPTable table new PdfPTable(3);
45:                   table.WidthPercentage 100;
46:                   table.DefaultCell.HorizontalAlignment Element.ALIGN_CENTER;
47:                   table.DefaultCell.VerticalAlignment Element.ALIGN_MIDDLE;
48:                   PdfPCell cell new PdfPCell(img);
49:                   cell.Padding 4;
50:                   cell.BackgroundColor new Color(00255);
51:                   cell.HorizontalAlignment Element.ALIGN_CENTER;
52:                   table.AddCell("I see a template on my right");
53:                   table.AddCell(cell);
54:                   table.AddCell("I see a template on my left");
55:                   table.AddCell(cell);
56:                   table.AddCell("I see a template everywhere");
57:                   table.AddCell(cell);
58:                   table.AddCell("I see a template on my right");
59:                   table.AddCell(cell);
60:                   table.AddCell("I see a template on my left");
61:               
62:                   Paragraph p1 new Paragraph("This is a template ");
63:                   p1.Add(ck);
64:                   p1.Add(" just here.");
65:                   p1.Leading img.ScaledHeight 1.1f;
66:                   document.Add(p1);
67:                   document.Add(table);
68:                   Paragraph p2 new Paragraph("More templates ");
69:                   p2.Leading img.ScaledHeight 1.1f;
70:                   p2.Alignment Element.ALIGN_JUSTIFIED;
71:                   img.ScalePercent(70);
72:                   for (int 020; ++k)
73:                       p2.Add(ck);
74:                   document.Add(p2);
75:                   // step 5: we close the document
76:                   document.Close();
77:               }
78:               catch (Exception de
79:               {
80:                   Console.Error.WriteLine(de.Message);
81:                   Console.Error.WriteLine(de.StackTrace);
82:               }
83:           }
84:       }
85:   }