This example demonstrates a few basic features of the
OpenPDF library. (OpenPDF is a fork of the iText library that uses a business-friendly license; the latest jar file can be found
here) The code creates a 2-page PDF document using various fonts, adds a header (containing the page number) and a footer, draws simple graphics (a couple of lines), and adds text both at an absolute position, and sequentially in a natural flow. It also adds an image, a table that shows various table features, and finally, rotated text.
With the help of the android-awt library, OpenPDF can even be used on Android.
An alternative to OpenPDF is Apache PdfBox, although its API is not as full-featured. PdfBox shows how to use it to create a PDF.
import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.*;
import com.lowagie.text.alignment.HorizontalAlignment;
import com.lowagie.text.pdf.*;
public class OpenPDFExample
{
public static void main (String[] args)
{
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
try
{
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("OpenPDFExample.pdf"));
BaseFont bf_helv = BaseFont.createFont(BaseFont.HELVETICA, "Cp1252", false);
BaseFont bf_times = BaseFont.createFont(BaseFont.TIMES_ROMAN, "Cp1252", false);
BaseFont bf_courier = BaseFont.createFont(BaseFont.COURIER, "Cp1252", false);
BaseFont bf_symbol = BaseFont.createFont(BaseFont.SYMBOL, "Cp1252", false);
HeaderFooter footer = new HeaderFooter(
new Phrase("This is page: ", new Font(bf_courier)), true);
footer.setBorder(Rectangle.NO_BORDER);
footer.setAlignment(Element.ALIGN_CENTER);
document.setFooter(footer);
HeaderFooter header = new HeaderFooter(
new Phrase("This is a header without a page number", new Font(bf_symbol)), false);
header.setAlignment(Element.ALIGN_CENTER);
document.setHeader(header);
document.open();
int y_line1 = 650;
int y_line2 = y_line1 - 50;
int y_line3 = y_line2 - 50;
PdfContentByte cb = writer.getDirectContent();
cb.setLineWidth(0f);
cb.moveTo(250, y_line3 - 100);
cb.lineTo(250, y_line1 + 100);
cb.moveTo(50, y_line1);
cb.lineTo(400, y_line1);
cb.moveTo(50, y_line2);
cb.lineTo(400, y_line2);
cb.moveTo(50, y_line3);
cb.lineTo(400, y_line3);
cb.stroke();
cb.beginText();
cb.setFontAndSize(bf_helv, 12);
String text = "Sample text for alignment";
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, text + " Center", 250, y_line1, 0);
cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, text + " Right", 250, y_line2, 0);
cb.showTextAligned(PdfContentByte.ALIGN_LEFT, text + " Left", 250, y_line3, 0);
cb.endText();
try {
Image simple = Image.getInstance("Simple.jpg");
simple.setAbsolutePosition(100, 100);
simple.scalePercent(50);
document.add(simple);
} catch (Exception ex) {
System.out.println("no image for you");
}
document.newPage();
Paragraph par = new Paragraph("bold paragraph");
par.getFont().setStyle(Font.BOLD);
document.add(par);
par = new Paragraph("italic paragraph");
par.getFont().setStyle(Font.ITALIC);
document.add(par);
par = new Paragraph("underlined and strike-through paragraph");
par.getFont().setStyle(Font.UNDERLINE | Font.STRIKETHRU);
document.add(par);
Table table = new Table(3);
table.setBorderWidth(2);
table.setBorderColor(new Color(0, 0, 255));
table.setPadding(5);
table.setSpacing(5);
Cell c = new Cell("header");
c.setHeader(true);
c.setColspan(3);
table.addCell(c);
table.endHeaders();
c = new Cell("example cell with rowspan 2 and red border");
c.setRowspan(2);
c.setBorderColor(new Color(255, 0, 0));
table.addCell(c);
table.addCell("1.1");
table.addCell("2.1");
table.addCell("1.2");
table.addCell("2.2");
c = new Cell("align center");
c.setHorizontalAlignment(HorizontalAlignment.CENTER);
table.addCell(c);
cell = new Cell("big cell");
cell.setRowspan(2);
cell.setColspan(2);
table.addCell(cell);
c = new Cell("align right");
c.setHorizontalAlignment(HorizontalAlignment.RIGHT);
table.addCell(c);
document.add(table);
cb.beginText();
cb.setFontAndSize(bf_times, 14);
cb.setTextMatrix(100, 300);
cb.showText("Text at position 100, 300.");
cb.endText();
PdfTemplate template = cb.createTemplate(300, 300);
template.beginText();
template.setFontAndSize(bf_times, 14);
template.showText("Rotated text at position 400, 200.");
template.endText();
float rotate = 90;
float x = 400;
float y = 200;
float angle = (float) (-rotate * (Math.PI / 180));
float xScale = (float) Math.cos(angle);
float yScale = (float) Math.cos(angle);
float xRot = (float) -Math.sin(angle);
float yRot = (float) Math.sin(angle);
cb.addTemplate(template, xScale, xRot, yRot, yScale, x, y);
document.close();
} catch (Exception ex) {
System.err.println(ex.getMessage());
}
}
}
CodeSnippets