Search Results for

    Show / Hide Table of Contents

    HelloWorld

    Version 6.1.0

    In this article

    • Introduction
    • Setting the font resolver
    • Creating the document
    • Rendering the document

    This article describes the simple HelloWorld sample of the PDFsharp.samples solution.

    Introduction

    The HelloWorld sample shows how to create a small MigraDoc document. It contains formatted text, a footer, a field inserting the current date and an image. The "HelloWorld,MigraDoc" project of the PDFsharp.Samples solution contains the code of this sample. You can find it here on GitHub.

    Below all the code of the sample is explained, structured in Creating the document, which explains the assembling of the document and its content, and Rendering the document, which means preparing and saving the document as a file.

    Setting the font resolver

    To make all fonts needed for the document resolvable, a font resolver is set when using the Core build of MigraDoc (see Choose a PDFsharp build):

    #if CORE
        // Core build does not use Windows fonts,
        // so set a FontResolver that handles the fonts our samples need.
        GlobalFontSettings.FontResolver = new SamplesFontResolver();
    #endif
    

    See full Program.cs file in PDFsharp.Sample repository.

    For your own projects you should create your own font resolver which supports all the fonts needed. But pay attention that SamplesFontResolver loads the fonts from the repository-specific assets folder of PDFsharp.Samples, while you must provide your font files in an appropriate way that works without repository.

    To learn about font resolving see Document Object Model / Document / MigraDoc settings / Font resolving.

    Creating the document

    Now, the sample executes its CreateDocument function, which contains the following lines of code:

    // Create a new MigraDoc document.
    var document = new Document();
    
    // Add a section to the document.
    var section = document.AddSection();
    

    See Program class of the HelloWorld sample in PDFsharp.Sample repository. Show resulting PDF

    These lines create the MigraDoc document and a section, as all contents of a document are organized in sections.

    To learn about how documents are structured see Document Object Model / Document / Document structure.

    The method continues with the following lines:

    // Add a paragraph to the section.
    var paragraph = section.AddParagraph();
    
    // Set font color.
    paragraph.Format.Font.Color = Colors.DarkBlue;
    
    // Add some text to the paragraph.
    paragraph.AddFormattedText("Hello, World!", TextFormat.Bold);
    

    See Program class of the HelloWorld sample in PDFsharp.Sample repository. Show resulting PDF

    These lines add a paragraph to the section. The font color of the whole paragraph is set to DarkBlue using direct formatting. A formatted text with the content "Hello, World!" is added to the paragraph. The whole content of the FormattedText instance is set to bold (also direct formatting here).

    To learn about paragraphs and formatted text see Document Object Model / Contents / Text.

    For information about direct formatting or, alternatively, using styles see Document Object Model / Document / Formatting.

    To explore the possibilities of formatting a font see Document Object Model / Formats / Font.

    Color is a property of the type Color (see Document Object Model / Formats / Colors for more information).

    After that a footer is created and filled:

    // Create the primary footer.
    var footer = section.Footers.Primary;
    
    // Add content to footer.
    paragraph = footer.AddParagraph();
    paragraph.Add(new DateField { Format = "yyyy/MM/dd HH:mm:ss" });
    paragraph.Format.Alignment = ParagraphAlignment.Center;
    

    See Program class of the HelloWorld sample in PDFsharp.Sample repository. Show resulting PDF

    Here a paragraph is added to the primary footer, which will be the only one in this document. A date field is added to the paragraph and the whole paragraph is set to center alignment using direct formatting.

    To learn about working with footers see Document Object Model / Contents / Headers & footers.

    For information about fields see Document Object Model / Contents / Fields.

    To explore the possibilities of formatting a paragraph see Document Object Model / Formats / Paragraph.

    Finally, an image is added to the section:

    // Add MigraDoc logo.
    string imagePath = IOUtility.GetAssetsPath(@"migradoc/images/MigraDoc-128x128.png")!;
    document.LastSection.AddImage(imagePath);
    

    See Program class of the HelloWorld sample in PDFsharp.Sample repository. Show resulting PDF

    The GetAssetsPath call only gets the path to the image, so important is only the second line, which adds the image to the section.

    For information about adding and formatting images see Document Object Model / Contents / Images.

    After returning from CreateDocument the Normal style is changed:

    var style = document.Styles[StyleNames.Normal]!;
    style.Font.Name = "Arial";
    

    See Program class of the HelloWorld sample in PDFsharp.Sample repository. Show resulting PDF

    Normal is the default style all other paragraph styles derive from. So, Arial will be inherited to all styles and to all objects of the document, as long it doesn’t get overridden.

    For information about using styles and their inheritance see Document Object Model / Document / Formatting.

    Rendering the document

    When the document creation is finished, rendering starts. At first, a PdfDocumentRenderer instance is created:

    // Create a renderer for the MigraDoc document.
    var pdfRenderer = new PdfDocumentRenderer
    {
        // Associate the MigraDoc document with a renderer.
        Document = document,
        PdfDocument =
        {
            // Change some settings before rendering the MigraDoc document.
            PageLayout = PdfPageLayout.SinglePage,
            ViewerPreferences =
            {
                FitWindow = true
            }
        }
    };
    

    See Program class of the HelloWorld sample in PDFsharp.Sample repository. Show resulting PDF

    These lines create the PdfDocumentRenderer instance, associate the created document with it, and set some settings of the PDF file.

    Then the MigraDoc document is rendered and saved:

    // Layout and render document to PDF.
    pdfRenderer.RenderDocument();
    
    // Save the document...
    var filename = PdfFileUtility.GetTempPdfFullFileName("samples-MigraDoc/HelloWorldMigraDoc");
    pdfRenderer.PdfDocument.Save(filename);
    
    // ...and start a viewer.
    PdfFileUtility.ShowDocument(filename);
    

    See Program class of the HelloWorld sample in PDFsharp.Sample repository. Show resulting PDF

    RenderDocument renders the MigraDoc Document instance to a PDFsharp PdfDocument instance. Save then saves the PDFsharp document to the given file.

    You can ignore the other lines, as they only get the filename and start the file in a PDF viewer.

    For information about rendering and saving a document to a PDF file or, alternatively, to an RTF file see Document Object Model / Document / Rendering.

    • Edit this page
    In this article
    Generated by DocFX  |   Privacy policy  |   Legal notice
    Back to top