Is it possible to merger several pdfs using iText7

后端 未结 3 2108
忘掉有多难
忘掉有多难 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: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.

提交回复
热议问题