0

I want to merge PDF files; around 20 files of 60 MB each. I am using iText API to merge it.

The problem is, I have to complete it within 2 seconds but my code is taking 8 seconds.

Any solution to speeding up merging PDF files?

private void mergeFiles(List<String> filesToBeMerged, String mergedFilePath) throws Exception {
    Document document = null;
    PdfCopy copy = null;
    PdfReader reader = null;
    BufferedOutputStream bos = null;
    int bufferSize = 8 * 1024 * 1024;

    String pdfLocation = "C:\\application\\projectone-working\\projectone\\web\\pdf\\";

    try {
        int fileIndex = 0;

        for (String file : filesToBeMerged)
        {
            reader = new PdfReader(pdfLocation+"/"+file);
            reader.consolidateNamedDestinations();

            int totalPages = reader.getNumberOfPages();

            if (fileIndex == 0) {
                document = new Document(reader.getPageSizeWithRotation(1));
                bos = new BufferedOutputStream(new FileOutputStream(mergedFilePath), bufferSize);
                copy = new PdfCopy(document, bos);
                document.open();
            }

            PdfImportedPage page;
            for (int currentPage = 1; currentPage <= totalPages; currentPage++) {
                page = copy.getImportedPage(reader, currentPage);
                copy.addPage(page);
            }

            PRAcroForm form = reader.getAcroForm();
            if (form != null) {
                copy.copyAcroForm(reader);
            }
        }
        document.close();

    } finally {
        if (reader != null) {
            reader.close();
        }
        if (bos != null) {
            bos.flush();
            bos.close();
        }
        if (copy != null) {
            copy.close();
        }
    }
}
mkl
  • 90,588
  • 15
  • 125
  • 265
Kick
  • 4,823
  • 3
  • 22
  • 29
  • Maybe start by adding the code you use? Har to know what to speed up otherwise – Oskar Kjellin Apr 25 '16 at 11:36
  • added code !!!!!!!!1 – Kick Apr 25 '16 at 11:57
  • To merge PDF files, a program has to parse it structurally; 20 times 60 MB might be quite a lot to parse. Thus, merging simply might take that time.... That been said, have you checked what your program does during that time? Is it doing much GC management? Or nearly only PDF stuff? – mkl Apr 26 '16 at 22:09
  • Use a profiler and analyze where most CPU time etc. is needed. As a starting point "jvisualvm" comes for free with every JDK (...\Program Files\Java\jdk1.8.0_XXX\bin\jvisualvm.exe). Or eclipse also has one (eclipse memory analyzer) If that is not enough check out that list: http://stackoverflow.com/questions/948549/open-source-java-profilers – Lonzak Apr 27 '16 at 07:42

0 Answers0