Developer Tools
|
Office Productivity Applications
|
Enterprise Solutions
|
|||||||||||||||||||||||







PDFOne Java makes it very easy to merge and split PDF documents. In this article, you will learn to perform these tasks.
Merging PDF documents can be performed using the merge() method of the PDFDocument class. This method takes an array list parameter containing pathnames of the files that need to be merged.
To illustrate this, I created a new PDF document “MergedDocument.pdf” by merging three PDF documents of different sizes namely “Envelope.pdf”, “ResellerLetter.pdf” and “ResellerForm.pdf”.
import java.util.ArrayList;
import java.io.IOException;
import com.gnostice.pdfone.PDFOne;
import com.gnostice.pdfone.PdfDocument;
import com.gnostice.pdfone.PdfException;
import com.gnostice.pdfone.PdfPageLayout;
import com.gnostice.pdfone.PdfWriter;
public class MergeDocuments
{
static
{
PDFOne.activate(
"T95VZE:W8HBPVA:74VQ8QV:LO4V8",
"9B1HRZAP:X5853ERNE:5EREMEGRQ:TX1R10");
}
public static void main(String[] args) throws IOException,
PdfException
{
/* Prepare a list of documents that need to be merged */
ArrayList al = new ArrayList();
al.add("Envelope.pdf");
al.add("ResellerLetter.pdf");
al.add("ResellerForm.pdf");
/* Create a PdfWriter instance for output PDF file */
PdfWriter w = PdfWriter.fileWriter("MergedDocument.pdf");
/*
Create a PdfDocument instance with
the PdfWriter instance
*/
PdfDocument d = new PdfDocument(w);
/* Merge documents in the list */
d.merge(al);
/* Set output file to be opened after it is written */
d.setOpenAfterSave(true);
/* Write the document object to file */
d.write();
/*
Close all I/O streams associated
with this writer object
*/
w.dispose();
}
}
And, here is the result.

PDFOne Java offers two ways of splitting PDF files:
extractPagesTo() method of PDFDocument class.split() method of PDFDocument class.
import com.gnostice.pdfone.PdfDocument;
import com.gnostice.pdfone.PdfException;
import com.gnostice.pdfone.PdfNeedFileNameHandler;
import com.gnostice.pdfone.PdfReader;
public class SplitDocuments
implements PdfNeedFileNameHandler
{
static int fileNoCount = 1;
static
{
PDFOne.activate("T95VZE:W8HBPVA:74VQ8QV:LO4V8",
"9B1HRZAP:X5853ERNE:5EREMEGRQ:TX1R10");
}
/*
Specify an event handler that will provide output filenames
*/
public void onNeedFileName(PdfDocument d,
StringBuffer fileName)
{
fileName.delete(0, fileName.length());
fileName.append("Split_File_" + (fileNoCount++) + ".pdf");
}
public static void main(String[] args)
throws IOException,
PdfException
{
/* Create a PdfReader instance for the PDF file */
PdfReader r = PdfReader.fileReader("DocumentToSplit.pdf");
/*
Create a PdfDocument instance with
the PdfReader instance
*/
PdfDocument d = new PdfDocument(r);
/* Extract pages 1, 2, & 4 */
d.extractPagesTo("ExtractedPages.pdf", "1-2,4");
/*
Specify class that implements the PdfNeedFileNameHandler
interface, whose onNeedFileName() method will be called
by the split() method below to obtain output filenames
*/
d.setOnNeedFileName(new SplitDocuments());
/*
Call the split method, which splits document, calls
interface method specified by setOnNeedFileName(),
obtains file names, and saves output files
*/
d.split(1);
/*
Close all I/O streams associated
with the reader object
*/
r.dispose();
}
}
The extractPagesTo() method takes the pathname of an output file and a page range as arguments. It extracts all pages in the specified page range and saves them to the specified output file.
The split() method takes a number as argument. It begins extracting consecutive pages totaling that number into new PDF documents until all pages in the original document are exhausted. The names of the new documents are provided by the onNeedFileName method of the PdfNeedFileNameHandler interface implemented by the class calling the split() method.
---o0O0o---
| Our Developer Tools | |
|---|---|
eDocEngine VCLA Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools. |
PDFtoolkit VCLA Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents. |
XtremePDFConverter VCLA Delphi/C++Builder component to intelligently convert PDF to user-friendly Word RTF documents. |
|
PDFOne .NETA .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications. |
XtremeDocumentStudio .NETMulti-format document-processing component suite for .NET developers |
PDFOne (for Java™)A Java™ PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java™ applications. |
XtremeFontEngine (for Java)Java font engine to render glyphs from Type 1, Type 2 (CFF), and TrueType fonts |
| Our Office Productivity Applications |
|---|
Free PDF ReaderA free, fast, and portable application for viewing, printing and converting PDF documents. |
| Privacy | Legal | Feedback | Newsletter | Resellers | © 2002-2013 Gnostice Information Technologies Private Limited. All rights reserved. |
This site is best viewed on a screen with minimum resolution of 1152 x 864 pixels. Windows XP users are advised to use Microsoft ClearType Tuning for optimal experience. Also, please use the latest version of a standards-compliant browser such as Firefox, Opera, or Dragon (Chromium).