2

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);
        }
    }
Kara
  • 6,115
  • 16
  • 50
  • 57
John Buckwell
  • 111
  • 2
  • 9

1 Answers1

2

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.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Many thanks for that. It certainly fixed the problem in my test method. Then I removed all the cb.BeginText() and cb.EndText() from my real method but the problem persists. I hesitate to post the whole method - it contains 300 lines of code. Is there any way to diagnose this problem? – John Buckwell Jan 10 '13 at 21:18
  • If you have Acrobat, you can use Preflight to do a syntax check on your PDF. You'll get a list of all the errors introduced in the content stream. – Bruno Lowagie Jan 11 '13 at 07:59
  • I obtained copy of Acrobat but Preflight provided no clues. Eventually I discovered by trial and error that you cannot mix PdfContentByte.ShowTextAligned with ColumnText(PdfContentByte) methods. Everything is now working fine. – John Buckwell Feb 08 '13 at 04:17
  • @JohnBuckwell, why I cannot mix `ShowTextAligned` with `ColumnText`? can you provide documentation about that? – tetri Aug 30 '16 at 11:33
  • 1
    @tetri I am the original developer of iText, so I hope you don't question my authority on the topic. If you use `ColumnText.showTextAligned()`, that method create a text object in your PDF. That text object starts with `BT` and ends with `ET`. The PDF specification forbids you to nest text objects (read ISO-32000-1 if you don't believe me), so you will get an error if you mix text graphics methods with `ColumnText.showTextAligned()`. You can mix those methods with `PdfContentByte.showTextAligned()`, but that's a different question. – Bruno Lowagie Aug 30 '16 at 11:59