0

I'm using Javascript within a PDF editor.

I'm using this reference https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/js_api_reference.pdf

I'm trying to set an action on a field when a mouseEnter event is triggered on that field. Below is my code.

var myDoc = app.newDoc(); // Create a blank doc
var Bbox = myDoc.getPageBox("Crop"); // Get crop box
var inch = 72;
// Add a text field at the top of the document
var f = myDoc.addField("Name.Last", "text", 0,[ inch, Bbox[1]-inch, 3*inch, Bbox[1]- inch - 14 ] );
f.setAction("MouseEnter", "f.textColor = color.yellow"); // Add an action

However i get the following error:

======== Field : mouse enter ========
[ Line: 00000 { ReferenceError } ] : f is not defined

I thought i had defined the field f using this line here:

var f = myDoc.addField("Name.Last", "text", 0,[ inch, Bbox[1]-inch, 3*inch, Bbox[1]- inch - 14 ] );

Why is the error saying my field is not defined?

olly
  • 323
  • 3
  • 10

1 Answers1

1

Adobe Reader cannot modify the page content of PDF files. Doc.addField() and Field.setAction() both attempt to make changes to the PDF that Reader cannot make.

You get the error because the field did not get added so f is undefined.

You can't create new PDF files using Reader either.

This code would likely run successfully, though I haven't tested it, in Adobe Acrobat Pro.

joelgeraci
  • 4,606
  • 1
  • 12
  • 19
  • ah, apologies. I think my use of "reader" was misleading. I am using Foxit PhantomPDF. I believe the Javascript reference I quoted is valid for Foxit PhantomPDF as well as Acrobat, because i've tested a lot of the other code within the reference in Foxit PhantomPDF and it works fine. – olly Jun 17 '20 at 15:51
  • 1
    You can't count on PhantomPDF to run *Acrobat* JavaScript in the same way that Acrobat Pro does. I tested your code in Acrobat Pro DC Build: 20.9.20067.384717 and it works just fine. – joelgeraci Jun 17 '20 at 22:17
  • 1
    Followup... The code also works in PhantomPDF Business Version: 10.0.0.35798 – joelgeraci Jun 17 '20 at 22:22
  • thanks, following your messages i tried completely restarting PhantomPDF and sure enough, the magic of turning something off and on again worked, and the code now works fine. No idea what was causing the initial error, but thanks for your help in helping me to resolve it. – olly Jun 18 '20 at 09:13