0

I have a method to concatenate a list of PDF's to a single PDF. I need to reduce the size of this file. I'm using iText to achieve this and below is my method.

public static void concatPDFs(List<InputStream> streamOfPDFFiles,
            OutputStream outputStream, boolean paginate) {

        Document document = new Document();
        try {
            List<InputStream> pdfs = streamOfPDFFiles;
            List<PdfReader> readers = new ArrayList<PdfReader>();
            int totalPages = 0;
            Iterator<InputStream> iteratorPDFs = pdfs.iterator();

            // Create Readers for the pdfs.
            while (iteratorPDFs.hasNext()) {
                InputStream pdf = iteratorPDFs.next();
                PdfReader pdfReader = new PdfReader(pdf);
                readers.add(pdfReader);
                totalPages += pdfReader.getNumberOfPages();
            }
            // Create a writer for the outputstream
            PdfWriter writer = PdfWriter.getInstance(document, outputStream);

            document.open();
            BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA,
                    BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
            PdfContentByte cb = writer.getDirectContent(); // Holds the PDF
            // data
            PdfImportedPage page;
            int currentPageNumber = 0;
            int pageOfCurrentReaderPDF = 0;
            Iterator<PdfReader> iteratorPDFReader = readers.iterator();

            // Loop through the PDF files and add to the output.
            while (iteratorPDFReader.hasNext()) {
                PdfReader pdfReader = iteratorPDFReader.next();

                // Create a new page in the target for each source page.
                while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
                    document.newPage();
                    pageOfCurrentReaderPDF++;
                    currentPageNumber++;

                    page = writer.getImportedPage(pdfReader,
                            pageOfCurrentReaderPDF);
                    writer.setFullCompression();
                    cb.addTemplate(page, 0, 0);
                    cb.getPdfWriter().setFullCompression();
                    // Code for pagination.
                    if (paginate) {
                        cb.beginText();
                        cb.setFontAndSize(bf, 9);
                        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, ""
                                + currentPageNumber + " of " + totalPages, 520,
                                5, 0);
                        cb.endText();
                    }
                }
                pageOfCurrentReaderPDF = 0;
            }

            outputStream.flush();
            document.close();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (document.isOpen())
                document.close();
            try {
                if (outputStream != null)
                    outputStream.close();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    }   

I have used writer.setFullCompression() method to compress the size of the file. However the reduction in the size is very minimal. Is there any way that I can reduce the size more? Please suggest.

NaveenBharadwaj
  • 1,212
  • 5
  • 19
  • 42
  • 1
    Is it much larger than the sum of the original files? – Henry Jan 18 '16 at 10:06
  • 1
    duplicate http://stackoverflow.com/questions/20614350/compress-pdf-with-large-images-via-java – hasnae Jan 18 '16 at 10:31
  • 4
    You are doing it wrong. Concatenation of documents should **never** be done with `PdfWriter`. Use `PdfSmartCopy` instead. This is explained in the documentation: [Why does the function to concatenate / merge PDFs cause issues in some cases?](http://itextpdf.com/question/why-does-function-concatenate-merge-pdfs-cause-issues-some-cases) If you use `PdfWriter`, you add a lot of redundant information. `PdfSmartCopy` reuses content that is duplicate. – Bruno Lowagie Jan 18 '16 at 10:34

0 Answers0