Converting text to PDF
Introduction
In this tutorial we will see how jPDF can be used to convert a simple text document (ASCII) to its PDF equivalent. By definition, ASCII documents do not have any formatting information - if your application needs to render bold, alignments, and colors - please refer to the RTFtoPDF tutorial.
Due to its size, the full source code won't be disclosed in this tutorial, but it can be found in the jPDF distribution (in the samples folder).
Converting to text
The conversion from ASCII text to PDF is pretty straightforward with a tool such as jPDF Creator. jPDF Creator is a PDF driver for the acclaimed Java2D API. Let us try to explain: a typical Java2D application will render graphical primitives to the screen or to a printer.
Using jPDF Creator, programmers can easily draw PDF contents using the same API as if they were rendering on the screen.
Below is an extract of a sample text document from the United Nations. We will convert it to PDF.
UNITED NATIONS
THE SECRETARY-GENERAL
ADDRESS TO THE GENERAL ASSEMBLY
New York, 6 June 2006
Mr. President, Excellencies,
Ladies and Gentlemen:
In preparation for the High-Level Dialogue on International Migration and
Development, which this Assembly is to hold on 14 and 15 September, I have the
honour to submit the report that you requested from me in resolution 59/241,
reiterated by resolution 60/227.
The report offers a comprehensive review of recent trends in international
migration, focusing particularly on the impact that it has on both countries
of destination and countries of origin. It draws on many previous studies,
including the very valuable one undertaken by the Global Commission on
International Migration, which presented its report and recommendations to me
last year.
...etc...
There are three main differences between the text above and its PDF equivalent:
- PDF documents have the notion of pagination.
- Lines are implicitly wrapped to the page boundaries.
- Certain characters, such as TABs, need to be treated differently as PDF has no notion of them.
Java2D provides all the methods to calculate text boundaries. It is just a matter of algorithmics to run the convertion.
The details of the algorithm won't be discussed in this tutorial. You may refer to the source code for further details. Using the sample code, the job narrows to the following code snippet:
public static void main(String arg) throws Exception
{ File textFile = new File("mytextfile.txt");
if (!textFile.exists())
throw new Exception("Source text file not found");
File pdfFile = new File("mypdffile.pdf");
TextToPDF instance = new TextToPDF();
instance.convertToPdf(textFile, pdfFile);
}
Result
The link below points to the PDF document, a result of the TXTtoPDF conversion.
Copyright © Crionics Inc. 2012 All rights reserved. Terms of Use | Privacy Policy
