How to create 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 create PDF document using IText Library?

Share This

The Portable Document Format (PDF) is a file format that helps to present data in a manner that is independent of application software, hardware, and operating systems. Each PDF file holds a description of a fixed-layout flat document, including text, fonts, graphics, and other information needed to display it.


What is iText?

Similar to the above-listed softwares iText is a Java PDF library using which, you can develop Java programs that create, convert, and manipulate PDF documents.
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.Document; import com.itextpdf.text.DocumentException; 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!")); doc.close(); } }

Happy Exploring!

No comments:

Post a Comment