Legacy notice!

iText 5 is the previous major version of iText's leading PDF SDK. iText 5 has been EOL, and is no longer developed. Switch your project to iText 7, integrating the latest developments.
Check related iText 7 content!

I need to change the value of a field in an existing PDF file.

I am using PdfReader, PdfStamper and AcroFields and that's working fine. But, in doing so, it is required to create a new PDF and I would like the change to be reflected in the existing PDF itself. If I am setting the destination filename to be the same as the original filename, then my application fails.

Posted on StackOverflow on Apr 18, 2013 by tk2013

You can't read a file and write to it simultaneously. Think of how Microsoft Word works: you can't open a Word document and write directly to it. Word always creates a temporary file, writes the changes to it, then replaces the original file with it, and then throws away the temporary file.

You can do that too:

  • read the original file with PdfReader;

  • create a temporary file for PdfStamper, and when you're done,

  • replace the original file with the temporary file.


  • read the original file into a byte[],

  • create PdfReader with this byte[], and

  • use the path to the original file for PdfStamper.

The latter option is more dangerous, as you'll lose the original file if you do something that causes an exception in PdfStamper. If I were you, I'd create a temporary file.