2012年11月27日火曜日

PDFBox create pdf from Graphics2d

- create a document
- add a page
- create a PDJpeg from a BufferedImage of the Graphics2D object
- add the PDJpeg to your page
- save the document

public static void main(String... _) throws Exception {
PDDocument doc = null;
try {
doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage(page);
PDXObjectImage ximage = null;

BufferedImage image = new BufferedImage(300, 300, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.drawString("Hallo PDFBox", 100, 100);
g.dispose();
ximage = new PDJpeg(doc, image);

/* Lade ein Bild */
// ximage = new PDJpeg(doc, new FileInputStream( "PFAD" ) );

PDPageContentStream contentStream = new PDPageContentStream(doc,page);
contentStream.drawImage(ximage, 20, 20);
contentStream.close();
doc.save("PFAD WO PDF GESPEICHERT WERDEN SOLL"); //anpassen
} finally {
if (doc != null) {
doc.close();
}
}
}

0 件のコメント:

コメントを投稿