Copy link to clipboard
Copied
trying to use action wizard to process many pdf forms and create a csv file for each, so far I have something like this but it does not seem to be working, any ideas?
if ( app.viewerVersion >= 7 ) {
this.createDataObject(this.title + ".xls");
// Get the file stream object of the embedded file
var oFile = this.getDataObjectContents(this.title + ".xls");
// Convert to a string
var myApp = util.stringFromStream(oFile, "utf-8");
// Get Field Names separated by comma
for (var i = 0; i < this.numFields; i++) {
myApp + this.getNthFieldName(i) + ",";
console.println("Field[" + i + "] = " + this.getNthFieldName(i));
}
// Add newline
var myApp = myApp + "\r\n";
// Get Field Values separated by comma
for (var i = 0; i < this.numFields; i++) {
var fieldName = this.getNthFieldName(i);
myApp + this.getField(fieldName).value + ",";
console.println("FieldValue[" + i + "] = " + this.getField(fieldName).value);
}
// Convert back to a file stream
var oFile = util.streamFromString(myApp, "uft-8");
// Now "overwrite" budget.xls
this.setDataObjectContents(this.title + ".xls", oFile);
} else {
app.alert("Acrobat 7.0 or later is required.");
}
Copy link to clipboard
Copied
In lines 18 and 30 you don't change myApp.
Copy link to clipboard
Copied
thanks but still doesn't generate the xls file, is there something else I'm missing?
Copy link to clipboard
Copied
Are there any errors when you run the code? What exactly is the result of running it?
Copy link to clipboard
Copied
Command Name: Execute JavaScript
Command Start Time: 2016-09-04 08:44:50
Command Status: Succeeded
Command Finish Time: 2016-09-04 08:44:50
running the action wizard I would want a csv file to be generated alongside the pdf but noting gets outputted
Copy link to clipboard
Copied
Error messages generated by the JS code will appear in the JS Console, which you can display by pressing Ctrl+J after the Action has finished running.
Also, it might be good if you post your new code.
One other thing: Did you add a method to save the PDF file after the Execute JavaScript command?
Copy link to clipboard
Copied
thanks again for the help, here is what I have
if ( app.viewerVersion >= 7 ) {
// Get the file stream object of the embedded file
var oFile = this.getDataObjectContents(this.title + ".xls");
// Convert to a string
var myApp = util.stringFromStream(oFile, "utf-8");
// Get Field Names separated by comma
for (var i = 0; i < this.numFields; i++) {
var myApp = myApp + this.getNthFieldName(i) + ",";
console.println("Field[" + i + "] = " + this.getNthFieldName(i));
}
// Add newline
var myApp = myApp + "\r\n";
// Get Field Values separated by comma
for (var i = 0; i < this.numFields; i++) {
var fieldName = this.getNthFieldName(i);
var myApp = myApp + this.getField(fieldName).value + ",";
console.println("FieldValue[" + i + "] = " + this.getField(fieldName).value);
}
// Convert back to a file stream
var oFile = util.streamFromString(myApp, "uft-8");
// Now "overwrite" budget.xls
this.setDataObjectContents(this.title + ".xls", oFile);
} else {
app.alert("Acrobat 7.0 or later is required.");
}
and the output is
Acrobat EScript Built-in Functions Version 11.0
Acrobat SOAP 11.0
TypeError: Invalid argument type.
Doc.getDataObjectContents:3:Batch undefined:Exec
===> Parameter cName.
Copy link to clipboard
Copied
You have various problems with your code. Let's start with the one you get an error message for.
What it basically says is that there is no attached file with that name... So you need to sort that out, first of all.
Copy link to clipboard
Copied
PS. Your entire viewerVersion check is unnecessary. Not only do I doubt someone is still using Acrobat 6 (or earlier), I'm not even sure it had batch processing capabilities, so it's a moot point, anyway. Get rid of it and simplify your code.
Copy link to clipboard
Copied
Regarding myApp: You don't need to use the "var" keyword each time. That is used to declare the variable the first time you use it.
After that if you want to add a new value to it simply use this format:
myApp += "Some new text";
Copy link to clipboard
Copied
Thanks again for everyone's help, I really appreciate it... cleaned up the code and it runs but closes out of adobe after it runs, no errors, but no files either.
var myApp;
// Get Field Names separated by comma
for (var i = 0; i < this.numFields; i++) {
myApp += myApp + this.getNthFieldName(i) + ",";
console.println("Field[" + i + "] = " + this.getNthFieldName(i));
}
// Add newline
myApp += myApp + "\r\n";
// Get Field Values separated by comma
for (var i = 0; i < this.numFields; i++) {
var fieldName = this.getNthFieldName(i);
myApp += myApp + this.getField(fieldName).value + ",";
console.println("FieldValue[" + i + "] = " + this.getField(fieldName).value);
}
// Convert back to a file stream
var oFile = util.streamFromString(myApp, "uft-8");
// Now "overwrite" budget.xls
this.setDataObjectContents(this.title + ".xls", oFile);
basically I get filled out forms that I want to convert to csv files. I was hoping to use the action wizard to do this in mass and then figure a way to run the action wizard command from dos
Copy link to clipboard
Copied
Did you add a command to save the files, after running this script?
Also, you still have problems with how you're generating the text.
You don't need to do this:
myApp += myApp + this.getNthFieldName(i) + ",";
The "+=" operator means "adds this new value on the right to the existing value on the left". So all you need is:
myApp += this.getNthFieldName(i) + ",";
Copy link to clipboard
Copied
Thanks for the quick reply....
I get the output in the console and it does not close the pdf file, but still can't figure out how to save the csv string (myApp) I created
var myApp;
// Get Field Names separated by comma
for (var i = 0; i < this.numFields; i++) {
myApp += this.getNthFieldName(i) + ",";
console.println("Field[" + i + "] = " + this.getNthFieldName(i));
}
// Add newline
myApp += "\r\n";
// Get Field Values separated by comma
for (var i = 0; i < this.numFields; i++) {
var fieldName = this.getNthFieldName(i);
myApp += this.getField(fieldName).value + ",";
console.println("FieldValue[" + i + "] = " + this.getField(fieldName).value);
}
// Create file
this.createDataObject(this.title + ".xls", myApp);
Copy link to clipboard
Copied
You didn't answer my question. Did you add a step to save the file after the JS code?
Copy link to clipboard
Copied
yes i add the save action after the JavaScript
Copy link to clipboard
Copied
is it possible to write to a file through the java script in the action wizard?
Copy link to clipboard
Copied
Yes, it is possible (Edit: Actually, I'm not sure what you mean, exactly. What "file" do you want to write to? The PDF file? Something else?)
Does your script work when you run it via the Console window?
Copy link to clipboard
Copied
yes it works when I output to the console window, but I want to output to a txt file on the computer, is that possible?
Copy link to clipboard
Copied
Not directly, no. You can use a Report and then save it as a text file, but that's a whole other mess...
Copy link to clipboard
Copied
I don't know if you are having any success with Adobe's JavaScript; but, you can use VB.net to get a CSV file for each PDF (acrobat PDF) file in a directory. You can use FDFToolkit.net and Visual Studio to create the application (windows, console or server side). Let me know if you need a custom programming solution.
Note: FDFToolkit.net and Visual Studio.net can be downloaded for free.
Example VB.net Code
Imports FDFApp.FDFDoc_Class
Imports FDFApp.FDFApp_Class
Imports System.IO
''' <summary>
''' Call clsPdfDirectory2Csv.createCsv("C:\path\to\pdf\directory\")
''' </summary>
''' <remarks></remarks>
Public Class clsPdfDirectory2Csv
Public Shared Sub createCsv(ByVal directory_path As String)
If Not String.IsNullOrEmpty(directory_path & "") Then
For Each f As String In Directory.GetFiles(directory_path)
If File.Exists(f) Then
If Path.GetExtension(f).Replace(".", "") = "pdf" Then
Try
Dim p As New FDFApp.FDFApp_Class, fdf As New FDFApp.FDFDoc_Class
fdf = p.PDFOpenFromFile(f, True, True, "")
Dim csv As String = CreateCSVRow(fdf.XDPGetAllFields().ToList())
File.WriteAllText(Path.GetDirectoryName(f).TrimEnd("\") & "\" & Path.GetFileNameWithoutExtension(f) & ".csv", csv.ToString, System.Text.Encoding.UTF8)
Catch ex As Exception
Err.Clear()
End Try
End If
End If
Next
End If
End Sub
Public Shared Function CreateCSVRow(ByVal fldArray As List(Of FDFApp.FDFDoc_Class.FDFField)) As String
Dim csvCols As New List(Of String)
Dim csvValue As String
Dim needQuotes As Boolean
Dim strCsv As String = ""
For i As Integer = 0 To fldArray.Count - 1
If Not String.IsNullOrEmpty(strCsv & "") Then
strCsv &= ","
End If
strCsv &= """" & fldArray(i).FieldName.ToString() & """"
Next
For i As Integer = 0 To fldArray.Count - 1
csvValue = fldArray(i).FieldValue(0)
needQuotes = (csvValue.IndexOf(",", StringComparison.InvariantCulture) >= 0 _
OrElse csvValue.IndexOf("""", StringComparison.InvariantCulture) >= 0 _
OrElse csvValue.IndexOf(vbCrLf, StringComparison.InvariantCulture) >= 0)
csvValue = csvValue.Replace("""", """""")
csvCols.Add(If(needQuotes, """" & csvValue & """", csvValue))
Next
strCsv &= Environment.NewLine & String.Join(",", csvCols.ToArray())
Return strCsv
End Function
End Class
Find more inspiration, events, and resources on the new Adobe Community
Explore Now