PDFRasterizer.NET 4.0 is now available. Supports .NET Core. Renders pixel-perfectly.

Add simple html text to PDF

The SimpleXhtmlShape lets you add text with simple HTML mark-up to a PDF document.

Add Html Text To PDF

Here is the code that uses the SimpleXhtmlShape:

// create a new document
Document document = new Document();

// append a new page (portrait Letter)
bool landscape = false;

Page page = new Page(PageSize.Letter, landscape);
document.Pages.Add(page);

string xhtml = "<?xml version='1.0'?><body xfa:APIVersion=\"PDFKit:3.0.0.0\" xfa:spec=\"2.1\" xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:xfa=\"http://www.xfa.org/schema/xfa-data/1.0/\">";
xhtml += "<p>With a simple Xhtml shape you can easily specify <b>bold</b> text, <i>italic</i> text, or <b><i>both</i></b>. ";
xhtml += "It is also possible to specify a different <span style='color:#ff0000'>color</span>,";
xhtml += "or a different <span style='font-family:courier'>font</span>, ";
xhtml += "<span style ='text-decoration:underline'>underline</span>, or ";
xhtml += "<span style ='text-decoration:line-through'>line-through</span>.</p> ";
xhtml += "<p>Of course, text can also be <span style='font-size:72'>big</span>.</p>";
xhtml += "<p>Note furthermore that &lt;p&gt; tags can be used to generate separate paragraphs.</p>";
xhtml += "</body>";

// stamp a text paragraph at the center

TranslateTransform translate = new TranslateTransform();
SimpleXhtmlShape xhtmlShape = new SimpleXhtmlShape();
xhtmlShape.Text = xhtml;
xhtmlShape.Transform = translate;

xhtmlShape.DefaultTextIndent = 20;
// center horizontally
xhtmlShape.Width = page.Width / 2;
translate.X = (page.Width - xhtmlShape.Width) / 2;

// Y corresponds to the top of the paragraph
// Translate the shape so it is centered vertically
translate.Y = page.Height / 2 + xhtmlShape.MeasuredHeight / 2;

page.VisualOverlay.Add(xhtmlShape);

using (FileStream file = new FileStream(@"..\..\formattedtext.pdf", FileMode.Create, FileAccess.Write))
{
    document.Write(file);
}
        ' create a new document
        Dim document As New Document()

        ' append a new page (portrait Letter)
        Dim landscape As Boolean = False

        Dim page As New Page(PageSize.Letter, landscape)
        document.Pages.Add(page)

        Dim xhtml As String = "<?xml version='1.0'?><body xfa:APIVersion=""PDFKit:3.0.0.0"" xfa:spec=""2.1"" xmlns=""http://www.w3.org/1999/xhtml"" xmlns:xfa=""http://www.xfa.org/schema/xfa-data/1.0/"">"
        xhtml += "<p>With a simple Xhtml shape you can easily specify <b>bold</b> text, <i>italic</i> text, or <b><i>both</i></b>. "
        xhtml += "It is also possible to specify a different <span style='color:#ff0000'>color</span>,"
        xhtml += "or a different <span style='font-family:courier'>font</span>, "
        xhtml += "<span style ='text-decoration:underline'>underline</span>, or "
        xhtml += "<span style ='text-decoration:line-through'>line-through</span>.</p> "
        xhtml += "<p>Of course, text can also be <span style='font-size:72'>big</span>.</p>"
        xhtml += "<p>Note furthermore that &lt;p&gt; tags can be used to generate separate paragraphs.</p>"
        xhtml += "</body>"

        ' stamp a text paragraph at the center

        Dim translate As New TranslateTransform()
        Dim xhtmlShape As New Shapes.SimpleXhtmlShape
        xhtmlShape.Text = xhtml
        xhtmlShape.Transform = translate

        xhtmlShape.DefaultTextIndent = 20
        ' center horizontally
        xhtmlShape.Width = page.Width / 2
        translate.X = (page.Width - xhtmlShape.Width) / 2

        ' Y corresponds to the top of the paragraph
        ' Translate the shape so it is centered vertically
        translate.Y = page.Height / 2 + xhtmlShape.MeasuredHeight / 2

        page.VisualOverlay.Add(xhtmlShape)

        Using file As New FileStream("..\..\formattedtext.pdf", FileMode.Create, FileAccess.Write)
            document.Write(file)
        End Using
Download PDFKit.NET 5.0
We will send you a download link

  • This field is for validation purposes and should be left unchanged.
Why do we ask your email address?
We send tips that speed up your evaluation
We let you know about bug fixes
You can always unsubscribe with one click
We never share your address with a 3rd party
Thank you for your download

We have sent an email with a download link.