12

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)
        {

        }  
    }

1 Answer 1

23

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.

5
  • appendchild is used in PDDocumentOutline in 1.8.8 now we am using 2.00 as addLast it make any difference Mar 10, 2016 at 10:46
  • @SenthilKumar that is a different question, but yes, you can use that. I see that it isn't mentioned in the migration guide. As a general rule - always look at the examples sources. There's also an issue for 2.0 documentation for suggestions (but not for "how to" questions!): issues.apache.org/jira/browse/PDFBOX-3030 I'll mention the outlines later today. Mar 10, 2016 at 10:59
  • @SenthilKumar If the answer was useful, please click on the green checkmark. (This applies to all the questions you asked here). If the answer was not helpful or unclear, give feedback to the person so that she/he can improve the answer. Mar 11, 2016 at 12:32
  • Hi @TilmanHausherr, I'm also confused about Memory I'm also using PDFbox (2.0.27), I set the main memory size to be 2M: MemoryUsageSetting.setupMainMemoryOnly(2000000L). While it throws "Maximum allowed scratch file memory exceeded." exception even though I'm quite sure that the total size of the files to be merged is smaller than 2M. Do you have any ideas about this? Should the buffering memory size be bigger than the total size of the files?
    – suanziliu
    Mar 21 at 6:34
  • You never know how big it can get. Files are highly compressed. Holding all this in java classes in memory can use much more. Mar 21 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.