【发布时间】:2020-06-16 07:25:13
【问题描述】:
我在使用 iText7 7.1.6 和 Visual Studio 2019 时遇到问题。我的程序已经运行了一年,但刚刚将 Visual Studio(社区)从 16.6.1 更新到版本 16.6.2,我进行了重建没有改变任何东西。现在,当我运行程序时,我得到一个 System,NullReferenceException 对象引用未设置为PdfFont font = PdfFontFactory.CreateFont(iText.IO.Font.Constants.StandardFonts.HELVETICA); 处的对象异常的实例。复制到我的笔记本电脑仍然运行 16.6.1 的完全相同的解决方案运行得很好。可能是由于VS版本?知道我可以从哪里开始寻找吗?
using (PdfDocument pdf = new PdfDocument(new PdfWriter(saveFileDialog1.FileName, new WriterProperties().AddXmpMetadata().SetPdfVersion(PdfVersion.PDF_2_0))))
{
PdfDocumentInfo info = pdf.GetDocumentInfo();
info.SetTitle("Old Info");
info.SetAuthor("G.F. Whitmarsh");
using (Document document = new Document(pdf, PageSize.A4, false)) // Don't flush immediately
{
try
{
document.SetTopMargin(120f);
document.SetBottomMargin(50f);
document.SetLeftMargin(50f);
MyEventHandler meh = new MyEventHandler();
pdf.AddEventHandler(PdfDocumentEvent.END_PAGE, meh);
meh.Boat = s;
meh.ToWeb = ToWeb.Text;
meh.DateToInternet = DateToInternet.Text;
PdfFont font = PdfFontFactory.CreateFont(iText.IO.Font.Constants.StandardFonts.HELVETICA); //>>> Here is the exception
PdfFont bold = PdfFontFactory.CreateFont(iText.IO.Font.Constants.StandardFonts.HELVETICA_BOLD);
编辑:抱歉回复晚了。这是堆栈跟踪。我已经尝试过使用最新的 iText7,但结果相同。第 19 行正在使用 system.IO; .NET 4.6
itext.io.dll!iText.IO.Font.FontCache.FontCache() Unknown
[Native to Managed Transition]
[Managed to Native Transition]
itext.io.dll!iText.IO.Font.FontProgramFactory.CreateFont(string name, byte[] fontProgram, bool cached) Unknown
itext.kernel.dll!iText.Kernel.Font.PdfFontFactory.CreateFont(string fontProgram) Unknown
> InfoOld.exe!InfoOld.Form1.PdfButton_Click(object sender, System.EventArgs e) Line 212 C#
System.Windows.Forms.dll!System.Windows.Forms.Control.OnClick(System.EventArgs e) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Button.OnClick(System.EventArgs e) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Button.OnMouseUp(System.Windows.Forms.MouseEventArgs mevent) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.WmMouseUp(ref System.Windows.Forms.Message m, System.Windows.Forms.MouseButtons button, int clicks) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.ButtonBase.WndProc(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Button.WndProc(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr hWnd, int msg, System.IntPtr wparam, System.IntPtr lparam) Unknown
[Native to Managed Transition]
[Managed to Native Transition]
System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(System.IntPtr dwComponentID, int reason, int pvLoopData) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int reason, System.Windows.Forms.ApplicationContext context) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int reason, System.Windows.Forms.ApplicationContext context) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Application.Run(System.Windows.Forms.Form mainForm) Unknown
InfoOld.exe!InfoOld.Program.Main() Line 19 C#
编辑 2:iText7 7.1.11 这个完整的控制台应用程序在 VS2019 16.6.1 下运行,但在 VS2019 16.6.2 下崩溃
using iText.Kernel.Font;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
namespace TestItext7
{
class Program
{
static void Main()
{
using (iText.Kernel.Pdf.PdfDocument pdf = new PdfDocument(new PdfWriter(@"c:\temp\tester.pdf", new WriterProperties().AddXmpMetadata().SetPdfVersion(PdfVersion.PDF_2_0))))
{
using (Document document = new Document(pdf, PageSize.A4, false)) // Don't flush immediately
{
document.SetTopMargin(120f);
document.SetBottomMargin(50f);
document.SetLeftMargin(50f);
PdfFont font = PdfFontFactory.CreateFont(iText.IO.Font.Constants.StandardFonts.HELVETICA);
PdfFont bold = PdfFontFactory.CreateFont(iText.IO.Font.Constants.StandardFonts.HELVETICA_BOLD);
document.Add(new Paragraph("Test Header").SetFont(bold).SetFontSize(10).SetFixedLeading(12));
document.Add(new Paragraph("Test Contents").SetFont(font).SetFontSize(10).SetFixedLeading(12));
System.Diagnostics.Process.Start(@"c:\temp\tester.pdf");
}
}
}
}
}
【问题讨论】:
-
嗨,请附上完整的堆栈跟踪。也请尝试使用最近的 iText 7.1.11 版本
-
请看我的新编辑,谢谢。
-
在创建 PDF 文件的
PdfDocument实例关闭之前,您在外部查看器中打开该 PDF 文件。这是一个错误。由于时间问题(例如,如果使用查看器启动进程比关闭程序中的PdfDocument实例花费的时间更长,但它可能看起来工作正常,但这种时间可能会随着时间而改变。 -
嗨,我在使用 Visual Studio 2019 16.6.2 和 iText7 7.1.13 时遇到了同样的问题。
-
嗨,我在使用 Visual Studio 2019 16.7.6 和 iText7 时遇到了同样的问题