How to add Chapters and Paragraphs in a PDF document using IText Library? - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Random Posts

How to add Chapters and Paragraphs in a PDF document using IText Library?

Share This



Content of ITextTest.java
package com.t4b.java.itext.test; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import com.itextpdf.text.Chapter; import com.itextpdf.text.Chunk; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Font; import com.itextpdf.text.FontFactory; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; public class ITextTest { public static final String filename = "pdfs/hello.pdf"; public static void main(String[] args) { try { File file = new File(filename); file.getParentFile().mkdirs(); new ITextTest().createPdf(filename); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { } } public void createPdf(String filename) throws DocumentException, IOException { Document doc = new Document(); PdfWriter.getInstance(doc, new FileOutputStream(filename)); doc.open(); doc.addTitle("This is Title"); doc.add(new Paragraph("This is simple paragraph!")); doc.add(new Paragraph("This is another paragraph!")); Font chapterFont = FontFactory.getFont(FontFactory.COURIER_BOLD, 14, Font.BOLDITALIC); Font paragraphFont = FontFactory.getFont(FontFactory.TIMES, 10, Font.NORMAL); Chunk chunk = new Chunk("This is the chapter title", chapterFont); Chapter chapter = new Chapter(new Paragraph(chunk), 1); chapter.setNumberDepth(0); chapter.add(new Paragraph("A paragraph in a chapter!", paragraphFont)); doc.add(chapter); doc.close(); } }

Happy Exploring!

No comments:

Post a Comment