Romias
Romias

Reputation: 14133

Adding multi page MigraDoc document to a PDFSharp document

In the Migradoc and PDFSharp samples page, there is one that draws a Migradoc Document into a PDFSharp Document: http://www.pdfsharp.net/wiki/MixMigraDocAndPdfSharp-sample.ashx

But what about if the Migradoc document I want to render has more than one page? In Migradoc you don't handle pages.. it is done automatically.

EDIT: FOUND MY WAY

Well, once you "Prepare()" the document... you have the FormattedDocument() method, and there you can see how many pages it ends up having. I added my own response to this below.

Upvotes: 2

Views: 2174

Answers (1)

Romias
Romias

Reputation: 14133

Once you Prepare() the Migradoc document, you have the layout of your document, and the number of pages. So, you just need to loop over each page of the MigraDoc document, and for each one you need to create a page in the PdfDocument:

private void SampleMultiplePage(ref PdfDocument document, Document migraDocument)
        {
            var pdfRenderer = new DocumentRenderer(migraDocument);

            pdfRenderer.PrepareDocument();

            int pages = pdfRenderer.FormattedDocument.PageCount;
            for (int i = 1; i <= pages; ++i)
            {
                var page = document.AddPage();

                PageInfo pageInfo = pdfRenderer.FormattedDocument.GetPageInfo(i);
                page.Width = pageInfo.Width;
                page.Height = pageInfo.Height;
                page.Orientation = pageInfo.Orientation;

                using (XGraphics gfx = XGraphics.FromPdfPage(page))
                {
                    // HACK²
                    gfx.MUH = PdfFontEncoding.Unicode;
                    gfx.MFEH = PdfFontEmbedding.Default;

                    pdfRenderer.RenderPage(gfx, i);
                }
            }
        }

Upvotes: 5

Related Questions