jPDFAssemble Developer Guide
jPDFAssemble Developer Guide
Contents
Introduction
Getting Started
PDFs Splitting: extracting, deleting pages
PDFs Merging: inserting pages, appending documents
Working with bookmarks
Getting basic document information
Distribution and JAR files
Javadoc API
Source Code Samples
Introduction
jPDFAssemble is a Java library that integrates seamlessly into your application to allow you to combine or split PDF documents as well as manipulate bookmarks in PDF documents. jPDFAssemble provides the following functions:
- Extract, delete pages from existing PDF documents
- Insert pages into new or existing PDF documents
- Append a PDF document to an existing PDF document
- Create or manipulate bookmarks
- Save to the file system or to Java output streams
Like all of our libraries, jPDFAssemble is built on top of Qoppa’s proprietary technology and so does not require any third party software or drivers.
Getting Started
The starting point for using jPDFAssemble is the com.qoppa.pdfAssemble.PDFAssemble class. This class is used to load PDF documents and then provides methods to combine or split documents and then save the resulting documents.
The class provides three constructors to load PDF files from the file system, a URL or an InputStream. All constructors take an additional parameter, an object that implements IPasswordHandler, that will be queried if the PDF file requires a password to open (called Open or User password). For PDF files that are not currently encrypted, this second parameter can be null:
PDFAssemble pdfDoc = new PDFAssemble (new URL("http://www.mysite.com/content.pdf"), null); |
Additionally, the class provides a fourth constructor to create a blank document. This constructor can be used when a new document needs to be created entirely by assembling pages from other PDF documents.
PDFs Splitting: Extracting, Deleting Pages
Once a PDF document has been loaded by instantiating the PDFAssemble class, pages in the document can be deleted or extracted to a new PDF.
// Delete last page in the document and then save to a PDF file pdfDoc.deletePage(pdfDoc.getPageCount() - 1); pdfDoc.save("c:\\somefile.pdf"); // Extract the first 10 pages to a PDF file pdfDoc.extractPages(0, 9, "c:\\somefile.pdf"); |
PDFs Merging: Inserting Pages, Appending Documents
jPDFAssemble also allows to merge documents by inserting pages from other PDF documents or appending whole documents into a PDF document. The document can be an existing document or a new document according to the constructor used when creating the PDFAssemble object. Once a PDFDocument object has been instantiated, the following code insert pages from another document.
// Insert first page from another document and save to a PDF file PDFAssemble otherPDF = new PDFAssemble("c:\\otherfile.pdf"); pdfDoc.insertPage(otherPDF, 0, 0); pdfDoc.save("c:\\somefile.pdf"); |
To append a page at the end of a document, use the appendPage method:
pdfDoc.appendPage(otherPDF, 0); // Append another document and save to a PDF file PDFAssemble otherPDF = new PDFAssemble("c:\\otherfile.pdf"); pdfDoc.appendDocument(otherPDF); pdfDoc.save("c:\\somefile.pdf"); |
Working with Bookmarks
jPDFAssemble allows to create new bookmarks or manipulate existing bookmarks in PDF documents. Here is an example on how to add bookmarks for each page of a PDF document:
// Get current bookmark root Bookmark rootBK = pdfDoc.getRootBookmark(); // Create a root bookmark if it's null if(rootBK == null) { rootBK = pdfDoc.createRootBookmark(); } // Add a bookmark for each page for (int i = 1; i <= pdfDoc.getPageCount(); i++) { Bookmark bk = rootBK.addChildBookmark("Page" + " " + i); // add a go to page i action to the bookmark pdfDoc.addGoToPage(bk, i); } // Save the document pdfDoc.saveDocument("doc1_bookmarks.pdf"); |
Getting / Setting Information about the PDF Document
To get basic information about the loaded PDF document, you need to get the DocumentInfo class accessible through PDFAssemble.getDocumentInfo. From this class, you can get and set information about the document such as title, author, subject, keywords, etc…
System.out.println(pdfDoc.getDocumentInfo().getTitle()); System.out.println(pdfDoc.getDocumentInfo().getAuthor()); System.out.println(pdfDoc.getDocumentInfo().getKeywords()); |
Distribution and JAR Files
Required and optional jar files for jPDFAssemble can be found on the jPDFAssemble Download page.