Blog

 
1:   using System;
2:   using System.IO;
3:  
4:   using iTextSharp.text;
5:   using iTextSharp.text.pdf;
6:  
7:   namespace iTextSharp.tutorial.Chap06
8:   {
9:       /// <summary>
10:       /// Images and other objects:
11:       /// Images inside a Table
12:       /// You can add an image to a Cell, but because of the technical complexity, 
13:       /// you have to take two side-effects into account:
14:       /// The width of a Table is calculated internally. 
15:       /// If an image doesn't fit the cell, the image is 'scaled-to-fit' automatically. 
16:       /// You can't use Image.TEXTWRAP or Image.UNDERLYING. 
17:       /// </summary>
18:       public class Chap0615 
19:       {
20:           public Chap0615() 
21:           {
22:               Console.WriteLine("Chapter 6 example 15: images in tables");
23:               // step 1: creation of a document-object
24:               Document document = new Document();
25:               try 
26:               {
27:                   // step 2:
28:                   // we create a writer that listens to the document
29:                   // and directs a PDF-stream to a file
30:                   PdfWriter.GetInstance(document, new FileStream("Chap0615.pdf", FileMode.Create));
31:                   // step 3: we open the document
32:                   document.Open();
33:                   // step 4: we create a table and add it to the document
34:                   Image img0 = Image.GetInstance("myKids.jpg");
35:                   img0.Alignment = Image.MIDDLE_ALIGN;
36:                   Image img1 = Image.GetInstance("pngnow.png");
37:                   img1.Alignment = Image.LEFT_ALIGN | Image.UNDERLYING;
38:                   Image img2 = Image.GetInstance("pngnow.png");
39:                   img2.Alignment = Image.RIGHT_ALIGN | Image.TEXTWRAP;
40:                   Image img3 = Image.GetInstance("pngnow.png");
41:                   img3.Alignment = Image.LEFT_ALIGN;
42:                   Image img4 = Image.GetInstance("pngnow.png");
43:                   img4.Alignment = Image.MIDDLE_ALIGN;
44:                   Image img5 = Image.GetInstance("pngnow.png");
45:                   img5.Alignment = Image.RIGHT_ALIGN;
46:                   Table table = new Table(3);
47:                   table.Padding = 2;
48:                   table.DefaultHorizontalAlignment = Element.ALIGN_CENTER;
49:                   // row 1
50:                   table.AddCell("I see an image\non my right");
51:                   Cell cell = new Cell("This is the image (aligned in the middle):");
52:                   cell.BackgroundColor = new Color(0xC0, 0xC0, 0xC0);
53:                   cell.Add(img0);
54:                   cell.Add(new Phrase("This was the image"));
55:                   table.AddCell(cell);
56:                   table.AddCell("I see an image\non my left");
57:                   // row 2
58:                   cell = new Cell("This is the image (left aligned):");
59:                   cell.Add(img1);
60:                   cell.Add(new Phrase("This was the image"));
61:                   table.AddCell(cell);
62:                   table.AddCell("I see images\neverywhere");
63:                   cell = new Cell("This is the image (right aligned):");
64:                   cell.Add(img2);
65:                   cell.Add(new Phrase("This was the image"));
66:                   table.AddCell(cell);
67:                   // row 3
68:                   table.AddCell("I see an image\non my right");
69:                   cell = new Cell(img3);
70:                   cell.Add(img4);
71:                   cell.Add(img5);
72:                   table.AddCell(cell);
73:                   table.AddCell("I see an image\non my left");
74:                   document.Add(table);
75:               }
76:               catch(DocumentException de) 
77:               {
78:                   Console.Error.WriteLine(de.Message);
79:               }
80:               catch(IOException ioe) 
81:               {
82:                   Console.Error.WriteLine(ioe.Message);
83:               }
84:               // step 5: we close the document
85:               document.Close();
86:           }
87:       }
88:   }