PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Dec 07, 2019 11:42 pm

All times are UTC




Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Mon Nov 25, 2019 9:46 pm 
Offline

Joined: Fri Nov 15, 2019 5:10 am
Posts: 5
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");


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


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