Products Technologies Demo Docs Blog Support Company

Convert HTML to PDF in ASP.NET Core C#

HTML snippets are often used as the basis in many applications for the creation of documents such as PDF files. Creating a PDF document from HTML content is demonstrated in this article.

Convert HTML to PDF in ASP.NET Core C#

Although TX Text Control offers many sophisticated ways to create PDF documents, including merging MS Word templates or creating from scratch, creating a PDF document from HTML snippets is a popular task. For many PDF libraries, this is the only way to create documents, and HTML is often used for storage of text snippets in databases.

This article describes how to programmatically generate PDF documents from HTML content in a .NET Console App.

Preparing the Application

For the purposes of this demo, a .NET 6 console application is built.

Prerequisites

The following tutorial requires a trial version of TX Text Control .NET Server.

  1. In Visual Studio, create a new Console App using .NET 6.

  2. In the Solution Explorer, select your created project and choose Manage NuGet Packages... from the Project main menu.

    Select Text Control Offline Packages from the Package source drop-down.

    Install the latest versions of the following package:

    • TXTextControl.TextControl.ASP.SDK

    Create PDF

Adding the Code

  1. Open the Program.cs file and add the following code:

    using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
    {
        tx.Create();
       
        string html = "<html><body><h1>Hello, World!</h1></body></html>";
     
        // load HTML string
        tx.Load(html, TXTextControl.StringStreamType.HTMLFormat);
        // save as PDF
        tx.Save("output.pdf", TXTextControl.StreamType.AdobePDF);
    }

Running the Application

  1. Run the application and check the output folder for the generated PDF document.

Create PDF

HTML with CSS

HTML snippets can contain CSS styles that are applied to the HTML elements. The following example shows an HTML snippet with a CSS style that is applied to a heading and a paragraph element:

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>Sample HTML</title>
    <style>
        h1 {
            color: red;
            font-size: 16pt;
            margin-bottom: 20px;
        }
        p {
            color: blue;
            text-align: center;
            border: 1px solid black;
            padding: 20px;
        }
        a {
            color: green;
            font-weight: bold;
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <h1>Sample HTML Page</h1>
    <p>This is a sample HTML page with CSS.</p>
    <p>For more information, please visit <a href="https://www.textcontrol.com">Text Control</a>.</p>
</body>
</html>

After you have converted this HTML to PDF using the TX Text Control, you will see the following results.

Create PDF

Modifying Styles

When loading HTML content, TX Text Control automatically generates styles and applies the styles defined in the HTML. After the HTML content is loaded, the stylesheets can be modified. The following code shows how to modify the BODY root style by setting a different font. All of the inherited styles will adapt to this newly defined font setting.

using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
{
    tx.Create();
   
    string html = File.ReadAllText("sample.html");
 
    // load HTML string
    tx.Load(html, TXTextControl.StringStreamType.HTMLFormat);

    // change HTML root style BODY
    TXTextControl.ParagraphStyle bodyStyle = tx.ParagraphStyles.GetItem("BODY");
    bodyStyle.FontName = "Times New Roman";
    bodyStyle.Apply();

    // save as PDF
    tx.Save("output.pdf", TXTextControl.StreamType.AdobePDF);
}

The loaded HTML with the new base font "Times New Roman" is shown in the following screenshot .

Create PDF

HTML with Images

The included images will be loaded from local or web paths and will be converted to PDF as well. This example shows an SVG image that is loaded from a web path, which is dynamically loaded and included in the output.

<body>
    <h1>Sample HTML Page</h1>

    <img src="https://s1-www.textcontrol.com/img/corporate_id/tx_logo.svg" />

    <p>This is a sample HTML page with CSS.</p>
    <p>For more information, please visit <a href="https://www.textcontrol.com">Text Control</a>.</p>
</body>

The following screenshot shows the created PDF document in Acrobat Reader.

Create PDF

Appending HTML Snippets

HTML snippets can be appended to the current document. This is useful when merging multiple snippets into one resulting document. The following code shows how to append an HTML snippet to the current document.

using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
{
    tx.Create();
   
    string html = File.ReadAllText("sample.html");
 
    string appendHtml = "<html><body><h1>Appended</h1></body></html>";

    // load HTML string
    tx.Load(html, TXTextControl.StringStreamType.HTMLFormat);

    tx.Append(appendHtml, 
      TXTextControl.StringStreamType.HTMLFormat, 
      TXTextControl.AppendSettings.StartWithNewParagraph);

    // save as PDF
    tx.Save("output.pdf", TXTextControl.StreamType.AdobePDF);
}

The following screenshot shows the resulting document with the appended HTML snippet.

Create PDF

You can use the Selection class to insert a snippet anywhere in the document.

using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
{
    tx.Create();
   
    string html = File.ReadAllText("sample.html");
 
    string appendHtml = "<html><body><h1>Appended</h1></body></html>";

    // load HTML string
    tx.Load(html, TXTextControl.StringStreamType.HTMLFormat);

    tx.Select(17, 0);
    tx.Selection.Load(appendHtml, TXTextControl.StringStreamType.HTMLFormat);

    // save as PDF
    tx.Save("output.pdf", TXTextControl.StreamType.AdobePDF);
}

The following screenshot shows the resulting document with the inserted HTML snippet.

Create PDF

Conclusion

In this blog post, we have learned several ways to convert HTML content into a PDF document. There are a number of other ways to create PDF documents using the TX Text Control.

Learn More

TX Text Control allows developers to create PDF files programmatically using C#. This article shows various ways to create Adobe PDF documents.

Creating PDF Files using TX Text Control .NET in C#

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

ASP.NET

Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.

ASP.NET Core
Angular
Blazor
JavaScript
React
  • Angular
  • Blazor
  • React
  • JavaScript
  • ASP.NET MVC, ASP.NET Core, and WebForms

Learn more Trial token Download trial

Related Posts

ASP.NETConversionDOCX

PDF Conversion in .NET: Convert DOCX, HTML and more with C#

PDF conversion in .NET is a standard requirement for generating invoices, templates, and accessible reports. This article provides an overview of PDF conversion capabilities using TX Text Control,…


ASP.NETASP.NET CoreDOCX

Sign Documents with a Self-Signed Digital ID From Adobe Acrobat Reader in…

This article shows how to create a self-signed digital ID using Adobe Acrobat Reader and how to use it to sign documents in .NET C#. The article also shows how to create a PDF document with a…


ASP.NETASP.NET CoreConvert

Programmatically Convert MS Word DOCX Documents to PDF in .NET C#

This article shows how to convert MS Word DOCX documents to PDF in .NET C# using the ServerTextControl component. The example shows how to load a DOCX file from a file or from a variable and how…


ASP.NETGenerative AIOpenAI

Chat PDF - A Generative AI Application for PDF Documents using TX Text…

This article shows how to create a generative AI application for PDF documents using TX Text Control and OpenAI functions in C#. The application uses the OpenAI GPT-3 engine to answer questions on…


ASP.NETForm FieldsPDF

Document Viewer: Save the Values of Form Fields in Documents

The TX Text Control Document Viewer is used to allow users to fill in form fields in documents. This article explains how to save a document with the values of the filled in form fields.