Robin Zhao wrote:

Hi all,
How to merge multiple PDF into a temporary PDF and show on browser?

That's as easy as 1, 2, 3.

temporary means don't save in hardisk.

Then why do you write the file to merge.pdf using a FileOutputStream?

I added some hints to your code.
I don't claim they will work immediately.

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

ByteArrayOutputStream baos = new ByteArrayOutputStream();

ConcatenateForms merge = new ConcatenateForms(baos);
merge.mergePDF(aList);

boas.writeTo(response.getOutputStream());

}


ConcatenateForms.java:

ByteArrayOutputStream baos;
public ConcatenateForms(ByteArrayOutputStream baos) {
   this.baos = baos;
}

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,

baos

/*

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();
           }
...
}

br,
Bruno


-------------------------------------------------------
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