Rendering
Version 6.1.0
In this article
This article describes how a MigraDoc document is rendered to a PDF or RTF file.
Rendering a PDF file
When a document is rendered to PDF, the flattening process will assign format values from styles and inheritance (see Formatting for more information) to the concrete objects (see Flattening). Furthermore, the final text flow is determined, all objects are positioned, and the pages are created only now. You get a ready to print document file, which is the primary purpose of PDF files.
You can render and save your document to a PDF file with these lines of code:
// Create a PDF renderer for the MigraDoc document.
var pdfRenderer = new PdfDocumentRenderer();
// Associate the MigraDoc document with a renderer.
pdfRenderer.Document = document;
// Layout and render document to PDF.
pdfRenderer.RenderDocument();
…
// Save the document.
pdfRenderer.Save(Filename);
See full RenderingPdfFile sample in PDFsharp.Sample repository. Show resulting PDF
Additional to the settings shown in Document settings there are settings you can configure at the PdfDocument object of the PdfDocumentRenderer instance. The PdfDocument is the PDFsharp object your MigraDoc document is rendered to before it is saved as a PDF file. It’s no problem to configure it before the RenderDocument method is called. Here are a few examples of what you can do:
// Let the PDF viewer show two pages of the file.
pdfRenderer.PdfDocument.PageLayout = PdfPageLayout.TwoPageLeft;
// Let the PDF viewer show the full pages.
pdfRenderer.PdfDocument.ViewerPreferences.FitWindow = true;
// Create the PDF file without compressing the content streams.
pdfRenderer.PdfDocument.Options.CompressContentStreams = false;
See full ChangingPdfSettings sample in PDFsharp.Sample repository. Show resulting PDF
Rendering an RTF file
Unlike PDF files, RTF files are not paginated "ready to print" but have a dynamic content. For example, you can change any style of the RTF or the page format. Or you can manually add, modify, or delete paragraphs. So, MigraDoc will not calculate final positions of objects and insert page breaks. This is all up to the application showing the file and not fixed at any time.
Only the flattening process is run to assign format values from styles and inheritance (see Formatting for more information) to the concrete objects (see Flattening).
You can render and save your document to an RTF file with these lines of code:
// Create an RTF renderer for the MigraDoc document.
var rtfRenderer = new RtfDocumentRenderer();
// Layout and render document to RTF.
rtfRenderer.Render(document, Filename, Environment.CurrentDirectory);
See full RenderingRtfFile sample in PDFsharp.Sample repository. Show resulting RTF
Note that the RtfDocumentRenderer unlike PdfDocumentRenderer requires a workingDirectory parameter (see Working directory) to render the file.
Environment.CurrentDirectory
passes the working directory of the application, which is also used as default value in PdfDocumentRenderer.
Working directory
At the PdfDocumentRenderer or RtfDocumentRenderer instance you can set the WorkingDirectory property. When saving the document to a relative path, this value will be the root to create your document at. If it is not set, the working directory of the application is used instead.
WorkingDirectory is furthermore used to control where images with relative paths shall be looked for.
You can find an example of using the WorkingDirectory property at Contents / Images.
Flattening
MigraDoc documents are highly dynamic and the format values to be applied from styles and inheritance are not set at the concrete objects while you assemble your document (see Formatting for more information).
The flattening process handles the styles and inheritance and applies the actual format values to the objects. It is started automatically when you render your document.
When using PdfDocumentRenderer you can also call the PrepareRenderPages method to execute the flattening process manually. Bear in mind that from then on nothing more will be applied by styles and inheritance, since all format attributes have a definite value.