Skip to main content
Participating Frequently
April 6, 2024
質問

PDF JS list form field names, but in order - and print to excel

  • April 6, 2024
  • 返信数 1.
  • 1163 ビュー

 

The code below lists all the field names in a PDF. However, it does so in alphabetical order. I would like to be able to get these into Excel, but in the order they appear through the PDF form itself.

Side question, whats the save as file option here - excel or csv?

Thanks all.

 

console.show();
  console.clear();
  console.println("Number of fields: " + this.numFields);
  for(var i = 0; i < this.numFields; i++)
    {
      console.println(this.getNthFieldName(i));
    }
このトピックへの返信は締め切られました。

返信数 1

Thom Parker
Community Expert
Community Expert
April 6, 2024

This requires some advanced scripting. 

Listing the fields in the order they appear in the PDF means analyzing the field locations (i.e. page number and rectangle).  Some fields may have multiple locations, so the sorting will need to take that into account. 

 To do this, collect all the fields into an array and sort the array according to the field's geometry.  

Here's an outline for the code, note that the sorting function is missing. 

 

var aFlds = [];
for(var i = 0; i < this.numFields; i++)
   aFlds .push(this.getField(this.getNthFieldName(i)));

var aFieldNames = aFlds.sort(function(oFld1,oFld2){...}).map(oFld=>oFld.name);
console.println(aFieldNames.join("\n"));

 

 

An Acrobat script cannot write anything  but a PDF directly to the users file system. It can however create an attachment to the PDF and then force that attachment to open.

In this case the script should create a csv file. Most computers will open this file type in Acrobat. 

Use the "doc.createDataObject()" function

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

 

Then use the "doc.openDataObject()" to force the attachment to open in Excel.

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

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
a_gunslimger作成者
Participating Frequently
May 8, 2024

Sorry for the delayed response! I found your reply in my junk folder.   Thanks for the info and ideas. It's probably going to be outside my wheelhouse, but I will investigate. I'm satrting with sort functions.