Assembling and splitting documents
Introduction
In this tutorial, we will see how you can merge andor split documents using jPDF Merger.
The need to merge documents dynamically is common across the PDF industry. Typically, you may want to aggregate documents together before they are emailed to a supplier. Or you may want to extract a given document section.
Regardless of the processing type, you have to remember that the unit of work is the page. In other words, you cannot easily merge (or split) a portion of a document page.
Crionics offers the best PDFmerger on the market. Our engine is not only fast, it does all the dirty work that no one else wants to do - namely:
- Merging document pages, of course
- Merging tables of content (aka outlines)
- Merging form fields (including automatic renaming in case of collisions)
- Merging destinations
When you merge two documents, chances are they may have elements in common - fonts, colors or graphics, for instance. In the case of a split, there is no need for any references to fonts or images that no longer exist in the split version.
jPDF Merger is smart enough to process only the resources it requires: nothing more! This, of course, has an impact on the final pdf size - as well as on the processing time.
This feature, along with the ones we discuss throughout this tutuorial, make jPDF Merger the perfect candidate for your merging/splitting requirements.
But let's get our hands dirty and see how to program the API...
Merging documents
The merger API is accessed through a getter method on the PdfDocument instance. It would be hard to make anything simpler to use:
// Open the document:
PdfDocument doc = new PdfDocument("doc1.pdf");
// Here you want to save the file to another destination
// to avoid overwriting the original
doc.saveAs("mydoc1_copy.pdf");
// Get the merger module:
MergerModule merger=doc.getMergerModule();
// Add the other documents:
merger.append("doc2.pdf");
merger.append("doc3.pdf");
// Close the document:
doc.close();
By default, jPDF Merger appends the document to the current instance. That's the reason why you may want to make a copy to prevent modifications to the original.
Splitting documents
Now that we've seen how to merge a document, let's see how we can do a split. The main difference is that splitting requires a list of the pages you want to extract.
// Open the document to split:
PdfDocument doc= new PdfDocument("mydoc.pdf");
// Here you want to save the file to another destination
// to avoid overwriting the original:
doc.saveAs("mydoc_copy.pdf");
// Define the list of pages to extract:
int[] pagesToRemove={1, 2, 3};
// Do the actual split:
doc.getMergerModule().splitPages(fileSplitted, pagesToRemove);
// Close the original document:
doc.close();
Easy right ?
Well, this concludes this tutorial on Crionics jPDF Merger.
Copyright © Crionics Inc. 2012 All rights reserved. Terms of Use | Privacy Policy
