Visual Studio 更新是否会破坏 iText7?

Ger*_*rry 5 itext7 visual-studio-2019

我在使用 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);
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

编辑:抱歉回复晚了。这是堆栈跟踪。我已经尝试过最新的 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#
Run Code Online (Sandbox Code Playgroud)

编辑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");
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

也许这会有帮助? 在此输入图像描述

itj*_*kie 1

我通过安装 Microsoft.DotNet.PlatformAbstractions 版本 1.1.0 修复了此问题