OnstartPage方法中的Stackoverflow错误 [英] Stackoverflow error in OnstartPage method

查看:183
本文介绍了OnstartPage方法中的Stackoverflow错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算根据从另一个文件中读取的值,为每个页面开始"使用不同的字符串.我已将其放置在我的onStartPage方法中,如下所示:

I intend to have a different String for every Start of Page depending on values I read from another File. I have Placed this in my onStartPage Method Like this:

@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错误:

I am getting this StackOverflow error:

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)

知道我该怎么做的任何人.

Anyone who knows what I should Do.

推荐答案

onStartPage非常脆弱. PdfPageEvent.onStartPage 关于无限循环的警告:

/**
 * 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在页面初始化期间被称为 ,但是对文档的添加要求页面初始化已经完成.

The reason is that onStartPage is called during page initialization but additions to the document require page initialization to already have finished.

@VigneshVino的建议(如果正确实现)将防止无限循环,但仍可能导致页面初始化的某些部分执行两次.这可能是无害的(将相同的变量两次设置为相同的值似乎无害),但也可能会产生不良的副作用(将相同的变量增加两次并非无害).特别是如果多个页面事件侦听器处于活动状态,则效果可能会令人不快.

@VigneshVino's proposal (if properly implemented), therefore, will prevent the infinite loop but might still cause some parts of page initialization to be executed twice. This might be harmless (setting the same variables to the same values twice seems harmless) but it also might have undesired side effects (incrementing the same variable twice is not harmless). Especially if multiple page event listeners are active, the effects might be irritating.

因此,我建议您通过页边距在页面顶部保留一些额外的空间,然后在onEndPage中填充该空间.

Thus, I would propose you leave some additional space at the top of the pages by means of margins and fill that space in an onEndPage instead.

PS::此外,

常见问题解答 为什么不建议在 onStartPage() 方法中添加内容?从5.2.4节中您会记得当前页面为空时,iText会忽略newPage()调用.当您从代码中显式调用此方法时,将执行该方法或将其忽略,但也会在iText中多次隐式调用此方法.对于空白页,应将其忽略,这一点很重要;否则,您最终会有很多不必要的新页面,这些页面会无意间留空.如果您使用onStartPage()方法添加内容,则始终存在不必要页面的风险.考虑保留onEndPage()方法添加内容更安全.

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.

这篇关于OnstartPage方法中的Stackoverflow错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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