MigraDoc Sample: Hello World

Modified on 2015/09/14 10:17 by Thomas Hövel — Categorized as: MigraDoc Samples, Samples

<< Previous^UpNext >>
This sample is the obligatory Hello World program for MigraDoc documents.

PDF Output File

See the PDF file created by this sample: output.pdf (9 kB)

Source Code Listing

The Main method:
static void Main(string[] args)
{
  // Create a MigraDoc document
  Document document = CreateDocument();
  document.UseCmykColor = true;
 
  // ===== Unicode encoding and font program embedding in MigraDoc is demonstrated here =====
 
  // A flag indicating whether to create a Unicode PDF or a WinAnsi PDF file.
  // This setting applies to all fonts used in the PDF document.
  // This setting has no effect on the RTF renderer.
  const bool unicode = false;
 
  // An enum indicating whether to embed fonts or not.
  // This setting applies to all font programs used in the document.
  // This setting has no effect on the RTF renderer.
  // (The term 'font program' is used by Adobe for a file containing a font. Technically a 'font file'
  // is a collection of small programs and each program renders the glyph of a character when executed.
  // Using a font in PDFsharp may lead to the embedding of one or more font programms, because each outline
  // (regular, bold, italic, bold+italic, ...) has its own fontprogram)
  const PdfFontEmbedding embedding = PdfFontEmbedding.Always;
 
  // ========================================================================================
 
  // Create a renderer for the MigraDoc document.
  PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(unicode, embedding);
 
  // Associate the MigraDoc document with a renderer
  pdfRenderer.Document = document;
 
  // Layout and render document to PDF
  pdfRenderer.RenderDocument();
 
  // Save the document...
  const string filename = "HelloWorld.pdf";
  pdfRenderer.PdfDocument.Save(filename);
  // ...and start a viewer.
  Process.Start(filename);
}

The CreateDocument method:
/// 
/// Creates an absolutely minimalistic document.
/// 
static Document CreateDocument()
{
  // Create a new MigraDoc document
  Document document = new Document();
 
  // Add a section to the document
  Section section = document.AddSection();
 
  // Add a paragraph to the section
  Paragraph paragraph = section.AddParagraph();
 
  paragraph.Format.Font.Color = Color.FromCmyk(100, 30, 20, 50);
 
  // Add some text to the paragraph
  paragraph.AddFormattedText("Hello, World!", TextFormat.Bold);
 
  return document;
}

Note: The samples on this site usually show and discuss code snippets only. The complete source code of the samples with solutions for Visual Studio is available from the download area on CodePlex.