【问题标题】:iTextSharp everything works but getting strange error when opening pdf documentiTextSharp 一切正常,但打开 pdf 文档时出现奇怪的错误
【发布时间】:2010-04-29 16:16:37
【问题描述】:

我正在使用 iTextSharp 即时创建我的 PDF 文档。 一切正常,我在代码中没有错误;但是,当我打开创建的 PDF 时,它给我一个错误,说该文档将无法正确显示,因为它包含错误。

下面是给我一个问题的代码:

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

如果我删除以下行:

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

然后我打开生成的 PDF 没有错误,否则我可以打开 PDF 并查看文档及其内容,包括该行。但是,然后我收到错误,即生成的文档有错误。

谁能告诉我怎么了?

提前致谢。 cb.Stroke();

【问题讨论】:

    标签: c# .net asp.net itextsharp


    【解决方案1】:

    在玩了一段时间(很长时间)之后,我找到了一个解决方法,现在文档不仅显示内容(如之前显示的那样),而且没有给出错误消息。

    我仍然不明白这个解决方案为什么以及如何工作,而之前的那个没有,但以防万一有人需要它或遇到类似问题。

    而不是这个:

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

    我用字体代码更改了绘制线条的代码位置。

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

    【讨论】:

    • “我仍然不明白这个解决方案为什么以及如何工作” - 这很简单:BeginText()...EndText() 包含一个 文本块 其中只允许文本绘制相关的和一些通用指令,但绝对不允许路径绘制指令。话虽如此,您的代码中仍然存在错误:一旦您开始创建路径 (MoveTo(...)),在该路径绘制一条之前,只允许进一步创建路径和最终绘制路径指令。因此,您的SetLineWidth 必须移到MoveTo 之前。您当前的代码仅适用于 PDF 查看器松懈。
    猜你喜欢
    • 2016-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-18
    • 2020-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多