Click here to Skip to main content
13,351,044 members (68,700 online)
Click here to Skip to main content
Add your own
alternative version


95 bookmarked
Posted 14 Dec 2005

Convert a Text File to a PDF File

, 14 Dec 2005
Rate this:
Please Sign up or sign in to vote.
This article shows how to convert a text file to PDF.

Sample Image


This article presents a basic text to PDF library. With this library, you can convert a plain text file to PDF format (PDF is an abbreviation of Portable Document Format). You can break your text file into PDF pages at any place (as long as you set up a page break "1" at column 1). This article also presents a sample application to demonstrate how to use this DLL.


I started writing this library because I really couldn’t find a good C# library for exporting my reports. Of course, you can create your own library to suit your own purposes.

Implementation of the Library

To achieve the page break functionality, I created StartPage, EndPage, StartObject, and StorePage functions. If the stream reader reads the page break "1" of the input text file, I'll be able to end this page and start a new page. That is the whole idea.

The Dotext function is as follows:

private void DoText(StreamReader sr)
    string strLine = string.Empty;

    //Start Page

        while (sr.Peek() >= 0)
            //Get one string at a time from the input text file
            strLine = sr.ReadLine()+"\r\n";

            //If yPos <= this.margin?
            if(yPos <= this.margin) 
                //Invoke EndPage and StartPage functions

            if(strLine == "" || strLine == null)
                //Is there a page break "1"?
                int cmpPageVal = String.Compare(strLine.Substring(0,1),"1");
                //Is there a Formfeed?
                int cmpfVal    = String.Compare(strLine.Substring(0,1),"\f");
                bool bl = false;

                if(cmpfVal == 0)
                    //Invoke EndPage and StartPage functions
                    //If there is a page break "1"
                    if (cmpPageVal == 0)
                        //Invoke EndPage and StartPage functions

                        //Remove the page break "1"
                        strLine = strLine.Remove(0,1);


                    //Convert "strLine" to a char array
                    char[] textchars=strLine.ToCharArray();

                    for (int index=0;index<textchars.Length;index++)
                        char c=textchars[index];

                        //If there is page break
                        if(c=='1' && strLine.Length == 2)
                            //new line
                        else if(c=='\n')
                            if (!bl)
                            bl = true;

                    if (!bl)


            //Set yPos
            yPos -= leadSize;
        }//for loop

        //Close file
        sr = null;

        //End page

    catch( Exception ex ) 
        string error = ("The process failed: " + ex.Message);

Using This Library

In the sample application, I put a Button in a .aspx page.

Here is the code behind the button Click function:

private void Button1_Click(object sender, System.EventArgs e)
    //The input text file "TextFile.txt"
    string fileName = @"TextFile.txt";
    string filePath = Server.MapPath("temp/" + fileName);
    // Create a new PdfWriter
    TextPDF.PdfWriter pdfWriter = 
       new TextPDF.PdfWriter(842.0f, 1190.0f, 10.0f, 10.0f);

    if(filePath.Length > 0)
        //Write to a PDF file


By using this library, you can easily covert a plain text file to a PDF format file. You can also create a page break anywhere in the text file.

Note: I put the PDF output file underneath the "c:\temp\txtPdf.pdf" directory; and the input text file under the "TextPdfSample\temp\TextFile.txt".


  • 12/10/2005 - Posted the article.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Florence FZ Li
Web Developer
United States United States
M.S.: Computer Science, B.S.: Physics, MCSD: .NET, MCSD: VS 6

Florence currently works at Confident Software, Inc. Atlanta, U.S.A. Besides programming, during her spare time she enjoys opera.

You may also be interested in...


Comments and Discussions

QuestionCan this support other fonts Pin
Member 1061447418-Feb-16 21:34
memberMember 1061447418-Feb-16 21:34 
GeneralMy vote of 4 Pin
DBRobinsonTX10-Feb-16 9:15
memberDBRobinsonTX10-Feb-16 9:15 
QuestionFont Embeded Pin
offir livnat10-Dec-13 1:50
memberoffir livnat10-Dec-13 1:50 
QuestionJapanese Charcter Pin
mck_4u14-Jun-12 1:32
membermck_4u14-Jun-12 1:32 
Its not working with japanese charcters. How can we use this for Japanese text files
GeneralMy vote of 5 Pin
Jean Graham28-Oct-11 4:03
memberJean Graham28-Oct-11 4:03 
GeneralEasy to improve Pin
Steen Holm Pedersen19-Nov-09 0:16
memberSteen Holm Pedersen19-Nov-09 0:16 
GeneralRe: Easy to improve Pin
Florence FZ Li19-Nov-09 10:58
memberFlorence FZ Li19-Nov-09 10:58 
GeneralRe: Easy to improve Pin
zoopnfunk19-Nov-09 16:28
memberzoopnfunk19-Nov-09 16:28 
GeneralRe: Easy to improve Pin
Florence FZ Li20-Nov-09 3:55
memberFlorence FZ Li20-Nov-09 3:55 
GeneralRe: Easy to improve [modified/resolved] Pin
zoopnfunk23-Nov-09 10:08
memberzoopnfunk23-Nov-09 10:08 
GeneralMy vote of 1 Pin
captainplanet012310-May-09 23:57
membercaptainplanet012310-May-09 23:57 
GeneralPagebreak using with iTextShar Pin
gantulgazagdsuren17-Dec-08 22:48
membergantulgazagdsuren17-Dec-08 22:48 
GeneralDon't work with all charapter Pin
NicolaFerrari27-Oct-08 1:37
memberNicolaFerrari27-Oct-08 1:37 
Questioncan any1 pls email to me the entire sourcode as i am not able to download the code due to server inconsistency Pin
snow7fire13-May-08 20:24
membersnow7fire13-May-08 20:24 
Questioncan any1 pls email to me the entire sourcode as i am not able to download the code due to server inconsistency Pin
snow7fire13-May-08 20:23
membersnow7fire13-May-08 20:23 
Questiontext to pdf Pin
snow7fire13-May-08 19:21
membersnow7fire13-May-08 19:21 
AnswerRe: text to pdf Pin
Florence FZ Li14-May-08 4:18
memberFlorence FZ Li14-May-08 4:18 
GeneralRe: text to pdf Pin
snow7fire18-May-08 21:45
membersnow7fire18-May-08 21:45 
GeneralRe: text to pdf Pin
Florence FZ Li5-Jun-08 4:49
memberFlorence FZ Li5-Jun-08 4:49 
GeneralRe: text to pdf Pin
Member 102920652-Oct-13 23:27
memberMember 102920652-Oct-13 23:27 
QuestionHow to insert logo? Pin
rao_1025-Oct-07 8:57
memberrao_1025-Oct-07 8:57 made my work easy Pin
rao_1019-Oct-07 11:13
memberrao_1019-Oct-07 11:13 
QuestionBlank first page Pin
kepat20-Sep-07 10:05
memberkepat20-Sep-07 10:05 
GeneralThanks... but need some explanation about code. Pin
mani_iips16-Aug-07 23:00
membermani_iips16-Aug-07 23:00 
GeneralThanks Pin
N_dog15-Jun-07 5:49
memberN_dog15-Jun-07 5:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 14 Dec 2005
Article Copyright 2005 by Florence FZ Li
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid