Blog

 
1:   using System;
2:   using System.IO;
3:  
4:   using iTextSharp.text;
5:   using iTextSharp.text.pdf;
6:  
7:  
8:   namespace iTextSharp.tutorial.Chap04
9:   {
10:       /// <summary>
11:       /// Chapters and Sections
12:       /// </summary>
13:       public class Chap0402
14:       {
15:           public Chap0402()
16:           {
17:               Console.WriteLine("Chapter 4 example 2: Chapters and Sections");
18:           
19:               // step 1: creation of a document-object
20:               Document document = new Document(PageSize.A4, 50, 50, 50, 50);
21:               try 
22:               {
23:                   // step 2: we create a writer that listens to the document
24:                   PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("Chap0402.pdf", FileMode.Create));
25:                   // step 3: we open the document
26:                   document.Open();
27:                   // step 4: we Add content to the document
28:                   // we define some fonts
29:                   Font chapterFont = FontFactory.GetFont(FontFactory.HELVETICA, 24, Font.NORMAL, new Color(255, 0, 0));
30:                   Font sectionFont = FontFactory.GetFont(FontFactory.HELVETICA, 20, Font.NORMAL, new Color(0, 0, 255));
31:                   Font subsectionFont = FontFactory.GetFont(FontFactory.HELVETICA, 18, Font.BOLD, new Color(0, 64, 64));
32:                   // we create some paragraphs
33:                   Paragraph blahblah = new Paragraph("blah blah blah blah blah blah blaah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah");
34:                   Paragraph blahblahblah = new Paragraph("blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blaah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah");
35:                   // this loop will create 7 chapters
36:                   for (int i = 1; i < 8; i++) 
37:                   {
38:                       Paragraph cTitle = new Paragraph("This is chapter " + i, chapterFont);
39:                       Chapter chapter = new Chapter(cTitle, i);
40:                   
41:                       if (i == 4) 
42:                       {
43:                           blahblahblah.Alignment = Element.ALIGN_JUSTIFIED;
44:                           blahblah.Alignment = Element.ALIGN_JUSTIFIED;
45:                           chapter.Add(blahblah);
46:                       }
47:                       if (i == 5) 
48:                       {
49:                           blahblahblah.Alignment = Element.ALIGN_CENTER;
50:                           blahblah.Alignment = Element.ALIGN_RIGHT;
51:                           chapter.Add(blahblah);
52:                       }
53:                       // Add a table in the 6th chapter
54:                       if (i == 6) 
55:                       {
56:                           blahblah.Alignment = Element.ALIGN_JUSTIFIED;
57:                       }
58:                       // in every chapter 3 sections will be Added
59:                       for (int j = 1; j < 4; j++) 
60:                       {
61:                           Paragraph sTitle = new Paragraph("This is section " + j + " in chapter " + i, sectionFont);
62:                           Section section = chapter.AddSection(sTitle, 1);
63:                           // in all chapters except the 1st one, some extra text is Added to section 3
64:                           if (j == 3 && i > 1) 
65:                           {
66:                               section.Add(blahblah);
67:                           }
68:                           // in every section 3 subsections are Added
69:                           for (int k = 1; k < 4; k++) 
70:                           {
71:                               Paragraph subTitle = new Paragraph("This is subsection " + k + " of section " + j, subsectionFont);
72:                               Section subsection = section.AddSection(subTitle, 3);
73:                               if (k == 1 && j == 3) 
74:                               {
75:                                   subsection.Add(blahblahblah);
76:                               }
77:                               subsection.Add(blahblah);
78:                           }
79:                           if (j == 2 && i > 2) 
80:                           {
81:                               section.Add(blahblahblah);
82:                           }
83:                       }
84:                       document.Add(chapter);
85:                   }
86:               }
87:               catch(Exception de) 
88:               {
89:                   Console.Error.WriteLine(de.StackTrace);
90:               }
91:               // step 5: we close the document
92:               document.Close();
93:  
94:           }
95:       }
96:   }