Text
Version 6.1.0
In this article
This article describes how to add text to a MigraDoc document.
Paragraphs
To include text into a document’s section, you will always add at least one paragraph (see Document / Document structure).
For this paragraph you can set a style to be used and / or format it directly by setting its paragraph.Format
properties (see Document / Formatting for more information).
There are also some other objects, you can add paragraphs to. You can call an AddParagraph method for example for a table cell (see Tables), a header or footer (see Headers & footers) or a text frame (see Text frames).
FormattedTexts
When you want a piece of text to be of a different format than the rest of its paragraph, you have to add a FormattedText object.
You can then set its formattedText.Font
properties or assign a different style (see Document / Formatting for more information).
Texts
When a piece of text has to be added without an own format, add a Text object. You may need a Text object to continue the text of a paragraph after adding a FormattedText object. Also when working with fields, you can continue the running text of the current paragraph or formatted text by adding a Text object.
Example
This is an example for a paragraph with one word of a differing format in the middle of the running text. If you split the running text in different objects, don't forget to include whitespace as needed.
// 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.");
See full Example sample in Text in PDFsharp.Sample repository. Show resulting PDF
As it is recommended to use styles in most applications, you would usually set a style with formattedText.Style = style;
instead of setting the Bold property directly (see Document / Formatting for more information).
Hyphenation
MigraDoc does not support automatic hyphenation. But like in Microsoft Word, you can work with soft hyphens (conditional hyphens) to split specific words, when needed:
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.");
See full Hyphenation sample in Text in PDFsharp.Sample repository. Show resulting PDF
Special characters
A number of special characters can be added by using the AddCharacter function. However, in most applications you can use their Unicode characters directly when adding text.
AddCharacter uses the SymbolName enum, which supports blanks and dashes of several widths and copyright (©), trademark (™), registered trademark (®), Bullet (•), not (¬) and Euro (€) signs as well as a non-breakable blank:
paragraph.AddCharacter(SymbolName.RegisteredTrademark);
See full SpecialCharacters sample in Text in PDFsharp.Sample repository. Show resulting PDF
For tabs and line breaks, you can use the specific methods AddTab and AddLineBreak as well.