0

I have a PDF form that has many form fields. My requirement is to export the form data (some of it anyway) into an excel (xls) format on a network drive that is being picked up and used by another process (that I do not have access or code to change) to load into a database using Acrobat Javascript when a button on the PDF is clicked. As this is a distributed form and the network drive is common, it did not make sense to setup ODBC connections to the database nor did I have access to do so.

The issues I am having are:

  1. I need to specifically name the Worksheet so that the process correctly processes the xls file.
  2. I need to save the information to the network drive without prompting the user, which the code below does.
  3. The fieldNames seem to be losing spaces when they export to xls.

So far nothing I tried has worked nor do any of the references I have gone though provide such information. I can push data into a .csv and .txt files and have tried creating a new Report something like the following:

var rep = new Report();
var values = "";
...
rep.writeText(values);

var docRep = rep.open("myreport.pdf");
docRep.saveAs("/c/temp/Upload.txt","com.adobe.acrobat.plain-text")
docRep.closeDoc(true);
app.alert("Upload File Saved", 3);

but it only allows the .txt extension not the xls or csv extension. I managed to export the csv in another way.

Below is a small snippet of my code:

var fieldNames = [];
var result ="";
fieldNames.push("Inn Code");
fieldValues.push('"' + this.getField("Hotel Info Inn Code").value + '"');
fieldNames.push("Business Unit");
fieldValues.push('"' + this.getField("Hotel Info Business Unit").value + '"');
for ( var i=0; i < fieldNames.length; i++ ) {
    result += (fieldNames[i] + "\t");
}
for ( var i=0; i < fieldValues.length; i++ ) {
    result += (fieldValues[i] + "\t");
}
this.createDataObject({cName: "Upload.xls", cValue: cMyC});
this.exportDataObject({cName: "Upload.xls", nLaunch: 0});

Any help or suggestions provided would be greatly appreciated!

eibersji
  • 1,218
  • 4
  • 29
  • 52
JeffC
  • 1
  • 1

1 Answers1

0

Update (For anyone else who encounters this need).

I determined that the tab name using the method above is the same as the save name. The other issue I encountered was that the xls file was really a csv with an xls extension. To resolve this I decided to export an csv I am formatting or a text file and an external process to reformat the file as an xls.

var fieldNames = [];
var fieldValues = [];
var result = '';

// FIELD VALUES
fieldNames.push('"Column Name 1"');
fieldValues.push('\r\n' + '"' + this.getField("Field Name 1").value + '"');

fieldNames.push('"Column Name 2"');
fieldValues.push('"' + this.getField("Field Name 2").value + '"');

for ( var i=0; i < fieldNames.length; i++ ) {
    if (i != fieldNames.length-1){
        result += (fieldNames[i] + ",");
    } else {
        result += (fieldNames[i]);
    }
}

for ( var i=0; i < fieldValues.length; i++ ) {
    if (i != fieldValues.length-1){
        result += (fieldValues[i] + ",");
    } else {
        result += (fieldValues[i]);
    }
}

I used the following to output a csv file:

this.createDataObject('UploadFile.csv', result);
this.exportDataObject({ cName:'UploadFile.csv', nLaunch:'0'});

The issue is that this prompts the user where to save the file and I wanted a silent specific location so I wound up doing the following in place of the create and export object above to output a txt file silently without prompting the user:

var rep = new Report();
rep.writeText(result);
var docRep = rep.open("myreport.pdf");
docRep.saveAs("/c/temp/UploadFile.txt","com.adobe.acrobat.plain-text")
docRep.closeDoc(true);

You will have to go into Edit > Preferences > Security (Enhanced) then choose the output folder (C:\Temp) in my case to output silently if you chose to employ this method.

JeffC
  • 1
  • 1