Published September 7, 2022
C# Text to PDF [Code Example Tutorial]
Over the past few years, the use of .NET Technology has increased rapidly, especially following the release of .NET Core, which ultimately increases the use of the C# programming language. C# is now one of the most famous and widely used programming languages supported by Microsoft. It is therefore now essential that any C# programmer now learns how to convert text into PDF files.
There are multiple use cases where we need to convert text into PDFs.
- Making Reports
- Converting Invoices into PDF
- Making a Text Editor
- Creating Fillable PDF Forms
Converting text files into PDF files
...and many more.
We need a third-party library to convert text into PDF documents. There are multiple options on the market, but some are paid, some are difficult to use, and some have performance issues. There is a library that is free for development and easy to use, so much so that we can use it to convert text into PDF using just one line of code. It also provides higher performance levels. This library is IronPDF.
IronPDF is supported by all .NET frameworks. It is developer-friendly and provides a variety of features in a single library, including creating PDFs from URLs, creating PDFs from text, converting HTML files to PDF files, converting text files to PDF files, and many more.
Let's take a look at an example where we convert text to PDF.
How to Convert TXT to PDF in C#
Create a Visual Studio Project
Open Microsoft Visual Studio. Click on Create New Project. Select the template "Console Application". I am using a console application for the demonstration, but you can use Windows Forms, ASP.NET Web forms, MVC, Web APIs, or any template as per your needs.
Select Next, Name the Project, Select Target Framework, and press Create. A new console project will be created.

Next, we need to install the NuGet Package for IronPDF.
IronPDF is a .NET library for generating, reading, editing, and saving PDF files in .NET projects. IronPDF features HTML-to-PDF for .NET 5 Core, Standard and Framework, with full HTML-to-PDF support including CSS3 and JS.
Install the NuGet Package
To install the NuGet Package, go to Tools > NuGet Package Manager > Package Manager Console. The following window will appear:

Next, write the following command in the Package Manager Console.
Install-package IronPDF
Press Enter.

This will install the IronPDF library. Once imported, we can use all the functionalities provided by this library anywhere in our project.
Convert Text to PDF
Next, let's address our main task here --- converting C# text into a PDF file.
We first need to reference the IronPDF library to our program.cs file. Write the following code snippet at the top of the file.
using IronPdf;
using IronPdf;
Imports IronPdf
Next, write the following code inside the main function. This code will convert text to PDF.
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>This is my PDF</h1><p>This is generated for the tutorial of C# txt to PDF</p>");
pdf.SaveAs(@"D:\Iron Software\textToPDF\myFirstPDF.pdf");
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>This is my PDF</h1><p>This is generated for the tutorial of C# txt to PDF</p>");
pdf.SaveAs(@"D:\Iron Software\textToPDF\myFirstPDF.pdf");
Dim renderer As New ChromePdfRenderer()
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>This is my PDF</h1><p>This is generated for the tutorial of C# txt to PDF</p>")
pdf.SaveAs("D:\Iron Software\textToPDF\myFirstPDF.pdf")
Code Explanation
We first need to create the object of the ChromePdfRenderer
. This object is responsible for converting text to PDF. In the second Line, the RenderHtmlAsPdf
function is called with the reference of our renderer object.
This will generate a PDF from the text passed in the argument of this function. That PDF will then be temporarily stored as a PDF Document type.
Finally, we saved our newly generated PDF file to our local drive using the SaveAs
function. Pass the path as an argument in the SaveAs
function.
Output
This is the output of our code. It is very easy to generate PDF programmatically from text.

TXT File to PDF file
In the above example, we have seen how to convert simple TXT to PDF. Now, let's suppose we have a text file that we want to convert into a PDF. In this example, we will learn how to convert a text document into a PDF document.
I have created a sample source TXT file as shown below.

The following code will convert a text file to PDF.
First, add the following namespace:
using System.IO;
using System.IO;
Imports System.IO
Write the following code snippet inside the main function.
string text = File.ReadAllText(@"D:\Iron Software\textToPDF\myTxtFile.txt");
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf(text);
pdf.SaveAs(@"D:\Iron Software\textToPDF\textFileToPDF.pdf");
string text = File.ReadAllText(@"D:\Iron Software\textToPDF\myTxtFile.txt");
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf(text);
pdf.SaveAs(@"D:\Iron Software\textToPDF\textFileToPDF.pdf");
Dim text As String = File.ReadAllText("D:\Iron Software\textToPDF\myTxtFile.txt")
Dim renderer As New ChromePdfRenderer()
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(text)
pdf.SaveAs("D:\Iron Software\textToPDF\textFileToPDF.pdf")
File.ReadAllText
will read all the text from the file specified in the argument of the function. This text is then held into a string variable.
This variable is then passed as an argument of the RenderHtmlAsPdf
() function. This function will convert text into a PDF document.
In the SaveAs
function, we can specify the output filename.
Output

In the above example, we have easily converted text into a new PDF document.
Add Watermark
Let's add a watermark to our newly created PDF. Watermarks can help to avoid the misuse of documents. You can set your own watermark as per your needs. Let's consider the following example:
pdf.WatermarkAllPages("<h1>my Watermark</h1>", IronPdf.Editing.WaterMarkLocation.TopCenter,45,45);
pdf.SaveAs(@"D:\Iron Software\textToPDF\myFirstPDF.pdf");
pdf.SaveAs(@"D:\Iron Software\textToPDF\myFirstPDF.pdf");
pdf.WatermarkAllPages("<h1>my Watermark</h1>", IronPdf.Editing.WaterMarkLocation.TopCenter,45,45);
pdf.SaveAs(@"D:\Iron Software\textToPDF\myFirstPDF.pdf");
pdf.SaveAs(@"D:\Iron Software\textToPDF\myFirstPDF.pdf");
pdf.WatermarkAllPages("<h1>my Watermark</h1>", IronPdf.Editing.WaterMarkLocation.TopCenter,45,45)
pdf.SaveAs("D:\Iron Software\textToPDF\myFirstPDF.pdf")
pdf.SaveAs("D:\Iron Software\textToPDF\myFirstPDF.pdf")
PDF is the variable of the PdfDocument
Type. The pdf.watermark
function will add a watermark to our document. Pass your watermark text as an argument of the function. I have passed "my watermark" as an example. The second argument is the watermark's location. There are multiple options available, and you can choose any. The third argument is opacity, and you can set the opacity as per your needs. I have set it as "45". The fourth argument I have passed is rotation. I have set rotation to 45, but you can set it to any, as per your needs.
Output
The following is the output generated by our sample code:

Print a PDF Document
Printing a PDF document using IronPDF is very easy --- just write the following line of code:
pdf.Print();
pdf.Print();
pdf.Print()
This will print a PDF document on your default printer. There are multiple printer settings available, and you can choose as per your requirements. For more details regarding PDF print settings, please refer to this link
Summary
In this tutorial, we have learned a very easy way to convert text into a PDF file. We have demonstrated examples with code. We have converted text to PDF, and we have also generated a PDF from a TXT file. Moreover, we have also learned to add watermarks to our documents.
There are multiple useful and interesting features provided by IronPDF, but it is impossible to cover them all here. For more details, please click here.
IronPDF is a part of the Iron Software suite. Iron Suite has an array of interesting products including IronXL, IronBarcode, IronOCR, and IronWebscraper. I can assure you that you will find all of these products helpful. You can save up to 250% by purchasing the complete Iron Suite, as you can currently get all five products for the price of just two. For more details, please click here.
I hope this article was understandable and easy to follow. For any queries, feel free to contact us.
You can download the software product from this link.