Developer Tools
|
Office Productivity Applications
|
Platform-Agnostic APIs
|
Home | Online Demos | Downloads | Buy Now | Support | About Us | News | Working Together | Contact Us
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.io.IOException; import java.util.ArrayList; import com.gnostice.pdfone.PdfDocument; import com.gnostice.pdfone.PdfException; public class MergeDocuments { public static void main(String[] args) throws IOException, PdfException { // Load pathnames of the documents that // need to be merged in a list ArrayList al = new ArrayList(); al.add("Envelope.pdf"); al.add("ResellerLetter.pdf"); al.add("ResellerForm.pdf"); // Create a document instance PdfDocument doc = new PdfDocument(); // Merge documents in the list doc.merge(al); // Save merged documents to file doc.save("MergedDocument.pdf"); doc.close(); } }
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 java.io.IOException; import com.gnostice.pdfone.PdfDocument; import com.gnostice.pdfone.PdfException; import com.gnostice.pdfone.PdfNeedFileNameHandler; public class SplitDocuments implements PdfNeedFileNameHandler { static int iFileCounter = 1; // Specify an event handler that will provide the output filenames public void onNeedFileName(PdfDocument d, StringBuffer fileName) { fileName.delete(0, fileName.length()); fileName.append("Split_File_" + (iFileCounter++) + ".pdf"); } public static void main(String[] args) throws IOException, PdfException { // Load the document that needs to be split PdfDocument d = new PdfDocument(); d.load("DocumentToSplit.pdf"); // Extract pages 1, 2, & 4 d.extractPagesTo("ExtractedPages.pdf", "1-2,4"); // Set PdfNeedFileNameHandler.onNeedFileName() handler, // which will provide file names for the split files d.setOnNeedFileName(new SplitDocuments()); // Split the document into 1-page documents d.split(1); // Close the input document d.close(); } }
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 .NET Developer Tools | |
---|---|
Gnostice Document Studio .NETMulti-format document-processing component suite for .NET developers. |
PDFOne .NETA .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications. |
Our Delphi/C++Builder developer tools | |
---|---|
Gnostice Document Studio DelphiMulti-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms. |
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. |
Our Java developer tools | |
---|---|
Gnostice Document Studio JavaMulti-format document-processing component suite for Java developers. |
PDFOne (for Java)A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications. |
Our Platform-Agnostic Cloud and On-Premises APIs | |
---|---|
StarDocsCloud-hosted and On-Premises REST-based document-processing and document-viewing APIs |
Privacy | Legal | Feedback | Newsletter | Blog | Resellers | © 2002-2024 Gnostice Information Technologies Private Limited. All rights reserved. |