PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sun Apr 28, 2024 6:04 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: Wed Feb 09, 2011 11:01 pm 
Offline

Joined: Sat Feb 05, 2011 7:30 pm
Posts: 15
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?


Top
 Profile  
Reply with quote  
PostPosted: Thu Feb 10, 2011 8:13 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
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:

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu Feb 10, 2011 6:01 pm 
Offline

Joined: Sat Feb 05, 2011 7:30 pm
Posts: 15
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.


Top
 Profile  
Reply with quote  
PostPosted: Fri Feb 11, 2011 5:46 pm 
Offline

Joined: Sat Feb 05, 2011 7:30 pm
Posts: 15
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.


Top
 Profile  
Reply with quote  
PostPosted: Mon Feb 28, 2011 8:18 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
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".

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Feb 28, 2011 5:57 pm 
Offline

Joined: Sat Feb 05, 2011 7:30 pm
Posts: 15
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 476 times
Top
 Profile  
Reply with quote  
 Post subject: PEBKAC
PostPosted: Tue Mar 01, 2011 10:06 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Mar 01, 2011 5:31 pm 
Offline

Joined: Sat Feb 05, 2011 7:30 pm
Posts: 15
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.


Top
 Profile  
Reply with quote  
PostPosted: Wed Mar 02, 2011 8:20 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Mar 02, 2011 10:09 pm 
Offline

Joined: Sat Feb 05, 2011 7:30 pm
Posts: 15
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!


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 341 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group