views:

251

answers:

1

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();

A: 

After playing with it for some more time (long time), I found a workaround, now the documents not only displays the content (as it displayed it before), but doesn't give an error message.

I still don't understand why and how this solution works and the one before did not, but just in case maybe somebody will need it or will have 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 places of code for drawn line with code for 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);
Dmitris