C#/VB.NET: Convert Text Files to PDF

A text file is a type of computer file that contains plain text. It can be viewed on almost any computer but has very basic and limited functionalities. If you would like to perform more manipulations on text files, such as inserting annotations or form fields, you can convert them to PDF. In this article, we will demonstrate how to convert text files to PDF in C# and VB.NET using Spire.PDF for .NET.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Convert Text Files to PDF in C# and VB.NET

The following are the main steps to convert a text file to PDF using Spire.PDF for .NET:

  • Read the text in the text file into a string object using File.ReadAllText() method.
  • Create a PdfDocument instance and add a page to the PDF file using PdfDocument.Pages.Add() method.
  • Create a PdfTextWidget instance from the text.
  • Draw the text onto the PDF page using PdfTextWidget.Draw() method.
  • Save the result file using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
using System.IO;

namespace ConvertTextToPdf
{
    class Program
    {
        static void Main(string[] args)
        {
            //Read the text from the text file
            string text = File.ReadAllText(@"Input.txt");

            //Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();
            //Add a page
            PdfPageBase page = pdf.Pages.Add();

            //Create a PdfFont instance
            PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 11);

            //Create a PdfTextLayout instance
            PdfTextLayout textLayout = new PdfTextLayout();
            textLayout.Break = PdfLayoutBreakType.FitPage;
            textLayout.Layout = PdfLayoutType.Paginate;

            //Create a PdfStringFormat instance
            PdfStringFormat format = new PdfStringFormat();
            format.Alignment = PdfTextAlignment.Justify;
            format.LineSpacing = 20f;

            //Create a PdfTextWidget instance from the text
            PdfTextWidget textWidget = new PdfTextWidget(text, font, PdfBrushes.Black);
            //Set string format
            textWidget.StringFormat = format;

            //Draw the text at the specified location of the page
            RectangleF bounds = new RectangleF(new PointF(10, 25), page.Canvas.ClientSize);
            textWidget.Draw(page, bounds, textLayout);

            //Save the result file
            pdf.SaveToFile("TextToPdf.pdf", FileFormat.PDF);
        }
    }
}

C#/VB.NET: Convert Text Files to PDF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.