简体   繁体   English

iTextSharp一切正常,但是打开pdf文档时出现奇怪的错误

[英]iTextSharp everything works but getting strange error when opening pdf document

I am using iTextSharp to create my PDF document on the fly. 我正在使用iTextSharp快速创建我的PDF文档。 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. 但是,当我打开创建的PDF时,出现错误,提示该文档将无法正确显示,因为其中包含错误。

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. 然后,打开生成的PDF不会出错,否则我可以打开PDF并查看文档及其内容(包括该行)。 However, then i get error that document been generated with error. 但是,然后我得到该文档已生成错误的错误。

Can somebody tell me what is wrong ? 有人可以告诉我怎么了吗?

Thanks in advance. 提前致谢。 cb.Stroke(); cb.Stroke();

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM