Hi All,

I've developed an application to process large PDF files (1800 pages/700Mb 
for the largest, although most are around 1000 pages/300Mb).  The process 
is all running on a Unix box as follows:

STEP 1)  SOME STUFF.....

STEP 2)  Glue all of the individual pages together using pdftk (which I 
understand uses iText, this take a few minutes).

STEP 3)  SOME STUFF.....

STEP 4)  Process the file through my iText program (see attached, with 
irrelevant bits removed) which broadly does the following:
        - Set some permission's.
        - Set the initial view (page, bookmarks panel open etc.)
        - Set the properties (title, author etc).
        - Add bookmarks (about 10Mb of XML).
        - Copy the pages (takes about 2 hours).
        - Sets the page offset (the first page is usually page 20)

STEP 5)  SOME OTHER STUFF.....

All of the above is pretty much instant except the copy pages which takes 
about 2 hours.  I'm guessing this can be speeded up since pdftk is 
comparatively fast.  I know the rest of the process will take up to 45 
minutes which is fine for my application but I'm hoping I can reduce the 2 
hours copy.

I've tried to adjust the first page of the document and then glue all of 
the pages together (I.e. perform step 4 before step 2) but the resultant 
PDF is missing all of the data (bookmarks, initial view, permission's 
etc).  So I think I have several options:

- Speeding up my copyPages loop.
- Find a way of gluing the pages and applying the data in one pass, i.e. 
merging step 2 and step 4 to a new step 2.
- Applying the information before gluing but ensuring it is not lost when 
glued, similar to the above but still using pdftk or similar if possible.

Any suggestions appreciated.

Jim
------------------------------------ SOME CODE FROM STEP 3 
----------------------------------------------
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.BadPdfFormatException;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfPageLabels;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.RandomAccessFileOrArray;
import com.lowagie.text.pdf.SimpleBookmark;

public class ChangePdf {
        Document document = null;
        PdfCopy pdfCopy = null;
 
        /**
         * Main control for the file.
         * @param xmlFile Xml file to read what to do from.
         * @param inputFile PDF file to adject.
         * @param outputFile Resultant PDF file.
         * @throws Exception 
         */
        public void process(String xmlFile, String inputFile, String 
outputFile) throws Exception {
                AdjustPdfXmlReader xmlReader = new AdjustPdfXmlReader();
                xmlReader.parse(xmlFile);

                System.out.println("Adjusting \"" + inputFile + "\" to \"" 
+ outputFile + "\" using \"" + xmlFile + "\".");
                document = new Document();
 
                try {
                        pdfCopy = new PdfCopy(document, new 
FileOutputStream(outputFile));
                        setPermissions();
                        setInitialView();
                        setProperties(xmlReader);
                        document.open();

                        if (xmlReader.getBookmarkFile() != null) {
                                addBookmarks(xmlReader.getBookmarkFile());
                        }

                        copyPages(inputFile, pdfCopy);

                        if (xmlReader.getFirstPage() != 1) {
 adjustPageNumbers(xmlReader.getFirstPage());
                        }

                        document.close(); 
                } catch (...) {
                        ...
                }
        }

        private void setPermissions() throws Exception {
                System.out.println("Setting permissions to only allow 
printing.");
                pdfCopy.setEncryption(
                                null, // No owner password.
                                null, // No user password.
                                PdfWriter.AllowPrinting,
                                0); // No encryption.
        }

        private void setInitialView() {
                System.out.println("Setting viewer preferences.");
                pdfCopy.setViewerPreferences(
                                PdfWriter.PageLayoutSinglePage |
                                PdfWriter.PageModeUseOutlines | // Thumbs 
for page thumbnails ot Outlines for bookmarks. 
                                PdfWriter.FitWindow |
                                PdfWriter.CenterWindow |
                                PdfWriter.DisplayDocTitle
                                );
        }
 
        private void setProperties(AdjustPdfXmlReader xmlReader) {
                System.out.println("Setting title to = " + 
xmlReader.getTitle());
                System.out.println("Setting author to = " + 
xmlReader.getAuthor());
                System.out.println("Setting subject to = " + 
xmlReader.getSubject());
                System.out.println("Setting keywords to = " + 
xmlReader.getKeywords());
                System.out.println("Setting creator to = " + 
xmlReader.getCreator());

                document.addTitle(xmlReader.getTitle());
                document.addAuthor(xmlReader.getAuthor());
                document.addSubject(xmlReader.getSubject());
                document.addKeywords(xmlReader.getKeywords());
                document.addCreator(xmlReader.getCreator()); // 
Application
        }

        private void addBookmarks(String bookmarkFile) throws Exception {
                System.out.println("Adding bookmarks from file  " + 
bookmarkFile); 
                FileInputStream bookmarkReader = new FileInputStream(new 
File(bookmarkFile));
 pdfCopy.setOutlines(SimpleBookmark.importFromXML(bookmarkReader));
            bookmarkReader.close();
        }

        public void copyPages(String pdf, PdfCopy pdfCopy) throws 
IOException, BadPdfFormatException {
                PdfReader pdfReader = new PdfReader(new 
RandomAccessFileOrArray(pdf), null);
                int pageCount = pdfReader.getNumberOfPages();

                for (int page = 1; page <= pageCount; page++) {
                        pdfCopy.addPage(pdfCopy.getImportedPage(pdfReader, 
page));
                }

                System.out.println("\rCopied " + pageCount + " pages.");
        } 

        private void adjustPageNumbers(int firstPage) {
                System.out.println("Adjusting page offset to " + firstPage 
+ " pages."); 
                PdfPageLabels pageLabels = new PdfPageLabels();
                pageLabels.addPageLabel(1, 
PdfPageLabels.DECIMAL_ARABIC_NUMERALS, "", firstPage);
                pdfCopy.setPageLabels(pageLabels);
        }
}
------------------------------------ END OF CODE 
----------------------------------------------


***********************************************************************************
This e-mail and attachments are intended for the above name only and may be 
confidential. If they have come to you in error, you must take no action based 
on them, nor must copy or show them to anyone; please reply to this e-mail and 
report the error.
Security warning: Please note that this e-mail has been created in the 
knowledge that the internet is not a one hundred percent secure communication 
medium. We advise that you understand and observe this lack of security when 
e-mailing us.
Virus: Although we have taken steps to ensure that this e-mail and attachments 
are free from any virus, we advise that in keeping with good practice the 
recipient should ensure they are actually virus free.  If you have received 
this e-mail in error please notify:
[EMAIL PROTECTED]



This message has been scanned for viruses by MailControl - www.mailcontrol.com
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/

Reply via email to