1. Products
  2.   PDF
  3.   Java
  4.   OpenPDF
 
  

Java Library for PDF Documents Management

Open Source Java API to Create, Edit & Manipulate PDF Files from your own applications.

OpenPDF is an open-source PDF library for Java developers. It allows creating & modifying PDF files from Java apps without any external dependencies. OpenPDF is licensed with an LGPL and MPL license & is a fork of iText version 4.

PDF is one of the World’s favorite document formats and still very useful. OpenPDF API support several important features, such as creation and modification of PDF documents, the addition of images to PDF, insertion of new pages to an existing PDF file, creation of paragraphs, the addition of header and footers, creation of TOC, content editing and more.

Previous Next

Getting Started with OpenPDF

Java 8 or later is required to use the OpenPDF library. All Java versions from 8 to Java 12 have been tested to work. It will compile the Java sources and package the binary classes into jar packages by default.

OpenPDF Maven Dependency

<dependency>
  <groupId>com.github.librepdf</groupId>
  <artifactId>openpdf</artifactId>
  <version>1.3.11</version>
</dependency>

Create & Edit PDF Files via Java API

OpenPDF provides the functionality for PDF document creation as well as modifications from Java applications. Software developers can easily create PDF documents with content & images. In order to create a new document, first of all, you need to create a document object and then create a writer that listens to the document and directs a PDF stream to the file. Once the document is created you can easily add paragraphs, add new pages, and insert images with ease.

Create PDF document - Java

// Intialize Document object
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("FileFormat.pdf"));
// Open document
document.open();
// Add pargraph
document.add(new Paragraph("FileFormat Developer Guide"));
// Close document
document.close();

Insert Images to PDF Documents via Java

OpenPDF allows Java programmers to insert images into PDF documents inside their own Java applications. Images always add more value to the piece of content. To insert an image, you need to provide an image name and location, then by calling the document object you can open the document and add the image on the desired page or location. Once done you just need to close the document in order to commit changes.

Add Image in PDF - Java

// Intialize Document object
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("output.pdf"));
// Open document
document.open();
Image jpg = Image.getInstance("sample.jpg");
document.add(jpg);
// Close document
document.close();

Add Lists to PDF Documents

OpenPDF API facilitates Java developers to add lists to PDF documents. You can create a list and then add list items to PDF with ease. You can also pass a symbol for marking the list items (Unicode character). You can also select a numbered or lettered list. There are also specialized classes for Roman letters and Greek letters.

Add Bookmarks to List in PDF via Java

 Document document = new Document(PageSize.A4);
PdfWriter instance = PdfWriter.getInstance(document, new FileOutputStream("out.pdf"));
instance.setViewerPreferences(PdfWriter.PageModeUseOutlines);
document.open();

List list = new List();
list.add(new ListItem(new Chunk("ABC").setLocalDestination("dest1")));
list.add(new ListItem(new Chunk("XYZ").setLocalDestination("dest2")));
document.add(list);

// add outline items
PdfOutline root = instance.getDirectContent().getRootOutline();
new PdfOutline(root, PdfAction.gotoLocalPage("dest1", false), "abc-item");
new PdfOutline(root, PdfAction.gotoLocalPage("dest2", false), "xyz-item");

document.close();

Adding Header and Footer to PDF Documents via Java

Headers and footers can hold important information about a document or data to help keep longer documents organized and make them easier to read. Headers and footers normally include additional information such as page numbers, dates, an author's name, and footnotes and so on. OpenPDF API enables Java developers to add header and footer to their PDF documents with just a couple of lines of code.

How to Start Header & Footer From First Page via Java

 Document document = new Document();
document.setPageSize(PageSize.A7.rotate()); // just to make output smaller
document.setMargins(15f, 15f, 24f, 20f);

HeaderFooter header = new HeaderFooter(new Phrase("This is a header."), false);
HeaderFooter footer = new HeaderFooter(new Phrase("This is a footer on page "), new Phrase("."));

document.setHeader(header);
document.setFooter(footer);

document.open(); // only open the document after header/footer have been set
document.add(new Paragraph("Hello World"));

document.add(Chunk.NEXTPAGE);
document.add(new Paragraph("Hello new page."));

   
 English