gpt4 book ai didi

java - OnstartPage 方法中的 Stackoverflow 错误

转载 作者:行者123 更新时间:2023-11-30 06:24:53 25 4
gpt4 key购买 nike

我打算根据从另一个文件读取的值为每个页首设置不同的字符串。我已将它放在我的 onStartPage 方法中,如下所示:

@Override
public void onStartPage(PdfWriter writer, Document output) {
try {
File finish = new File("C:/Statements final/");
File[] finf = finish.listFiles();
Font f1 = new Font(Font.NORMAL, 12);
f1.setColor(Color.BLACK);
String firstline = "";
for (int k = 0; k < filenames1.length; k++) {
FileInputStream fs = new FileInputStream("C:/Statements final/" + filenames1[k]);
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
for (int i = 0; i < 0; i++) {
br.readLine();
}
firstline = br.readLine();

System.out.println(firstline);

output.add(new Paragraph(new Phrase(new Chunk(firstline, f1))));
}
} catch (Exception ex) {
System.out.println(ex);
}
}

我收到这个 StackOverflow 错误:

Exception in thread "main" java.lang.StackOverflowError
at java.lang.String.toLowerCase(String.java:2524)
at com.lowagie.text.pdf.PdfEncodings.convertToBytes(PdfEncodings.java:149)
at com.lowagie.text.pdf.BaseFont.convertToBytes(BaseFont.java:795)
at com.lowagie.text.pdf.FontDetails.convertToBytes(FontDetails.java:160)
at com.lowagie.text.pdf.PdfContentByte.showText2(PdfContentByte.java:1386)
at com.lowagie.text.pdf.PdfContentByte.showText(PdfContentByte.java:1396)
at com.lowagie.text.pdf.PdfDocument.writeLineToContent(PdfDocument.java:1587)
at com.lowagie.text.pdf.ColumnText.go(ColumnText.java:841)
at com.lowagie.text.pdf.ColumnText.go(ColumnText.java:752)
at com.lowagie.text.pdf.PdfPRow.writeCells(PdfPRow.java:513)
at com.lowagie.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:511)
at com.lowagie.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:587)
at com.lowagie.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:543)
at com.lowagie.text.pdf.PdfDocument.newPage(PdfDocument.java:830)
at com.lowagie.text.pdf.PdfDocument.carriageReturn(PdfDocument.java:1192)
at com.lowagie.text.pdf.PdfDocument.add(PdfDocument.java:482)
at estatement.Border.onStartPage(Border.java:112)

任何知道我应该做什么的人。

最佳答案

onStartPage 如果您尝试更改其中的文档内容,则非常脆弱,请参见。 PdfPageEvent.onStartPage 的 JavaDoc 注释关于无限循环的警告:

/**
* Called when a page is initialized.
* <P>
* Note that if even if a page is not written this method is still
* called. It is preferable to use <CODE>onEndPage</CODE> to avoid
* infinite loops.
*
* @param writer the <CODE>PdfWriter</CODE> for this document
* @param document the document
*/
public void onStartPage(PdfWriter writer, Document document);

原因是 onStartPage 在页面初始化期间被调用,但是添加文档需要页面初始化已经完成。

因此,@VigneshVino 的提议(如果实现得当)将防止无限循环,但仍可能导致页面初始化的某些部分被执行两次。这可能是无害的(将相同的变量设置为相同的值两次似乎无害),但它也可能会产生不良副作用(将相同的变量递增两次并非无害)。尤其是当多个页面事件监听器处于 Activity 状态时,效果可能会令人恼火。

因此,我建议您通过页边距在页面顶部留出一些额外空间,并改为在 onEndPage 中填充该空间。

PS:此外,在 iText in Action — 2nd Edition 的第 150 页上有一个关于使用 onStartPage()FAQ :

FAQ Why is it not advised to add content in the onStartPage() method? You'll remember from section 5.2.4 that iText ignores newPage() calls when the current page is empty. This method is executed — or ignored — when you call it explicitly from your code, but it's also invoked implicitly from within iText on multiple occasions. It's important that it's ignored for empty pages; otherwise you'd end up with plenty of unwanted new pages that are unintentionally left blank. If you add content in an onStartPage() method, there's always a risk of having unwanted pages. Consider it more safe to reserve the onEndPage() method for adding content.

关于java - OnstartPage 方法中的 Stackoverflow 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16438048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com