Getsuyodev, Programming is mainly used when constructing an application. Programming requires knowledge of application domains, algorithms, and programming language expertise. Programming can be developed in different logic based on developer knowledge.

How to create PDF file from Text file



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 most straightforward file format, which limits users only to edit words. From the following steps, you can easily convert a text file to a PDF formatted 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.

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 a 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 writing each line, yPoint will increase 40 pixels line space then only you get a reasonable 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 looks like the following image:

The following program shows how to generate a PDF file from the 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());
            }
        }
    }
}

Comments are closed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More