Click here to Skip to main content
15,867,141 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

 
GeneralRe: How to perform reverse job Pin
Sasan30-Sep-03 22:30
Sasan30-Sep-03 22:30 
GeneralRe: How to perform reverse job Pin
vrbcik19-Oct-03 9:35
vrbcik19-Oct-03 9:35 
GeneralRe: How to perform reverse job Pin
RoverMan6-Nov-03 11:07
RoverMan6-Nov-03 11:07 
GeneralRe: How to perform reverse job Pin
Sasan14-Feb-04 3:50
Sasan14-Feb-04 3:50 
AnswerRe: How to perform reverse job Pin
Aris Adrianto S13-Feb-04 23:58
Aris Adrianto S13-Feb-04 23:58 
GeneralRe: How to perform reverse job Pin
Sasan14-Feb-04 4:05
Sasan14-Feb-04 4:05 
QuestionHow to create TOC tree? Pin
activeray4-Aug-03 17:24
activeray4-Aug-03 17:24 
QuestionHow to create TOC tree? Pin
activeray4-Aug-03 17:24
activeray4-Aug-03 17:24 
AnswerRe: How to create TOC tree? Pin
Abbas_Riazi4-Aug-03 17:49
professionalAbbas_Riazi4-Aug-03 17:49 
GeneralPrint to postscript Pin
unitrunker4-Aug-03 16:15
unitrunker4-Aug-03 16:15 
GeneralRe: Print to postscript Pin
Abbas_Riazi4-Aug-03 18:02
professionalAbbas_Riazi4-Aug-03 18:02 
GeneralRe: Print to postscript Pin
unitrunker5-Aug-03 7:33
unitrunker5-Aug-03 7:33 
GeneralRe: Print to postscript Pin
Abbas_Riazi5-Aug-03 18:00
professionalAbbas_Riazi5-Aug-03 18:00 
GeneralRe: Print to postscript Pin
unitrunker6-Aug-03 7:55
unitrunker6-Aug-03 7:55 
GeneralRe: Print to postscript Pin
Abbas_Riazi8-Aug-03 17:44
professionalAbbas_Riazi8-Aug-03 17:44 
GeneralRe: Print to postscript Pin
unitrunker9-Aug-03 6:59
unitrunker9-Aug-03 6:59 
GeneralHTMLDOC Pin
Anonymous4-Aug-03 7:45
Anonymous4-Aug-03 7:45 
GeneralRe: HTMLDOC Pin
Abbas_Riazi4-Aug-03 18:20
professionalAbbas_Riazi4-Aug-03 18:20 
GeneralFree PDF Converter available Pin
Wesner Moise4-Aug-03 7:42
Wesner Moise4-Aug-03 7:42 

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.