# 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.

- **Author:** Bjoern Meyer
- **Published:** 2023-10-23
- **Modified:** 2026-07-17
- **Description:** 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.
- **5 min read** (884 words)
- **Tags:**
  - ASP.NET
  - ServerTextControl
  - PDF
  - HTML
- **Web URL:** https://www.textcontrol.com/blog/2023/10/23/convert-html-to-pdf-in-aspnet-core-csharp/
- **LLMs URL:** https://www.textcontrol.com/blog/2023/10/23/convert-html-to-pdf-in-aspnet-core-csharp/llms.txt
- **LLMs-Full URL:** https://www.textcontrol.com/blog/2023/10/23/convert-html-to-pdf-in-aspnet-core-csharp/llms-full.txt

---

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.
> 
> - [Download Trial Version](https://www.textcontrol.com/product/tx-text-control-dotnet-server/download/)

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](https://s1-www.textcontrol.com/assets/dist/blog/2023/10/23/a/assets/step1.webp "Create PDF")

### Adding the Code

3. 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

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

![Create PDF](https://s1-www.textcontrol.com/assets/dist/blog/2023/10/23/a/assets/results.webp "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:

```

<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](https://s1-www.textcontrol.com/assets/dist/blog/2023/10/23/a/assets/results2.webp "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](https://s1-www.textcontrol.com/assets/dist/blog/2023/10/23/a/assets/results4.webp "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](https://s1-www.textcontrol.com/assets/dist/blog/2023/10/23/a/assets/results3.webp "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](https://s1-www.textcontrol.com/assets/dist/blog/2023/10/23/a/assets/results5.webp "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](https://s1-www.textcontrol.com/assets/dist/blog/2023/10/23/a/assets/results6.webp "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# ](https://www.textcontrol.com/blog/2022/05/24/creating-pdf-files-using-tx-text-control-dotnet/llms-full.txt)

---

## About Bjoern Meyer

As CEO, Bjoern is the visionary behind our strategic direction and business development, bridging the gap between our customers and engineering teams. His deep passion for coding and web technologies drives the creation of innovative products. If you're at a tech conference, be sure to stop by our booth - you'll most likely meet Bjoern in person. With an advanced graduate degree (Dipl. Inf.) in Computer Science, specializing in AI, from the University of Bremen, Bjoern brings significant expertise to his role. In his spare time, Bjoern enjoys running, paragliding, mountain biking, and playing the piano.

- [LinkedIn](https://www.linkedin.com/in/bjoernmeyer/)
- [X](https://x.com/txbjoern)
- [GitHub](https://github.com/bjoerntx)

---

## Related Posts

- [Create Fillable PDFs from HTML Forms in C# ASP.NET Core Using a WYSIWYG Template](https://www.textcontrol.com/blog/2026/03/17/create-fillable-pdfs-from-html-forms-in-csharp-aspnet-core-using-a-wysiwyg-template/llms.txt)
- [Why HTML to PDF Conversion is Often the Wrong Choice for Business Documents in C# .NET](https://www.textcontrol.com/blog/2026/03/13/why-html-to-pdf-conversion-is-often-the-wrong-choice-for-business-documents-in-csharp-dot-net/llms.txt)
- [PDF Conversion in .NET: Convert DOCX, HTML and more with C#](https://www.textcontrol.com/blog/2025/08/05/pdf-conversion-in-dotnet-convert-docx-html-and-more-with-csharp/llms.txt)
- [Sign Documents with a Self-Signed Digital ID From Adobe Acrobat Reader in .NET C#](https://www.textcontrol.com/blog/2024/08/12/sign-documents-with-a-self-signed-digital-id-from-adobe-acrobat-reader-in-net-c-sharp/llms.txt)
- [Programmatically Convert MS Word DOCX Documents to PDF in .NET C#](https://www.textcontrol.com/blog/2024/08/09/programmatically-convert-ms-word-docx-documents-to-pdf-in-net-c-sharp/llms.txt)
- [Chat PDF - A Generative AI Application for PDF Documents using TX Text Control and OpenAI Functions in C#](https://www.textcontrol.com/blog/2024/02/23/ask-pdf-a-generative-ai-application-for-pdf-documents-using-tx-text-control-and-openai-functions-in-c-sharp/llms.txt)
- [Document Viewer: Save the Values of Form Fields in Documents](https://www.textcontrol.com/blog/2023/12/19/document-viewer-save-the-values-of-form-fields-in-documents/llms.txt)
- [Store Documents as PDF/A using C# - A Future-Proof Archiving Format](https://www.textcontrol.com/blog/2023/10/24/store-documents-as-pdfaa-using-csharp-a-futureproof-archiving-format/llms.txt)
- [Generate PDF Documents from MS Word DOCX Templates in ASP.NET Core C#](https://www.textcontrol.com/blog/2023/10/20/generate-pdf-documents-from-ms-word-docx-templates-in-aspnet-core-csharp/llms.txt)
- [How to Load and View PDF Documents in ASP.NET Core C#](https://www.textcontrol.com/blog/2023/10/20/how-to-load-and-view-pdf-documents-in-aspnet-core-csharp/llms.txt)
- [Document Viewer: Save the Values of Form Fields in Documents](https://www.textcontrol.com/blog/2023/10/19/document-viewer-save-the-values-of-form-fields-in-documents/llms.txt)
- [How to Create and Deploy PDF Forms in ASP.NET Core C#](https://www.textcontrol.com/blog/2023/10/17/how-to-create-and-deploy-pdf-forms-in-aspnet-core-csharp/llms.txt)
- [AI Natural Language Document Generation with MCP and TX Text Control .NET](https://www.textcontrol.com/blog/2026/07/16/ai-natural-language-document-generation-with-mcp-server-and-tx-text-control-dotnet/llms.txt)
- [C# Document Generation: A Developer's Guide for .NET](https://www.textcontrol.com/blog/2026/07/08/csharp-document-generation-developer-guide-for-dotnet/llms.txt)
- [Validating PDF/UA Documents in .NET C#: A Practical Guide](https://www.textcontrol.com/blog/2026/07/06/validating-pdf-ua-documents-in-dotnet-csharp/llms.txt)
- [Converting HTML to Markdown in C# .NET](https://www.textcontrol.com/blog/2026/06/11/converting-html-to-markdown-in-csharp-dot-net/llms.txt)
- [TX Text Control vs IronPDF for Enterprise PDF Workflows: Complete Comparison Guide](https://www.textcontrol.com/blog/2026/04/28/tx-text-control-vs-ironpdf-for-enterprise-pdf-workflows-complete-comparison-guide/llms.txt)
- [Using QR Codes in PDF Documents in C# .NET](https://www.textcontrol.com/blog/2026/04/21/using-qr-codes-in-pdf-documents-in-csharp-dotnet/llms.txt)
- [Programmatically Fill, Flatten, and Export DOCX Form Templates to PDF in C# .NET](https://www.textcontrol.com/blog/2026/04/10/programmatically-fill-flatten-and-export-docx-form-templates-to-pdf-in-csharp-dotnet/llms.txt)
- [Why Structured E-Invoices Still Need Tamper Protection using C# and .NET](https://www.textcontrol.com/blog/2026/03/24/why-structured-e-invoices-still-need-tamper-protection-using-csharp-and-dotnet/llms.txt)
- [A Complete Guide to Converting Markdown to PDF in .NET C#](https://www.textcontrol.com/blog/2026/01/07/a-complete-guide-to-converting-markdown-to-pdf-in-dotnet-csharp/llms.txt)
- [Why PDF Creation Belongs at the End of the Business Process](https://www.textcontrol.com/blog/2026/01/02/why-pdf-creation-belongs-at-the-end-of-the-business-process/llms.txt)
- [Designing the Perfect PDF Form with TX Text Control in .NET C#](https://www.textcontrol.com/blog/2025/12/16/designing-the-perfect-pdf-form-with-tx-text-control-in-dotnet-csharp/llms.txt)
- [Why Defining MIME Types for PDF/A Attachments Is Essential](https://www.textcontrol.com/blog/2025/12/10/why-defining-mime-types-for-pdfa-attachments-is-essential/llms.txt)
- [Validate Digital Signatures and the Integrity of PDF Documents in C# .NET](https://www.textcontrol.com/blog/2025/11/14/validate-digital-signatures-and-the-integrity-of-pdf-documents-in-csharp-dotnet/llms.txt)
