PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sun Jul 21, 2024 3:56 pm

All times are UTC

Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Tue Aug 18, 2015 12:10 pm 
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 951
Location: CCAA

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

If you want to have a progress indicator while creating the PDF, you will need a few lines more:
// 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).

// 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:
// 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/

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  [ 1 post ] 

All times are UTC

Who is online

Users browsing this forum: No registered users and 3 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