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:       /// Phrase
11:       /// </summary>
12:       public class Chap0202
13:       {
14:           public Chap0202()
15:           {
16:               Console.WriteLine("Chapter 2 example 2: Phrases");
17:           
18:               // step 1: creation of a document-object
19:               Document document new Document();
20:           
21:               try 
22:               {
23:               
24:                   // step 2:
25:                   // we create a writer that listens to the document
26:                   // and directs a PDF-stream to a file
27:                   PdfWriter.GetInstance(documentnew FileStream("Chap0202.pdf",FileMode.Create));
28:               
29:                   // step 3: we open the document
30:                   document.Open();
31:               
32:                   // step 4: we Add a paragraph to the document
33:                   Phrase phrase0 new Phrase();
34:                   Phrase phrase1 new Phrase("(1) this is a phrase\n");
35:                   // In this example the leading is passed as a parameter
36:                   Phrase phrase2 new Phrase(24"(2) this is a phrase with leading 24. You can only see the difference if the line is long enough. Do you see it? There is more space between this line and the previous one.\n");
37:                   // When a Font is passed (explicitely or embedded in a chunk),
38:                   // the default leading = 1.5 * size of the font
39:                   Phrase phrase3 new Phrase("(3) this is a phrase with a red, normal font Courier, size 20. As you can see the leading is automatically changed.\n"FontFactory.GetFont(FontFactory.COURIER20Font.NORMALnew Color(25500)));
40:                   Phrase phrase4 new Phrase(new Chunk("(4) this is a phrase\n"));
41:                   Phrase phrase5 new Phrase(18new Chunk("(5) this is a phrase in Helvetica, bold, red and size 16 with a given leading of 18 points.\n"FontFactory.GetFont(FontFactory.HELVETICA16Font.BOLDnew Color(25500))));
42:                   // A Phrase can contains several chunks with different fonts
43:                   Phrase phrase6 new Phrase("(6)");
44:                   Chunk chunk new Chunk(" This is a font: ");
45:                   phrase6.Add(chunk);
46:                   phrase6.Add(new Chunk("Helvetica"FontFactory.GetFont(FontFactory.HELVETICA12)));
47:                   phrase6.Add(chunk);
48:                   phrase6.Add(new Chunk("Times New Roman"FontFactory.GetFont(FontFactory.TIMES_ROMAN12)));
49:                   phrase6.Add(chunk);
50:                   phrase6.Add(new Chunk("Courier"FontFactory.GetFont(FontFactory.COURIER12)));
51:                   phrase6.Add(chunk);
52:                   phrase6.Add(new Chunk("Symbol"FontFactory.GetFont(FontFactory.SYMBOL12)));
53:                   phrase6.Add(chunk);
54:                   phrase6.Add(new Chunk("ZapfDingBats"FontFactory.GetFont(FontFactory.ZAPFDINGBATS12)));
55:                   Phrase phrase7 new Phrase("(7) if you don't Add a newline yourself, all phrases are glued to eachother!");
56:               
57:                   document.Add(phrase1);
58:                   document.Add(phrase2);
59:                   document.Add(phrase3);
60:                   document.Add(phrase4);
61:                   document.Add(phrase5);
62:                   document.Add(phrase6);
63:                   document.Add(phrase7);
64:               
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:       }
80:   }