Convert Text to PDF in C#, VB.NET

Converting text to PDF is based on the reason below: text is listed as the simplest file format, which limits users only to edit words. However, in most cases, people need do more tasks such as protect files, store the documents on a company's server, view document by freeware. All these functions are hardly to be realized neither by text nor other formats. Thus, convert text to PDF is indispensable. Different from other tools, Spire.PDF, as a professional PDF component, allows you not only to easily convert text to PDF via C#, VB.NET but also to Extract text and images out of the PDF file.

Using Spire.PDF, you can realize the whole task on any platform of .NET, Silverlight and WPF. Before you start, please Download Spire.PDF first, and then, follow code samples to convert Text to PDF in C#, VB.NET

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Pdf;
using System.IO;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace TexttoPDF
    class Program
        static void Main(string[] args)
            string text = File.ReadAllText("TestDocument.txt");
            PdfDocument doc = new PdfDocument();
            PdfSection section = doc.Sections.Add();
            PdfPageBase page = section.Pages.Add();
            PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 11);
            PdfStringFormat format = new PdfStringFormat();
            format.LineSpacing = 20f;
            PdfBrush brush = PdfBrushes.Black;
            PdfTextWidget textWidget = new PdfTextWidget(text, font, brush);
            float y = 0;
            PdfTextLayout textLayout = new PdfTextLayout();
            textLayout.Break = PdfLayoutBreakType.FitPage;
            textLayout.Layout = PdfLayoutType.Paginate;
            RectangleF bounds = new RectangleF(new PointF(0, y), page.Canvas.ClientSize);
            textWidget.StringFormat = format;
            textWidget.Draw(page, bounds, textLayout);
            doc.SaveToFile("TxtToPDf.pdf", FileFormat.PDF);
Imports Spire.Pdf.Graphics
Imports Spire.Pdf
Imports System.IO
Imports System.Drawing

Module Module1

    Sub Main()
        Dim text As String = File.ReadAllText("TestDocument.txt")
        Dim doc As New PdfDocument()
        Dim section As PdfSection = doc.Sections.Add()
        Dim page As PdfPageBase = section.Pages.Add()
        Dim font As New PdfFont(PdfFontFamily.Helvetica, 11)
        Dim format As New PdfStringFormat()
        format.LineSpacing = 20.0F
        Dim brush As PdfBrush = PdfBrushes.Black
        Dim textWidget As New PdfTextWidget(text, font, brush)
        Dim y As Single = 0
        Dim textLayout As New PdfTextLayout()
        textLayout.Break = PdfLayoutBreakType.FitPage
        textLayout.Layout = PdfLayoutType.Paginate
        Dim bounds As New RectangleF(New PointF(0, y), page.Canvas.ClientSize)
        textWidget.StringFormat = format
        textWidget.Draw(page, bounds, textLayout)
        doc.SaveToFile("TxtToPDf.pdf", FileFormat.PDF)

    End Sub

End Module

Below is an effective screenshot of text to PDF, please have a look.

Text to PDF