I'm trying to test extraction of a single page from a PDF document, but I'm getting a NullReferenceException
whenever I try.
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
I've tried adding writer.CloseStream = false
, but I still end up with the same 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