Chapter and Section examples

This is a code example of iText PDF, discover more.

8th November 2015
admin-marketing

Switch code for this example

The Chapter and Section objects are being abandoned. They don't have any added value. Creating of bookmarks is done using the PdfOutline object.



ChapterAndTitle.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/28431148/itext-chapter-font-overrides-paragraph-font
  4.  */
  5. package sandbox.objects;
  6.  
  7. import com.itextpdf.text.Chapter;
  8. import com.itextpdf.text.Chunk;
  9. import com.itextpdf.text.Document;
  10. import com.itextpdf.text.DocumentException;
  11. import com.itextpdf.text.Font;
  12. import com.itextpdf.text.FontFactory;
  13. import com.itextpdf.text.Paragraph;
  14. import com.itextpdf.text.pdf.PdfWriter;
  15.  
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import sandbox.WrapToTest;
  20.  
  21. @WrapToTest
  22. public class ChapterAndTitle {
  23.     public static final String DEST = "results/objects/chapter_title.pdf";
  24.    
  25.     public static void main(String[] args) throws IOException, DocumentException {
  26.         File file = new File(DEST);
  27.         file.getParentFile().mkdirs();
  28.         new ChapterAndTitle().createPdf(DEST);
  29.     }
  30.    
  31.     public void createPdf(String dest) throws IOException, DocumentException {
  32.         Document document = new Document();
  33.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  34.         document.open();
  35.         Font chapterFont = FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLDITALIC);
  36.         Font paragraphFont = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL);
  37.         Chunk chunk = new Chunk("This is the title", chapterFont);
  38.         Chapter chapter = new Chapter(new Paragraph(chunk), 1);
  39.         chapter.setNumberDepth(0);
  40.         chapter.add(new Paragraph("This is the paragraph", paragraphFont));
  41.         document.add(chapter);
  42.         document.close();
  43.     }
  44. }
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now