gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 16:54:20 25 4
gpt4 key购买 nike

我正在使用 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();

最佳答案

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

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

取而代之的是:

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

关于c# - iTextSharp 一切正常,但在打开 pdf 文档时出现奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2738922/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com