Blog

 
1:   using System;
2:   using System.IO;
3:  
4:   using iTextSharp.text;
5:   using iTextSharp.text.pdf;
6:  
7:   namespace iTextSharp.tutorial.Chap02
8:   {
9:       /// <summary>
10:       /// Chap0201a
11:       /// </summary>
12:       public class Chap0201a
13:       {
14:           public Chap0201a()
15:           {
16:               Console.WriteLine("Chapter 2 example 1: Horizontal space in Paragraphs");
17:               Document.Compress true;
18:               // step 1: creation of a document-object
19:               Document document new Document();
20:  
21:  
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.GetInstance(documentnew FileStream("Chap0201a.pdf",FileMode.Create));
30:  
31:                   // step 3: we open the document
32:                   document.Open();
33:  
34:                   // step 4: we add content to the document
35:                   Font[] fonts new Font[14];
36:                   fonts[13] = FontFactory.GetFont(FontFactory.COURIER7Font.NORMAL);
37:                   fonts[12] = FontFactory.GetFont(FontFactory.COURIER14Font.BOLD);
38:                   fonts[11] = FontFactory.GetFont(FontFactory.COURIER8Font.ITALIC);
39:                   fonts[10] = FontFactory.GetFont(FontFactory.COURIER13Font.BOLD Font.ITALIC);
40:                   fonts[9] = FontFactory.GetFont(FontFactory.HELVETICA9Font.NORMAL);
41:                   fonts[8] = FontFactory.GetFont(FontFactory.HELVETICA12Font.BOLD);
42:                   fonts[7] = FontFactory.GetFont(FontFactory.HELVETICA12Font.ITALIC);
43:                   fonts[6] = FontFactory.GetFont(FontFactory.HELVETICA11Font.BOLD Font.ITALIC);
44:                   fonts[5] = FontFactory.GetFont(FontFactory.TIMES_ROMAN12Font.NORMAL);
45:                   fonts[4] = FontFactory.GetFont(FontFactory.TIMES_ROMAN16Font.BOLD);
46:                   fonts[3] = FontFactory.GetFont(FontFactory.TIMES_ROMAN12Font.ITALIC);
47:                   fonts[2] = FontFactory.GetFont(FontFactory.TIMES_ROMAN32Font.BOLD Font.ITALIC);
48:                   fonts[1] = FontFactory.GetFont(FontFactory.SYMBOL5Font.NORMAL);
49:                   fonts[0] = FontFactory.GetFont(FontFactory.ZAPFDINGBATS5Font.NORMAL);
50:                   for (int 014i++) 
51:                   {
52:                       Chunk chunk new Chunk("This is some text in some"fonts[i]);
53:                       document.Add(new Phrase(chunk));
54:                       document.Add(new Phrase(new Chunk(" font. ",
55:                           fonts[i]).SetTextRise((== 0) ? -6)));
56:                   }
57:                   document.Add(new Phrase(new Chunk("This text is underlined ",
58:                       FontFactory.GetFont(FontFactory.HELVETICA12Font.UNDERLINE))));
59:                   document.Add(new Phrase(new Chunk("This font is of type ITALIC | STRIKETHRU ",
60:                       FontFactory.GetFont(FontFactory.HELVETICA12Font.ITALIC Font.STRIKETHRU))));
61:                   Chunk ck new Chunk(" This text has a yellow background color "FontFactory.GetFont(FontFactory.HELVETICA12));
62:                   ck.SetBackground(new Color(0xFF0xFF0x00));
63:                   document.Add(new Phrase(ck));
64:  
65:                   document.Add(new Chunk("text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text"));
66:  
67:  
68:                   Image img Image.GetInstance("pngnow.png");
69:                   ck new Chunk(img00);
70:                   Phrase p1 new Phrase("This is an image (not) hanging into the above text");
71:                   p1.Add(ck);
72:                   p1.Add(" just here.");
73:                   document.Add(p1);
74:  
75:                   p1 new Phrase();
76:                   ck new Chunk(" As we can see the clashing is fixed for Images and"); ck.SetBackground(new Color(0xFF0xFF0x00));    p1.Add(ck);
77:                   ck new Chunk(" oversized font chunks"fonts[2]); ck.SetBackground(new Color(0xFF0xFF0x00));    p1.Add(ck);
78:                   ck new Chunk(", but no change of leading is introduced by "); ck.SetBackground(new Color(0xFF0xFF0x00));    p1.Add(ck);
79:                   ck new Chunk("risen "fonts[7]);    ck.SetBackground(new Color(0xFF0xFF0x00));    p1.Add(ck);
80:                   ck new Chunk("(i.e. ");ck.SetBackground(new Color(0xFF0xFF0x00)); p1.Add(ck);
81:                   ck new Chunk("subscript").SetTextRise(-9);ck.SetBackground(new Color(0xFF0xFF0x00)); p1.Add(ck);
82:                   ck new Chunk(" or "); ck.SetBackground(new Color(0xFF0xFF0x00)); p1.Add(ck);
83:                   ck new Chunk("superscirpt").SetTextRise(9); ck.SetBackground(new Color(0xFF0xFF0x00)); p1.Add(ck);
84:                   ck new Chunk(") text! "); ck.SetBackground(new Color(0xFF0xFF0x00)); p1.Add(ck);
85:                   document.Add(p1);
86:  
87:                   document.Add(new Chunk(" text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text"));
88:  
89:                   ck new Chunk(img0, -19);
90:                   p1 new Phrase("This is an image (not) hanging in the subsequent text");
91:                   p1.Add(ck);
92:                   p1.Add(" just here.");
93:                   document.Add(p1);
94:  
95:                   document.Add(new Chunk("text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text"));
96:  
97:                   ck new Chunk(img0, -7);
98:                   p1 new Phrase("This is an image aligned to 'its' baseline ");
99:                   p1.Add(ck);
100:                   p1.Add(" just here.");
101:                   document.Add(p1);
102:  
103:                   document.Add(new Chunk("text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text"));
104:  
105:  
106:               }
107:               catch(DocumentException de
108:               {
109:                   Console.WriteLine(de.Message);
110:               }
111:               catch(IOException ioe
112:               {
113:                   Console.WriteLine(ioe.Message);
114:               }
115:  
116:               // step 5: we close the document
117:               document.Close();
118:           }
119:       }
120:   }