Both PDFsharp and MigraDoc provide a lot of AddXxx functions. Typically these functions return the newly created objects.
Once you’ve learned the basic principles it’s quite easy to work with. Intellisense helps a lot then. And the new help system that integrates with the Visual Studio online help will provide information about any class in PDFsharp and MigraDoc – simply by hitting F1.
MigraDoc - Getting Started
A good place to start is the Samples folder that comes with the source code. This wiki also shows the
MigraDoc Samples.
We’ll discuss a few lines of the Hello World sample here.
We start with a new document:
Document document = new Document();
With MigraDoc, we don’t add pages, we add sections (at least one):
Section section = document.AddSection();
Adding text is simple:
section.AddParagraph("Hello, World!");
Adding empty paragraphs is even simpler:
section.AddParagraph();
Store the newly created object for modification:
Paragraph paragraph = section.AddParagraph();
paragraph.Format.Font.Color = Color.FromCmyk(100, 30, 20, 50);
paragraph.AddFormattedText("Hello, World!", TextFormat.Underline);
AddFormattedText also returns an object:
FormattedText ft = paragraph.AddFormattedText("Small text",
TextFormat.Bold);
ft.Font.Size = 6;
And there’s much more that can be added: AddTable, AddImage, AddHyperlink, AddBookmark, AddPageField, AddPageBreak, ...
With MigraDoc you can create PDF or RTF. Just select the appropriate renderer:
PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(false,
PdfFontEmbedding.Always);
Pass the document to the renderer:
pdfRenderer.Document = document;
Let the renderer do its job:
pdfRenderer.RenderDocument();
Save the PDF to a file:
string filename = "HelloWorld.pdf";
pdfRenderer.PdfDocument.Save(filename);
A PC application might show the file:
Process.Start(filename);