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 increased the use of the C# programming language. It is therefore now essential that any C# programmer now learns how to convert text into PDF files.

There are multiple use cases where it is needed to convert text into PDFs.

  1. Making Reports
  2. Converting Invoices into PDF
  3. Making a Text Editor
  4. Creating Fillable PDF Forms
  5. Converting text files into PDF files

    ...and many more.

It is necessary to have 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 all it takes is just one line of code to convert text into PDF. 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 of how to convert text to PDF.

Create a Visual Studio Project

Open Microsoft Visual Studio. Click on Create New Project. Select the template "Console Application" for simplicity, 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.

C# Text to PDF (Code Example Tutorial), Figure 1: Create a new Console Application in Visual Studio Create a new Console Application in Visual Studio

Next, 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:

C# Text to PDF (Code Example Tutorial), Figure 2: Package Manager Console Package Manager Console

Next, write the following command in the Package Manager Console.

Install-Package IronPdf

Press Enter.

C# Text to PDF (Code Example Tutorial), Figure 3: Installation progress in the Package Manager Console Installation progress in the Package Manager Console

This will install the IronPDF library to be able to use all the functionalities provided by this library anywhere in the project.

Convert Text to PDF

Next, let's address the main task here --- converting C# text into a PDF file.

Firstly, reference the IronPDF library to the program.cs file. Write the following code snippet at the top of the file.

using IronPdf;
using IronPdf;
Imports IronPdf
VB   C#

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")
VB   C#

Code Explanation

Firstly, 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 the 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, the newly generated PDF file is saved to the local drive using the SaveAs function. Pass the path as an argument in the SaveAs function.

Output

This is the output of the above code. It is very easy to generate PDF programmatically from text.

C# Text to PDF (Code Example Tutorial), Figure 4: The output PDF file from the code sample The output PDF file from the code sample

TXT File to PDF file

In the above example, it shows how to convert simple TXT to PDF. Now, this example will demonstrate how to convert a text document into a PDF document.

Given a sample source TXT file as shown below.

C# Text to PDF (Code Example Tutorial), Figure 5: The sample TXT file The sample TXT file

The following code will convert a text file to PDF.

First, add the following namespace:

using System.IO;
using System.IO;
Imports System.IO
VB   C#

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")
VB   C#

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.

Finally, specify the output filename in the SaveAs function.

Output

C# Text to PDF (Code Example Tutorial), Figure 6: The output PDF file from a TXT file The output PDF file from a TXT file

In the above example, it is very easy to convert text into a new PDF document.

Add Watermark

Let's add a watermark to this newly created PDF. Watermarks can help to avoid the misuse of documents. You can set your watermark as per your needs. Let's consider the following example:

pdf.ApplyWatermark("<h1>my Watermark</h1>",45 ,45, IronPdf.Editing.VerticalAlignment.Top, IronPdf.Editing.HorizontalAlignment.Center);
pdf.SaveAs(@"D:\Iron Software\textToPDF\myFirstPDF.pdf");
pdf.ApplyWatermark("<h1>my Watermark</h1>",45 ,45, IronPdf.Editing.VerticalAlignment.Top, IronPdf.Editing.HorizontalAlignment.Center);
pdf.SaveAs(@"D:\Iron Software\textToPDF\myFirstPDF.pdf");
pdf.ApplyWatermark("<h1>my Watermark</h1>",45,45, IronPdf.Editing.VerticalAlignment.Top, IronPdf.Editing.HorizontalAlignment.Center)
pdf.SaveAs("D:\Iron Software\textToPDF\myFirstPDF.pdf")
VB   C#

The pdf variable holds a PdfDocument type. The ApplyWatermark function will add a watermark to the document. Pass your watermark text as an argument of the function such as "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. For example: "45". The fourth argument is rotation and it sets the rotation to 45, but you can set it to any, as per your needs.

Output

The following is the output generated by the sample code:

C# Text to PDF (Code Example Tutorial), Figure 7: The PDF file with the watermark in the center The PDF file with the watermark in the center

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()
VB   C#

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 sample page

Summary

This tutorial showed a very easy way to convert text into a PDF file with step-by-step examples and code explanations: convert text to PDF, generate a PDF from a TXT file and print this PDF file. Moreover, learned to add watermarks to the documents.

There are multiple useful and interesting features provided by IronPDF such as rendering charts in PDFs, adding barcodes, enhancing security with passwords, and even handling PDF forms, but it is impossible to cover them all here. For more details, please click on the homepage.

IronPDF is a part of the Iron Software suite. Iron Suite has an array of interesting products including IronXL, IronBarcode, IronOCR, and IronWebscraper. It is assured 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.