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.A450505050);
21:               try 
22:               {
23:                   // step 2: we create a writer that listens to the document
24:                   PdfWriter writer PdfWriter.GetInstance(documentnew 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.HELVETICA24Font.NORMALnew Color(25500));
30:                   Font sectionFont FontFactory.GetFont(FontFactory.HELVETICA20Font.NORMALnew Color(00255));
31:                   Font subsectionFont FontFactory.GetFont(FontFactory.HELVETICA18Font.BOLDnew Color(06464));
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 18i++) 
37:                   {
38:                       Paragraph cTitle new Paragraph("This is chapter " ichapterFont);
39:                       Chapter chapter new Chapter(cTitlei);
40:                   
41:                       if (== 4
42:                       {
43:                           blahblahblah.Alignment Element.ALIGN_JUSTIFIED;
44:                           blahblah.Alignment Element.ALIGN_JUSTIFIED;
45:                           chapter.Add(blahblah);
46:                       }
47:                       if (== 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 (== 6
55:                       {
56:                           blahblah.Alignment Element.ALIGN_JUSTIFIED;
57:                       }
58:                       // in every chapter 3 sections will be Added
59:                       for (int 14j++) 
60:                       {
61:                           Paragraph sTitle new Paragraph("This is section " " in chapter " isectionFont);
62:                           Section section chapter.AddSection(sTitle1);
63:                           // in all chapters except the 1st one, some extra text is Added to section 3
64:                           if (== && 1
65:                           {
66:                               section.Add(blahblah);
67:                           }
68:                           // in every section 3 subsections are Added
69:                           for (int 14k++) 
70:                           {
71:                               Paragraph subTitle new Paragraph("This is subsection " " of section " jsubsectionFont);
72:                               Section subsection section.AddSection(subTitle3);
73:                               if (== && == 3
74:                               {
75:                                   subsection.Add(blahblahblah);
76:                               }
77:                               subsection.Add(blahblah);
78:                           }
79:                           if (== && 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:   }