PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Saving One PdfPage to Two PdfDocuments via MemoryStream
https://forum.pdfsharp.net/viewtopic.php?f=8&t=4065
Page 1 of 1

Author:  pdfuser1 [ Mon Nov 25, 2019 9:46 pm ]
Post subject:  Saving One PdfPage to Two PdfDocuments via MemoryStream

In my project, I needed a way to
  • Create a bunch of individual one-page PDF files;
  • Save them to individual folders;
  • And then create a single PDF file cosisting of all the individual pages.

My first attempt at solving the problem worked, but I went the inefficient route of
  • Saving each individual one-page PDF file;
  • Re-reading each individual one-page PDF file from the file system;
  • Adding the re-read pages into a batch document;
  • And finally saving the batch file.

I wanted to avoid having to re-read the individual files from the file system. Naturally, you'd expect to be able to do (in pseudocode):
Code:
// 1. Create PdfDocuments pdfDoc1 and pdfDoc2
// 2. Create a PdfPage pdfPage1
// 3. Add pdfPage1 to pdfDoc1
// 4. Add pdfPage1 to pdfDoc2

or
Code:
// 1. Create PdfDocuments pdfDoc1 and pdfDoc2
// 2. Create a PdfPage pdfPage1
// 2a. Copy pdfPage1 to a new PdfPage pdfPage2
// 3. Add pdfPage1 to pdfDoc1
// 4. Add pdfPage2 to pdfDoc2

Unfortunately, the first approach won't work because a PdfPage can only be added to a single document. The second approach doesn't work because PdfPage.Copy() doesn't seem to work correctly in PDFsharp.

But you can still add a PdfPage to two different PdfDocuments by using MemoryStream. The revised approach will be as follows:
Code:
// 1. Create PdfDocuments pdfDoc1 and pdfDoc2
// 2. Create a PdfPage pdfPage1
// 3. Add pdfPage1 to pdfDoc1
// 4. Save the pdfDoc1 (with its one page) to MemoryStream ms1
// 5. Open ms1 as a PdfDocument and create a PdfPage pdfPage2 from it
// 6. Add pdfPage2 to pdfDoc2

The actual code is actually straight forward. Here is the gist:
Code:
''''''''''''''''''''''''''
' VB.NET
''''''''''''''''''''''''''

Dim pdfDoc1 As New PdfDocument
Dim pdfDoc2 As New PdfDocument
Dim pdfPage1 As New PdfPage

' Design / add info to your page
pdfDoc1.AddPage(pdfPage1)

Using ms1 As New MemoryStream
     pdfDoc1.Save(ms1)
     pdfDoc2.AddPage(PdfReader.Open(ms1, PdfDocumentOpenMode.Import).Pages(0))
End Using

' Now both pdfDoc1 and pdfDoc2 have the contents of pdfPage1
' You can save each file to the file system when ready
pdfDoc1.Save("file1.pdf")
pdfDoc2.Save("file2.pdf")

Code:
////////////////////////
// C# (Untested)
////////////////////////

PdfDocument pdfDoc1 = new PdfDocument();
PdfDocument pdfDoc2 = new PdfDocument();
PdfPage pdfPage1 = new PdfPage();

// Design / add info to your page
pdfDoc1.AddPage(pdfPage1);

using (MemoryStream ms1 = new MemoryStream())
{
     pdfDoc1.Save(ms1);
     pdfDoc2.AddPage(PdfReader.Open(ms1, PdfDocumentOpenMode.Import).Pages[0]);
}

// Now both pdfDoc1 and pdfDoc2 have the contents of pdfPage1
// You can save each file to the file system when ready
pdfDoc1.Save("file1.pdf");
pdfDoc2.Save("file2.pdf");

Author:  rsoeung [ Fri Jan 22, 2021 5:42 pm ]
Post subject:  Re: Saving One PdfPage to Two PdfDocuments via MemoryStream

Once a page is created and added to a PdfDocument, the page's Owner property is set to that document and there's no way of overwriting the Owner. So you'd have to just get the contents of the page.

Code:

string doc1 = @"C:\doc1.pdf";
string doc2 = @"C:\doc2.pdf";

// variable to hold page contents
CSequence contents;

// create 1st document and page
var d1 = new PdfDocument(doc1);
var page = d1.AddPage();

// you'd have to create a method to add contents to page
// refer to http://www.pdfsharp.net/wiki/HelloWorld-sample.ashx for example
AddContentToPage(page);

// close document for content to write to page
d1.Close();

// reopen 1st document to get page contents
var doc = PdfReader.Open(doc1, PdfDocumentOpenMode.Modify);
// grab page's contents
contents = ContentReader.ReadContent(doc.Pages[0]);
doc.Close();

// repeat the below 3 lines to add same content to different pdf documents
var d2 = new PdfDocument(doc2);
d2.AddPage().Contents.ReplaceContent(contents);
d2.Close();





Best,
Reas

Author:  pdfuser1 [ Sat Jan 23, 2021 2:22 am ]
Post subject:  Re: Saving One PdfPage to Two PdfDocuments via MemoryStream

rsoeung wrote:
Once a page is created and added to a PdfDocument, the page's Owner property is set to that document and there's no way of overwriting the Owner. So you'd have to just get the contents of the page.

Thank you for commenting, but I am not sure how it relates to my original post. Did you mean to reply to some other topic instead?

Author:  rsoeung [ Mon Feb 01, 2021 10:42 pm ]
Post subject:  Re: Saving One PdfPage to Two PdfDocuments via MemoryStream

pdfuser1 wrote:
Thank you for commenting, but I am not sure how it relates to my original post. Did you mean to reply to some other topic instead?


In your example code you are trying to reuse the same PdfPage for two different documents. That's not possible because once a PdfPage is added to the first document, it's owner property is set to that document. Therefore, to make copies of this page you'd have to extract it's contents and insert it into the 2nd document's PdfPage.

Author:  pdfuser1 [ Tue Feb 02, 2021 3:22 am ]
Post subject:  Re: Saving One PdfPage to Two PdfDocuments via MemoryStream

rsoeung wrote:
In your example code you are trying to reuse the same PdfPage for two different documents.
...
Therefore, to make copies of this page you'd have to extract it's contents and insert it into the 2nd document's PdfPage.

You may have missed where I said "I wanted to avoid having to re-read the individual files from the file system". Unless I am misreading your code, I believe you are doing just that while my solution avoids it.

P.S. Just to give you a bit more context, see the details of my use case here. The MemoryStream solution saved 25% off the total run time.

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