• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 424
  • Last Modified:

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
0
eldoz12
Asked:
eldoz12
  • 6
  • 3
1 Solution
 
eldoz12Author Commented:
Experts plz advice
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> new FileOutputStream(outFile)

Try new FileOutputStream ( outFile, true )
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Also, you should be maintaining a reference to the FileOutputStream and closing it in a finally block.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
eldoz12Author Commented:
I will try what u asked me to ....
0
 
eldoz12Author Commented:
>>> Try new FileOutputStream ( outFile, true )
I  tried this but still same effect , Its overwrite of the entier output file and not a append
0
 
eldoz12Author Commented:
plz advice
0
 
eldoz12Author Commented:
mayankeagle , i checked the api and the advice you gave looks correct , but for some reason , I am still having a problem
0
 
fargoCommented:
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
0
 
eldoz12Author 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 .
0
 
Mayank SAssociate Director - Product EngineeringCommented:
So that worked?
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now