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

2017-02-28 09:32:11 Written by  support iceblue
Rate this item
(0 votes)

Spire.PDF provides support to render simple HTML string in a PDF document by using PdfHTMLTextElement class. (Only available on .NET, .Net Core/ .NET Standard doesn't offer PdfHTMLTextElement & PdfMetafileLayoutFormat 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/2),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#]
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;


namespace InsertHTMLStyledTexttoPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //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 HTML styled text to PDF using "
                             + "Spire.PDF for .NET. ";

            //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 / 2), format);
            doc.SaveToFile("Output.pdf");
        }
    }
}
[VB.NET]
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports System.Drawing


Namespace InsertHTMLStyledTexttoPDF
	Class Program
		Private Shared Sub Main(args As String())
			'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 HTML styled text to PDF using " + "Spire.PDF for .NET. "

			'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 / 2), format)
			doc.SaveToFile("Output.pdf")
		End Sub
	End Class
End Namespace

Additional Info

  • tutorial_title: Insert HTML Styled Text to PDF in C#, VB.NET
Last modified on Sunday, 26 September 2021 01:56