Text & formatted text
Use plain text for simple labels and HTML when a paragraph needs inline formatting. Add plain text, HTML-formatted text, and a styled text box to PDFs. This example follows AddTextToPdf.cs: FormattedText adds an inline HTML span, and StyledText applies CSS, padding, and a border directly to a text element.
Use Text for both plain and HTML-formatted content, then style the text element directly.
What you’ll create
- A PDF containing “Normal text and blue text.” with only “blue text” colored blue.
- A separate PDF containing “A styled paragraph.” in purple, 18-point text inside a black border with 10-point padding.
AddTextToPdf_html_output.pdfandAddTextToPdf_styled_output.pdfin your chosen output directory.
Both documents use the default page settings. Run calls the two methods and saves their outputs separately.
See Styling text using CSS for more advanced text styling at document, page, and element levels.
Example code
Add the following class to a console project that references the DynamicPDF Flow API. See the quick start for an overview. The class creates two text examples and saves each document to an existing output directory.
using System;
using System.IO;
using DynamicPDF.Flow;
namespace FluentExamples.Examples;
internal static class AddTextToPdf
{
public static void Run(string outputRoot)
{
FormattedText(outputRoot);
StyledText(outputRoot);
}
public static void FormattedText(string outputRoot)
{
var path = Path.Combine(
outputRoot, "AddTextToPdf_html_output.pdf");
DocumentBuilder.Create()
.Title("HTML-formatted text")
.Pages(page => page
.Text("Normal text and "
+ "<span style='color: blue;'>"
+ "blue text</span>."))
.Save(path);
Console.WriteLine($"Created {path}");
}
public static void StyledText(string outputRoot)
{
var path = Path.Combine(
outputRoot, "AddTextToPdf_styled_output.pdf");
DocumentBuilder.Create()
.Title("Styled text box")
.Pages(page => page
.Text("A styled paragraph.", text => text
.Style("font-size: 18pt; color: purple;")
.Padding(10, LengthUnit.Point)
.Border(1, LengthUnit.Point, "black")))
.Save(path);
Console.WriteLine($"Created {path}");
}
}
Imports System
Imports System.IO
Imports DynamicPDF.Flow
Friend Module AddTextToPdf
Public Sub Run(outputRoot As String)
FormattedText(outputRoot)
StyledText(outputRoot)
End Sub
Public Sub FormattedText(outputRoot As String)
Dim outputPath = Path.Combine(
outputRoot, "AddTextToPdf_html_output.pdf")
DocumentBuilder.Create().
Title("HTML-formatted text").
Pages(Sub(page) page.
Text("Normal text and " &
"<span style='color: blue;'>" &
"blue text</span>.")).
Save(outputPath)
Console.WriteLine($"Created {outputPath}")
End Sub
Public Sub StyledText(outputRoot As String)
Dim outputPath = Path.Combine(
outputRoot, "AddTextToPdf_styled_output.pdf")
DocumentBuilder.Create().
Title("Styled text box").
Pages(Sub(page) page.
Text("A styled paragraph.", Sub(text) text.
Style("font-size: 18pt; color: purple;").
Padding(10, unit:=LengthUnit.Point).
Border(1, LengthUnit.Point, "black"))).
Save(outputPath)
Console.WriteLine($"Created {outputPath}")
End Sub
End Module
How it works
1. Choose where to save the PDFs
Run calls FormattedText followed by StyledText. Each method uses Path.Combine to construct its output path under outputRoot. Make sure the output directory exists before calling either method.
2. Add HTML-formatted text
FormattedText starts a document with DocumentBuilder.Create() and sets its title to HTML-formatted text. Inside .Pages(...), .Text(...) accepts a string containing both plain text and an HTML span.
The span's style='color: blue;' colors only “blue text” blue. The surrounding text and final period keep the default styling. The same Text API handles plain and HTML-formatted content; this example does not require a separate formatted text area or HTML text area builder.
3. Style a text element and its box
StyledText creates a separate document titled Styled text box. Its .Text(...) callback receives a TextElementBuilder and applies .Style("font-size: 18pt; color: purple;") directly to that element.
.Padding(10, LengthUnit.Point) adds 10 points of space around the text, and .Border(1, LengthUnit.Point, "black") adds a solid, 1-point black border. The text sits inside that border. The string contains plain text rather than an additional <p> wrapper, so the text and box styling apply to the same element.
4. Save both documents
Each .Save(path) builds and saves its PDF directly through the fluent builder. FormattedText saves AddTextToPdf_html_output.pdf; StyledText saves AddTextToPdf_styled_output.pdf. Their distinct filenames keep both results in the same output directory. Saving overwrites an existing file with the corresponding name, and Console.WriteLine prints each output path.
See TextMargin.cs for examples of setting text margins, and see TextPageBreak.cs for an example of using page breaks.
Next steps
Change the span's inline color or adjust the text element's font size, padding, and border. Explore Styling text using CSS for document, page, and element styles, Create a PDF for plain text and document metadata, or browse all examples.