0

I'm trying to use PdfContentByte and ColumnText to display notes imported from customer orders. The document and notes display correctly however, when I scroll down at all in the document, I get an error saying "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" I've looked around and found answers like: PDFs generated using itextsharp giving error at the time of first print command But the method doesn't appear to have any nesting issues:

  private void DisplayNotes(OrderVerificationData ovNotes)
    {
        _notes = ovNotes.orderNotes;
        _resultNotes = _notes.Split(_notesSeparators, StringSplitOptions.RemoveEmptyEntries);
        Phrase notesDisplay = FormatPhrase("");
        PdfContentByte dc = _instance.DirectContent;
        var ct = new ColumnText(dc);
        foreach (string t in _resultNotes)
        {
            notesDisplay.Add(t);
            notesDisplay.Add(Environment.NewLine);
        }

        dc.BeginText();
        dc.SetFontAndSize(BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false), 10f);
        dc.SetTextMatrix(50f, _verticalAlign);

        if (_verticalAlign <= 40)//new page stuff, error still occurs when commented out
        {
            dc.EndText();
            FooterCreation(ovNotes);
            _document.NewPage();
            _numberOfPages += 1;
            _verticalAlign = 690;
            dc.BeginText();
            dc.SetFontAndSize(BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false), 9f);
            dc.SetTextMatrix(50f, _verticalAlign);
        }

        ct.SetSimpleColumn(notesDisplay, 55f, 0f, 400f, _verticalAlign, 10f, Element.ALIGN_LEFT);
        ct.Go();
        dc.EndText();
    }

I don't recall column text needing to be closed, nor could I find anything that said it had to be. Is there something else that's causing this issue? Removing all usage of this method from the rest of the file causes the pdf to be displayed properly.

Community
  • 1
  • 1
  • it appears I thought that the Columns were were meant to be within dc.BeginText() and dc.EndText(), removing those fixes the error. – wschlosser May 05 '15 at 22:07

1 Answers1

2

You are creating illegal PDF syntax. A text object is created by introducing the BT (begin text) and ET (end text) operator. There are different ways to introduce these operators using iTextSharp.

One way is by adding these operators using low-level syntax:

dc.BeginText();
dc.SetFontAndSize(BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false), 10f);
dc.SetTextMatrix(50f, _verticalAlign);
dc.ShowText("Some text");
dc.EndText();

The other way is by using ColumnText, a class that hides all the low-level operations:

var ct = new ColumnText(dc);
ct.SetSimpleColumn(notesDisplay, 55f, 0f, 400f, _verticalAlign, 10f, Element.ALIGN_LEFT);
ct.Go();

For reasons unknown, you are mixing both approaches, creating code that is very hard to understand, maintain, debug. Please remove all the lines that use the dc variable, except for the line where dc is used as a parameter to create the ColumnText object.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165