Merge two pdf files

Help
2014-06-26
2014-12-19
  • Matthias Nick

    Matthias Nick - 2014-06-26

    Hi,
    I have got 2 pdf files (a.pdf and b.pdf). b.pdf contains some forms.

    I want to merge both files into a new one / I want to append both files to one file.

    Is this somehow possible?

     
  • Maurizio M. Gavioli

    For my current needs (see below), I got fairly acceptable results simply using the copyDeep(Map map) method. For instance, before any copying/merging across documents:

    HashMap resMap = new HashMap();
    

    then, whenever a page is to be copied to another document:

    PDPage sourcePage = <your source document page>;
    PDPage destPage = (PDPage)PDPage.META.createNew();
    CSContent destContent = CSContent.createNew();
    CSCreator destCreator = CSCreator.createFromContent(destContent, sourcePage);
    // any required setup for the destCreator goes here
    // copy the page contents
    destCreator.copy(sourcePage.getContentStream());
    // copy the resources, if needed
    if (sourcePage.getResources() != null) {
        COSObject cosRes = sourcePage.getResources().cosGetObject().copyDeep(resMap);
        PDResources pdRes = (PDResources)PDResources.META.createFromCos(cosRes);
        destPage.setResources(pdRes);
    }
    // any additional content management might go here
    // add dest. pages to dest. document
    destCreator.close();
    destPage.cosAddContents(destContent.createStream());
    dstDoc.addPageNode(destPage);
    

    Iteratively passing resMap to copyDeep() filters out a good deal of shared resources from being copied again and again.

    For my needs, this approach is quite enough, but I have to do with conceptually simple documents: maybe large in size, but made (almost) only of text and PS graphics. Having to do with acroforms or other objects may make things more complex, but I believe this could be a good starting point.

    I'm sure a smarter copying/merging algorithm could be devised (and suggestions are welcome), but I suspect that gains in resource economy risk to be small, while the code risks to grow exponentially.

    Thanks for developing jPod!

    Maurizio

     

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks