Click here to Skip to main content
15,397,981 members
Articles / Programming Languages / C++
Posted 3 Aug 2003


192 bookmarked


Rate me:
Please Sign up or sign in to vote.
4.86/5 (59 votes)
3 Aug 20034 min read
A free utility to convert text files to Adobe PDF.

Image 1


This is a free utility to convert any text file to PDF format. As you know PDF is an abbreviation of Portable Document Format. This means, you can view, edit and print this type of document in many platforms including Microsoft Windows (3.1, 9X, ME, 2000, XP, 2003), Linux, BSD, Sun Solaris, Unix, Mac, etc.

For conversion of text file to PDF, I use PDFLib library, a very simple collection of APIs for creating PDF files on the fly. This library is free for personal usage and requires a license for commercial products. In other words, you must purchase a license for developing commercial products.

What is PDF?

Portable Document Format (PDF) is the de facto standard for secure and reliable distribution and exchange of electronic documents and forms around the world, with a ten-year track record. PDF is a universal file format that preserves the fonts, images, graphics, and layout of any source document, regardless of the application and platform used to create it. Adobe® PDF files are compact and complete, and can be shared, viewed, and printed by anyone with free Adobe Reader® software. To date, more than 500 million copies of the software has been distributed. You can convert any document to Adobe PDF using Adobe Acrobat® software products, enabling business, engineering, and creative professionals to create, distribute, and exchange secure and reliable Adobe PDF documents. For more information, see the Adobe Acrobat family.

Governments and enterprises around the world have adopted PDF to streamline document management, increase productivity, and reduce reliance on paper. For example, PDF is the standard format for the electronic submission of drug approvals to the U.S. Food and Drug Administration (FDA), and for electronic case filing in U.S. federal courts. It is also used by the governments of the United Kingdom and Germany for electronic document exchange. Finally, the ISO's PDF/X specification is the standard file format used for the digital distribution of advertisements for publication.

An open file format specification, PDF is available to anyone who wants to develop tools to create, view, or manipulate PDF documents. Indeed, more than 1,800 vendors offer PDF-based solutions, ensuring that organizations that adopt the PDF standard have a variety of tools to leverage the Portable Document Format and to customize document processes.

Consumers benefit from PDF's platform ubiquity as well, because Adobe Reader lets them view rich-media PDF slide shows and electronic cards created using Adobe Photoshop® Album software.

What is PDFlib?

PDFlib is a development tool for PDF-enabling your software, or generating PDF on your server. PDFlib saves you the intricate details of PDF generation by offering a simple-to-use API for programmatically creating PDF files from within your own server- or client-side software. PDFlib doesn't make use of third-party software for generating PDF, nor does it require any other tools.

How the program works?

Starting point is selecting a text file that you want to convert to PDF (figure 1). After this, you must select your desired font. Arial 12 is default font for converting, but your hand is free to choose any true type font. The selected font will be embedded in the PDF file.

Logic of the program is very simple. Just read entire text file, then compute width of strings and add them to PDF file line by line. Adding text, page and font is the duty of PDFLib. Figure 2 shows this logic.

Image 2

For controlling on your PDF file, there is some options. For example, how Acrobat shows your PDF, what is your page size (A4, Letter, ...), margins and open mode (thumbnail, bookmark, ...). Figure 3 shows options dialog of Text2PDF utility.

Image 3

If you checked "Launch PDF after conversion" check box, Acrobat Reader will be launched and shows your PDF (Figure 4).

Final PDF - Text2PDF Utility

Using the library

After downloading PDFlib library, add following header files and *.cpp files to your project: pdflib.h, pdflib.hpp, pdflib.cpp

Then add pdflib.lib to your project and at the top of your source code include pdflib.h (APIs) or pdflib.hpp (PDFLib class definition). Here is a sample:

#include <span class="code-string">"pdflib.hpp"</span>

void CreatePDF()
    PDFlib pdf;
    pdf.set_parameter("compatibility", "1.4");    //Compatible for Acrobat 5
    if ("test.pdf")==-1)
        AfxMessageBox("Can not create PDF file.", MB_ICONERROR);
    pdf.begin_page(a4_width, a4_height);
    //Font will be embedded in PDF
    int font=pdf.findfont("Arial", "winansi", 1);    
    pdf.setfont(font, 12);

    pdf.set_text_pos(10, a4_height - 10);"Hello World!");


If you do not have Acrobat Reader, download it by clicking this icon: Sample screenshot



This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

