PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Tue Mar 19, 2024 10:40 am

All times are UTC




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

Joined: Sat Mar 14, 2015 10:15 am
Posts: 905
Location: CCAA
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/

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


Top
 Profile  
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 2 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