How to get and set the field format for all fields in pdf

I created an EditTextField in PDF using the iTextSharp library. I can set FieldName to EditTextField . I also want to set DataFormat for it . I will get an XML file containing only the NameName and Value field. So, when merging Value to PDF Template, I want to check the DataFormat and according to this I want to convert the value and set it.

I added EditTextField. I can add a name to the text box. I want to add a format (like DateTime format) to a TextField. So next time I can extract all the text fields from the PDF and check the format and according to the format, I can programmatically set this data.

enter image description here

TextField _DOBtext = new TextField(pdfStamper.Writer,
    new Rectangle(40, 670, 110, 650), "patient-dob");
_DOBtext.SetFormat("DateFormat", "mm/dd/yyyy"); // we want to set format like this.
pdfStamper.AddAnnotation(_DOBtext.GetTextField(), 1);
pdfStamper.Close();

And when processing the same PDF file to fill in the data, we first check the name AcrofieldName, and the logic is written below (its non-working code)

var GetField = pdfStamper.Acrofields.Field.where(u=>u.Key == "patient-dob").FirstOrDefault();
var Format = GetField.GetFormat(); // we want like this feature
if (Format != null) {
    if (Format.Type = "DateTime") {
        value=string.Format(data, Format.FormatString));
        stamper.AcroFields.SetField(fieldId, value); //fieldId = "patient-dob"
    }
}

Please help me install and get DataFormat

(ediit: add iTextSharp tag)

+4
source share
3 answers

The reason iText does not support something like SetFormat()and GetFormat()is because the PDF format does not support it.

, Adobe Acrobat . , , - JavaScript, . PDF- ( Chrome, Firefox IE) , . JS , , - AFDate_FormatEx(). , PDF :

enter image description here

, . , JS, , . , Adobe, . - :

var aditionalActions = reader.AcroFields.Fields["patient-dob"].GetWidget(0).GetAsDict(PdfName.AA);

SetAdditionalActions() PdfFormField.

, , , .

, - , , . , , - /TM, , ( , FDF HTTP POST).

var _DOBtext = new TextField(writer, new iTextSharp.text.Rectangle(0, 0, 110, 650), "patient-dob");
var tf = _DOBtext.GetTextField();
tf.MappingName = "patient-dob:date:mm/dd/yyyy";

:

var mappingName = reader.AcroFields.Fields["patient-dob"].GetWidget(0).GetAsString(PdfName.TM);

, , . , . PDF .

enter image description here

+3

- PDF AcroForm fillup.. :

    private string fill_form(string output_file)
    {
        using (PdfReader _pdfReader = new PdfReader(FormPath))
        {
            using (PdfStamper _pdfStamper = new PdfStamper(_pdfReader, new FileStream(output_file, FileMode.Create)))
            {
                _pdfStamper.AcroFields.GenerateAppearances = true;

                foreach (var _field in _pdfStamper.AcroFields.Fields)
                    foreach (TemplateField _spField in _lstFields)
                    {
                        if (_field.Key.Equals(_spField.Name))
                        {
                            switch (_spField.Type )
                            {
                                case TemplateFieldType.Text:
                                    _pdfStamper.AcroFields.SetField(_field.Key, _spField.Value);
                                    break;

                                case TemplateFieldType.Checkbox:
                                    //TODO: check Value field cannot be set != OnValue, Offvalue
                                    if (_spField.Value == _spField.OnValue)
                                        _pdfStamper.AcroFields.SetField(_field.Key, _spField.OnValue);
                                    else
                                        _pdfStamper.AcroFields.SetField(_field.Key, _spField.OffValue);
                                    break;
                            }
                        }
                    }

                _pdfStamper.FormFlattening = true;
            }
        }

        return output_file;
    }

, - . , .

+1

This worked for me (using iText7)

textBox.SetAdditionalAction(PdfName.F, iText.Kernel.Pdf.Action.PdfAction.CreateJavaScript("AFDate_FormatEx(\"yyyy-mm-dd\");"));
textBox.SetAdditionalAction(PdfName.K, iText.Kernel.Pdf.Action.PdfAction.CreateJavaScript("AFDate_KeystrokeEx(\"yyyy-mm-dd\");"));

thanks to the iText RUPS tool.

0
source

All Articles