CEO Solaris Electronics LLC
United Arab Emirates United Arab Emirates
I was born in Shiraz, a very beautiful famous city in Iran. I started programming when I was 12 years old with GWBASIC. Since now, I worked with various programming languages from Basic, Foxpro, C/C++, Visual Basic, Pascal to MATLAB and now Visual C++.
I graduated from Iran University of Science & Technology in Communication Eng., and now work as a system programmer for a telecommunication industry.
I wrote several programs and drivers for Synthesizers, Power Amplifiers, GPIB, GPS devices, Radio cards, Data Acquisition cards and so many related devices.
I'm author of several books like Learning C (primary and advanced), Learning Visual Basic, API application for VB, Teach Yourself Object Oriented Programming (OOP) and etc.
I'm winner of January, May, August 2003 and April 2005 best article of month competition, my articles are:

You can see list of my articles, by clicking here

Comments and Discussions

QuestionError 404 for link for the library text2PDF Pin
Member 1218825630-Jun-22 23:58
MemberMember 1218825630-Jun-22 23:58 
GeneralMy vote of 5 Pin
Amir Mohammad Nasrollahi27-Jul-13 6:13
professionalAmir Mohammad Nasrollahi27-Jul-13 6:13 
GeneralMy vote of 1 Pin
DPScollector21-Jul-13 6:32
MemberDPScollector21-Jul-13 6:32 
QuestionHow to convert folder and subfolders of text to pdf? Pin
daveofgv14-Mar-13 4:35
Memberdaveofgv14-Mar-13 4:35 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey26-Feb-12 19:44
professionalManoj Kumar Choubey26-Feb-12 19:44 
Questionthankful for you Pin
amir 25-Aug-11 18:41
Memberamir 25-Aug-11 18:41 
GeneralBig Text file Handling Pin
semc8217-Mar-10 17:11
Membersemc8217-Mar-10 17:11 
Questionhow create table ? and how insert image ? Pin
Gege868-Mar-09 9:41
MemberGege868-Mar-09 9:41 
QuestionPDF to Text format in dot net Pin
Member 339317118-Jan-09 19:47
MemberMember 339317118-Jan-09 19:47 
Questionabout C# Pin
alvi_du21-Nov-07 18:35
Memberalvi_du21-Nov-07 18:35 
GeneralEntering data on PDF form Pin
elgee7722-Jun-07 12:26
Memberelgee7722-Jun-07 12:26 
Questionpdf & devc++ Pin
mitnick200611-Jun-07 9:04
Membermitnick200611-Jun-07 9:04 
QuestionPDF View Pin
M.DJO25-Nov-06 1:39
MemberM.DJO25-Nov-06 1:39 
AnswerRe: PDF View Pin
mesajflaviu19-Apr-10 23:29
Membermesajflaviu19-Apr-10 23:29 
Generalconsole mode application [modified] Pin
Mei You Qian3-Oct-06 4:48
professionalMei You Qian3-Oct-06 4:48 
GeneralWord 2 PDF Pin
karnatavamsi10-May-06 1:07
Memberkarnatavamsi10-May-06 1:07 
Questiondid u remove these file ? Pin
hell_angel122116-Mar-06 22:43
Memberhell_angel122116-Mar-06 22:43 
GeneralMake read only pdf Pin
Omar.Pessoa20-Feb-06 1:53
MemberOmar.Pessoa20-Feb-06 1:53 
GeneralRe: Make read only pdf Pin
Omar.Pessoa20-Feb-06 2:16
MemberOmar.Pessoa20-Feb-06 2:16 
AnswerRe: Make read only pdf Pin
Abbas_Riazi20-Feb-06 3:12
professionalAbbas_Riazi20-Feb-06 3:12 
GeneralRe: Make read only pdf Pin
Omar.Pessoa21-Feb-06 9:44
MemberOmar.Pessoa21-Feb-06 9:44 
AnswerRe: Make read only pdf Pin
Abbas_Riazi21-Feb-06 10:05
professionalAbbas_Riazi21-Feb-06 10:05 
QuestionHow about html to pdf? Pin
itbuff30-Jan-06 0:58
Memberitbuff30-Jan-06 0:58 
AnswerRe: How about html to pdf? Pin
Abbas_Riazi30-Jan-06 4:54
professionalAbbas_Riazi30-Jan-06 4:54 
QuestionProperty Pin
amir mortazavi23-Oct-05 19:02
Memberamir mortazavi23-Oct-05 19:02 

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.