c# - After filling PDF with iTextSharp, form is not savable/ usage rights removed -
i working on process use itextsharp partially fill pdf form. form emailed user , user finish completing form. form submitted email our email account , processed.
my problem once pdf sent out user, when user opens form , data can not saved. original pdf used template can filled out saved. somehow, during process of using itextsharp, form losing usage rights. there way retain usage rights while using itextsharp?
i hoping here can point out going wrong, or point me in right direction. time , help.
below code:
using dapper; using itextsharp.text.pdf; using nlog; using pilotdispatch.domain.model; using system; using system.collections; using system.collections.generic; using system.configuration; using system.data.oledb; using system.io; using system.linq; namespace eticketpdffactory { class program { const string templateitextpdfpath = @"c:\pdf\factory\eticketformportrait.pdf"; const string outputitextpdfpath = @"c:\pdf\factory\eticketformportraitout.pdf"; private static readonly logger logger = logmanager.getcurrentclasslogger(); static void main(string[] args) { var logid = string.empty; if (args.length > 0) { logid = args[0]; } else { logger.error("no logid supplied"); return; } var rundownquery = string.format( "select * rundown_table left join vessels on vessels.callsign = rundown_table.call_sign rundown_table.log_id = '{0}'", logid); eticketpdf ticket; ienumerable<pilottransportation> pilottransportations = null; using (var cn = new oledbconnection(configurationmanager.connectionstrings["pervasiveconnection"].connectionstring)) { cn.open(); // ticket = cn.query<eticketpdf>(pervasivequerystring, new { log_id = logid }).firstordefault(); // not working pervasive ticket = cn.query<eticketpdf>(rundownquery).firstordefault(); if (ticket != null) { var transportationquery = string.format( "select * pilottransportation pilotcode = '{0}'", ticket.pilot_code); pilottransportations = cn.query<pilottransportation>(transportationquery); } cn.close(); } if (ticket == null) { logger.error("no records found given logid"); return; } var pilotoptions = pilottransportations.select(opt => string.format("{0} - {1}", opt.vendorid, opt.name)).toarray(); var reader = new pdfreader(templateitextpdfpath); var stamper = new pdfstamper(reader, new filestream(outputitextpdfpath, filemode.open)); //var stamper = new pdfstamper(reader, new filestream(outputitextpdfpath, filemode.createnew, fileaccess.write), '\0', true); var formfields = stamper.acrofields; formfields.setlistoption("form[0].page1[0].vendoridfrom1[0]", null, pilotoptions); formfields.setlistoption("form[0].page1[0].vendoridto1[0]", null, pilotoptions); formfields.setlistoption("form[0].page1[0].vendoridfrom2[0]", null, pilotoptions); formfields.setlistoption("form[0].page1[0].vendoridto2[0]", null, pilotoptions); var properties = ticket.gettype().getproperties(); foreach (var prop in properties) { var name = prop.name; var propval = prop.getvalue(ticket, null); if (propval != null) { if (name == "order_date") { if(convert.todatetime(propval).year < 1902)continue; } formfields.setfield(name, propval.tostring()); } } reader.removeusagerights(); stamper.close(); reader.close(); file.copy(outputitextpdfpath, path.getdirectoryname(outputitextpdfpath) + "/" + logid + ".pdf"); console.writeline("finished"); console.readline(); } } }
once again, in advance pointers or can offer me.
the answer commented out in own source code.
you need:
var stamper = new pdfstamper(reader, new filestream(...), '\0', true);
instead of:
var stamper = new pdfstamper(reader, new filestream(...));
your question duplicate of how correctly fill in xfa form data using itextsharp allow editing , saving result in acrobat xi , can find more elaborate answer here.
also: complain usage rights removed, if so, why see line in code:
reader.removeusagerights();
that line removes usage rights , therfore form can no longer saved locally when using adobe reader.
Comments
Post a Comment