Click here to Skip to main content
15,867,748 members
Articles / Programming Languages / C++
Article

Text2PDF

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

Image 1

Introduction

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 "pdflib.hpp"

void CreatePDF()
{
    PDFlib pdf;
    pdf.set_parameter("compatibility", "1.4");    //Compatible for Acrobat 5
    
    if (pdf.open("test.pdf")==-1)
    {
        AfxMessageBox("Can not create PDF file.", MB_ICONERROR);
        return;
    }
    
    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);
    
    pdf.show("Hello World!");
    
    pdf.end_page();

    pdf.close();
}

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

Enjoy!

License

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


Written By
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
Member 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
DPScollector21-Jul-13 6:32 
QuestionHow to convert folder and subfolders of text to pdf? Pin
daveofgv14-Mar-13 4:35
daveofgv14-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
amir 25-Aug-11 18:41 
GeneralBig Text file Handling Pin
semc8217-Mar-10 17:11
semc8217-Mar-10 17:11 
This tool is not able to handle very big text file say in MB's.
Providing a big file for pdf conversion do not work.Please suggest
Questionhow create table ? and how insert image ? Pin
Gege868-Mar-09 9:41
Gege868-Mar-09 9:41 
QuestionPDF to Text format in dot net Pin
Member 339317118-Jan-09 19:47
Member 339317118-Jan-09 19:47 
Questionabout C# Pin
alvi_du21-Nov-07 18:35
alvi_du21-Nov-07 18:35 
GeneralEntering data on PDF form Pin
elgee7722-Jun-07 12:26
elgee7722-Jun-07 12:26 
Questionpdf & devc++ Pin
mitnick200611-Jun-07 9:04
mitnick200611-Jun-07 9:04 
QuestionPDF View Pin
M.DJO25-Nov-06 1:39
M.DJO25-Nov-06 1:39 
AnswerRe: PDF View Pin
mesajflaviu19-Apr-10 23:29
mesajflaviu19-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
karnatavamsi10-May-06 1:07 
Questiondid u remove these file ? Pin
hell_angel122116-Mar-06 22:43
hell_angel122116-Mar-06 22:43 
GeneralMake read only pdf Pin
Omar.Pessoa20-Feb-06 1:53
Omar.Pessoa20-Feb-06 1:53 
GeneralRe: Make read only pdf Pin
Omar.Pessoa20-Feb-06 2:16
Omar.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
Omar.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
itbuff30-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
amir 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.