----- Original Message ----- 
From: "Carsten Hammer" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Saturday, September 11, 2004 13:50
Subject: [iText-questions] fastest way to add a page to a pdf document using
itext


> Hi,
> what is the fastest way to add an (empty) page to the end of a document?
> I need to convert a lot of big (~50-100MB) PDF documents to documents
> with an even number of pages because of the paper post processing. It
> should be done by adding a single empty page.

The docs are big with many pages or not so many pages but big images? The
type of doc will impact the memory needs.


> I could copy every single page to a new document. But this seems to be
> not very fast. Is it possible to use PDFStamper somehow for this?
>

Yes. That's quite easy, see the example:

import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
import java.util.*;
import java.io.*;
import java.awt.Color;

public class AppendEmptyPage {

    public static void main(String[] args) {
        try {
            PdfReader reader = new PdfReader("original.pdf");
            PdfStamper stp = new PdfStamper(reader, new
FileOutputStream("destination.pdf"));
            PdfWriter writer = stp.getWriter();
            PdfDictionary page = reader.getPageN(reader.getNumberOfPages());
            PdfDictionary parent =
(PdfDictionary)PdfReader.getPdfObject(page.get(PdfName.PARENT));
            PdfArray kids =
(PdfArray)PdfReader.getPdfObject(parent.get(PdfName.KIDS));
            PdfIndirectReference ref = writer.getPdfIndirectReference();
            kids.add(ref);
            PdfDictionary newPage = new PdfDictionary(PdfName.PAGE);
            newPage.merge(page);
            newPage.remove(PdfName.CONTENTS);
            newPage.put(PdfName.RESOURCES, new PdfDictionary());
            writer.addToBody(newPage, ref);
            while (parent != null) {
                PdfNumber count =
(PdfNumber)PdfReader.getPdfObject(parent.get(PdfName.COUNT));
                parent.put(PdfName.COUNT, new PdfNumber(count.intValue() +
1));
                parent =
(PdfDictionary)PdfReader.getPdfObject(parent.get(PdfName.PARENT));
            }
            stp.close();
            System.out.println("Fim.");
        }
        catch (Exception e) {
            e.printStackTrace(System.out);
        }
    }
}

> Reading of the page number of a document on a windows share from linux
> seems to be not very fast. Does reading of the page number of a pdf file
> mean that itext needs to read the complete file? Or does

Yes.

> java.io.RandomAccessFile not work very good through samba?

It's slow because of the many seeks. Copy the file to your local disk.

Best Regards,
Paulo Soares



-------------------------------------------------------
This SF.Net email is sponsored by: YOU BE THE JUDGE. Be one of 170
Project Admins to receive an Apple iPod Mini FREE for your judgement on
who ports your project to Linux PPC the best. Sponsored by IBM. 
Deadline: Sept. 13. Go here: http://sf.net/ppc_contest.php
_______________________________________________
iText-questions mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Reply via email to