I have a form field which is of type date and has an associated date format string.
I have noticed that the field type is TextBoxField and not DateField? Should it not be date field?
Is there any way to get access to this date format string?
Attached is an example PDF file.
form1.pdf (70.9 KB)
@douglasdallas
you can use com.aspose.pdf.PdfAction.getECMAScriptString() that appeared from 23.12
Thanks for the quick reply.
It seems there are no actions available on the field? If you try with the PDF I attached - the field is called ‘fill_date’ ?
The code I’ve tried is as follows:
TextBoxField tbf = (TextBoxField)tb;
PdfActionCollection actionCollection = tbf.getPdfActions();
for (PdfAction action : actionCollection) {
String out = action.getECMAScriptString();
}
Thanks
@douglasdallas
I wrote a code example (in C#, but I think you can understand it) to display formatting information for a TextBoxFields in document.
var doc = new Document(dataDir + "form1.pdf");
foreach(Field field in doc.Form.Fields)
{
if (field is TextBoxField)
{
var tbf = field as TextBoxField;
if (tbf.Actions != null && tbf.Actions.OnFormat != null)
Console.WriteLine(tbf.Actions.OnFormat.GetECMAScriptString());
}
}
Ah thanks! I was using getPdfActions() instead of getAnnotationActions()…
@douglasdallas
Glad your issue has been resolved.
Here is the Java code snippet in case it is useful to anyone else…
....
if (tb instanceof TextBoxField tbf) {
if (tbf.getAnnotationActions() != null && tbf.getAnnotationActions().getOnFormat() != null) {
String ecmaScriptString = tbf.getAnnotationActions().getOnFormat().getECMAScriptString();
if (ecmaScriptString != null) {
// we have script
}
}
}
.....
Thanks again for your support with this.
@douglasdallas
Thanks, I’ll keep it in mind
Now on to the next bit; I’m having issues setting the date value.
I’ve a date form field with a specific format in Acrobat - but it seems to ignore this. And sometimes displays blank - sometimes partial date?
The date format is set like this:
image.png (54.9 KB)
What format should the date be (it needs to be a string) set in the TextBoxField setValue?
Should it be in the format that is set in Acrobat - which would appear to be Javascript date format? Or the standard ISO date?
I need to set the value and for it to respect the formatting that the customer has chosen to apply to the field.
Thanks.
@douglasdallas
For the option indicated in the attached image (dd-mmmm-yy), the assigned data must contain the day, month in full notation and year in two digits.
For example “10-jaNuarY-24”. Certain adjustments are made and, say, “1-2-24” should also pass, but if the submitted data differs greatly from the specified format, the assignment will not be performed.
Thanks for the reply.
This is what we have tried and it would appear not to be working.
I have a PDF form, with a date format of ‘dd-mmm-yy’ which shows in Adobe as ‘08-Feb-24’. In my test program if I set the TextboxField value (string) to ‘08-Feb-24’ then when it ends up in the PDF it is blank for some reason. If I pick it from the date picker and save the value inside Adobe it works as expected.
Strangely if I set the date format in Adobe to be ‘dd/mm/yy’ and set the value as ‘02/08/2024’ then it would appear to work. If looks like combinations of the format and the value seem to have an issue.
Is there any specical processing that goes on behind the scenes? Could a change be made to accept a date?
@douglasdallas
sergei.shibanov:
month in full notation
mmmm for month implies the full (not abbreviated) name of the month.
For the option with abbreviated month names, use the mask mmm (not mmmm).
@sergei.shibanov
In my example I am using abbreviated month - so mmm (not mmmm).
When setting the string value - should it always match what the predefined format has be set to?
@douglasdallas
Please attach the document you are working with and the code used for the value set.
Here is roughly how the code is… the field we are setting the value for is ‘fill_formatted_date1’.
form1.pdf (97.9 KB)
// rawValueDate is a LocalDate
processedValue = "10-Feb-24"; //rawValueDate.format(DateTimeFormatter.ofPattern("MM/dd/yyyy"));
Arrays.stream(document.getForm().getFields())
.forEach(tb -> {
if (tb instanceof TextBoxField tbf) {
tbf.setValue(valueToSet);
}
})
Thanks
@douglasdallas
Using the code below I set “10-Feb-24” to the ‘fill_formatted_date1’ field and it works.
var doc = new Document(dataDir + "form1.pdf");
TextBoxField tb = doc.Form.Fields[6] as TextBoxField;
Console.WriteLine($"FullName: {tb.FullName}");
tb.Value = "10-Feb-24";
Console.WriteLine($"Result value: {tb.Value}");
doc.Save(dataDir + "Filled-out.pdf");
Filled-out.pdf (57.8 KB)
Does this behave the same when using the Java library?
@douglasdallas
Yes, it should be worked exactly the same.
Here is the contents of the unit test that is failing with the value comparison… as the value is null…
String name = "fill_formatted_date1";
String value = "16-Feb-24";
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("artifacts/form_with_field_formatting.pdf");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try (Document setValueDocument = new Document(inputStream)) {
TextBoxField textBoxField = Arrays.stream(setValueDocument.getForm().getFields())
.filter(f -> f.getFullName().equals(name)).map(TextBoxField.class::cast).findFirst().orElse(null);
textBoxField.setValue(value);
setValueDocument.save(outputStream);
}
byte[] byteArray = outputStream.toByteArray();
// check the generated pdf has the value set
try (Document getValueDocument = new Document(new ByteArrayInputStream(byteArray))) {
TextBoxField textBoxField = Arrays.stream(getValueDocument.getForm().getFields())
.filter(f -> f.getFullName().equals(name)).map(TextBoxField.class::cast).findFirst().orElse(null);
assertNotNull(textBoxField);
assertEquals(name, textBoxField.getFullName());
// this assert fails
assertEquals(value, textBoxField.getValue());
}
// dump out pdf
try (FileOutputStream fos = new FileOutputStream("out.pdf")) {
fos.write(byteArray);
}
I must be doing something wrongly… I’m on version aspose-pdf-23.12.jar.
form_with_field_formatting.pdf (22.3 KB)
out.pdf (17.1 KB)
@douglasdallas
This week I have already completed the work, next week I will write to you.
1 Like