无法弄清楚如何使用PDFBox [英] Cannot figure out how to use PDFBox

查看:614
本文介绍了无法弄清楚如何使用PDFBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个PDF文件,其中包含文档中的大量文本框和来自其他类的文本框。我正在使用PDFBox。

I am trying to create a PDF file with a lot of text boxes in the document and textfields from another class. I am using PDFBox.

好的,创建一个新文件很简单,编写一行文本很容易。现在,当我尝试插入下一个文本行或文本字段时,它会覆盖内容。

OK, creating a new file is easy and writing one line of text is easy. Now, when I am trying to insert the next text line or textfield, it overwrites the content.

    PDDocument doc = null;
    PDPage page = null;

       try{
           doc = new PDDocument();
           page = new PDPage();

           doc.addPage(page);
           PDFont font = PDType1Font.HELVETICA_BOLD;

           PDPageContentStream title = new PDPageContentStream(doc, page);
           title.beginText();
           title.setFont( font, 14 );
           title.moveTextPositionByAmount( 230, 720 );
           title.drawString("DISPATCH SUMMARY");
           title.endText();
           title.close();

           PDPageContentStream title1 = new PDPageContentStream(doc, page);
           title1.beginText();
           title1.setFont( font, 11 );
           title1.moveTextPositionByAmount( 30, 620 );
           title1.drawString("DEPARTURE");
           title1.endText();
           title1.close();


           doc.save("PDFWithText.pdf");
           doc.close();
    } catch (Exception e){
        System.out.println(e);
    }

它确实给我一个错误:你正在覆盖现有内容,你应该使用追加模式。

It does give me an error: "You are overwriting an existing content, you should use the append mode".

所以我正在尝试 title1.appendRawCommands(String),但它不是工作。

So I am trying title1.appendRawCommands(String), but it is not working.

如何添加新文本框和文本字段(来自其他类)?我已经在互联网上阅读了数十个教程,但他们只显示创建一行。

How would I add new text boxes and textfields (from another class)? I have read tens of tutorials on Internet, but they only show creating one line.

推荐答案

PDPageContentStream title1 = new PDPageContentStream(doc, page, true, true);

OP将此作为答案发布,因此这将标记为系统有一个答案

此外,如果第一个内容流包含实质上改变图形状态的操作,例如通过更改当前转换矩阵,并且希望新内容流从这些更改开始,应该使用具有三个布尔参数的构造函数:

Furthermore, if the first content stream contains operations substantially changing the graphics state, e.g. by changing the current transformation matrix, and one wants the new content stream to start with these changes reverted, one should use the constructor with three boolean parameters:

PDPageContentStream title1 = new PDPageContentStream(doc, page, true, true, true);

这篇关于无法弄清楚如何使用PDFBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