问题
I have a problem with iText7 7.1.6 and Visual Studio 2019. My program has been running for a year now, but having just updated Visual Studio (Community) to version 16.6.2 from 16.6.1, I did a rebuild without changing anything. Now, when I run the program, I get a System,NullReferenceException Object reference not set to an instance of an object exception at PdfFont font = PdfFontFactory.CreateFont(iText.IO.Font.Constants.StandardFonts.HELVETICA);
The exact same solution copied to my laptop still running 16.6.1 runs just fine. Could it maybe be due to the VS version? Any idea where I can start looking?
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);
EDIT: Sorry for the late reply. Here is the stack trace. I had already tried with the newest iText7, but the same result. Line 19 is using 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#
EDIT 2: iText7 7.1.11 This complete console application runs under VS2019 16.6.1 but crashes under 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");
}
}
}
}
}
Maybe this will help?
回答1:
I fixed this by installing Microsoft.DotNet.PlatformAbstractions version 1.1.0
回答2:
I fixed it by uninstalling Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
.
Updating Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
to version 3.1.10
did not help.
来源:https://stackoverflow.com/questions/62403090/does-visual-studio-update-break-itext7