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());
                    // 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);

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

                    // step 4: we add content
                    PdfImportedPage page;
                    for (int i = start_page; i < n; ) {
                        page = writer.getImportedPage(reader, i);
                    PRAcroForm form = reader.getAcroForm();
                    if (form != null)

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

plz advice
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

eldoz12Author Commented:
Experts plz advice
Mayank SAssociate Director - Product EngineeringCommented:
>> new FileOutputStream(outFile)

Try new FileOutputStream ( outFile, true )

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mayank SAssociate Director - Product EngineeringCommented:
Also, you should be maintaining a reference to the FileOutputStream and closing it in a finally block.
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

eldoz12Author Commented:
I will try what u asked me to ....
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
eldoz12Author Commented:
plz advice
eldoz12Author Commented:
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; ) {
                        page = writer.getImportedPage(reader, i);

see what page is getting processed and added.

Otherwise all looks fine.

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 .
Mayank SAssociate Director - Product EngineeringCommented:
So that worked?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.