Link to home
Start Free TrialLog in
Avatar of eldoz12
eldoz12

asked on

PDF Concatenate problem

hello Experts ,
I have 2 PDF files , I need to append the content of one file to the  of the other file ,
if the two PDF files are output file and scr file ,
The prog needs to read from scr file and add the content to the output file  at a specified page . currently I am using a Itext sample prog to do it ,
The only problem is that the content of the output file is completely over-written by the scr file , and the output file becomes a copy of the scr file .


public boolean PdfConcatenate(int start_page,FormFile scr_filename) {

              System.out.println("PdfCopy example");
            try {
                int pageOffset = 0;
                ArrayList master = new ArrayList();
                int f = 0;

                  /**
                   * Get output file from location at which cool file is downloaded to
                    **/

                String outFile = "Z:\\pdf\\app\\coolpdffiles\\716048.PDF";
                Document document = null;
                PdfCopy  writer = null;

                    // we create a reader for a certain document
                    PdfReader reader = new PdfReader(scr_filename.getInputStream());
                    reader.consolidateNamedDestinations();
                    // we retrieve the total number of pages
                    int n = reader.getNumberOfPages();
                    pageOffset += n;
                    List bookmarks = SimpleBookmark.getBookmark(reader);
                    if (bookmarks != null) {
                        if (pageOffset != 0)
                               SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, null);
                              master.addAll(bookmarks);
                    }


                        // 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 = start_page; i < n; ) {
                        ++i;
                        page = writer.getImportedPage(reader, i);
                        writer.addPage(page);
                    }
                    PRAcroForm form = reader.getAcroForm();
                    if (form != null)
                        writer.copyAcroForm(reader);

                if (master.size() > 0)
                    writer.setOutlines(master);
                // step 5: we close the document
                      document.close();
                      return true;
            }
            catch(Exception e) {
                e.printStackTrace();
                return false;
            }


plz advice
Avatar of eldoz12
eldoz12

ASKER

Experts plz advice
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Also, you should be maintaining a reference to the FileOutputStream and closing it in a finally block.
Avatar of eldoz12

ASKER

I will try what u asked me to ....
Avatar of eldoz12

ASKER

>>> Try new FileOutputStream ( outFile, true )
I  tried this but still same effect , Its overwrite of the entier output file and not a append
Avatar of eldoz12

ASKER

plz advice
Avatar of eldoz12

ASKER

mayankeagle , i checked the api and the advice you gave looks correct , but for some reason , I am still having a problem
i believe, the start_page parameter is not correctly handled.
Try debugging the start_page handling..here

for (int i = start_page; i < n; ) {
                        ++i;
                        page = writer.getImportedPage(reader, i);
                        writer.addPage(page);
                    }

see what page is getting processed and added.

Otherwise all looks fine.

fargo
Avatar of eldoz12

ASKER

the start_page here is used to read from the src_file , it just say that start reading from the (start_page) in the src_file ,
Like if the person wants to append the scr_file from the 3 rd page then it starts reading from the 3 rd page .
So that worked?