iTextSharp ColumnText.SetSimpleColumn Addtext causes Error in Adobe Reader X 10.1.5

﹥>﹥吖頭↗ 提交于 2019-12-02 12:40:15

问题


The code below illustrates a problem I have with iTextSharp. Everything works perfectly. The pdf file is created and appears correct on the screen. When I print the pdf from Adobe Reader X, it looks exactly right but Adobe reports "An error exists on this page. Acrobat may not display the page correctly. Please contact the person who created the PDF document to correct the problem."

Unfortunately, the file has to be attached to an email and sent to clients. The error message is not a good look and I want to fix it. It happens in all versions of Reader that I have tried, including 10.1.15 installed today.

I have iTextSharp 5.3.4.0 under Windows 7 Pro SP1

    private void writeTestDoc()
    {
        string fname = "test.pdf";
        float textWidth = 500;
        float leftMgn = 60;
        float rubricTop = 720;
        float leftPad = 5;
        float topPad = 12;
        float leading = 0;
        BaseFont baseCalibri = BaseFont.CreateFont("c:/windows/fonts/calibri.ttf", BaseFont.WINANSI, true);
        Font plainFont = new Font(baseCalibri, 11, Font.NORMAL);
        Document document = new Document();
        try
        {
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(fname, FileMode.Create));
            document.Open();
            PdfContentByte cb = writer.DirectContent;
            cb.BeginText();
            ColumnText ct = new ColumnText(cb);
            float boxTop = rubricTop;
            ct.SetSimpleColumn(leftMgn + leftPad, boxTop - topPad, leftMgn + textWidth, boxTop, leading, Element.ALIGN_CENTER);
            ct.AddText(new Phrase("A test message", plainFont));
            ct.Go();
            cb.EndText();
            document.Close();
        }
        catch (Exception ex)
        {
            writeFile("ERROR in writeTestDoc " + ex.Message);
        }
    }

回答1:


Remove cb.BeginText(); and cb.EndText();. It's illegal for BT/ET text objects to be nested. Report the place where you've found the documentation that told you to use BeginText()/EndText in combination with ColumnText, so that we can ask the author to correct it from his or her documentation.



来源:https://stackoverflow.com/questions/14246221/itextsharp-columntext-setsimplecolumn-addtext-causes-error-in-adobe-reader-x-10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!