Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
13

Automatic XML generation as an attachment in the PDF

Participant ,
May 18, 2024 May 18, 2024

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

 

 

TOPICS
Create PDFs , Edit and convert PDFs , How to , JavaScript , PDF , PDF forms
675
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 20, 2024 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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 30, 2024 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 31, 2024 May 31, 2024
LATEST

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines