iTextSharp GetInstance - NullReferenceException 错误

问题描述 投票:0回答:3

希望有人能发现问题。我将 DataGridView 保存为 PDF,并在下面的 GetInstance 代码行上收到错误。我已经验证 sfd.FileName 具有有效值并且流不为空。

System.NullReferenceException:“未将对象引用设置为对象的实例。”

using (FileStream stream = new FileStream (sfd.FileName, FileMode.Create)) {

   iTextSharp.text.Document pdfDoc =  new iTextSharp.text.Document (PageSize.A4, 10f, 20f, 20f, 10f);

   PdfWriter.GetInstance (pdfDoc, stream);   // NullReferenceException on this line.
   pdfDoc.Open();
   pdfDoc.Add (pdfTable);
   pdfDoc.Close();
   stream.Close();
}

完整方法代码:

private void SavePDF () {

    if (grdKeywordSearch.Rows.Count > 0) {
        SaveFileDialog sfd = new SaveFileDialog();
        sfd.Filter = "PDF (*.pdf)|*.pdf";
        sfd.FileName = "Output.pdf";
        bool fileError = false;
        if (sfd.ShowDialog () == DialogResult.OK) {
            if (File.Exists (sfd.FileName)) {
                try {
                    File.Delete (sfd.FileName);
                } catch (IOException ex) {
                    fileError = true;
                    MessageBox.Show ("It wasn't possible to write the data to the disk." + ex.Message);
                }
            }
            if (!fileError) {
                try {
                    PdfPTable pdfTable = new PdfPTable (grdKeywordSearch.Columns.Count);
                    pdfTable.DefaultCell.Padding = 3;
                    pdfTable.WidthPercentage = 100;
                    pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;

                    foreach (DataGridViewColumn column in grdKeywordSearch.Columns) {
                        PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText));
                        pdfTable.AddCell (cell);
                    }

                    foreach (DataGridViewRow row in grdKeywordSearch.Rows) {
                        foreach (DataGridViewCell cell in row.Cells) {
                            pdfTable.AddCell (cell.Value.ToString());
                        }
                    }

                    using (FileStream stream = new FileStream (sfd.FileName, FileMode.Create)) {

                        iTextSharp.text.Document pdfDoc =  new iTextSharp.text.Document (PageSize.A4, 10f, 20f, 20f, 10f);

                        PdfWriter.GetInstance (pdfDoc, stream);
                        pdfDoc.Open();
                        pdfDoc.Add (pdfTable);
                        pdfDoc.Close();
                        stream.Close();
                    }

                    MessageBox.Show ("Data Exported Successfully.", "Info");
                } catch (Exception ex) {
                    MessageBox.Show ("Error :" + ex.Message);
                }
            }
        }
    } else {
        MessageBox.Show ("Nothing to export.", "Info");
    }

}

通过此代码添加堆栈跟踪,错误前一行:

Console.WriteLine (new System.Diagnostics.StackTrace().ToString());

线程 0x4508 已退出,代码为 0 (0x0)。线程 0x6078 有 退出并显示代码 0 (0x0)。在 Clarity.frmClarity.SavePDF() 处 Clarity.frmClarity.btnSavePDF_Click(对象发送者,EventArgs e)位于 System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons 按钮,Int32 单击)位于 System.Windows.Forms.Control.WndProc(Message&m) at System.Windows.Forms.ButtonBase.WndProc(Message&m) at System.Windows.Forms.Button.WndProc(Message&m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg、IntPtr wparam、IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID、Int32 原因、Int32 pvLoopData)位于 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 原因,ApplicationContext 上下文)位于 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 原因,ApplicationContext 上下文)位于 Clarity.Program.Main()

c# itext nullreferenceexception
3个回答
2
投票

我找到了解决方案,尽管我不明白。您必须在调试选项下打开“Just My Code”。我这样做后错误就消失了。


1
投票

我正在使用 VS 2022。“打开“Just My Code””也对我有用。 就我而言,抱怨的是 PdfStamper:

var pdfStamper = new PdfStamper(pdfReader, new FileStream(pdfOutputFilePath, FileMode.Create));

正在创建一个空文件。另外,在断点时,由于异常,在立即窗口中,我能够将 new FileStream(pdfOutputFilePath, FileMode.Create) 分配给变量以及 .Write(byte) 和 .Close(),因此它不是“权限”问题。

此外,当我从 bin 文件夹运行 .exe 时,该应用程序运行良好,当我将类似代码放入 LinqPad 6 和 7 中时,该应用程序也运行良好。我还尝试“以管理员身份”运行 VS,但这不起作用。

似乎这里报告了同样的问题(所以我也会在那里发布答案) 对所有 PDF 和非空流使用 PdfStamper 时,iTextSharp 空引用错误


0
投票

那么为什么是-1? “Just My Code”在 VS 2022 17.9.2 中为我工作。

© www.soinside.com 2019 - 2024. All rights reserved.