c# - Flattening PDF without /AcroForm fields with /Annot Widget -
i'm struggling flattening using itextsharp 5.5.10. code represent variety of efforts flatten pdf following code found on , google searches.
void process() { pdfreader reader = new pdfreader(minfilename); // 1 type of fields acrofields formfields = reader.acrofields; int countacrofields = formfields.fields.count; // 2 type of fields pracroform form = reader.acroform; list<pracroform.fieldinformation> fieldlist = form.fields; // 3 think same 2 pdfdictionary acroform = reader.catalog.getasdict(pdfname.acroform); pdfarray acrofields = (pdfarray)pdfreader.getpdfobject(acroform.get(pdfname.fields), acroform); // 4 type of fields xfaform xfaform = formfields.xfa; bool flatten = false; if (countacrofields > 0) { //no fields found } else if (fieldlist.count > 0) { //no fields found } else if (xfaform.xfapresent == true) { //no xfaforms found readxfa(reader); } else { // yes, there annotations , code extracts them not flatten them pdfdictionary page = reader.getpagen(1); pdfarray annotsarray = page.getasarray(pdfname.annots); if ( annotsarray == null) return; else { list<string> namedfieldtoflatten = new list<string>(); foreach (var item in annotsarray) { var annot = (pdfdictionary)pdfreader.getpdfobject(item); var type = pdfreader.getpdfobject(annot.get(pdfname.type)).tostring(); //expecting /annot var subtype = pdfreader.getpdfobject(annot.get(pdfname.subtype)).tostring(); //expecting /widget var fieldtype = pdfreader.getpdfobject(annot.get(pdfname.ft)).tostring(); //expecting /tx if (annot.get(pdfname.type).equals(pdfname.annot) && annot.get(pdfname.subtype).equals(pdfname.widget)) { if (annot.get(pdfname.ft).equals(pdfname.tx)) { flatten = true; var textlabel = pdfreader.getpdfobject(annot.get(pdfname.t)).tostring(); //name of textbox field namedfieldtoflatten.add(textlabel); var fieldvalue = pdfreader.getpdfobject(annot.get(pdfname.v)).tostring(); //value of textbox console.writeline($"found label={textlabel} value={fieldvalue}"); } } } if (flatten == true) { // flatten pdf [11/9/2016 15:10:06] string foldername = path.getdirectoryname(minfilename); string basename = path.getfilenamewithoutextension(minfilename); string outname = $"{foldername}\\{basename}_flat.pdf"; using (var fstream = new filestream(outname, filemode.create)) { //this totally removes fields instead of flattening them var stamper = new pdfstamper(reader, fstream) { formflattening = true, freetextflattening = true, annotationflattening = true }; var stamperform = stamper.acrofields; stamperform.generateappearances = true; foreach (var item in namedfieldtoflatten) { stamper.partialformflattening(item); } stamper.close(); reader.close(); } } } } }
any tips on how turn "fields" text flatten in acrobat?
this pdf i'm trying flatten.
this output of code.
the problem form broken. indeed has /annots
, , annotations widget annotations have entries meant entries in field dictionary, there no form in pdf. or rather: there form, /fields
array empty.
as /fields
array empty, there no fields flatten. widget annotations removed , don't see anything. not itext bug: need fix form before filling out.
in java, you'd fix form this:
pdfreader reader = new pdfreader(src); pdfdictionary root = reader.getcatalog(); pdfdictionary form = root.getasdict(pdfname.acroform); pdfarray fields = form.getasarray(pdfname.fields); pdfdictionary page; pdfarray annots; (int = 1; <= reader.getnumberofpages(); i++) { page = reader.getpagen(i); annots = page.getasarray(pdfname.annots); (int j = 0; j < annots.size(); j++) { fields.add(annots.getasindirectobject(j)); } } pdfstamper stamper = new pdfstamper(reader, new fileoutputstream(dest)); stamper.close(); reader.close();
it should easy port c#. i'm not c# developer, (untested) attempt @ making port:
pdfreader reader = new pdfreader(src); pdfdictionary root = reader.catalog; pdfdictionary form = root.getasdict(pdfname.acroform); pdfarray fields = form.getasarray(pdfname.fields); pdfdictionary page; pdfarray annots; (int = 1; <= reader.numberofpages; i++) { page = reader.getpagen(i); annots = page.getasarray(pdfname.annots); (int j = 0; j < annots.size; j++) { fields.add(annots.getasindirectobject(j)); } } pdfstamper stamper = new pdfstamper(reader, new filestream(dest, filemode.create)); stamper.close(); reader.close();
once you've fixed form this, you'll able flatten correctly.
Comments
Post a Comment