We help IT Professionals succeed at work.

PDF Concatenate problem

eldoz12
eldoz12 asked
on
Medium Priority
462 Views
Last Modified: 2008-02-20
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
Comment
Watch Question

Author

Commented:
Experts plz advice
Principal Technologist
CERTIFIED EXPERT
Commented:
>> new FileOutputStream(outFile)

Try new FileOutputStream ( outFile, true )

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Also, you should be maintaining a reference to the FileOutputStream and closing it in a finally block.

Author

Commented:
I will try what u asked me to ....

Author

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

Author

Commented:
plz advice

Author

Commented:
mayankeagle , i checked the api and the advice you gave looks correct , but for some reason , I am still having a problem

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

Author

Commented:
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 .
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
So that worked?
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.