PDFBox LayerUtility - Importing layers into existing PDF

前端 未结 1 1640
执念已碎
执念已碎 2021-01-14 02:33

I am using pdfbox to manipulate PDF content. I have a big PDF file (say 500 pages). I also have a few other single page PDF files containing only a single image which are ar

相关标签:
1条回答
  • 2021-01-14 02:56

    With code like the following I don't see any unepected growth of size:

    PDDocument bigDocument = PDDocument.load(BIG_SOURCE_FILE);
    LayerUtility layerUtility = new LayerUtility(bigDocument);
    List bigPages = bigDocument.getDocumentCatalog().getAllPages();
    
    // import each page to superimpose only once
    PDDocument firstSuperDocument = PDDocument.load(FIRST_SUPER_FILE);
    PDXObjectForm firstForm = layerUtility.importPageAsForm(firstSuperDocument, 0);
    
    PDDocument secondSuperDocument = PDDocument.load(SECOND_SUPER_FILE);
    PDXObjectForm secondForm = layerUtility.importPageAsForm(secondSuperDocument, 0);
    
    // These things can easily be done in a loop, too
    AffineTransform affineTransform = new AffineTransform(); // Identity... your requirements may differ
    layerUtility.appendFormAsLayer((PDPage) bigPages.get(0), firstForm, affineTransform, "Superimposed0");
    layerUtility.appendFormAsLayer((PDPage) bigPages.get(1), secondForm, affineTransform, "Superimposed1");
    layerUtility.appendFormAsLayer((PDPage) bigPages.get(2), firstForm, affineTransform, "Superimposed2");
    
    bigDocument.save(BIG_TARGET_FILE);
    

    As you see I superimposed the first page of FIRST_SUPER_FILE on two pages of the target file but I only imported the page once. Thus, also the resources of that imported page are imported only once.

    This approach is open for loops, too, but don't import the same page multiple times! Instead import all required template pages once up front as forms and in the later loop reference those forms again and again.

    (I hope this solves your issue. If not, supply more code and the sample PDFs to reproduce your issue.)

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