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");

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