Here is a sample Java program that uses an API library called iText for creating PDF in Java.
Download: itextpdf.zip
PDF sample: test.pdf
This one sample includes two programs of different execution styles:
iText provides three ways to create PDF. This sample is trying the three ways in each page of the total three pages -> see test.pdf
The direct PDF writing of page 2 tries the same text, filled shape and line drawing as in the Hand-written PDF sample.
Layout sample files as below, compile and run the program. You need iText library (itext-2.1.3.jar) and East Asian fonts (iTextAsian.jar).
[layout of files]
test.gif
test.pdf <- this is created when you run the program
test
iTextNative.java
PdfSyntax.java
Java2D.java
PDFbyiText.java
[compile]
javac -classpath iText-2.1.3.jar;. test/iTextNative.java
javac -classpath iText-2.1.3.jar;. test/PdfSyntax.java
javac -classpath iText-2.1.3.jar;. test/Java2D.java
javac -classpath iText-2.1.3.jar;. test/PDFbyiText.java
[run]
java -classpath iText-2.1.3.jar;iTextAsian.jar;. test.PDFbyiText
In addition to files compiled above, compile PdfServlet.java. You need to point to the servlet-api.jar.
javac -classpath iText-2.1.3.jar;<location-of-servlet-api.jar>;. test/PdfServlet.java
Layout files in the servlet container as below:
<tomcat-install-directory>/webapps
itext
test.gif
WEB-INF
web.xml
classes/test
iTextNative.class
Java2D.class
PDFbyiText.class
PdfServlet.class
PdfSyntax.class
lib
iText-2.1.3.jar
iTextAsian.jar
Try accessing this servlet (for example by http://localhost:8080/itext), you can see the same contents as test.pdf above.
Presented by: Kobu.Com
Copyright © 2008 Kobu.Com. All rights reserved.
Updated 2008 Nov 02
The published sample code is a prototype and is not complete.
Please refrain from duplicating the sample code and its document in another place.
This page is link-free. We welcome your questions and comments.