How to convert text file to pdf

The Portable Document Format (PDF) stands as a universally recognized and accepted file format, widely utilized for official documentation purposes. By encapsulating content in a PDF, formatting errors stemming from text file incompatibilities are effectively mitigated, ensuring the preservation of document integrity. Notably, PDFs are highly favored for essential documents, such as resumes and critical records.

Text file into a PDF format

The process of converting a text file into a PDF format is a straightforward endeavor. Using the Pdfsharp open-source library, developers can effortlessly create and manipulate PDF documents programmatically within the .NET ecosystem. This powerful library provides comprehensive functionalities for generating and customizing PDFs, enhancing the flexibility and control over the resulting document.

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 VB.NET 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 step you should Imports the necessary namespaces.

Imports PdfSharp Imports PdfSharp.Drawing Imports PdfSharp.Pdf

In order to read from a text file, you should create a Text Reader Object.

Dim readFile As System.IO.TextReader = New StreamReader("testfile.txt")

Then you have to create a PDF Object for creating your new PDF file.

Dim pdf As PdfDocument = New PdfDocument

and create the page.

Dim pdfPage As PdfPage = pdf.AddPage

Also initialize the Graphics and Font

Dim graph As XGraphics = XGraphics.FromPdfPage(pdfPage) Dim font As XFont = New XFont("Verdana", 20, XFontStyle.Regular)

Now you can read from the text file and write the content to the PDF Object.

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 set as "yPoint", because after write the each line yPoint will increment 40 pixels line spacs then only you will get a good line space.

yPoint = yPoint + 40

When you finish reading and writing, then you can save the PDF Object.

pdf.Save("yourflename.pdf")

You can include the path when you specify the file name.

Then close the Reader Object.

readFile.Close() readFile = Nothing

After save the file , you can double click and open the pdf file. Then you can see the content in PDF file same as in Text file.

convert text file to pdf file

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

Full Source VB.NET
Imports System.IO Imports PdfSharp Imports PdfSharp.Drawing Imports PdfSharp.Pdf Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Try Dim line As String Dim readFile As System.IO.TextReader = New StreamReader("Text.txt") Dim yPoint As Integer = 0 Dim pdf As PdfDocument = New PdfDocument pdf.Info.Title = "Text File to PDF" Dim pdfPage As PdfPage = pdf.AddPage Dim graph As XGraphics = XGraphics.FromPdfPage(pdfPage) Dim font As XFont = New XFont("Verdana", 20, XFontStyle.Regular) While True line = readFile.ReadLine() If line Is Nothing Then Exit While Else graph.DrawString(line, font, XBrushes.Black, _ New XRect(40, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft) yPoint = yPoint + 40 End If End While Dim pdfFilename As String = "txttopdf.pdf" pdf.Save(pdfFilename) readFile.Close() readFile = Nothing Process.Start(pdfFilename) Catch ex As Exception MsgBox(ex.ToString) End Try End Sub End Class

Conclusion

The Portable Document Format (PDF) stands as a widely recognized and utilized file format, safeguarding against formatting errors and serving as a preferred choice for official documents. Converting a text file to a PDF format is a straightforward endeavor, facilitated by the Pdfsharp open-source library, which enables developers to programmatically create and manipulate PDF documents within .NET supported languages. By adhering to the aforementioned steps, one can effortlessly convert a text file into a PDF format document, ensuring the seamless preservation and representation of content.