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

Folder Level Javascripts Not Working - Save To Txt

New Here ,
Dec 05, 2019 Dec 05, 2019

Copy link to clipboard

Copied

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. 

TOPICS
Acrobat SDK and JavaScript

Views

825

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

correct answers 3 Correct answers

Community Expert , Dec 05, 2019 Dec 05, 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.

Votes

Translate

Translate
Community Expert , Dec 05, 2019 Dec 05, 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.

Votes

Translate

Translate
Community Expert , Dec 05, 2019 Dec 05, 2019

Move flattenPages and saveAs before closeDoc.

Votes

Translate

Translate
Community Expert ,
Dec 05, 2019 Dec 05, 2019

Copy link to clipboard

Copied

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 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
New Here ,
Dec 05, 2019 Dec 05, 2019

Copy link to clipboard

Copied

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]);

 

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 ,
Dec 05, 2019 Dec 05, 2019

Copy link to clipboard

Copied

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 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
New Here ,
Dec 05, 2019 Dec 05, 2019

Copy link to clipboard

Copied

The data is showing up in the field and we have been able to pass the data previously as a file extension.

I'm afraid this is some weird Mac compatability issue.

Do you have any other ideas?

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 ,
Dec 05, 2019 Dec 05, 2019

Copy link to clipboard

Copied

Try this:

Add myDoc.flattenPages ();

before saveAs

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
New Here ,
Dec 05, 2019 Dec 05, 2019

Copy link to clipboard

Copied

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?

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 ,
Dec 05, 2019 Dec 05, 2019

Copy link to clipboard

Copied

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.

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
New Here ,
Dec 05, 2019 Dec 05, 2019

Copy link to clipboard

Copied

LATEST

Flatten pages was giving me an error so I removed it after moving save as to before close Doc.

 

It is officially working now, and this is the code:

 

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

});

Thanks everyone for all of the support! This stump has been slowing us up a bit!

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 ,
Dec 05, 2019 Dec 05, 2019

Copy link to clipboard

Copied

Move flattenPages and saveAs before closeDoc.

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 ,
Dec 05, 2019 Dec 05, 2019

Copy link to clipboard

Copied

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?

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
New Here ,
Dec 05, 2019 Dec 05, 2019

Copy link to clipboard

Copied

Very good insight, let me swap that out.

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