@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?
@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?
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?
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
Are you working with a license?
Check how a simpler version of the code works for you.
Yes, the license is being used. I’ve tried the simplified version (as near as via Java) and it doesn’t work for me.
Have you tried in Java with my example and the attached PDF file and you are not seeing the issue?
Thanks for your help.
/*
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");
*/
Document setValueDocument = new Document("/tmp/form_with_field_formatting.pdf");
Field fill_formatted_date1 = setValueDocument.getForm().getFields()[0];
System.out.printf("FullName: %s\n", fill_formatted_date1.getFullName());
fill_formatted_date1.setValue("16-Feb-24");
String theValue = fill_formatted_date1.getValue();
System.out.printf("Result value: %s\n", theValue);
setValueDocument.save("out_simple.pdf");
Not yet, I mostly work in C#.
If necessary, I will do some reproducing in Java, but it will take time.
Still a question - are you using the latest version of the library (24.1 because changes for working with months as strings were added later than getECMAScriptString() on 23.12)?
We are using 2023.12; I will download 2024.01 and try to see if that resolves. Thanks.
It works in 2024.01 - the simple approach of hard coding a value - I’lll see if it works when in our product.
To confirm; what string format for the date should be set? As it’s not a date object - I don’t see how it can work with all the display formats?
The customer expects the date to the formatted as per the defined display date.
For example if it’s a date only, should it always be set as ‘MM-dd-yyyy’?
Or should we take the formatting that has been defined as part of the ECMA script and use it? This presents a problem as this date format string is Javascript which is different from Java - so it would need converted somehow.
Thanks
@douglasdallas
I didn’t really understand your question.
Text fields according to the Pdf specification can contain ECMA scripts (java scripts). Which for OnFormat in some way influence (convert) the entered value. The entered data may be formatted as numbers with a certain format, dates, etc.
This can be set, for example, in Acrobat in the text field options.
Sorry, not the best explaination.
How about, from another angle, I have a value in our database which is a date field - and I would like to populate the form field in the PDF. To do this, using your API I need to set the value as a string.
What should the string value be set to, so that it works with any of the display values set within Adobe?
@douglasdallas
Thank you, now I understand your question.
If you are working with a document field about which you don’t know what parameters it has, you can:
int len = "AFDate_FormatEx(\"".Length;
int formatLen = jsString.Length;
string format = jsString.Remove(0, len).Remove(formatLen - len - 2, 2);
var doc = new Document(dataDir + "form1.pdf");
TextBoxField tb = doc.Form.Fields[6] as TextBoxField;
Console.WriteLine($"FullName: {tb.FullName}");
// Set Action.OnFormat to null.
tb.Actions.OnFormat = null;
tb.Value = "Any string";
Console.WriteLine($"Result value: {tb.Value}");
doc.Save(dataDir + "FilledWithAnyString.pdf");
var doc = new Document(dataDir + "form1.pdf");
TextBoxField tb = doc.Form.Fields[6] as TextBoxField;
Console.WriteLine($"FullName: {tb.FullName}");
// Set Action.OnFormat to the desired format.
// Not worked ?
string ECMAScriptString = "AFDate_FormatEx(\"dd/m/yy\")";
tb.Value = "";
tb.Actions.OnFormat = new JavascriptAction(ECMAScriptString);
tb.Value = "19 02 2024";
Console.WriteLine($"Result value: {tb.Value}");
doc.Save(dataDir + "FilledWithAnyString.pdf");
Thanks for your reply. I’m not sure I understand it fully.
Are your steps:
If so, how does Adobe know how to apply the formatting on the date when we re-add it, if it only has the string value and doesn’t know which part of the day, month or year?
not steps, variants
Variant 1. Find out the date format for the text field and generate the assigned string in accordance with this format.
Variant 2. Clear the formatting for the text field and after that you can assign any string to it.
Variant 3. Set some date format for the field and, knowing it, form a suitable string. (I’ll have to check it again and if it doesn’t work I’ll assign a task to the development team).
Ah, gottcha - thanks!
@douglasdallas
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): PDFNET-56612
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.