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

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

Community Beginner ,
Apr 06, 2024 Apr 06, 2024

Copy link to clipboard

Copied

 

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));
    }
TOPICS
JavaScript , PDF

Views

508

Translate

Translate

Report

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 ,
Apr 06, 2024 Apr 06, 2024

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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 Beginner ,
May 08, 2024 May 08, 2024

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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 Beginner ,
May 08, 2024 May 08, 2024

Copy link to clipboard

Copied

Is it possible with the basic console script I posted to send the results to clip board, or txt file?

Votes

Translate

Translate

Report

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 08, 2024 May 08, 2024

Copy link to clipboard

Copied

LATEST

You can select and copy the content of the console.

Votes

Translate

Translate

Report

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