提取一个页面的结果,我想测试从PDF文档中的单个页面的提取,但我发现了一个NullReferenceException
每当我尝试。中的NullReferenceException
var document = new Document();
var stream = new MemoryStream();
var writer = PdfWriter.GetInstance(document, stream);
document.Open();
document.Add(new Paragraph("This is page 1."));
document.NewPage();
document.Add(new Paragraph("This is page 2."));
document.Close();
var copystream = new MemoryStream();
var copy = new PdfCopy(document, copystream);
copy.Open();
var reader = new PdfReader(stream.ToArray());
var page = copy.GetImportedPage(reader, 2);
copy.AddPage(page);
copy.Close(); // code throws exception here
我已经尝试添加writer.CloseStream = false
,但我还是落得同样的NullReferenceException
:
Object reference not set to an instance of an object.
at iTextSharp.text.Document.get_Left()
at iTextSharp.text.pdf.PdfDocument.SetNewPageSizeAndMargins()
at iTextSharp.text.pdf.PdfDocument.NewPage()
at iTextSharp.text.pdf.PdfDocument.Close()
at iTextSharp.text.pdf.PdfCopy.Close()
at iTextTest.Controllers.HomeController.Index() in line 41
刚刚添加,我的不好。的 –
可能重复[什么是一个NullReferenceException,如何解决呢?(http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Servy
那么这似乎是iTextSharp中的一个错误。他们可能希望在那里添加空处理并抛出适当的异常,如“无边距设置”或任何根本原因。 [浏览来源:'Left'属性不'返回pageSize.GetLeft(marginLeft);'](http://sourceforge.net/p/itextsharp/code/HEAD/tree/trunk/src/core/iTextSharp/text /Document.cs),其中'pageSize'大概是'null'。 – CodeCaster