Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge document with PDFMergerUtility in pdfbox 2.00

Tags:

java

merge

pdfbox

Pdfbox Merge Document with 1.8.xx as like mergePdf.mergeDocuments() it working fine .now pdfbox version 2.0.0 contain some argument like org.apache.pdfbox.multipdf.PDFMergerUtility.mergeDocuments(MemoryUsageSetting arg0) what is MemoryUsageSetting how to use with mergeDocuments.I read as like Merge the list of source documents, saving the result in the destination file. kindly provide some code equivalent to version 2.0.0

public void combine()
    {
        try
        {
        PDFMergerUtility mergePdf = new PDFMergerUtility();
        String folder ="pdf";
        File _folder = new File(folder);
        File[] filesInFolder;
        filesInFolder = _folder.listFiles();
        for (File string : filesInFolder)
        {
            mergePdf.addSource(string);    
        }
    mergePdf.setDestinationFileName("Combined.pdf");
    mergePdf.mergeDocuments();
        }
        catch(Exception e)
        {

        }  
    }
like image 717
senthil kumar Avatar asked Mar 10 '16 08:03

senthil kumar


People also ask

How do I combine multiple PDF files into one in Java?

First we have to instantiate the PDFMergerUtility class. Second we have to set the destination file using the setDestinationFileName() method. Now we have to set the source files using the addSource() method. Final step we have to merge the documents using the mergeDocuments() method of the PDFMergerUtility class.

How do I merge documents online?

Select the files you want to merge using the Acrobat PDF combiner tool. Reorder the files if needed. Click Merge files. Sign in to download or share the merged file.

Is PDFBox free for commercial use?

Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative ...


1 Answers

According to the javadoc, MemoryUsageSetting controls how memory/temporary files are used for buffering.

The two easiest usages are:

MemoryUsageSetting.setupMainMemoryOnly()

this sets buffering memory usage to only use main-memory (no temporary file) which is not restricted in size.

MemoryUsageSetting.setupTempFileOnly()

this sets buffering memory usage to only use temporary file(s) (no main-memory) which is not restricted in size.

So for you, the call would be

mergePdf.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());

or

mergePdf.mergeDocuments(MemoryUsageSetting.setupTempFileOnly());

Or just pass null. This will default to main memory only. That's also what the javadoc tells: memUsageSetting defines how memory is used for buffering PDF streams; in case of null unrestricted main memory is used.

like image 59
Tilman Hausherr Avatar answered Oct 11 '22 01:10

Tilman Hausherr