/* * This code is free software. It may only be copied or modified * if you include the following copyright notice: * * --> Copyright 2001 by Bruno Lowagie <-- * --> Copyright 2004 by Kazuya Ujihara <-- * * This code is example code of 'iText .NET'. * See http://www.ujihara.jp/iTextdotNET/examples.html * * This code is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */ using System; using com.lowagie.text; using com.lowagie.text.pdf; using System.IO; using GDI=System.Drawing; public class Chap0402 { public static void Main(String[] args) { Console.WriteLine("Chapter 4 example 2: Chapters and Sections"); // step 1: creation of a document-object Document document = new Document(PageSize.A4, 50, 50, 50, 50); // step 2: we create a writer that listens to the document PdfWriter writer=PdfWriter.getInstance(document, new FileStream("Chap0402.pdf", FileMode.Create)); // step 3: we open the document document.open(); // step 4: we add content to the document // we define some fonts Font chapterFont = FontFactory.getFont(FontFactory.HELVETICA, 24, Font.NORMAL, GDI.Color.Red); Font sectionFont = FontFactory.getFont(FontFactory.HELVETICA, 20, Font.NORMAL, GDI.Color.Blue); Font subsectionFont = FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLD, GDI.Color.DarkCyan); // we create some paragraphs 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"); 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"); // this loop will create 7 chapters for (int i = 1; i < 8; i++) { Paragraph cTitle = new Paragraph("This is chapter " + i, chapterFont); Chapter chapter = new Chapter(cTitle, i); if (i == 4) { blahblahblah.setAlignment(Element.__Value.ALIGN_JUSTIFIED); blahblah.setAlignment(Element.__Value.ALIGN_JUSTIFIED); chapter.add(blahblah); } if (i == 5) { blahblahblah.setAlignment(Element.__Value.ALIGN_CENTER); blahblah.setAlignment(Element.__Value.ALIGN_RIGHT); chapter.add(blahblah); } // add a table in the 6th chapter if (i == 6) { blahblah.setAlignment(Element.__Value.ALIGN_JUSTIFIED); } // in every chapter 3 sections will be added for (int j = 1; j < 4; j++) { Paragraph sTitle = new Paragraph("This is section " + j + " in chapter " + i, sectionFont); Section section = chapter.addSection(sTitle, 1); // in all chapters except the 1st one, some extra text is added to section 3 if (j == 3 && i > 1) { section.add(blahblah); } // in every section 3 subsections are added for (int k = 1; k < 4; k++) { Paragraph subTitle = new Paragraph("This is subsection " + k + " of section " + j, subsectionFont); Section subsection = section.addSection(subTitle, 3); if (k == 1 && j == 3) { subsection.add(blahblahblah); } subsection.add(blahblah); } if (j == 2 && i > 2) { section.add(blahblahblah); } } document.add(chapter); } // step 5: we close the document document.close(); } }