iTextSharp everything works but getting strange error when opening pdf document

Posted by Dmitris on Stack Overflow See other posts from Stack Overflow or by Dmitris
Published on 2010-04-29T16:16:37Z Indexed on 2010/05/04 18:38 UTC
Read the original article Hit count: 388

Filed under:
|
|
|

Hello everybody,

I am using iTextSharp to create my PDF document on the fly. Everything works fine, and i get no errors in the code; however, when i open created PDF it gives me error saying that document will be not displayed properly because it contain errors.

Here is the code bellow that gives me a 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 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 am getting no error opening generated PDF, otherwise i can open PDF and see the document and it's content including the line. However, then i get error that document been generated with error.

Can somebody tell me what is wrong ?

Thanks in advance. cb.Stroke();

© Stack Overflow or respective owner

Related posts about itextsharp

Related posts about c#