Is it possible to merger several pdfs using iText7

后端 未结 3 2083
忘掉有多难
忘掉有多难 2020-12-06 18:50

I have several datasheets for products. Each is a separate file. What I want to do is to use iText to generate a summary / recommended set of actions, based on answers to a

相关标签:
3条回答
  • 2020-12-06 19:24

    The question doesn't specify the language, so I'm adding an answer using C#; this works for me. I'm creating three separate but related PDFs then combining them into one.

    After creating the three separate PDF docs and adding data to them, I combine them this way:

    PdfDocument pdfCombined = new PdfDocument(new PdfWriter(destCombined));
    PdfMerger merger = new PdfMerger(pdfCombined);
    
    PdfDocument pdfReaderExecSumm = new PdfDocument(new PdfReader(destExecSumm));
    merger.Merge(pdfReaderExecSumm, 1, pdfReaderExecSumm.GetNumberOfPages());
    
    PdfDocument pdfReaderPhrases = new PdfDocument(new PdfReader(destPhrases));
    merger.Merge(pdfReaderPhrases, 1, pdfReaderPhrases.GetNumberOfPages());
    
    PdfDocument pdfReaderUncommonWords = new PdfDocument(new PdfReader(destUncommonWords));
    merger.Merge(pdfReaderUncommonWords, 1, pdfReaderUncommonWords.GetNumberOfPages());
    
    pdfCombined.Close();
    

    So the combined PDF is a PDFWriter type of PdfDocument, and the merged pieces parts are PdfReader types of PdfDocuments, and the PdfMerger is the glue that binds it all together.

    0 讨论(0)
  • 2020-12-06 19:31

    If you want to add two array of bytes and return one array of bytes as PDF/A

    public static byte[] mergePDF(byte [] first, byte [] second) throws IOException {
        // Initialize PDF writer
        ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
        PdfWriter writer = new PdfWriter(arrayOutputStream);
        
    
        // Initialize PDF document
        PdfADocument pdf = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_1B, new PdfOutputIntent("Custom", "",
                "https://www.color.org", "sRGB IEC61966-2.1", new FileInputStream("sRGB_CS_profile.icm")));
    
    
        PdfMerger merger = new PdfMerger(pdf);
    
        //Add pages from the first document
        PdfDocument firstSourcePdf = new PdfDocument(new PdfReader(new ByteArrayInputStream(first)));
        merger.merge(firstSourcePdf, 1, firstSourcePdf.getNumberOfPages());
    
        //Add pages from the second pdf document
        PdfDocument secondSourcePdf = new PdfDocument(new PdfReader(new ByteArrayInputStream(second)));
        merger.merge(secondSourcePdf, 1, secondSourcePdf.getNumberOfPages());
        
        firstSourcePdf.close();
        secondSourcePdf.close();
        writer.close();
        pdf.close();
    
    
        return arrayOutputStream.toByteArray();
    }
    
    0 讨论(0)
  • 2020-12-06 19:36

    Yes, you can merge PDFs using iText 7. E.g. look at the iText 7 Jump-Start tutorial sample C06E04_88th_Oscar_Combine, the pivotal code is:

    PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
    PdfMerger merger = new PdfMerger(pdf);
    
    //Add pages from the first document
    PdfDocument firstSourcePdf = new PdfDocument(new PdfReader(SRC1));
    merger.merge(firstSourcePdf, 1, firstSourcePdf.getNumberOfPages());
    
    //Add pages from the second pdf document
    PdfDocument secondSourcePdf = new PdfDocument(new PdfReader(SRC2));
    merger.merge(secondSourcePdf, 1, secondSourcePdf.getNumberOfPages());
    
    firstSourcePdf.close();
    secondSourcePdf.close();
    pdf.close();
    

    (C06E04_88th_Oscar_Combine method createPdf)


    Depending on your use case, you might want to use the PdfDenseMerger with its helper class PageVerticalAnalyzer instead of the PdfMerger here. It attempts to put content from multiple source pages onto a single target page and corresponds to the iText 5 PdfVeryDenseMergeTool from this answer. Due to the nature of PDF files this only works for PDFs without headers, footers, and similar artifacts.

    0 讨论(0)
提交回复
热议问题