Skip to main content
PDF_IS_FOR_READING
Participating Frequently
December 5, 2019
Answered

Folder Level Javascripts Not Working - Save To Txt

  • December 5, 2019
  • 3 replies
  • 1726 views

Hello. We are using folder level javascripts to save data from our PDF files to a text file for data imports. This worked very well on windows but now were trying to use the same system on a Mac Mojave 10.14.6 machine.

 

Currently it is creating the text file but it is not writing the data to the file. In this situation, the variable for our data is "lines". We ensured that the data is coming from lines by making its value ".txt" and adding it to the path.

 

Here is the code:

 

qipSaveCSV = app.trustedFunction(function(doc, path, lines) {
	app.beginPriv(); 
	myDoc = app.newDoc();
	myDoc.saveAs(path, "com.adobe.acrobat.plain-text","", false, true);
	var f = myDoc.addField("lines", "text", 0, [0, 0, 0, 0]);
	f.multiline = true;
	f.value = lines;	
	myDoc.saveAs(path, "com.adobe.acrobat.plain-text", "", false, true);
	myDoc.closeDoc(true);
	app.endPriv(); 
});

 

 If you have any ideas as to why this isn't functioning properly, help would be greatly appreciated. 

This topic has been closed for replies.
Correct answer Bernd Alheit

Move flattenPages and saveAs before closeDoc.

3 replies

try67
Community Expert
Community Expert
December 5, 2019

Don't use a variable called "path". There's a built-in property of the Doc object with that name and it can cause conflicts.

Change it to something else.

Also, why are you saving the file twice as a text file?

PDF_IS_FOR_READING
Participating Frequently
December 5, 2019

Very good insight, let me swap that out.

Bernd Alheit
Community Expert
Community Expert
December 5, 2019

Try this:

Add myDoc.flattenPages ();

before saveAs

PDF_IS_FOR_READING
Participating Frequently
December 5, 2019

Hey Bernd.

 

I tried that, and this is the code as of now, with no luck.

qipSaveCSV = app.trustedFunction(function(doc, path, lines) {
	
	app.beginPriv(); 
	myDoc = app.newDoc();
	myDoc.flattenPages ();
	myDoc.saveAs(path, "com.adobe.acrobat.plain-text","", false, true);
	var f = myDoc.addField("lines", "text",0,[20, 100, 100, 20]);
	f.multiline = true;
	f.value = lines;	
	
	
	myDoc.closeDoc(true);
	app.endPriv();

});

 

Any other suggestions?

try67
Community Expert
Community Expert
December 5, 2019

You need to move the saveAs command just before the closeDoc command. What you're doing now is saving the file before adding the text to it... Of course that will yield an empty text file.

Thom Parker
Community Expert
Community Expert
December 5, 2019

So first, are there any errors reported in the Console Window?

Next, you don't need the two SaveAs operations, one is just fine.

And finally, this code is creating a blank document, with a text field of zero size. There is no reason for the data to appear in the final document. The text field should be of a decent size. Like page size.

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
PDF_IS_FOR_READING
Participating Frequently
December 5, 2019

No Errors in the Console Window.  I removed one of the SaveAs and it still just creates a blank file.  We tried the code below but still got a blank file.  Any suggestions?

 

var f = myDoc.addField("lines", "text",0 , [20, 100, 100, 20]);

 

Thom Parker
Community Expert
Community Expert
December 5, 2019

Remove the line that closes the file, so you can see the result. Does the "lines" text appear in the text field?

If not, then perhaps there is a missing conversion setting for text files. One that includes form fields in the conversion. 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often