pdfbox 创建pdf文档

这篇博客介绍了如何使用Java库PDFBox创建PDF文档,包括添加页面、设置字体、写入文本以及绘制线条。示例代码展示了创建一个包含'Hello World!'的PDF文件的过程,并指出PDFBox的坐标系统原点位于页面的左下角。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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的坐标系统,远点在左下角!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值