[英]Stackoverflow error in OnstartPage method
我打算根據從另一個文件讀取的值,為每個“頁面開始”使用不同的字符串。 我已將其放置在我的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的建議(如果正確實現)將防止無限循環,但仍可能導致頁面初始化的某些部分執行兩次。 這可能是無害的(將相同的變量兩次設置為相同的值似乎是無害的),但也可能會產生不良的副作用(將相同的變量增加兩次並非無害)。 特別是如果多個頁面事件偵聽器處於活動狀態,則效果可能令人不快。
因此,我建議您通過頁邊距在頁面頂部保留一些額外的空間,並將其填充在onEndPage
。
PS:另外, 在《 iText in Action —第二版》的第150頁上,有一個關於onStartPage()
使用的常見問題解答 :
常見問題解答 為什么不建議在
onStartPage()
方法中 添加內容 ? 您會從5.2.4節中記住,當當前頁面為空時,iText會忽略newPage()
調用。 當您從代碼中顯式調用此方法時,將執行該方法或將其忽略,但也會在iText中多次隱式調用此方法。 對於空白頁,將其忽略是很重要的; 否則,您最終會有很多不必要的新頁面,這些頁面會無意間留空。 如果在onStartPage()
方法中添加內容,則始終存在不必要頁面的風險。 考慮到保留onEndPage()
方法來添加內容更為安全。
您需要在對onStartPage(writer,output)
調用中onStartPage(writer,output)
一些條件(例如if
)。如果不是,則每次onStartPage
調用時都會遞歸調用。無論堆棧的最大大小如何,都會給您無限循環這會導致StackOverflow
錯誤。
而且,如果事先有一些條件,顯然您會陷入一種情況,那就是它始終如一地評估為真(或導致進行遞歸調用的任何因素)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.