2

我正在使用 itextsharp 并尝试使用

PdfAction action = PdfAction.GotoLocalPage(1, pdfDest, writer);

这是一个静态方法,但我总是得到这个错误

Server Error in '/' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 178: 
Line 179:            //create a new action to send the document to our new destination.
Line 180:            PdfAction action = PdfAction.GotoLocalPage(1, pdfDest, writer);
Line 181:
Line 182:            //set the open action for our writer object


Source File: VoucherService.cs    Line: 180

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
   iTextSharp.text.pdf.PdfWriter.GetPageReference(Int32 page) +326
   iTextSharp.text.pdf.PdfAction.GotoLocalPage(Int32 page, PdfDestination dest, PdfWriter writer) +49
   OnlineStudentPlanner.Framework.Services.VoucherService.SetupPdfDoc(String invoiceNumber, String logoPath, Document doc, MemoryStream memoryStream, PdfWriter writer, PdfPTable& table) in VoucherService.cs:180
   OnlineStudentPlanner.Framework.Services.VoucherService.GenerateVouchers(Int32 qty, Int32 voucherSize, String invoiceNumber, String logoPath, Int32 siteWideQty, IEnumerable`1 validDomains, Boolean siteWideVoucher) in VoucherService.cs:55
   OnlineStudentPlanner.WebUI.Areas.Admin.Controllers.HomeController.GenerateVouchers(GenerateVouchersVm vm) in Admin\Controllers\HomeController.cs:52
   lambda_method(Closure , ControllerBase , Object[] ) +163
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +208
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
   System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +55
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +263
   System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +191
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
   System.Web.Mvc.Controller.ExecuteCore() +116
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50
   System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8970061
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184

这是我的代码

Document doc = new Document();
MemoryStream memoryStream = new MemoryStream();
PdfPTable table;

doc.Open();

PdfWriter writer = PdfWriter.GetInstance(doc, memoryStream);
writer.CloseStream = false;

SetupPdfDoc(invoiceNumber, logoPath, doc, memoryStream, writer, out table);

private  void SetupPdfDoc(string invoiceNumber, string logoPath, Document doc,  MemoryStream memoryStream, PdfWriter writer, out PdfPTable table)
{
    PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, doc.PageSize.Height, 0.75f);
    doc.AddTitle(String.Format("Vouchers-{0}", invoiceNumber));
}
4

2 回答 2

3

这是因为您试图在没有任何页面的空 PDF 文档上进行操作。

更改以下三行:

        doc.Open();

        PdfWriter writer = PdfWriter.GetInstance(doc, memoryStream);
        writer.CloseStream = false;

到:

        PdfWriter writer = PdfWriter.GetInstance(doc, memoryStream);
        writer.CloseStream = false;
        doc.Open();
        doc.NewPage();

应该可以解决您的问题。

于 2012-08-05T19:42:42.880 回答
0

您是否检查过 pdfDest 和 writer 是否不为空?它在以下行引发异常:

 iTextSharp.text.pdf.PdfWriter.GetPageReference(Int32 page) +326

我会假设 writer 不为 null,因此 pdfDest 为 null 或 iTextSharp 中存在错误,并且传递错误的页面会导致异常(错误的异常,应该是 OutOfBounds 或其他东西)。

于 2012-08-05T19:45:30.303 回答