Search in sources :

Example 1 with PRAcroForm

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();
}
Also used : PdfImportedPage(com.lowagie.text.pdf.PdfImportedPage) PdfCopy(com.lowagie.text.pdf.PdfCopy) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PdfReader(com.lowagie.text.pdf.PdfReader) Document(com.lowagie.text.Document) PRAcroForm(com.lowagie.text.pdf.PRAcroForm)

Aggregations

Document (com.lowagie.text.Document)1 PRAcroForm (com.lowagie.text.pdf.PRAcroForm)1 PdfCopy (com.lowagie.text.pdf.PdfCopy)1 PdfImportedPage (com.lowagie.text.pdf.PdfImportedPage)1 PdfReader (com.lowagie.text.pdf.PdfReader)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1