Skip to main content
Inspiring
May 18, 2024
Question

Automatic XML generation as an attachment in the PDF

  • May 18, 2024
  • 1 reply
  • 1153 views

Hi everyone,

Please excuse my limited knowledge with this question as I just wanted to first clarify if it is possible before exploring how to do it 🙂

 

In my form, I want to implement a button that users can click to check if any field is still pending and then save the form as a PDF file with a different name.

 

My question is (I know they can save it in XML format): When the file is exported in PDF format, can it also generate the XML at the same time and include it in the PDF as an attachment automatically? as I will need both files (it was generating confusing requesting users to upload both files).

 

Is that possible?

Thanks a lot

 

 

This topic has been closed for replies.

1 reply

Thom Parker
Adobe Expert
May 20, 2024

This would only be possible for a script that has privilege.  And I'm not sure it would work in Reader, Pro might be required. 

Read this article on how to write a script to save a PDF. 

https://www.pdfscripting.com/public/How-to-Save-a-PDF-2.cfm

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
KaxkulAuthor
Inspiring
May 30, 2024

Many thanks @Thom Parker. After reading through what you shared, I'm still unsure. I use the code below in the button to generate the submission form (basically to check if all fields completed) :

var emptyFields = [];

for (var i = 0; i < this.numFields; i++) {
var f = this.getField(this.getNthFieldName(i));

var aPages = (typeof(f.page) == "object") ? f.page : [f.page];
aPages = aPages.filter(a => a > -1 && a < 9);

if (f.type !== "button" && f.required && aPages.length && f.required) {
if (f.valueAsString === f.defaultValue) {
emptyFields.push(f.name);
}
}
}

if (emptyFields.length > 0) {
app.alert("Please complete the following fields:\n" + emptyFields.join("\n"));
} else {
app.execMenuItem("SaveAs");
}

 

To generate the PDF file for submission, I thought it would be possible to attach a copy of the form in a "*.xfdf" embedded automatically in the PDF

Thom Parker
Adobe Expert
May 31, 2024

You can generate the XFDF for the form and make it an attachment. However, adding an attachment requires privilege, so it can't be automatically done from a document script. 

See these entries in the Acrobat JavaScript Reference:

https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsapiref/doc.html#exportasxfdf

https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsapiref/doc.html#createdataobject

 

Those are the two functions you'll need to make this work. 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often