【问题标题】:Does Visual Studio update break iText7?Visual Studio 更新是否会破坏 iText7?
【发布时间】: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 时遇到了同样的问题

标签: visual-studio-2019 itext7


【解决方案1】:

我通过安装 Microsoft.DotNet.PlatformAbstractions 1.1.0 版解决了这个问题

【讨论】:

  • 试过这个,但它不会安装。我只是将 itext.io.dll 从异常中排除,没有任何不利影响。
  • 尝试安装此软件包但没有帮助。所以切换到 VSCode 进行调试和工作!
【解决方案2】:

我通过卸载Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation 修复了它。 将 Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation 更新为 version 3.1.10 没有帮助。

【讨论】:

    【解决方案3】:

    如果您在非调试模式下运行应用程序,则不会抛出异常。忽略此异常 => 工具 > 选项 > 调试 > 常规 > 启用仅我的代码复选框

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-19
      • 1970-01-01
      • 1970-01-01
      • 2023-02-04
      • 2017-04-12
      • 2015-12-04
      • 2013-11-12
      • 2016-11-17
      相关资源
      最近更新 更多