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:
- Create a
GdPictureDocumentConverter
object. - Load the source document by passing its path to the
LoadFromFile
method. Recommended: Specify the source document format with a member of theDocumentFormat
enumeration. - 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");
Using gdpictureDocumentConverter As 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")End Using
Used methods
Related topics
Optional configuration properties
Optionally, configure the conversion with the following properties of the GdPictureDocumentConverter
object:
PdfEnableLinearization
is a Boolean value that specifies whether to linearize the output PDF to enable Fast Web View mode.TxtFontBold
TxtFontFamily
TxtFontItalic
TxtFontSize
TxtHorizontalTextAlignment
TxtPageHeight
TxtPageMarginBottom
TxtPageMarginLeft
TxtPageMarginRight
TxtPageMarginTop
TxtPageWidth
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");
Using gdpictureDocumentConverter As 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")End Using
Used methods and properties
Related topics