Welcome GuestLogin

PDFsharp and MigraDoc Wiki

RSS RSS

Navigation




Quick Search
»

PoweredBy

Visit the new Website for PDFsharp & MigraDoc Foundation 6.0 for .NET 6 and find information about the new version for Windows, Linux, and other platforms.

<< 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.


Visit the new Website for PDFsharp & MigraDoc Foundation 6.0 for .NET 6 and find information about the new version for Windows, Linux, and other platforms.



Miscellaneous
Home
PDFsharp
FAQ
Samples
Articles
MigraDoc
FAQ
Samples
Articles

ScrewTurn Wiki version 3.0.5.600. Some of the icons created by FamFamFam.

Impressum - Privacy Policy, Data Protection Declaration, Legal Notice