PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Apr 27, 2024 5:47 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: Mon Jul 13, 2009 4:51 pm 
Offline
Supporter

Joined: Fri May 15, 2009 3:28 pm
Posts: 96
Hi,

I have soem classes, each of which create a seperate printout, sometimes, these printouts need to be combined. I know I could save the files, then combine them into one PDF, but it would be better if I could just combine the PdfDocument objects into one PdfDocument object and then save this.

Is there a way? the samples only seem to cover combining saved files.

I am currently trying to do it with a function like this:
Code:
public PdfDocument combineDocs(PdfDocument doc1, PdfDocument doc2)
        {
            PdfDocument _combinedDoc = new PdfDocument();
                for (int i = 0; i < doc1.PageCount; i++)
                {
                    try
                    {
                        _combinedDoc.InsertPage(_combinedDoc.PageCount + 1, doc1.Pages[i]);
                    }
                    catch
                    {

                    }
                }
                for (int q = doc1.PageCount; q <= doc2.PageCount; q++)
                {
                    _combinedDoc.InsertPage(_combinedDoc.PageCount + 1, doc2.Pages[q]);
                }
            return _combinedDoc;
        }

but this errors saying i need to set PdfDocumentOpenMode to modify - and this enumerator only seems to be needed when opening a saved file, rather than when manipulating PdfDocument objects.

is what I am trying to do possible? or do I have to save each document, then combine them as per the samples?

thanks!

Mike


Top
 Profile  
Reply with quote  
PostPosted: Tue Jul 14, 2009 8:39 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
Hi!

I think the clean solution is modifying your classes to accept a PdfDocument as an input parameter so your classes emit their pages directly to the correct document (maybe by having two constructors with PdfDocument as an "optional" parameter).

I dunno if calling Clone() will help:
Using "(PdfPage)(doc1.Pages[i].Clone())" instead of "doc1.Pages[i]".
If this works it would be a simple and quick solution.

To avoid file IO you can still save the PdfDocuments to MemoryStreams and process them like the samples do.

I don't know how many classes you have, but I'd prefer the clean solution.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Jul 14, 2009 9:06 am 
Offline
Supporter

Joined: Fri May 15, 2009 3:28 pm
Posts: 96
Hi Thomas,

using .Clone returns a PdfDictionary object, rather then the required page - so it seems this isnt the avenue to persue!

As I have quite a few classes already build and the code in them is quite extensive, overloading them would mean maintaining lots and lots of code, so I will try the memory streams method.

thnaks for your help - its much appreciated!

Mike


Top
 Profile  
Reply with quote  
PostPosted: Tue Jul 14, 2009 9:40 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
mikesowerbutts wrote:
using .Clone returns a PdfDictionary object, rather then the required page - so it seems this isnt the avenue to persue!

Have you tried it?
If the cast "(PdfPage)" works then it's a PdfPage ... (I haven't tested it but I bet the cast works).

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Jul 14, 2009 9:51 am 
Offline
Supporter

Joined: Fri May 15, 2009 3:28 pm
Posts: 96
Hi Thomas,

I stupidly didnt even think about casting it! it does work, but when I try to call the .Save function to save the combined doc to a file I get the following error message:

"Item has already been added. Key in dictionary: '5 0' Key being added: '5 0'"

Any ideas?


Top
 Profile  
Reply with quote  
PostPosted: Tue Jul 14, 2009 11:12 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
Sorry, Mike!

Save to MemoryStream, read from MemoryStream (or create a XForm) - should be a few lines of code only and it'll be the perfect Clone function for a whole document.

BTW: I'm afraid that PDF files will be smaller if you implement the clean solution.
If file size matters, this is another incentive for the clean solution.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Jul 14, 2009 11:38 am 
Offline
Supporter

Joined: Fri May 15, 2009 3:28 pm
Posts: 96
How do I save to a memorystream?

I have been searching around on the forum this morning and cant really find how to do it!


Top
 Profile  
Reply with quote  
PostPosted: Tue Jul 14, 2009 12:41 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
You can call PdfDocument.Save() with either a filename or a stream.
Just pass in a newly created MemoryStream.

Don't forget to set the position to 0 before reading from that stream (with the Modify flag).

Code:
public void Save(Stream stream, bool closeStream)

I think I'd try setting closeStream to false in that scenario.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Jul 14, 2009 12:43 pm 
Offline
Supporter

Joined: Fri May 15, 2009 3:28 pm
Posts: 96
hi, thanks - i actually just worked this out! i had been trying this, this morning, but not using the additional parameter so the stream doesnt close.

pretty much sorted now.

Mike


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 342 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