asp.net - How to update a PDF without creating a new PDF? -
i required replace word in existing pdf acrofield word. using pdfstamper of itextsharp same , working fine. but, in doing required create new pdf , change reflected in existing pdf itself. if setting destination filename same original filename no change being reflected.i new itextsharp , there doing wrong? please help.. providing piece of code using
private void listfieldnames(string s) { try { string pdftemplate = @"z:\temp\pdf\passportapplicationform_main_english_v1.0.pdf"; string newfile = @"z:\temp\pdf\passportapplicationform_main_english_v1.0.pdf"; pdfreader pdfreader = new pdfreader(pdftemplate); (int page = 1; page <= pdfreader.numberofpages; page++) { pdfreader reader = new pdfreader((string)pdftemplate); using (pdfstamper stamper = new pdfstamper(reader, new filestream(newfile, filemode.create, fileaccess.readwrite))) { acrofields form = stamper.acrofields; var fieldkeys = form.fields.keys; foreach (string fieldkey in fieldkeys) { //replace address form field custom data if (fieldkey.contains("address")) { form.setfield(fieldkey, s); } } stamper.formflattening = true; stamper.close(); } } }
as documented in book itext in action, can't read file , write simultaneously. think of how word works: can't open word document , write directly it. word creates temporary file, writes changes it, replaces original file , throws away temporary file.
you can too:
- read original file
pdfreader
, - create temporary file
pdfstamper
, , when you're done, - replace original file temporary file.
or:
- read original file
byte[]
, - create
pdfreader
byte[]
, and - use path original file
pdfstamper
.
this second option more dangerous, you'll lose original file if causes exception in pdfstamper
.
Comments
Post a Comment