How to create PDF file from Text file

Pdfsharp is indeed a widely used open-source framework that enables the programmatic creation of PDF files. In numerous scenarios, PDF documents are preferred over plain text files due to their enhanced capabilities. While text files are considered the simplest file format, limiting users to basic word editing, converting a text file to a PDF document can provide additional functionality and flexibility.

You can freely download the Assemblies version from the following link: Download PDFsharp Assemblies

After download the zip file, extract it and add the reference to your c# project.

pdf assembly files

If you want to know the step by step tutorial on how to create your first pdf file programmatically, follow the link : How to create PDF file programmatically

Steps to create PDF file programmatically.

First you need a Text Reader Object to read the text from Text file.

System.IO.TextReader readFile = new StreamReader("Text.txt");

Next step is to create a PDF Object and a Page Object.

PdfDocument pdf = new PdfDocument(); PdfPage pdfPage = pdf.AddPage();

Also initialize the Graphics and Font

XGraphics graph = XGraphics.FromPdfPage(pdfPage); XFont font = new XFont("Verdana", 20, XFontStyle.Regular );

Now your PDF Object is ready to write the contents, so the next step is to read the content from the Text file and write to the PDF .

line = readFile.ReadLine(); graph.DrawString(line, font, XBrushes.Black, new XRect(40, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);

In the above code we set X as 40 pixels from the left side and Y as a variable "yPoint", because after write the each line, yPoint will increase 40 pixels line spce then only you get a resonable space between lines.

yPoint = yPoint + 40;

After read all the line from Txt file, you can save the PDF file with your desired name with .pdf extension.

pdf.Save("yourflename.pdf");

Then close the Reader Object.

readFile.Close(); readFile = null;

Your pdf file look like the following image:

convert text file to pdf file

The following program shows how to generate a PDF file from Text file content.

Full Source C#
using System; using System.Windows.Forms; using System.Diagnostics; using PdfSharp; using PdfSharp.Drawing; using PdfSharp.Pdf; using System.IO; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { try { string line = null; System.IO.TextReader readFile = new StreamReader("Text.txt"); int yPoint = 0; PdfDocument pdf = new PdfDocument(); pdf.Info.Title = "TXT to PDF"; PdfPage pdfPage = pdf.AddPage(); XGraphics graph = XGraphics.FromPdfPage(pdfPage); XFont font = new XFont("Verdana", 20, XFontStyle.Regular ); while (true) { line = readFile.ReadLine(); if (line == null) { break; // TODO: might not be correct. Was : Exit While } else { graph.DrawString(line, font, XBrushes.Black, new XRect(40, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft); yPoint = yPoint + 40; } } string pdfFilename = "txttopdf.pdf"; pdf.Save(pdfFilename); readFile.Close(); readFile = null; Process.Start(pdfFilename); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } }