Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Text2PDF

0.00/5 (No votes)
3 Aug 2003 8  
A free utility to convert text files to Adobe PDF.

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.

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.

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