[英]iTextSharp GetInstance - NullReferenceException error
希望有人能发现问题。 我正在将 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(Object sender, EventArgs e) 在 System.Windows.Forms.Control.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnClick(EventArgs e) 在System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 在 System.Windows.Forms.Control.WndProc(Message& m) 在 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 reason, Int32 pvLoopData) 在 System.Windows .Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at Clarity.Program.Main()
我找到了解决方案,虽然我不明白。 您必须在调试选项下打开“仅我的代码”。 我这样做后错误就消失了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.