Обновление Visual Studio нарушает работу iText7? - PullRequest
0 голосов
/ 16 июня 2020

У меня проблема с iText7 7.1.6 и Visual Studio 2019. Моя программа работает уже год, но только что обновив Visual Studio (Community) до версии 16.6.2 с версии 16.6.1, я выполнил перестройку ничего не меняя. Теперь, когда я запускаю программу, я получаю ссылку на объект 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);

enter image description here

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? введите описание изображения здесь

...