As easy as 1, 2, 3...
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).
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 me 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.
Okay, but let's get back to the real purpose of this
tutorial... Below is an extract of a sample text document from the
United Nations.
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:
Java2D provides all the methods to calculate text
boundaries. This feature, along with pattern matching detection, will
do most of the job.
As we said earlier, the details of the algorithm won't be
discussed in this tutorial. You can refer to the source code for
further details.
Using the sample, the works comes down 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);
}