PDFBox之文档创建

1.创建一个空的PDF

下面的小例子表示如何使用PDFBox来创建一个新的PDF文档。

 // 创建一个空的文档
PDDocument document = new PDDocument();

// 创建一个空的Page然后添加到文档中
PDPage blankPage = new PDPage();
document.addPage( blankPage );

// 保存文档
document.save("BlankPage.pdf");

// 一定要确保最后文档是别关闭的
document.close();

1.1举例说明

    public static void createPDFFile() {
        
        PDDocument document = null;
        PDPage blankPage = null;

        try {
            document = new PDDocument();
            blankPage = new PDPage();
            
            document.addPage(blankPage);
            document.save("D:" + File.separator + "pdfBox.pdf");
            
        } catch (COSVisitorException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                document.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

2.使用PDF字体的Hello World

// 创建一个文档并且添加一个Page
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage( page );

// 创建一个FONTT
PDFont font = PDType1Font.HELVETICA_BOLD;

// 创建一个待加入的文档流
PDPageContentStream contentStream = new PDPageContentStream(document, page);

// 使用选择的字体定义一个文本内容
contentStream.beginText();
contentStream.setFont( font, 12 );
contentStream.moveTextPositionByAmount( 100, 700 );
contentStream.drawString( "Hello World" );
contentStream.endText();

// 关闭内容流
contentStream.close();

// 保存结果并且关闭文档对象
document.save( "Hello World.pdf");
document.close();

2.1举例说明

    public static void usePdfFont() {

        PDDocument document = new PDDocument();
        PDPage page = new PDPage();
        PDPageContentStream contentStream = null;
    

        PDFont font = PDType1Font.HELVETICA_BOLD;

        try {
            document.addPage(page);
            contentStream = new PDPageContentStream(document, page);
            contentStream.beginText();
            contentStream.setFont(font, 12);
            contentStream.moveTextPositionByAmount(100, 700);
            contentStream.drawString("Hello World");
            contentStream.endText();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                contentStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        try {
            document.save("D:" + File.separator + "Hello World.pdf");
        } catch (COSVisitorException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                document.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

转载于:https://www.cnblogs.com/zhangminghui/p/4700626.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值