bezmax
bezmax

Reputation: 26132

Inserting multiple copies of a page into a PDF

As far as I know, PDFs support references to objects. So, for example, if I use the same image 100 times in a document - it can be optimized to use same image in each place greatly saving PDF size.

Does PDF support the same with full pages of data? If yes, how can I do it with iText library?

My problem is, I have a huge document of ~500 pages, and every 2-3 pages I need to insert the same template page that is read from other PDF document. The code looks likes this:

    protected static void addAppendix(PdfWriter writer, Document document, InputStream appendixStream)
            throws IOException {
        PdfContentByte cb = writer.getDirectContent();
        PdfReader reader = new PdfReader(appendixStream);
        for (int idx = 1; idx <= reader.getNumberOfPages(); ++idx) {
            document.newPage();
            PdfImportedPage imported = writer.getImportedPage(reader, idx);
            cb.addTemplate(imported, 0, 0);
        }
        writer.freeReader();
    }

However, this increases the PDF size greatly, especially if the appendix contains some large images and stuff. Is there any way to somehow optimize the size of my document?

Upvotes: 1

Views: 908

Answers (2)

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

The answer by Fabrizio is correct: if you don't free the reader, a page that was already added won't be added redundantly.

I want to add an extra comment/answer: if you're talking about adding extra pages to an existing PDF, you don't want to use PdfWriter to do this. I know: there are plenty of examples on the internet that tell you to do so, but those aren't endorsed by the original developer of iText (being: me).

If you want to add pages to an existing PDF, you should use PdfCopy or PdfSmartCopy. The advantage of using PdfCopy is that you preserve interactive content, such as links and annotations. The advantage of using its subclass PdfSmartCopy is that redundant objects are removed. For instance: if the same page stream is present 100 times redundantly in the same document, PdfSmartCopy will remove 99 instances and refer to the one and only remaining stream object.

Upvotes: 3

Fabrizio Accatino
Fabrizio Accatino

Reputation: 2292

Do not destroy the PdfReader but reuse it.

Steps:
1 - create the Document and PdfWriter pointing to output.pdf
2 - open the PdfReader pointing to your appendix.pdf
3 - add content to your pages
4 - add appendix pages reading form PdfReader
5 - repeat step 3 and 4 as you need
6 - close PdfRerader
7 - close PdfWriter and Document

Upvotes: 3

Related Questions