PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.net/

MigraDoc: Show Progress While Rendering a Document
http://forum.pdfsharp.net/viewtopic.php?f=8&t=3172
Page 1 of 1

Author:  TH-Soft [ Tue Aug 18, 2015 12:10 pm ]
Post subject:  MigraDoc: Show Progress While Rendering a Document

Hi!

The MigraDoc samples only need one line of code to create a PDF file for a MigraDoc document:
Code:
// Classical rendering without progress indicator.
// Layout and render document to PDF
pdfRenderer.RenderDocument();


If you want to have a progress indicator while creating the PDF, you will need a few lines more:
Code:
// Alternative rendering with progress indicator.
// Set a callback for phase 1.
pdfRenderer.DocumentRenderer.PrepareDocumentProgress += PrepareDocumentProgress;
// Now start phase 1: Preparing pages (i.e. calculate the layout).
pdfRenderer.PrepareRenderPages();

// Now phase 2: create the PDF pages.
Console.WriteLine("\r\nRendering document ...");

int pages = pdfRenderer.DocumentRenderer.FormattedDocument.PageCount;
for (int i = 1; i <= pages; ++i)
{
    var page = pdfRenderer.PdfDocument.AddPage();
    Console.Write("\rRendering page " + i + "/" + pages);

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

    using (XGraphics gfx = XGraphics.FromPdfPage(page))
    {
        gfx.MUH = pdfRenderer.Unicode ? PdfFontEncoding.Unicode : PdfFontEncoding.WinAnsi;
        gfx.MFEH = pdfRenderer.FontEmbedding;
        pdfRenderer.DocumentRenderer.RenderPage(gfx, i);
    }
}
Console.WriteLine("\r\nSaving document ...");


The callback routine used for phase 1:
Code:
// Sample progress callback, called while preparing a document (while MigraDoc measures items, creates line breaks, page breaks etc.).
private static void PrepareDocumentProgress(object sender, DocumentRenderer.PrepareDocumentProgressEventArgs e)
{
    int percent = e.Value * 100 / e.Maximum;
    if (percent != lastPercentHack)
    {
        Console.Write("\rPreparing " + percent + "%");
    }
}
private static int lastPercentHack = -1;


See my blog for more information:
http://developer.th-soft.com/developer/ ... -document/

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