Blog

 
1:   using System;
2:   using System.IO;
3:  
4:   using iTextSharp.text;
5:   using iTextSharp.text.pdf;
6:  
7:  
8:   namespace iTextSharp.tutorial.Chap02
9:   {
10:       /// <summary>
11:       /// Paragraph
12:       /// </summary>
13:       public class Chap0205
14:       {
15:           public Chap0205()
16:           {
17:               Console.WriteLine("Chapter 2 example 5: Paragraphs");
18:           
19:               // step 1: creation of a document-object
20:               Document document new Document();
21:           
22:               try 
23:               {
24:               
25:                   // step 2:
26:                   // we create a writer that listens to the document
27:                   // and directs a PDF-stream to a file
28:                   PdfWriter.GetInstance(documentnew FileStream("Chap0205.pdf"FileMode.Create));
29:               
30:                   // step 3: we open the document
31:                   document.Open();
32:               
33:                   // step 4: we Add a paragraph to the document
34:                   Paragraph p1 new Paragraph(new Chunk("This is my first paragraph. ",
35:                       FontFactory.GetFont(FontFactory.HELVETICA10)));
36:                   p1.Add("The leading of this paragraph is calculated automagically. ");
37:                   p1.Add("The default leading is 1.5 times the fontsize. ");
38:                   p1.Add(new Chunk("You can Add chunks "));
39:                   p1.Add(new Phrase("or you can Add phrases. "));
40:                   p1.Add(new Phrase("Unless you change the leading with the method setLeading, the leading doesn't change if you Add text with another leading. This can lead to some problems."FontFactory.GetFont(FontFactory.HELVETICA18)));
41:                   document.Add(p1);
42:                   Paragraph p2 new Paragraph(new Phrase("This is my second paragraph. ",
43:                       FontFactory.GetFont(FontFactory.HELVETICA12)));
44:                   p2.Add("As you can see, it started on a new line.");
45:                   document.Add(p2);
46:                   Paragraph p3 new Paragraph("This is my third paragraph.",
47:                       FontFactory.GetFont(FontFactory.HELVETICA12));
48:                   document.Add(p3);
49:               }
50:               catch(DocumentException de
51:               {
52:                   Console.Error.WriteLine(de.Message);
53:               }
54:               catch(IOException ioe
55:               {
56:                   Console.Error.WriteLine(ioe.Message);
57:               }
58:           
59:               // step 5: we close the document
60:               document.Close();
61:  
62:           }
63:       }
64:   }