use of com.lowagie.text.pdf.PRAcroForm in project jaffa-framework by jaffa-projects.
the class MultiFormPrintEngine method mergePdf.
/**
* Merge a list of generated Pdf Documents together
* @param documents
* @throws java.io.IOException
* @throws com.lowagie.text.DocumentException
* @return byte[]
*/
public static byte[] mergePdf(List<byte[]> documents) throws IOException, DocumentException {
int pageOffset = 0;
ArrayList master = new ArrayList();
ByteArrayOutputStream output = new ByteArrayOutputStream();
Document document = null;
PdfCopy writer = null;
boolean first = true;
for (Iterator<byte[]> it = documents.iterator(); it.hasNext(); ) {
// we create a reader for a certain document
PdfReader reader = new PdfReader(it.next());
reader.consolidateNamedDestinations();
// we retrieve the total number of pages
int n = reader.getNumberOfPages();
List bookmarks = SimpleBookmark.getBookmark(reader);
if (bookmarks != null) {
if (pageOffset != 0)
SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, null);
master.addAll(bookmarks);
}
pageOffset += n;
if (first) {
first = false;
// step 1: creation of a document-object
document = new Document(reader.getPageSizeWithRotation(1));
// step 2: we create a writer that listens to the document
writer = new PdfCopy(document, output);
// step 3: we open the document
document.open();
}
// step 4: we add content
PdfImportedPage page;
for (int i = 0; i < n; ) {
++i;
page = writer.getImportedPage(reader, i);
writer.addPage(page);
}
PRAcroForm form = reader.getAcroForm();
if (form != null)
writer.copyAcroForm(reader);
}
if (master.size() > 0)
writer.setOutlines(master);
// step 5: we close the document
if (document != null)
document.close();
return output.toByteArray();
}
Aggregations