ITextSharp works, but strange error occurs when opening pdf document

I am using iTextSharp to generate my PDF document on the fly. Everything works fine and I don't get any errors in the code; however, when I open the generated PDF, it gives me a message that the document will not display correctly because it contains errors.

Here is the code below that is giving me the problem:

public class pdfevents : PdfPageEventHelper
{
    public override void OnEndPage(PdfWriter writer, Document document)
    {
        base.OnEndPage(writer, document);
        PdfContentByte cb = writer.DirectContent;
        cb.BeginText();

        cb.SetTextMatrix(20, document.GetBottom(-30));
        BaseFont bf = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        cb.SetFontAndSize(bf, 10);

        //thats is the piece of code that makes problems
        //if i remove it then document displays without error
        cb.MoveTo(15F, document.GetBottom(-15));
        cb.SetLineWidth(0.5F);
        cb.LineTo(document.GetRight(0), document.GetBottom(-15));
        cb.Stroke();

        cb.ShowText(DateTime.Now.ToLongDateString());

        int n = writer.PageNumber;
        cb.SetTextMatrix(document.GetRight(20), document.GetBottom(-30));
        cb.ShowText(" - " + n + " - ");

        cb.EndText();
    }
}

      

If I remove the following lines:

//thats is the piece of code that makes problems
            //if i remove it then document displays without error
            cb.MoveTo(15F, document.GetBottom(-15));
            cb.SetLineWidth(0.5F);
            cb.LineTo(document.GetRight(0), document.GetBottom(-15));

      

Then I don't get the message to open the generated PDF file, otherwise I can open the PDF document and see its contents including the string. However, I get the error that the document was generated with an error.

Can someone tell me what is wrong?

Thanks in advance. cb.Stroke ();

+2


a source to share


1 answer


After playing with it for some more time (long time), I found a workaround, now the documents not only display the content (as shown earlier), but also do not give an error message.

I still don’t understand why and how this solution works, and one was not there before, but just in case, maybe someone will need it or will have a similar problem.

Instead of this:



PdfContentByte cb = writer.DirectContent;
        cb.BeginText();

        cb.SetTextMatrix(20, document.GetBottom(-30));
        BaseFont bf = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        cb.SetFontAndSize(bf, 10);

        //thats is the piece of code that makes problems
        //if i remove it then document displays without error
        cb.MoveTo(15F, document.GetBottom(-15));
        cb.SetLineWidth(0.5F);
        cb.LineTo(document.GetRight(0), document.GetBottom(-15));
        cb.Stroke();

      

I changed the code places for the drawn line with the code for the font.

base.OnEndPage(writer, document);
        PdfContentByte cb = writer.DirectContent;


        ////making a line
        cb.MoveTo(15F, document.GetBottom(-15));
        cb.SetLineWidth(0.5F);
        cb.LineTo(document.GetRight(-10), document.GetBottom(-15));
        cb.Stroke();

 cb.BeginText();

            cb.SetTextMatrix(20, document.GetBottom(-30));
            BaseFont bf = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252,  BaseFont.NOT_EMBEDDED);
            cb.SetFontAndSize(bf, 10);

      

0


a source







All Articles