Convert text to PDF in C#

TXT to PDF

This article explains how to convert TEXT files to PDFs.

Creating a PDF from a TEXT file

To create a PDF from a TEXT file, follow the steps below:

  1. Create a GdPictureDocumentConverter object.
  2. Load the source document by passing its path to the LoadFromFile method. Recommended: Specify the source document format with a member of the DocumentFormat enumeration.
  3. Save the output in a new PDF document with the SaveAsPDF method.

The example below creates a PDF document from a TEXT file:

using GdPictureDocumentConverter gdpictureDocumentConverter = new GdPictureDocumentConverter();
// Load the source document.
gdpictureDocumentConverter.LoadFromFile(@"C:\temp\source.text", GdPicture14.DocumentFormat.DocumentFormatTEXT);
// Save the output in a new PDF document.
gdpictureDocumentConverter.SaveAsPDF(@"C:\temp\output.pdf");

Optional configuration properties

Optionally, configure the conversion with the following properties of the GdPictureDocumentConverter object:

The example below creates a PDF document from a TXT file with a custom configuration:

using GdPictureDocumentConverter gdpictureDocumentConverter = new GdPictureDocumentConverter();
// Load the source document.
gdpictureDocumentConverter.LoadFromFile(@"C:\temp\source.txt", GdPicture14.DocumentFormat.DocumentFormatTXT);
// Configure the conversion.
gdpictureDocumentConverter.TxtFontFamily = "Arial";
gdpictureDocumentConverter.TxtFontSize = 16;
// Save the output in a new PDF document.
gdpictureDocumentConverter.SaveAsPDF(@"C:\temp\output.pdf");

Used methods and properties

Related topics