2
votes

I am trying to edit a single-paged pdf like following:

loop until all content was used{ 1. copys the first page 2. add some content }

Right now, everything is working expect when I try to close both document and stamper I get an error that the first one was already closed.

Thing is: if document was closed I can open it but there are only copies of pages, no content was added

If stamper was closed, the document is corrupt, but when I remove the document.NewPage() it workes, but only for the first page.

I tried creating a new instance of stamper in every iteration, but I get following error:

An unhandled exception of type 'iTextSharp.text.DocumentException' occurred in itextsharp.dll

Additional information: The original document was reused. Read it again from file.

I did close the stamper in every iteration, but it didn't help.

1
A very similar question has recently been asked by a user with a similar name... - mkl
You cannot reuse PdfStamper because PdfStamper changes the underlying PdfReader while you alter the content of the existing PDF. You need to create a new PdfStamper for every new output file. This is documented. See the FAQ entry on page 185 of chapter 6 of ye old iText in Action book. (Beats me why no one ever reads the documentation.) - Bruno Lowagie

1 Answers

-1
votes

Both your New PdfReader() and New PdfStamper() should be inside the loop. Also, Close your PdfStamper inside the loop