Net-informations.com
SiteMap  | About    

How to create PDF file from Text file

pdf - Portable Document Format

Pdfsharp is a popular open source framework which could be used to create PDF files programmatically. In many situations we need PDF documents instead of text documents because text files are listed as the simplest file format, which limits users only to edit words. From the following steps you can easily convert a text file to a PDF formated 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 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.




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());
            }
        }
    }
}




net-informations.com (C) 2016    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.