Copy link to clipboard
Copied
Dear all,
Following this discussion on creating button to add/remove section in fillable form : https://community.adobe.com/t5/acrobat-discussions/button-to-add-remove-section-in-fillable-form/td-...
I started coding a similar form. I'm currently encountering the NotAllowedError when calling the newPage() functions or the insertPage() functions in my script.
Could anyone provide guidance ?
Thanks in advance,
Copy link to clipboard
Copied
Where do you use the function?
Copy link to clipboard
Copied
Dear Bernd,
Thank you for your answer and for your intense activity on the forum. Your answers always provide very helpful guidance.
I'm not very used to the terms, but if im correct, I don't think I'm camling the function from the folder level.
I code my script on Visual Code Studio and then copy-paste it to the "app scripts" window in the javascript editor of Adobe Accrobat.
Copy link to clipboard
Copied
Try this:
Call the function in the Javascript console (ctrl-j).
Copy link to clipboard
Copied
NotAllowedError as well
Copy link to clipboard
Copied
Is the document protected or signed?
Try a other document.
Copy link to clipboard
Copied
I’m only working on blank documents.
It appears that using a new document permits the use of the newPage() function when called from the global function (as shown in my first post)
But when called from within another function it replicates the notAllowedError.
Function test2() {
global.doc.newPage(); // works
try {
global.doc.addScript( ´´ duplicate section’’)
function duplicateSection() {
try {
if (newSectionBottom < 0) {
global.doc.newPage(); // doesn’t work
}
}
}
}
}
Copy link to clipboard
Copied
There are a LOT of errors in your code.
- You can't declare a function with Function. JS is case-sensitive.
- You can't use try without a catch clause.
- You can't use this type of quotes in your code: ´´ ’’
- The addScript method requires two parameters.
- You never declare and assign a value to the newSectionBottom variable.
- You never call the second function (which you don't really need at all), so of course it doesn't execute.
- etc.
You need to simplify your code a lot and remove all the unnecessary stuff from it. There's also no need I can see to use global variables for this.
Copy link to clipboard
Copied
Hey Tr67
Thank you very much for your answer.
When I wrote the text above, I couldn't copy-paste the code so I only wrote the structure with all the typos you noticed. Sorry for that.
The actual code doesn't have the errors you mentionned apart from not having two parameters for addScript, which I'll look into today but never gave an error.
The code is below and also accessible on Github.
I will leave it open source and post it as an answer to the original thread in case other users are interested in it following the unavailability of LiveCycle Designer for non-corporate users.
It would be awesome if you could provide guidance on how to properly use the newPage() function.
Thanks a lot in advance,
https://github.com/wmerna/pdf/blob/main/main.js
Copy link to clipboard
Copied
Also, looking at the global variables, I tried without but encoutered many errors when using the "this" method on anything from within a function.
Copy link to clipboard
Copied
So are you running this code on an LCD file?
Copy link to clipboard
Copied
In Adobe Acrobat the script works without any error message.
Copy link to clipboard
Copied
I'm not using an LCD file, I'm only working on blank new documents.
if the code works for you Bernd, then something must be wrong with my Adobe Acrobat settings.
I'm using the lastest version, I just recently bought it.
I've encountered similar issue with calling "this." method on fully blank documents and for no apparent reason the code I showed you was working when the others weren't.
I'll look in-depth in my settings and try to find a solution. I'll post an update if I find anything
Thank you guys
Copy link to clipboard
Copied
There are many issues with the script. For example, the use of the global object in the document scripts. These global values are not persistent. So if the document is saved, closed, and reopened, the global values will no longer exists. The global object variables are also completely unnecessary. Also, it is pointless to set global.doc to this? The keyword "this" refers to the current object. If you don't know the current context then this may or may not be the current document object. There is also no point in defining a function and then running it? Why not just run the code. In general this script represents some very bad coding. And it would never work for several reasons.
Probably the issue giving you the error, is that the "doc.newPage()" function can only be run from a privileged context. It is not being run from a privileged context. So, this script could never work as written.
Another error and reason this script could never work is that the "doc.addScript()" function takes a text string of the script, not a function object.
And finally, there is no reason to run the "addScript" function every time this function is run to add a new page, since these script will already exits.
Ok, lets just say this script is total garbage and doesn't do what you want it to do. Whoever posted it to GitHub has no idea what they are doing.
It looks like you want to add a new page to a PDF. The correct way to do this is to use Page Tempates. Search on this form for the key workd "Page Templage".
Copy link to clipboard
Copied
Hey Thom,
Thank you for the roasting, I'll improve and propose something better.
I have to say that the addscript() function with a string and a function object as parameter worked fine. But again, as you mentionned, not efficient.
Copy link to clipboard
Copied
You are correct, the addScript works with a named function. It does this because the passed function converts directly to a string of the function defintion, which is the required input.
Sorry about the roasting. I think because so many people are posting crap generated by an AI, that bad code is more upsetting that usual.
Copy link to clipboard
Copied
Hey @Bernd Alheit , @try67 , @Thom Parker ,
I'm still working on improving the code. I managed to have some things working: sections duplicated, deleted and bottom sections moving depending on duplication or deletion.
As I can't call the newPage() function from an unpriviledged location (eg. consequence of the duplicate button on the form), I decided to have natively a pdf with 10 blank pages on which the sections can be duplicated and the blank pages are deleted when the document is saved.
Now I need to reproduce a pdf similar to the one from the original topic (eg. the canadian form).
It feels I can't create the PDF from the Adobe Accrobat "GUI" but only code it in order to be able to access the fields in my code. To tackle this, I've unsuccesfully tried using the getAnnot and the getField functions.
To try other solutions, I started coding the PDF on my own. However, the PDF reference feels quite unclear sometimes.
In order to create the PDF from code (sections, titles ect) I'm currently using the FreeText type of the Annotation object but to change the text color, I need to use the "span" properties and to change the text font, the span properties is not working, I need to change the defaultStyle of my annotation.
I'm still reading the reference but could you kindly either send some good code example of pdf creation through the API so I can learn by reading or simply send the correct functions to call in order to create the texts, change their color and fonts because addAnnotation really seems a weird way but I haven't found anything else.
Thank you very much in advance,
Copy link to clipboard
Copied
It sounds like you need some training.
However, if you are going to get solutions from this forum, I would suggest posting questions on a single topic/issue.
Copy link to clipboard
Copied
Thanks Thom, best training is practice, I'm glad to have found a challenge that pushes my knowledge while doing something I enjoy.
If I had a single question, it would be: what's the best way to create a title with chosen font and chosen color using the javascript API ?
I know the question sounds really dumb ! But the single properties I've found related to this are:
- textColor: which needs to be added to a span object and then added to the richContents property of the FreeText annotation.
- fontFamily: which needs to be used while modifying the defaultStyle property of the FreeText. This doesn't access local fonts: I've tried without success calling "Gotham-Bold" that I can choose from my acrobat drop down menu.
If you'd kindly direct me toward chosen documentation/reference pieces, give me some code to read or simply indicate the appropriate way of doing this, it would be amazing.
Thanks
Copy link to clipboard
Copied
Use a form field. If it's a single line of text that is all one font, size, and color, then the simplest method is to use a read only form field.
Copy link to clipboard
Copied
thank you