pdfbox创建pdf文档,hello world!
记下代码:
public class CreatPdfFile {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
PDDocument document = null;
try{
document = new PDDocument();
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
System.out.println();
PDFont font = PDType1Font.HELVETICA_BOLD;
PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.beginText();
contentStream.setFont(font, 20);
contentStream.newLineAtOffset(100, 100);
//contentStream.showText("Hello World !");
//contentStream.newLine();
contentStream.showText("Hello World !");
//contentStream.showTextWithPositioning(args);
contentStream.newLineAtOffset(0, 200);
contentStream.setFont(font, 200);
contentStream.showText("The individual calls to add resources such as PDResources.addFont(PDFont font) and PDResources.addXObject(PDXObject xobject, String prefix) have been replaced with PDResources.add(resource type) where resource type represents the different resource classes such as PDFont, PDAbstractPattern and so on. The add method now supports all the different type of resources available.");
contentStream.endText();
contentStream.moveTo(0, 0);
contentStream.lineTo(300, 300);
contentStream.stroke();
//contentStream.drawLine(0, 0, 100, 100);
contentStream.close();
document.save("./data/practice/HelloWorld.pdf");
System.out.println(PDRectangle.A4.getWidth());
System.out.println(PDRectangle.A4.getHeight());
System.out.println(PDRectangle.A4.getLowerLeftX());
System.out.println(PDRectangle.A4.getLowerLeftY());
System.out.println(PDRectangle.A4.getUpperRightX());
System.out.println(PDRectangle.A4.getUpperRightY());
} catch (IOException e) {
e.printStackTrace();
}
finally {
document.close();
}
return;
}
}
发现pdfbox的坐标系统,远点在左下角!
这篇博客介绍了如何使用Java库PDFBox创建PDF文档,包括添加页面、设置字体、写入文本以及绘制线条。示例代码展示了创建一个包含'Hello World!'的PDF文件的过程,并指出PDFBox的坐标系统原点位于页面的左下角。
8136

被折叠的 条评论
为什么被折叠?



