Hi all,
How to merge multiple PDF into a temporary PDF and show on browser?
For example: there are multiple PDF files in c:\pdf, i want to merge them into one temporary PDF, temporary means don't save in hardisk.

What i am done is: i create a servlet, inside servlet it call another class to merge PDF, servlet will send file name to the class, the class will return the PDF to the servlet, servlet will show post the merged PDF to browser.

I refered to the example give by iText: ProgressServlet, Concatenate.class. But it is not working. Please give me some advice on my source code. Or give me your soluction.

Here is my code:
MergePDF.servlet:
...
public void run() {
//use ArrayList save file name, because it may be many file
ArrayList aList = new ArrayList();
aList.add("ChapterSection.pdf");
aList.add("SimpleAnnotations1.pdf");
//call another class to merge PDF
ConcatenateForms merge = new ConcatenateForms();
merge.mergePDF(aList);
}
....


ConcatenateForms.java:
...
public void mergePDF(ArrayList filename) {
...
try {
               int pageOffset = 0;
               ArrayList master = new ArrayList();
               int f = 0;
               int count = 0;
               count = filename.size();
               String outFile = "Merge.pdf";
               Document document = null;
               PdfCopy  writer = null;
               while (f < count -1) {
                   // we create a reader for a certain document
PdfReader reader = new PdfReader((String)(filename.get(count)));
                   reader.consolidateNamedDestinations();
                   // we retrieve the total number of pages
                   int n = reader.getNumberOfPages();
                   List bookmarks = SimpleBookmark.getBookmark(reader);
                   if (bookmarks != null) {
                       if (pageOffset != 0)
SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, null);
                       master.addAll(bookmarks);
                   }
                   pageOffset += n;

                   if (f == 0) {
                       // step 1: creation of a document-object
document = new Document(reader.getPageSizeWithRotation(1)); // step 2: we create a writer that listens to the document writer = new PdfCopy(document, new FileOutputStream(outFile));
                       // step 3: we open the document
                       document.open();
                   }
                   // step 4: we add content
                   PdfImportedPage page;
                   for (int i = 0; i < n; ) {
                       ++i;
                       page = writer.getImportedPage(reader, i);
                       writer.addPage(page);
                   }
                   PRAcroForm form = reader.getAcroForm();
                   if (form != null)
                       writer.copyAcroForm(reader);
                   f++;
               }
               if (master.size() > 0)
                   writer.setOutlines(master);
               // step 5: we close the document

               document.close();
           }
           catch(Exception e) {
               e.printStackTrace();
           }
...
}



Please post your questions to the mailing list instead of to me personally.
Thanks in advance,
Regards
Robin

_________________________________________________________________
Get an advanced look at the new version of MSN Messenger. http://messenger.msn.com.sg/Beta/Default.aspx



-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Reply via email to