How to Insert HTML Styled Text to PDF in C#, VB.NET

Spire.PDF provides support to render simple HTML string in a PDF document by using PdfHTMLTextElement class. This class supports a set of basic HTML tags including Font, B, I, U, Sub, Sup and BR. For complex HTML rendering with CSS, please check Convert HTML String to PDF.

Following code snippets demonstrates how we can insert HTML styled text to PDF.

Step 1: Create a new PDF document, add a page to it.

PdfDocument doc = new PdfDocument();
PdfNewPage page = doc.Pages.Add() as PdfNewPage;

Step 2: Define HTML string.

string htmlText= "This demo shows how we can insert <u><i>HTML styled text</i></u> to PDF using "
                 + "<font color='#FF4500'>Spire.PDF for .NET</font>. ";

Step 3: Render HTML text.

PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 5);
PdfBrush brush = PdfBrushes.Black;
PdfHTMLTextElement richTextElement = new PdfHTMLTextElement(htmlText, font, brush);
richTextElement.TextAlign = TextAlign.Left;

Step 4: Format page layout to enable that the HTML text will break into multiple pages if the content exceeds one page.

PdfMetafileLayoutFormat format = new PdfMetafileLayoutFormat();
format.Layout = PdfLayoutType.Paginate;
format.Break = PdfLayoutBreakType.FitPage;

Step 5: Draw HTML string on page.

richTextElement.Draw(page, new RectangleF(0, 20, page.GetClientSize().Width, page.GetClientSize().Height), format);

Step 6: Save the document.

doc.SaveToFile("Output.pdf");

Output:

How to Insert HTML Styled Text to PDF in C#, VB.NET

Full Code:

[C#]
//Create a Pdf document
PdfDocument doc = new PdfDocument();

//Add a new page
PdfNewPage page = doc.Pages.Add() as PdfNewPage;

//HTML string
string htmlText= "This demo shows how we can insert <u><i>HTML styled text</i></u> to PDF using "
                 + "<font color='#FF4500'>Spire.PDF for .NET</font>. ";

//Render HTML text
PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 5);
PdfBrush brush = PdfBrushes.Black;
PdfHTMLTextElement richTextElement = new PdfHTMLTextElement(htmlText, font, brush);
richTextElement.TextAlign = TextAlign.Left;

//Format Layout
PdfMetafileLayoutFormat format = new PdfMetafileLayoutFormat();
format.Layout = PdfLayoutType.Paginate;
format.Break = PdfLayoutBreakType.FitPage;

//Draw htmlString  
richTextElement.Draw(page, new RectangleF(0, 20, page.GetClientSize().Width, page.GetClientSize().Height), format);
doc.SaveToFile("Output.pdf");
[VB.NET]
'Create a Pdf document
Dim doc As New PdfDocument()

'Add a new page
Dim page As PdfNewPage = TryCast(doc.Pages.Add(), PdfNewPage)

'HTML string
Dim htmlText As String = "This demo shows how we can insert <u><i>HTML styled text</i></u> to PDF using " 
    & "<font color='#FF4500'>Spire.PDF for .NET</font>. "

'Render HTML text
Dim font As New PdfFont(PdfFontFamily.Helvetica, 5)
Dim brush As PdfBrush = PdfBrushes.Black
Dim richTextElement As New PdfHTMLTextElement(htmlText, font, brush)
richTextElement.TextAlign = TextAlign.Left

'Format Layout
Dim format As New PdfMetafileLayoutFormat()
format.Layout = PdfLayoutType.Paginate
format.Break = PdfLayoutBreakType.FitPage

'Draw htmlString  
richTextElement.Draw(page, New RectangleF(0, 20, page.GetClientSize().Width, page.GetClientSize().Height), format)
doc.SaveToFile("Output.pdf")