### C# Image Example (Object Initializer)
Source: https://docs.pdfsharp.net/MigraDoc/Overview/GrammarByExample.html
A more concise C# example using object initializers.
```C#
var image = new Image("logo_e.gif")
{
Height = "1cm"
};
```
--------------------------------
### Inheritance Example with Comments
Source: https://docs.pdfsharp.net/MigraDoc/DOM/Document/Formatting.html
The same inheritance example with added comments explaining inherited and overridden properties.
```csharp
// Set and use styles and direct formatting to show, how inheritance works.
// Overridden and inherited values are explained for each style and document object in the following comments.
// Set Normal style.
var style = document.Styles[StyleNames.Normal];
style.Font.Name = "Times New Roman";
style.Font.Size = Unit.FromPoint(12);
// Set Heading1 style.
style = document.Styles[StyleNames.Heading1];
style.Font.Size = Unit.FromPoint(20);
style.Font.Bold = true;
style.ParagraphFormat.SpaceBefore = Unit.FromPoint(20); // Overrides 'Unit.FromPoint(12)' from Normal.
// 'Font.Name = "Times New Roman"' will be inherited from Normal.
// Set Heading2 style.
style = document.Styles[StyleNames.Heading2];
style.Font.Size = Unit.FromPoint(18); // Overrides 'Unit.FromPoint(20)' from Heading1.
style.Font.Italic = true;
style.ParagraphFormat.SpaceBefore = Unit.FromPoint(10); // Overrides 'Unit.FromPoint(20)' from Heading1.
style.ParagraphFormat.SpaceAfter = Unit.FromPoint(10);
// 'Font.Bold = true' will be inherited from Heading1.
// 'Font.Name = "Times New Roman"' will be inherited from Heading1 > Normal.
…
section.AddParagraph("Example for inheritance.");
// 'Font.Name = "Times New Roman"' will be inherited from Normal.
// 'Font.Size = Unit.FromPoint(12)' will be inherited from Normal.
var paragraph1 = section.AddParagraph("Heading 1: ");
paragraph1.Style = StyleNames.Heading1;
paragraph1.Format.Font.Underline = Underline.Single;
// 'Font.Size = Unit.FromPoint(20)' will be inherited from Heading1.
// 'Font.Bold = true' will be inherited from Heading1.
// 'ParagraphFormat.SpaceBefore = Unit.FromPoint(20)' will be inherited from Heading1.
// 'Font.Name = "Times New Roman"' will be inherited from Heading1 > Normal.
var formattedText1 = paragraph1.AddFormattedText("Red");
formattedText1.Color = Colors.Red;
formattedText1.Bold = false; // Overrides 'true' from paragraph1 > Heading1.
// 'Font.Underline' = will be inherited from paragraph1.
// 'Font.Size = Unit.FromPoint(20)' will be inherited from paragraph1 > Heading1.
// 'ParagraphFormat.SpaceBefore = Unit.FromPoint(20)' from paragraph1 > Heading1 is not relevant for formattedText1.
```
--------------------------------
### Inheritance Example with Comments
Source: https://docs.pdfsharp.net/MigraDoc/DOM/Contents/Tables.html
The same code as the previous example, but with added comments explaining inherited or overridden properties at each level.
```csharp
// Set and use direct formatting at different levels to show how inheritance works in tables.
// Overridden and inherited values are explained for each document object in the following comments.
// Add table and set format.
var table = section.AddTable();
table.Borders.Visible = true;
table.Format.Font.Size = Unit.FromPoint(8);
// Add first column.
var columnA = table.AddColumn();
// 'Borders.Visible = true' will be inherited from table.
// 'Format.Font.Size = Unit.FromPoint(8)' will be inherited from table.
// Add second column and set format.
var columnB = table.AddColumn();
columnB.Format.Font.Bold = true;
// 'Borders.Visible = true' will be inherited from table.
// 'Format.Font.Size = Unit.FromPoint(8)' will be inherited from table.
// Add third column and set format.
var columnC = table.AddColumn();
columnC.Format.Alignment = ParagraphAlignment.Center;
columnC.Format.Font.Color = Colors.Red;
// 'Borders.Visible = true' will be inherited from table.
// 'Format.Font.Size = Unit.FromPoint(8)' will be inherited from table.
// Set format for all rows.
table.Rows.Height = Unit.FromPoint(25);
// Add first row and set format.
var row1 = table.AddRow();
row1.VerticalAlignment = VerticalAlignment.Center;
// 'Borders.Visible = true' will be inherited from table.
// 'Format.Font.Size = Unit.FromPoint(8)' will be inherited from table.
// 'Height = Unit.FromPoint(25)' will be inherited from rows.
// Get first cell of row1.
var cellA1 = row1[0];
// 'Borders.Visible = true' will be inherited from row1 (same value as columnA) > table.
// 'Format.Font.Size = Unit.FromPoint(8)' will be inherited from row1 (same value as columnA) > table.
// 'Height = Unit.FromPoint(25)' will be inherited from row1 > rows.
// 'VerticalAlignment = Center' will be inherited from row1.
// Add paragraph to cellA1.
cellA1.AddParagraph("Text A1");
// 'Format.Font.Size = Unit.FromPoint(8)' will be inherited from cellA1 > row1 (same value as columnA) > table.
// Get second cell of row1.
var cellB1 = row1[1];
```
--------------------------------
### C# Image Example (Basic)
Source: https://docs.pdfsharp.net/MigraDoc/Overview/GrammarByExample.html
The equivalent of the MDDDL image example in C#.
```C#
var image = new Image("logo_e.gif");
image.Height = "1cm";
```
--------------------------------
### Inheritance Example
Source: https://docs.pdfsharp.net/MigraDoc/DOM/Document/Formatting.html
Demonstrates how styles and direct formatting are applied and inherited in PDFsharp.
```csharp
// Set and use styles and direct formatting to show, how inheritance works.
// Set Normal style.
var style = document.Styles[StyleNames.Normal];
style.Font.Name = "Times New Roman";
style.Font.Size = Unit.FromPoint(12);
// Set Heading1 style.
style = document.Styles[StyleNames.Heading1];
style.Font.Size = Unit.FromPoint(20);
style.Font.Bold = true;
style.ParagraphFormat.SpaceBefore = Unit.FromPoint(20);
// Set Heading2 style.
style = document.Styles[StyleNames.Heading2];
style.Font.Size = Unit.FromPoint(18);
style.Font.Italic = true;
style.ParagraphFormat.SpaceBefore = Unit.FromPoint(10);
style.ParagraphFormat.SpaceAfter = Unit.FromPoint(10);
…
section.AddParagraph("Example for inheritance.");
var paragraph1 = section.AddParagraph("Heading 1: ");
paragraph1.Style = StyleNames.Heading1;
paragraph1.Format.Font.Underline = Underline.Single;
var formattedText1 = paragraph1.AddFormattedText("Red");
formattedText1.Color = Colors.Red;
formattedText1.Bold = false;
var paragraph2 = section.AddParagraph("Heading 2: ");
paragraph2.Style = StyleNames.Heading2;
var formattedText2 = paragraph2.AddFormattedText("Blue");
formattedText2.Color = Colors.Blue;
formattedText2.Italic = false;
var paragraph3 = section.AddParagraph("Whole text in ");
paragraph3.Style = StyleNames.Normal;
paragraph3.Format.Font.Color = Colors.Green;
var formattedText3 = paragraph3.AddFormattedText("green");
formattedText3.Bold = true;
```
--------------------------------
### Direct Formatting Example
Source: https://docs.pdfsharp.net/MigraDoc/DOM/Document/Formatting.html
Demonstrates setting font size and space before for a paragraph using direct formatting.
```C#
paragraph.Format.Font.Size = Unit.FromPoint(20);
paragraph.Format.SpaceBefore = Unit.FromPoint(7);
```
--------------------------------
### Example using the base API
Source: https://docs.pdfsharp.net/PDFsharp/Topics/PDF-Features/Accessibility.html
This example demonstrates structuring code with curly braces to clarify which drawing instructions belong to which structure element, including handling of trailing spaces for screen reader readability.
```C#
// Start an Article structure element.
sb.BeginElement(PdfGroupingElementTag.Article);
{
// Create a Page and an XGraphics object as usual.
var page = document.AddPage();
var gfx = XGraphics.FromPdfPage(page);
// Start a Heading1 structure element.
sb.BeginElement(PdfBlockLevelElementTag.Heading1);
{
gfx.DrawString("Header Text", fontH1, XBrushes.DarkBlue, 50, 100);
}
// End the Heading1 element.
sb.End();
// Start a Paragraph structure element.
sb.BeginElement(PdfBlockLevelElementTag.Paragraph);
{
// This string contains a trailing space needed for screen readers.
gfx.DrawString("Line one ", font, XBrushes.DarkBlue, 50, 200);
gfx.DrawString("Line two", font, XBrushes.DarkBlue, 50, 250);
}
// End the Paragraph structure element.
sb.End();
}
// End the Article structure element.
sb.End();
```
--------------------------------
### Font Resolver Setup
Source: https://docs.pdfsharp.net/PDFsharp/Topics/Start/First-PDF.html
Sets the FailsafeFontResolver for Core builds to handle font mapping.
```csharp
if (Capabilities.Build.IsCoreBuild)
GlobalFontSettings.FontResolver = new FailsafeFontResolver();
```
--------------------------------
### Clone PDFsharp and PDFsharp.Samples repositories
Source: https://docs.pdfsharp.net/General/Overview/Build-PDFsharp.html
Recommended way to get the PDFsharp and PDFsharp.Samples repositories from GitHub.
```git
git clone https://github.com/empira/PDFsharp.git
git clone https://github.com/empira/PDFsharp.Samples.git
```
--------------------------------
### Directory.Packages.props example
Source: https://docs.pdfsharp.net/General/Overview/Local-NuGet-Packages.html
Example of how to update the PDFsharp package version in the Directory.Packages.props file.
```xml
6.0.0-your-branch-42
```
--------------------------------
### Inheritance Example
Source: https://docs.pdfsharp.net/MigraDoc/DOM/Contents/Tables.html
Demonstrates how to set and use direct formatting at different levels within a table to show how inheritance works.
```csharp
// Set and use direct formatting at different levels to show how inheritance works in tables.
// Add table and set format.
var table = section.AddTable();
table.Borders.Visible = true;
table.Format.Font.Size = Unit.FromPoint(8);
// Add first column.
var columnA = table.AddColumn();
// Add second column and set format.
var columnB = table.AddColumn();
columnB.Format.Font.Bold = true;
// Add third column and set format.
var columnC = table.AddColumn();
columnC.Format.Alignment = ParagraphAlignment.Center;
columnC.Format.Font.Color = Colors.Red;
// Set format for all rows.
table.Rows.Height = Unit.FromPoint(25);
// Add first row and set format.
var row1 = table.AddRow();
row1.VerticalAlignment = VerticalAlignment.Center;
// Get first cell of row1.
var cellA1 = row1[0];
// Add paragraph to cellA1.
cellA1.AddParagraph("Text A1");
// Get second cell of row1.
var cellB1 = row1[1];
// Add paragraph to cellB1.
cellB1.AddParagraph("Text B1");
// Get third cell of row1.
var cellC1 = row1[2];
// Add paragraph to cellC1.
cellC1.AddParagraph("Text C1");
// Add second row and set format.
var row2 = table.AddRow();
row2.Format.Font.Italic = true;
// Get first cell of row2.
var cellA2 = row2[0];
// Add paragraph to cellA2.
cellA2.AddParagraph("Text A2");
// Get second cell of row2.
var cellB2 = row2[1];
// Add paragraph to cellB2.
cellB2.AddParagraph("Text B2");
// Get third cell of row2.
var cellC2 = row2[2];
// Add paragraph to cellC2.
cellC2.AddParagraph("Text C2");
// Add third row and set format.
var row3 = table.AddRow();
row3.VerticalAlignment = VerticalAlignment.Bottom;
row3.Format.Font.Color = Colors.Green;
// Get first cell of row3.
var cellA3 = row3[0];
// Add paragraph to cellA3.
cellA3.AddParagraph("Text A3");
// Get second cell of row3.
var cellB3 = row3[1];
// Add paragraph to cellB3.
cellB3.AddParagraph("Text B3");
// Get third cell of row3.
var cellC3 = row3[2];
// Add paragraph to cellC3.
cellC3.AddParagraph("Text C3");
```
--------------------------------
### Reset to DefaultPageSetup
Source: https://docs.pdfsharp.net/MigraDoc/DOM/Formats/PageSetup.html
Resets the section's page setup to the document's default page setup by cloning it.
```C#
// Reset to DefaultPageSetup.
section.PageSetup = document.DefaultPageSetup.Clone();
```
--------------------------------
### Document Structure Example
Source: https://docs.pdfsharp.net/MigraDoc/DOM/Document/DocumentStructure.html
Provides a comprehensive example of structuring a document with multiple sections and nested headings.
```csharp
// Add a first section for "Heading 1" to the document.
var section1 = document.AddSection();
// Add a heading of level 1 to the first section.
var headingChapter1 = section1.AddParagraph("Heading of chapter 1");
headingChapter1.Style = StyleNames.Heading1;
// Add content of chapter 1.
var paragraphChapter1 = section1.AddParagraph("Content of chapter 1");
// Add a heading of level 2.
var headingChapter1_1 = section1.AddParagraph("Heading of chapter 1.1");
headingChapter1_1.Style = StyleNames.Heading2;
// Add content of chapter 1.1.
var paragraphChapter1_1 = section1.AddParagraph("Content of chapter 1.1");
// Add another heading of level 2.
var headingChapter1_2 = section1.AddParagraph("Heading of chapter 1.2");
headingChapter1_2.Style = StyleNames.Heading2;
// Add content of chapter 1.2.
var paragraphChapter1_2 = section1.AddParagraph("Content of chapter 1.2");
// Add a heading of level 3.
var headingChapter1_2_1 = section1.AddParagraph("Heading of chapter 1.2.1");
headingChapter1_2_1.Style = StyleNames.Heading3;
// Add content of chapter 1.2.1.
var paragraphChapter1_2_1 = section1.AddParagraph("Content of chapter 1.2.1");
// Add a second section for the next chapters to the document.
var section2 = document.AddSection();
// Add another heading of level 1 to the second section.
var headingChapter2 = section2.AddParagraph("Heading of chapter 2");
headingChapter2.Style = StyleNames.Heading1;
// Add content of chapter 2.
var paragraphChapter2 = section2.AddParagraph("Content of chapter 2");
```
--------------------------------
### Initialize Logger Factory
Source: https://docs.pdfsharp.net/PDFsharp/Topics/Logging.html
Example of creating and setting a logger factory for PDFsharp at application startup.
```csharp
static void Main(…)
{
// Create and set a logger factory.
using var loggerFactory = LoggerFactory.Create(builder =>
{
builder
.AddFilter("PDFsharp", LogLevel.Information)
.AddConsole();
});
// Set the logger factory.
LogHost.Factory = loggerFactory;
// Create your PDF document with PDFsharp.
…
}
```
--------------------------------
### Creating a simple table
Source: https://docs.pdfsharp.net/MigraDoc/DOM/Contents/Tables.html
This example demonstrates how to add a table to a section, define columns with specific widths, add rows, and populate cells with paragraphs.
```C#
// Add table.
var table = section.AddTable();
table.Borders.Visible = true;
// Add first column.
var columnA = table.AddColumn(Unit.FromCentimeter(2));
// Add second column.
var columnB = table.AddColumn(Unit.FromCentimeter(3));
// Add first row.
var row1 = table.AddRow();
// Add paragraph to first cell of row1.
var cellA1 = row1[0];
cellA1.AddParagraph("Text A1");
// Add paragraph to second cell of row1.
var cellB1 = row1[1];
cellB1.AddParagraph("Text B1 contains more text.");
```
--------------------------------
### Setting row height
Source: https://docs.pdfsharp.net/MigraDoc/DOM/Contents/Tables.html
This example shows how to set a specific height for a row in a table.
```C#
// Set row height.
row.Height = Unit.FromCentimeter(2.5);
```
--------------------------------
### Add a section
Source: https://docs.pdfsharp.net/MigraDoc/Topics/Start/HelloMigraDoc.html
Starts a new section in the document, which is the first step in adding a cover page.
```C#
var section = document.AddSection();
```
--------------------------------
### Defining Styles
Source: https://docs.pdfsharp.net/MigraDoc/Topics/Start/HelloMigraDoc.html
Example of defining paragraph format properties and adding a tab stop for a style.
```C#
style.ParagraphFormat.Alignment = ParagraphAlignment.Justify;
style.ParagraphFormat.Borders.Width = 2.5;
style.ParagraphFormat.Borders.Distance = "3pt";
style.ParagraphFormat.Shading.Color = Colors.SkyBlue;
// Create a new style called TOC based on style Normal.
style = document.Styles.AddStyle(TocStyle, StyleNames.Normal);
style.ParagraphFormat.AddTabStop("16cm", TabAlignment.Right, TabLeader.Dots);
style.ParagraphFormat.Font.Color = Colors.Blue;
```
--------------------------------
### Set Page Format and Orientation
Source: https://docs.pdfsharp.net/MigraDoc/DOM/Formats/PageSetup.html
Sets the page format to A5 and orientation to Landscape.
```C#
section.PageSetup.PageFormat = PageFormat.A5;
section.PageSetup.Orientation = Orientation.Landscape;
```
--------------------------------
### Modify Base Style
Source: https://docs.pdfsharp.net/MigraDoc/DOM/Document/Formatting.html
Example of how to modify base styles and create new style layers.
```csharp
const string headingBaseStyleName = "Heading";
// Set bold for all heading styles on new style layer.
var style = document.AddStyle(headingBaseStyleName, StyleNames.Normal);
style.Font.Bold = true;
// Change Heading1 to inherit from the new style layer.
style = document.Styles[StyleNames.Heading1];
style.BaseStyle = headingBaseStyleName;
style.Font.Size = Unit.FromPoint(20);
```
--------------------------------
### Creating a table header row
Source: https://docs.pdfsharp.net/MigraDoc/DOM/Contents/Tables.html
This example illustrates how to designate the first row of a table as a header row and apply a style to it.
```C#
// Add first row as header.
var row1 = table.AddRow();
row1.HeadingFormat = true;
row1.Style = tableHeaderStyleName;
// Add paragraph to first cell of row1.
row1[0].AddParagraph("Header A1");
// Add paragraph to second cell of row1.
row1[1].AddParagraph("Header B1");
```
--------------------------------
### Enable Mirrored Margins
Source: https://docs.pdfsharp.net/MigraDoc/DOM/Formats/PageSetup.html
Enables a symmetric page layout by setting MirrorMargins to true.
```C#
section.PageSetup.MirrorMargins = true;
```
--------------------------------
### NuGet.Config for WSL2
Source: https://docs.pdfsharp.net/General/Overview/Local-NuGet-Packages.html
Example of a NuGet.Config file for WSL2 to reference a local package source on Windows.
```xml
```
--------------------------------
### Inheritance Example
Source: https://docs.pdfsharp.net/MigraDoc/DOM/Document/Formatting.html
This code snippet illustrates how formatting properties like font name, size, and color are inherited from parent styles and elements in MigraDoc.
```C#
var paragraph2 = section.AddParagraph("Heading 2: ");
paragraph2.Style = StyleNames.Heading2;
// 'Font.Size = Unit.FromPoint(18)' will be inherited from Heading2.
// 'Font.Italic = true' will be inherited from Heading2.
// 'ParagraphFormat.SpaceBefore = Unit.FromPoint(10)' will be inherited from Heading2.
// 'ParagraphFormat.SpaceAfter = Unit.FromPoint(10)' will be inherited from Heading2.
// 'Font.Bold = true' will be inherited from Heading2 > Heading1.
// 'Font.Name = "Times New Roman"' will be inherited from Heading2 > Heading1 > Normal.
var formattedText2 = paragraph2.AddFormattedText("Blue");
formattedText2.Color = Colors.Blue;
formattedText2.Italic = false; // Overrides 'true' from paragraph2 > Heading2.
// 'Font.Size = Unit.FromPoint(18)' will be inherited from Heading2.
// 'ParagraphFormat.SpaceBefore = Unit.FromPoint(10)' from Heading2 is not relevant for formattedText2.
// 'ParagraphFormat.SpaceAfter = Unit.FromPoint(10)' from Heading2 is not relevant for formattedText2.
// 'Font.Bold = true' will be inherited from Heading2 > Heading1.
// 'Font.Name = "Times New Roman"' will be inherited from Heading2 > Heading1 > Normal.
var paragraph3 = section.AddParagraph("Whole text in ");
paragraph3.Style = StyleNames.Normal;
paragraph3.Format.Font.Color = Colors.Green;
// 'Font.Name = "Times New Roman"' will be inherited from Normal.
// 'Font.Size = Unit.FromPoint(12)' will be inherited from Normal.
var formattedText3 = paragraph3.AddFormattedText("green");
formattedText3.Bold = true;
// 'Font.Color = Colors.Green' will be inherited from paragraph3.
// 'Font.Name = "Times New Roman"' will be inherited from Normal.
// 'Font.Size = Unit.FromPoint(12)' will be inherited from Normal.
```
--------------------------------
### Define Section
Source: https://docs.pdfsharp.net/MigraDoc/Topics/Start/HelloMigraDoc.html
Adds a new section to the document and configures page setup for odd and even pages, starting page numbering at 1.
```C#
var section = document.AddSection();
section.PageSetup.OddAndEvenPagesHeaderFooter = true;
section.PageSetup.StartingNumber = 1;
```
--------------------------------
### MDDDL Image Example
Source: https://docs.pdfsharp.net/MigraDoc/Overview/GrammarByExample.html
An example of how to include an image using MDDDL syntax.
```MDDDL
\image("logo_e.gif")[Height = "1cm"]
```
--------------------------------
### Get UAManager
Source: https://docs.pdfsharp.net/PDFsharp/Topics/PDF-Features/Accessibility.html
Get the UAManager for the document and create it, if necessary. This must be done before any other operation on the document.
```C#
// Get the UAManager for the document and create it, if necessary.
var uaManager = UAManager.ForDocument(pdfDocument);
```
--------------------------------
### Creating the document and a section
Source: https://docs.pdfsharp.net/MigraDoc/Topics/Start/HelloWorld.html
Initializes a new MigraDoc document and adds the first section to it. Document content is organized within sections.
```C#
// Create a new MigraDoc document.
var document = new Document();
// Add a section to the document.
var section = document.AddSection();
```
--------------------------------
### Create PdfDocumentRenderer
Source: https://docs.pdfsharp.net/MigraDoc/Topics/Start/HelloWorld.html
Creates a PdfDocumentRenderer instance, associates the MigraDoc document with it, and sets some PDF file settings.
```C#
// 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
}
}
};
```
--------------------------------
### Example of adding a registered trademark symbol
Source: https://docs.pdfsharp.net/MigraDoc/DOM/Contents/Text.html
This example demonstrates how to add a registered trademark symbol using the AddCharacter function with SymbolName.RegisteredTrademark.
```C#
paragraph.AddCharacter(SymbolName.RegisteredTrademark);
```
--------------------------------
### Example of adding text with different formatting
Source: https://docs.pdfsharp.net/MigraDoc/DOM/Contents/Text.html
This example demonstrates how to add a paragraph with a portion of text formatted differently using FormattedText.
```C#
// Add a paragraph with the first part of the text to be default formatted.
var paragraph = section.AddParagraph("This is an example for a ");
// Add a FormattedText to the paragraph.
var formattedText = paragraph.AddFormattedText("bold");
// Format it. The properties of formattedText.Font can be set directly on formattedText.
formattedText.Bold = true;
// Add a Text with the rest of the running text to the paragraph.
paragraph.AddText(" word in the middle of a sentence.");
```
--------------------------------
### Sample Chart Creation
Source: https://docs.pdfsharp.net/MigraDoc/Topics/Start/HelloMigraDoc.html
This code snippet demonstrates how to create and configure a chart with two data series, X-axis labels, and styling.
```C#
var paragraph = document.LastSection.AddParagraph("Chart Overview", StyleNames.Heading1);
paragraph.AddBookmark(BookmarkName);
document.LastSection.AddParagraph("Sample Chart", StyleNames.Heading2);
var chart = new Chart();
chart.Left = 0;
chart.Width = Unit.FromCentimeter(16);
chart.Height = Unit.FromCentimeter(12);
var series = chart.SeriesCollection.AddSeries();
series.ChartType = ChartType.Column2D;
series.Add(1, 17, 45, 5, 3, 20, 11, 23, 8, 19);
series.HasDataLabel = true;
series = chart.SeriesCollection.AddSeries();
series.ChartType = ChartType.Line;
series.Add(41, 7, 5, 45, 13, 10, 21, 13, 18, 9);
var xSeries = chart.XValues.AddXSeries();
xSeries.Add("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N");
chart.XAxis.Title.Caption = "X-Axis";
chart.YAxis.MajorTickMark = TickMarkType.Outside;
chart.YAxis.HasMajorGridlines = true;
chart.PlotArea.LineFormat.Color = Colors.DarkGray;
chart.PlotArea.LineFormat.Width = 1;
document.LastSection.Add(chart);
```
--------------------------------
### Example of using soft hyphens
Source: https://docs.pdfsharp.net/MigraDoc/DOM/Contents/Text.html
This example shows how to insert soft hyphens into a long word to allow hyphenation at specific positions using Unicode escape sequences.
```C#
var paragraph = section.AddParagraph("Soft hyphens can be inserted into a long word, " +
"to allow hyphenation at specific positions: \"super\u00ADcali\u00ADfragilistic\u00ADexpialidocious\". " +
"Therefore, insert the Unicode escape sequence with the soft hyphen’s character number (00AD). " +
"In an UTF encoded file you can alternatively insert the soft hyphen’s character directly.");
```
--------------------------------
### Create PDF Document
Source: https://docs.pdfsharp.net/PDFsharp/Topics/Start/First-PDF.html
Creates a new, empty PDF document in memory.
```csharp
var document = new PdfDocument();
```
--------------------------------
### Colliding Borders Example
Source: https://docs.pdfsharp.net/MigraDoc/DOM/Formats/BordersAndLineFormat.html
This example demonstrates how different border formats are applied to adjacent cells in a table, illustrating how MigraDoc resolves conflicts when borders collide.
```csharp
// Add paragraph to first cell of row2.
var cellA2 = row2[0];
cellA2.AddParagraph("Text A2");
// Set cellA2 to a thin border.
cellA2.Borders.Width = Unit.FromPoint(1);
cellA2.Borders.Color = Colors.DarkGray;
// Add paragraph to second cell of row2.
var cellB2 = row2[1];
cellB2.AddParagraph("Text B2");
// Set cellB2 to a thin dotted light border.
cellB2.Borders.Width = Unit.FromPoint(1);
cellB2.Borders.Color = Colors.LightGray;
cellB2.Borders.Style = BorderStyle.Dot;
// Add paragraph to third cell of row2.
var cellC2 = row2[2];
cellC2.AddParagraph("Text C2");
// Set cellC2 to a thin border.
cellC2.Borders.Width = Unit.FromPoint(1);
cellC2.Borders.Color = Colors.DarkGray;
// Add paragraph to fourth cell of row2.
var cellD2 = row2[3];
cellD2.AddParagraph("Text D2");
// Set cellD2 to a thick dotted light border.
cellD2.Borders.Width = Unit.FromPoint(3);
cellD2.Borders.Color = Colors.LightGray;
cellD2.Borders.Style = BorderStyle.Dot;
```
--------------------------------
### Create a Git repository with a commit
Source: https://docs.pdfsharp.net/General/Overview/Build-PDFsharp.html
Instructions to create a local Git repository with at least one commit, necessary when downloading a ZIP file from GitHub.
```git
git init
git config --local user.email "you@example.com"
git config --local user.name "Your Name"
git add .
git commit -m "Init"
```
--------------------------------
### Merging Cells
Source: https://docs.pdfsharp.net/MigraDoc/DOM/Contents/Tables.html
Example of merging cells horizontally and vertically.
```C#
// Merge cell one row down and one column right.
cell.MergeDown = 1;
cell.MergeRight = 1;
```
--------------------------------
### Render and Save Document
Source: https://docs.pdfsharp.net/MigraDoc/Topics/Start/HelloWorld.html
Renders the MigraDoc document to a PDFsharp PdfDocument instance and saves it to a file, then opens it in a viewer.
```C#
// 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);
```
--------------------------------
### Create Font for Text
Source: https://docs.pdfsharp.net/PDFsharp/Topics/Start/First-PDF.html
Creates a font object for drawing text.
```csharp
var font = new XFont("Times New Roman", 20, XFontStyleEx.BoldItalic);
```
--------------------------------
### Change Page Size and Orientation
Source: https://docs.pdfsharp.net/PDFsharp/Topics/Start/First-PDF.html
Demonstrates how to change the page size to A3 and orientation to Landscape.
```csharp
page.Size = PageSize.A3;
page.Orientation = PageOrientation.Landscape;
```
--------------------------------
### Creating and filling the primary footer
Source: https://docs.pdfsharp.net/MigraDoc/Topics/Start/HelloWorld.html
Creates a paragraph in the primary footer, adds a date field with a specific format, and centers the paragraph alignment.
```C#
// 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;
```
--------------------------------
### Simple Table
Source: https://docs.pdfsharp.net/MigraDoc/Topics/Start/HelloMigraDoc.html
This code snippet demonstrates how to create and format a simple table with two columns and three rows, including borders and shading.
```C#
document.LastSection.AddParagraph("Simple Tables", StyleNames.Heading2);
var table = new Table
{
Borders =
{
Width = 0.75
}
};
var column = table.AddColumn(Unit.FromCentimeter(2));
column.Format.Alignment = ParagraphAlignment.Center;
table.AddColumn(Unit.FromCentimeter(5));
var row = table.AddRow();
row.Shading.Color = Colors.PaleGoldenrod;
row.HeadingFormat = true;
var cell = row.Cells[0];
cell.AddParagraph("Itemus");
cell = row.Cells[1];
cell.AddParagraph("Descriptum");
row = table.AddRow();
cell = row.Cells[0];
cell.AddParagraph("1");
cell = row.Cells[1];
cell.AddParagraph(FillerText.ShortText);
row = table.AddRow();
cell = row.Cells[0];
cell.AddParagraph("2");
cell = row.Cells[1];
cell.AddParagraph(FillerText.Text);
table.SetEdge(0, 0, 2, 3, Edge.Box, BorderStyle.Single, 1.5, Colors.Black);
document.LastSection.Add(table);
```
--------------------------------
### Configure PDF Document Settings
Source: https://docs.pdfsharp.net/MigraDoc/DOM/Document/Rendering.html
Examples of configuring PDFsharp PdfDocument settings before rendering.
```C#
// 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;
```
--------------------------------
### Tag a specific version
Source: https://docs.pdfsharp.net/General/Overview/Build-PDFsharp.html
How to tag a specific version number for the build.
```git
git tag v6.2.0
```
--------------------------------
### Calling AddFootnote
Source: https://docs.pdfsharp.net/MigraDoc/DOM/Contents/Footnotes.html
Example of how to call the AddFootnote function to add a footnote with specific text.
```csharp
// Add the footnote and its text.
AddFootnote(section, 1, bookmarkPrefix, "X must be a value between 1 and 10.");
```
--------------------------------
### Using a Style for a Paragraph
Source: https://docs.pdfsharp.net/MigraDoc/DOM/Document/Formatting.html
Shows how to assign a predefined style (Heading1) to a paragraph object.
```C#
// Use Heading1 style for paragraph.
paragraph.Style = StyleNames.Heading1;
```
--------------------------------
### Define Headers
Source: https://docs.pdfsharp.net/MigraDoc/Topics/Start/HelloMigraDoc.html
Sets up primary (odd page) and even page headers with different text alignments.
```C#
// For the odd page header, add a paragraph with right aligned text due to the given tab.
// See definition of style StyleNames.Header.
var header = section.Headers.Primary;
header.AddParagraph("\tOdd Page Header");
// For the even page header, add a paragraph with left aligned text due to the missing tab.
// See definition of style StyleNames.Header.
header = section.Headers.EvenPage;
header.AddParagraph("Even Page Header");
```
--------------------------------
### SectionStart
Source: https://docs.pdfsharp.net/MigraDoc/DOM/Document/PageControl.html
Defines if a section shall explicitly start on an odd or on an even page.
```C#
section.PageSetup.SectionStart = BreakType.BreakOddPage;
```
```C#
section.PageSetup.SectionStart = BreakType.BreakEvenPage;
```
--------------------------------
### Signature Field Position
Source: https://docs.pdfsharp.net/PDFsharp/Topics/PDF-Features/Signatures.html
Example of calculating the position for a signature field on a PDF page.
```csharp
var pdfPosition = xGraphics.Transformer.WorldToDefaultPage(new XPoint(144, 600));
```
--------------------------------
### Creating XFont objects
Source: https://docs.pdfsharp.net/PDFsharp/Topics/Fonts/Font-Resolving-%286.1%29.html
Example of creating XFont objects for regular and bold Arial.
```C#
var myFont = new XFont("Arial", 10, XFontStyleEx.Regular);
var myBoldFont = new XFont("Arial", 10, XFontStyleEx.Bold);
```
--------------------------------
### Avoiding Page Breaks
Source: https://docs.pdfsharp.net/MigraDoc/DOM/Contents/Tables.html
Example of keeping a row and subsequent rows together to avoid page breaks.
```C#
// Keep rows 3, 4 and 5 together.
row3.KeepWith = 2;
```
--------------------------------
### Setting exact row height
Source: https://docs.pdfsharp.net/MigraDoc/DOM/Contents/Tables.html
This example demonstrates how to set an exact row height using RowHeightRule.Exactly.
```C#
// Set row height.
row.HeightRule = RowHeightRule.Exactly;
row.Height = Unit.FromCentimeter(2.5);
```
--------------------------------
### Demonstrating Text Alignment
Source: https://docs.pdfsharp.net/MigraDoc/Topics/Start/HelloMigraDoc.html
This code snippet demonstrates how to add paragraphs with different text alignments (Left, Right, Center, Justify).
```C#
document.LastSection.AddParagraph("Alignment", StyleNames.Heading2);
document.LastSection.AddParagraph("Left Aligned", StyleNames.Heading3);
var paragraph = document.LastSection.AddParagraph();
paragraph.Format.Alignment = ParagraphAlignment.Left;
paragraph.AddText(FillerText.Text);
document.LastSection.AddParagraph("Right Aligned", StyleNames.Heading3);
paragraph = document.LastSection.AddParagraph();
paragraph.Format.Alignment = ParagraphAlignment.Right;
paragraph.AddText(FillerText.Text);
document.LastSection.AddParagraph("Centered", StyleNames.Heading3);
paragraph = document.LastSection.AddParagraph();
paragraph.Format.Alignment = ParagraphAlignment.Center;
paragraph.AddText(FillerText.Text);
document.LastSection.AddParagraph("Justified", StyleNames.Heading3);
paragraph = document.LastSection.AddParagraph();
paragraph.Format.Alignment = ParagraphAlignment.Justify;
paragraph.AddText(FillerText.MediumText);
```
--------------------------------
### Render and save the document to PDF
Source: https://docs.pdfsharp.net/MigraDoc/Topics/Start/HelloMigraDoc.html
This snippet shows how to render the MigraDoc document to a PDFsharp PdfDocument instance, save it to a file, and then open it in a viewer.
```C#
// Layout and render document to PDF.
pdfRenderer.RenderDocument();
// Save the document...
var filename = PdfFileUtility.GetTempPdfFullFileName("samples-MigraDoc/HelloMigraDoc.pdf");
pdfRenderer.PdfDocument.Save(filename);
// ...and start a viewer.
PdfFileUtility.ShowDocument(filename);
```
--------------------------------
### Create PdfDocumentRenderer instance
Source: https://docs.pdfsharp.net/MigraDoc/Topics/Start/HelloMigraDoc.html
This snippet shows how to create a PdfDocumentRenderer instance, associate the MigraDoc document with it, and set some initial PDF settings.
```C#
// 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
}
}
};
```
--------------------------------
### Complete Source Code
Source: https://docs.pdfsharp.net/PDFsharp/Topics/Start/First-PDF.html
The complete source code for creating a simple PDF document with PDFsharp.
```csharp
// PDFsharp - A .NET library for processing PDF
// See the LICENSE file in the solution root for more information.
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Fonts;
using PdfSharp.Pdf;
using PdfSharp.Quality;
using PdfSharp.Snippets.Font;
// Create a new PDF document.
var document = new PdfDocument();
document.Info.Title = "Created with PDFsharp";
document.Info.Subject = "Just a simple Hello-World program.";
// Create an empty page in this document.
var page = document.AddPage();
// Get an XGraphics object for drawing on this page.
var gfx = XGraphics.FromPdfPage(page);
// Draw two lines with a red default pen.
var width = page.Width.Point;
var height = page.Height.Point;
gfx.DrawLine(XPens.Red, 0, 0, width, height);
gfx.DrawLine(XPens.Red, width, 0, 0, height);
// Draw a circle with a red pen which is 1.5 point thick.
var r = width / 5;
gfx.DrawEllipse(new XPen(XColors.Red, 1.5), XBrushes.White, new XRect(width / 2 - r, height / 2 - r, 2 * r, 2 * r));
// Create a font.
var font = new XFont("Times New Roman", 20, XFontStyleEx.BoldItalic);
// Draw the text.
gfx.DrawString("Hello, PDFsharp!", font, XBrushes.Black,
new XRect(0, 0, page.Width, page.Height), XStringFormats.Center);
// Save the document...
var filename = IOUtility.GetTempPdfFullFileName("HelloWorldSample");
document.Save(filename);
// ...and start a viewer.
PdfFileUtility.StartPdfViewer(filename);
```
--------------------------------
### Set Individual Page Size
Source: https://docs.pdfsharp.net/MigraDoc/DOM/Formats/PageSetup.html
Sets an individual page size using centimeters.
```C#
section.PageSetup.PageWidth = Unit.FromCentimeter(15);
section.PageSetup.PageHeight = Unit.FromCentimeter(20);
```