private void FillPdfForm(string originalPdfFile, string destPdfFile, PdfFormMap formMap, Dictionary <string, dynamic> dbValues) { using PdfReader reader = new PdfReader(originalPdfFile); using PdfWriter writer = new PdfWriter(destPdfFile); reader.SetUnethicalReading(true); using PdfDocument pdf = new PdfDocument(reader, writer, new StampingProperties().UseAppendMode()); var form = PdfAcroForm.GetAcroForm(pdf, false); foreach (var fieldMap in formMap.FieldMaps) { PdfFormField formField = form.GetField(fieldMap.Name); if (formField == null || formField.IsReadOnly()) { continue; } //string[] states = formField.GetAppearanceStates(); //string statesStr = string.Empty; //foreach (var state in states) //{ // statesStr += $"{state} | "; //} //if (!string.IsNullOrEmpty(statesStr)) // list.Add($"[{formField.GetType().Name}] {field} - {statesStr}"); if (!MatchCondition(fieldMap.Condition, dbValues)) { continue; } string value = (string)ResolveDynamicValue(fieldMap.Value, dbValues); if (!string.IsNullOrEmpty(value)) { if (formField is PdfButtonFormField buttonFormField) { formField = buttonFormField.SetCheckType(PdfFormField.TYPE_CHECK); if (buttonFormField.IsRadio()) { formField = buttonFormField.SetRadio(true); } } formField.SetValue(value); } } }
public ActionResult <PdfFormMap> Post([FromBody] PdfFormMap map) { if (!ModelState.IsValid) { return(BadRequest()); } try { var pdfFormMap = _pdfFormRepository.Add(map); return(Ok(pdfFormMap)); } catch (EntityAlreadyExistsException) { return(Conflict()); } }
public ActionResult Put(string pdfCode, [FromBody] PdfFormMap updatedMap) { if (string.IsNullOrEmpty(pdfCode)) { return(NotFound(pdfCode)); } if (updatedMap.Id != pdfCode) { return(BadRequest(updatedMap.Id)); } try { _pdfFormRepository.Update(updatedMap); return(Ok()); } catch (EntityNotFoundException) { return(NotFound(pdfCode)); } }