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 for ASP.NET.

  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);
    }
    view raw test.cs hosted with ❤ by GitHub

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>
view raw test.html hosted with ❤ by GitHub

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);
}
view raw test.cs hosted with ❤ by GitHub

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>
view raw test.html hosted with ❤ by GitHub

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);
}
view raw test.cs hosted with ❤ by GitHub

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

Create PDF

You can use the Selection TX Text Control .NET Server for ASP.NET
TXTextControl Namespace
Selection Class
Load Method
Exchanges the currently selected text with text in a certain format.
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);
}
view raw test.cs hosted with ❤ by GitHub

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#