PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.de/

Combining PDF Files is Adding an Additional Page
http://forum.pdfsharp.de/viewtopic.php?f=2&t=1540
Page 1 of 1

Author:  GaryR [ Wed Feb 09, 2011 11:01 pm ]
Post subject:  Combining PDF Files is Adding an Additional Page

I am looping an array list adding each 1 page pdf file to the first pdf file and it works fine. After I run the following code I get an expected output of:

Added C:\My Stuff\File 2.pdf to C:\My Stuff\File 1.pdf
Added C:\My Stuff\File 3.pdf to C:\My Stuff\File 1.pdf

However what is odd is that when I open File1.pdf, it is 4 pages not 3 and in a wierd order.
Page1 is File1 and has a footer of C:\My Stuff\File 1.pdf • 1
Page2 is File3 and has a footer of C:\My Stuff\File 3.pdf • 1
Page3 is File2 and has a footer of C:\My Stuff\File 2.pdf • 2
Page4 is blank and has a footer of C:\My Stuff\File 3.pdf • 2

Code:
ArrayList files = new ArrayList();
files.Add(@"C:\My Stuff\File 1.pdf");
files.Add(@"C:\My Stuff\File 2.pdf");
files.Add(@"C:\My Stuff\File 3.pdf");
           
string filename1 = "";
foreach (string file in files)
{
     if (filename1 == "")
     {
          filename1 = file;
          continue;
     }
               
     CombinePdfFiles(filename1, file);
     Debug.WriteLine("Added " + file + " to " + filename1);
}


I could inclide the CombinePdfFiles method but it is basically just copied code from the samples. The only thing I am doing different is not creatiing a new file for all the combined reports.

Any ideas?

Author:  Thomas Hoevel [ Thu Feb 10, 2011 8:13 am ]
Post subject:  Re: Combining PDF Files is Adding an Additional Page

GaryR wrote:
I could inclide the CombinePdfFiles method but it is basically just copied code from the samples.

The code you provide looks correct. The rest is just basically copied, so your program must work. :wink:

Author:  GaryR [ Thu Feb 10, 2011 6:01 pm ]
Post subject:  Re: Combining PDF Files is Adding an Additional Page

It does work. What I said was ...

However what is odd is that when I open File1.pdf, it is 4 pages not 3 and in a wierd order.
Page1 is File1 and has a footer of C:\My Stuff\File 1.pdf • 1
Page2 is File3 and has a footer of C:\My Stuff\File 3.pdf • 1
Page3 is File2 and has a footer of C:\My Stuff\File 2.pdf • 2
Page4 is blank and has a footer of C:\My Stuff\File 3.pdf • 2

Any ideas why it would do this? This is not correct.

Author:  GaryR [ Fri Feb 11, 2011 5:46 pm ]
Post subject:  Re: Combining PDF Files is Adding an Additional Page

I was able to reproduce the issue using only the Combine PDF files sample code (Variant1) with no customizing so this might be a bug. Can anyone confirm for me?

For Example:
Take 3 one page simple PDFfiles (File1.pdf, File2.pdf, File3.pdf). Combine File1.pdf and File2.pdf into a new file named Combined.pdf. Open it and see that it is only 2 pages, just as we expected.

No take Combined.pdf and File3.pdf and combine them into a new file named Final.pdf. Open Final.pdf to see that it is 4 pages, not 3 as expected and the pages are out of order.

Author:  Thomas Hoevel [ Mon Feb 28, 2011 8:18 am ]
Post subject:  Re: Combining PDF Files is Adding an Additional Page

GaryR wrote:
I was able to reproduce the issue using only the Combine PDF files sample code (Variant1) with no customizing so this might be a bug. Can anyone confirm for me?

I'm afraid I don't fully understand what you are doing.
Without a fully working sample project (ZIP files can be uploaded here) I won't investigate this "issue".

Author:  GaryR [ Mon Feb 28, 2011 5:57 pm ]
Post subject:  Re: Combining PDF Files is Adding an Additional Page

I am combining 3 pdf files into 1 pdf file using the sample code.

I am trying to upload my project but it keeps telling me that it is too large so I have removed all but the form and 3 test pdf files. If this is not enough for you to see what is happening, let me know and I will get it to you.

Thanks!

Attachments:
Combining PDF Files.zip [243.14 KiB]
Downloaded 639 times

Author:  Thomas Hoevel [ Tue Mar 01, 2011 10:06 am ]
Post subject:  PEBKAC

GaryR wrote:
This is not correct.

This is correct.
No problem with PDFsharp.

File 1 has 1 page, file 2 has 1 page, file 3 has 1 page.
Your code combines files 1 and 2 creating a new file with 2 pages and overwrites file 1.
Then it combines the overwritten file 1 (which has 2 pages now) with file 3.

So you get the 1st page from the new file 1 which is page 1, then the 1st page from file 3 which is page 3, then the 2nd page from the new file 1 which is the original file 2, then an empty page because there is only 1 page in file 3.

PDFsharp works fine - and as I said the error is not in the codesnippet you were showing in this thread.

A more efficient code that keeps the output file open until all pages have been added will be faster - and will avoid this nasty side-effect.

Author:  GaryR [ Tue Mar 01, 2011 5:31 pm ]
Post subject:  Re: Combining PDF Files is Adding an Additional Page

This is making my head hurt :)

All files are only 1 page. So adding F2 to F1 should result in F1 with 2 pages. Then adding F3 to F1 should result in F1 with 3 pages. At least it seems like it should be like that to me. I guess I don't understand how it works.

You mentioned a way of keeping the file open so that the process is faster and so that there won't be additional pages added. Can you copy/pase a code example for me to look at? Thanks again for looking into this for me.

Author:  Thomas Hoevel [ Wed Mar 02, 2011 8:20 am ]
Post subject:  Re: Combining PDF Files is Adding an Additional Page

The combiner takes the first page from file 1, then the first page from file 2, then the second page from file 1, then the second page from file 2, ...
Empty pages are inserted if one file has more pages than the other.

In your case (second call), you merge the files "[1][2]" and "[3]" leading to "[1][3][2][<blank page>]".

I cannot write code for you.
I'd write a "Combiner" class with. Filename could be passed as a parameter to the constructor, then have an AddFile method and a Save or Close method. All you need is in the sample, requires just a little re-arranging.

Author:  GaryR [ Wed Mar 02, 2011 10:09 pm ]
Post subject:  Re: Combining PDF Files is Adding an Additional Page

Oh, I didn't mean for you to write code for me. Figured you would have some laying around that you could copy/paste. No worries I got it. I did as you suggested and only opened 1 file at a time and then didn't save the output doc until I was finished.

Thanks!

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/