PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri Aug 14, 2020 10:35 pm

All times are UTC

Forum rules

Please read this before posting on this forum: Forum Rules

Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Sat Oct 28, 2017 7:01 pm 

Joined: Tue Sep 05, 2017 4:42 pm
Posts: 5
Bear with me, this is hard to explain.....

I am using MigraDoc to create a PDF, very successfully. I have now identified a requirement to take the pages from a second, existing PDF and effectively embed each page as an image into the PDF that I am creating. I have got this far:

        private void AddPdf(Cell cell, byte[] pdf)
            var stream = new MemoryStream(pdf);
            var document = PdfReader.Open(stream);

            var pageCount = 0;

            foreach (var pdfPage in document.Pages)

                cell.AddParagraph("Page " + pageCount);

                // Need to add the PDF page itself here

I have a PDF in the byte array, and I think it is being understood because the number of pages found is correct.

I have read here that it is possible to create an image from a PDF page. However, I am struggling to get anywhere with the PdfPage object that I have. I suspect I am muddled because the example is based on MigraDoc, but I have a PDFsharp object.

I did discover that if I do this, with my PDF byte array in content:

var image = "base64:" + Convert.ToBase64String(content);

Then I do appear to get the first page of the PDF embedded as an image in the document that I am creating. That is very close to what I need, but I need to get each page in turn, not just the first.

I feel like I am very close and just need a push in the right direction.



Reply with quote  
PostPosted: Sun Oct 29, 2017 11:57 am 
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 563
Location: CCAA
NickL wrote:
I feel like I am very close and just need a push in the right direction.
There are two products: PDFsharp and MigraDoc.
You are mixing MigraDoc and PDfsharp.

One option: save the byte[] to a temporary file and append the desired page number to the filename (giving e.g. "c:\test.pdf#2" for the second page). You can still use PdfReader.Open to query the page count, but apart from that you only use MigraDoc.

The other option does not need a temporary file, but is more difficult to explain and to implement.
See here:
http://pdfsharp.net/wiki/MixMigraDocAnd ... ample.ashx
The loop from the first link cannot just be used to show progress, it can also be used to draw PDF pages from the byte[] onto PDF pages created by MigraDoc - you just have to figure out where to draw them.

Best regards
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)

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

All times are UTC

Who is online

Users browsing this forum: Baidu [Spider] and 33 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