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

combine PdfDocument objects
http://forum.pdfsharp.de/viewtopic.php?f=2&t=788
Page 1 of 1

Author:  mikesowerbutts [ Mon Jul 13, 2009 4:51 pm ]
Post subject:  combine PdfDocument objects

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

Author:  Thomas Hoevel [ Tue Jul 14, 2009 8:39 am ]
Post subject:  Re: combine PdfDocument objects

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.

Author:  mikesowerbutts [ Tue Jul 14, 2009 9:06 am ]
Post subject:  Re: combine PdfDocument objects

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

Author:  Thomas Hoevel [ Tue Jul 14, 2009 9:40 am ]
Post subject:  Re: combine PdfDocument objects

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).

Author:  mikesowerbutts [ Tue Jul 14, 2009 9:51 am ]
Post subject:  Re: combine PdfDocument objects

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?

Author:  Thomas Hoevel [ Tue Jul 14, 2009 11:12 am ]
Post subject:  Re: combine PdfDocument objects

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.

Author:  mikesowerbutts [ Tue Jul 14, 2009 11:38 am ]
Post subject:  Re: combine PdfDocument objects

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!

Author:  Thomas Hoevel [ Tue Jul 14, 2009 12:41 pm ]
Post subject:  Re: combine PdfDocument objects

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.

Author:  mikesowerbutts [ Tue Jul 14, 2009 12:43 pm ]
Post subject:  Re: combine PdfDocument objects

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

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