I'm having some difficulty dealing with memory constraints while concatenating a large number of files.  The heap on the VM spikes dramatically and I'm trying to determine what I can to do to minimize the impact.  I could have up to a few thousand pages from a few hundred documents.  I've been using PDFCopy and recently switched to PDFCopyFields based on one of the responses to a similar post.  I've noticed no difference.  What can I do to ensure that the output stream is flushed after each read pdf is added to the new merged/concatenated document?  Is there a smarter way to do what I'm doing?

The code I use and the research I performed are below.  I've looked at two different postings and have looked at concat_pdf.java.  I apologize in advance if I have missed the answer somewhere else.  

Here are the postings I read to try to find an answer:
  • http://thread.gmane.org/gmane.comp.java.lib.itext.general/16977/focus=16977
  • http://article.gmane.org/gmane.comp.java.lib.itext.general/18809/match=itext+concatenate+large+files

Thanks in advance for your help,

Mark


// code starts here

            OutputStream out = printService.getOutputStream();
//            Document document = new Document();
//            PdfCopy writer = new PdfCopy(document, out);
//            document.open();
            PdfCopyFields writer = new PdfCopyFields(out);

            while (current != null) {
                Doc doc = current.getDoc();

                // get the doc flavor; if it's url then get a handle to the
                // stream
                // so we can close each doc when we're done with it
                if (doc.getDocFlavor().getRepresentationClassName().equals(
                        URL.class.getName())) {
                    in = doc.getStreamForBytes();

                    // create a new doc with the input stream doc flavor
                    doc = new SimpleDoc(in, new DocFlavor.INPUT_STREAM(doc
                            .getDocFlavor().getMimeType()), doc.getAttributes());
                }

                Doc printedDoc = printToDoc(doc, attributes);
                byte[] docBytes = convertToByteArray(printedDoc.getStreamForBytes());
                PdfReader reader = new PdfReader(docBytes);
                int numberOfPages = reader.getNumberOfPages();
               
//                for(int i = 1; i <= numberOfPages; i++){
//                    PdfImportedPage page = writer.getImportedPage(reader, i);
//                    writer.addPage(page);
//                }
                writer.addDocument(reader);
                writer.getWriter().freeReader(reader);
               
                close(in);
                current = current.next();
                notifyJobListenersOnCompletion();
                writer.getWriter().flush();
            }
           
            writer.close();
//            document.close();          

Reply via email to