我正在尝试使用 Apache PDFBox 根据通过对象传入的数据生成 PDF。由于数据量可变,我使用名为 volY
的变量来跟踪写入信息的 y 位置。如果 volY 大于 700,我会关闭正在写入的内容流,生成新页面和新内容流,然后开始写入新页面。我用来写入 pdf 的方法返回一个代表字符串高度的整数,我将其添加到 volY
中。由于某种原因,当我在迭代元素时尝试访问 volY 时,出现空指针异常。
这是我生成 PDF 的代码:
public void generateSection(PDPage startingPage, PDPageContentStream cs, List<TestSection> sections) throws IOException {
/*
* TODO
* verify list sequence integrity and reorder if not valid.
*/
int volY = 350;
PDPage page = startingPage;
PDPageContentStream vcs = cs;
// Iterate through sections
for(int i = 0; i < sections.size(); i++) {
if(volY > 700) {
vcs.close();
page = createPage();
vcs = new PDPageContentStream(pd, page);
volY = 50;
}
if(sections.get(i).isUrgent())
cs.setNonStrokingColor(URGENT);
drawString(sections.get(i).getType().getName(), 60, volY, vcs, page, 18);
cs.setNonStrokingColor(REGULAR_TEXT);
drawLine(vcs, 60, flipY(page, volY+8), 560);
volY += 30;
// Iterate through Items in section
TestSection s = sections.get(i);
for(int y = 0; y < s.getElements().size(); y++ ) {
TestReportElement re = s.getElements().get(y);
TestSubSection subSection = (TestSubSection)re;
volY++;
drawHeader(re.getTitle(), "a", volY, page, vcs);
for(int z = 0; z < subSection.getItems().size(); z++) {
//volY doesn't exist here for some reason? At the very least it's not modifiable.
if(vcs == null) {
System.err.println("VCS IS NULL");
System.exit(3);
}
TestInspectionItem ti = subSection.getItems().get(z);
vcs.setNonStrokingColor(BOLD_TEXT);
System.out.println(volY);
drawMultipleStrings(ti.getPrompt(), volY, vcs, page, z);
vcs.setNonStrokingColor(REGULAR_TEXT);
for(int z1 = 0; z1 < ti.getResponses().size(); z1++) {
if (volY > 700) {
vcs.close();
page = createPage();
vcs = new PDPageContentStream(pd, page);
volY = 50;
}
String text = ti.getResponses().get(z1);
drawMultipleStrings(text, volY+15, vcs, page, z1);
}
if (volY > 700) {
vcs.close();
page = createPage();
vcs = new PDPageContentStream(pd, page);
volY = 50;
}
}
if (volY > 700) {
vcs.close();
page = createPage();
vcs = new PDPageContentStream(pd, page);
volY = 50;
}
}
// Add 70 to account for a new section.
volY += 70;
}
vcs.close();
}
这是我用来绘制多个字符串的代码:
private int drawMultipleStrings(String text, int y, PDPageContentStream cs, PDPage page, int index) {
// Page is 900 units wide
// Assume font size is 13
int strSize = text.length();
int height = 0;
String textVal = text;
List<String> allText = new ArrayList<>();
int xVal = index % 2 == 0 ? 60 : 300;
if(strSize > 40) {
while(textVal.length() > 40) {
for (int i = 40; i > 0; i--) {
if (textVal.charAt(i) == ' ') {
allText.add(textVal.substring(0, i));
textVal = textVal.substring(i);
break;
}
}
}
allText.add(textVal);
for(int ind = 0; ind < allText.size(); ind++) {
String s = allText.get(ind);
if(s.charAt(0) == ' ') {
s = s.substring(1);
drawString(s, xVal, y+(13*ind), cs, page, 13);
} else {
// This should only trigger on the first iteration.
drawString(s, xVal, y+(13*ind), cs, page, 13);
}
height += 13;
}
// Allows items to be displayed in 2 columns based on the index
return index % 2 == 0 ? 0 : height + 32;
} else {
drawString(text, index % 2 == 0 ? 60: 300, y, cs, page, 13);
return 13;
}
}
此代码工作正常,但如果我将 drawMultipleStrings(ti.getPrompt(), volY, vcs, page, z);
更改为 volY += drawMultipleStrings(ti.getPrompt() , volY, vcs, page, z);
它抛出此异常:
java.lang.NullPointerException
at org.apache.pdfbox.pdmodel.PDPageContentStream.writeOperand(PDPageContentStream.java:2429)
at org.apache.pdfbox.pdmodel.PDPageContentStream.setNonStrokingColor(PDPageContentStream.java:1316)
at org.apache.pdfbox.pdmodel.PDPageContentStream.setNonStrokingColor(PDPageContentStream.java:1348)
at compliancego.report.PdfService.generateSection(PdfService.java:206)
at compliancego.report.PdfService.generateHeader(PdfService.java:176)
at compliancego.report.PdfService.<init>(PdfService.java:97)
at compliancego.report.PdfService.main(PdfService.java:73)
起初我以为这是因为有些数据不存在可写入,但如果我不更新 volY,它就可以正常工作。然后我认为这是创建新页面但流存在时未创建 PDPageContentStream 的问题。
预先感谢您的帮助!
最佳答案
您最初创建一个本地页面内容流变量,并使用您收到的作为参数的页面内容流进行初始化:
PDPageContentStream vcs = cs;
页面更改后,您将关闭 vcs
中的当前页面内容流,然后将 vcs
设置为新页面上的新流:
if (volY > 700) {
vcs.close();
page = createPage();
vcs = new PDPageContentStream(pd, page);
volY = 50;
}
但是在 sections
循环中的两行代码中,您在 cs
而不是 vcs
上绘制:
cs.setNonStrokingColor(URGENT);
...
cs.setNonStrokingColor(REGULAR_TEXT);
在第一个页面更改期间,您关闭了指向与 cs
相同的流的 vcs
。因此,这些颜色设置指令是在封闭流 cs
上绘制的,这会导致观察到的 NullPointerException
。
要解决此问题,请在此处也使用 vcs
而不是 cs
。
只有在您更改后才会出现这种情况的原因
drawMultipleStrings(ti.getPrompt(), volY, vcs, page, z);
至
volY += drawMultipleStrings(ti.getPrompt(), volY, vcs, page, z);
最有可能的是,在此更改之前 volY
从未增长到足以触发页面更改,但此后却发生了。
关于java - Integer 尝试修改它时抛出 NullPointer 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55545903/