PDFsharp & MigraDoc Foundation

Embedding pages from one PDF in another
Page 1 of 1

Author:  NickL [ Sat Oct 28, 2017 7:01 pm ]
Post subject:  Embedding pages from one PDF in another

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.



Author:  TH-Soft [ Sun Oct 29, 2017 11:57 am ]
Post subject:  Re: Embedding pages from one PDF in another

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.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group