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

NotAllowedErro when calling newPage() or insertPages()

Explorer ,
May 25, 2024 May 25, 2024

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. 

function test2() {
    global.doc = this;
    global.doc.newPage();
 }


Could anyone provide guidance ? 

Thanks in advance, 

TOPICS
Acrobat SDK and JavaScript

Views

837

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 ,
May 25, 2024 May 25, 2024

Copy link to clipboard

Copied

Where do you use the function?

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
Explorer ,
May 26, 2024 May 26, 2024

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. 

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 ,
May 26, 2024 May 26, 2024

Copy link to clipboard

Copied

Try this:

Call the function in the Javascript console (ctrl-j).

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
Explorer ,
May 26, 2024 May 26, 2024

Copy link to clipboard

Copied

NotAllowedError as well 

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 ,
May 27, 2024 May 27, 2024

Copy link to clipboard

Copied

Is the document protected or signed?

Try a other document.

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
Explorer ,
May 27, 2024 May 27, 2024

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

                    }

                }

            }

      }

}

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 ,
May 27, 2024 May 27, 2024

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.

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
Explorer ,
May 27, 2024 May 27, 2024

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

function test2() {
    // Initialize global variables
    global.counter = 1;
    global.doc = this;
    global.doc.newPage();
    global.spacing = 40; // Define the space between sections
    global.pageHeight = global.doc.getPageBox("Crop", 0).height; // Height of the page

    // Define initial positions for the sections
    global.firstSection = { a: 100, b: 700, c: 200, d: 720 };
    global.secondSection = { a: 100, b: 600, c: 200, d: 620 };

    // Ensure global.doc is initialized
    if (typeof global.doc === "object") {
        console.println("The document is well initialized");
    }
    if (typeof global.doc === "undefined") {
        console.println("The document is undefined");
        return;
    }

    try {
        // Add the first section's title field
        var titleField = global.doc.addField("SectionTitle", "text", 0, [global.firstSection.a, global.firstSection.b, global.firstSection.c, global.firstSection.d]);
        if (titleField == null) {
            console.println("Failed to create title field");
            return;
        }
        titleField.value = "This is the value of my field";
        console.println("Title field created successfully.");

        // Add the first section's entry field
        var entryField = global.doc.addField("EntryField", "text", 0, [global.firstSection.a, global.firstSection.b - 20, global.firstSection.c, global.firstSection.d - 20]);
        if (entryField == null) {
            console.println("Failed to create entry field");
            return;
        }
        entryField.value = "";
        console.println("Entry field created successfully.");

        // Add the second section's title field
        var secondTitleField = global.doc.addField("SecondSectionTitle", "text", 0, [global.secondSection.a, global.secondSection.b, global.secondSection.c, global.secondSection.d]);
        if (secondTitleField == null) {
            console.println("Failed to create second title field");
            return;
        }
        secondTitleField.value = "This is the second section title";
        console.println("Second title field created successfully.");

        // Add the second section's entry field
        var secondEntryField = global.doc.addField("SecondEntryField", "text", 0, [global.secondSection.a, global.secondSection.b - 20, global.secondSection.c, global.secondSection.d - 20]);
        if (secondEntryField == null) {
            console.println("Failed to create second entry field");
            return;
        }
        secondEntryField.value = "";
        console.println("Second entry field created successfully.");

        // Add a button to duplicate the template section
        var duplicateButton = global.doc.addField("DuplicateButton", "button", 0, [100, 100, 200, 150]);
        if (duplicateButton == null) {
            console.println("Failed to create button");
            return;
        }
        duplicateButton.buttonSetCaption("Duplicate Section");
        duplicateButton.setAction("MouseUp", "duplicateSection();");
        console.println("Duplicate button created successfully.");

        // Add a button to delete the last section
        var deleteButton = global.doc.addField("DeleteButton", "button", 0, [220, 100, 320, 150]);
        if (deleteButton == null) {
            console.println("Failed to create button");
            return;
        }
        deleteButton.buttonSetCaption("Delete Last Section");
        deleteButton.setAction("MouseUp", "deleteLastSection();");
        console.println("Delete button created successfully.");

        // Define the duplicateSection function
        global.doc.addScript("duplicateSection",
            function duplicateSection() {
                try {
                    var currentPage = global.doc.numPages - 1; // get current page index
                    var currentY = global.firstSection.b - (global.counter * global.spacing);
                     
                    // Calculate the bottom boundary of the new section's title field
                    var newSectionBottom = currentY - 20;
                    console.println(newSectionBottom);

                    if (newSectionBottom < 0) { // If the new section's title field's bottom boundary is below the page height
                        global.doc.newPage();
                        currentPage += 1; // move to the new page index
                        global.counter = 1; // Reset counter for the new page
                        currentY = global.pageHeight - 40; // Reset position to the top of the new page
                    }

                    var newTitlePos = [global.firstSection.a, currentY, global.firstSection.c, currentY + 20];
                    var newEntryPos = [global.firstSection.a, currentY - 20, global.firstSection.c, currentY];

                    var newTitleField = global.doc.addField("SectionTitle" + global.counter, "text", currentPage, newTitlePos);
                    if (newTitleField == null) {
                        console.println("Failed to create new title field");
                        return;
                    }
                    newTitleField.value = "This is the value of my field";

                    var newEntryField = global.doc.addField("EntryField" + global.counter, "text", currentPage, newEntryPos);
                    if (newEntryField == null) {
                        console.println("Failed to create new entry field");
                        return;
                    }
                    newEntryField.value = "";

                    console.println("New section created successfully.");

               
                    global.counter += 1;

                    // Adjust the position of the second section
                    adjustSecondSection(currentPage);
                } catch (e) {
                    console.println("Error duplicating section: " + e);
                }
            }
        );

        // Define the deleteLastSection function
        global.doc.addScript("deleteLastSection",
            function deleteLastSection() {
                try {
                    if (global.counter > 1) {
                        global.counter -= 1;
                        var titleField = global.doc.getField("SectionTitle" + global.counter);
                        if (titleField != null) {
                            global.doc.removeField(titleField.name);
                        } else {
                            console.println("Title field not found");
                        }

                        var entryField = global.doc.getField("EntryField" + global.counter);
                        if (entryField != null) {
                            global.doc.removeField(entryField.name);
                        } else {
                            console.println("Entry field not found");
                        }

                        console.println("Last section deleted successfully.");

                        // Adjust the position of the second section
                        adjustSecondSection(global.doc.numPages - 1);
                    } else {
                        console.println("No sections to delete.");
                    }
                } catch (e) {
                    console.println("Error deleting section: " + e);
                }
            }
        );

        // Define the adjustSecondSection function
        global.doc.addScript("adjustSecondSection",
            function adjustSecondSection(currentPage) {
                try {
                    var newSecondTitlePos = [global.secondSection.a, global.secondSection.b - (global.counter * global.spacing), global.secondSection.c, global.secondSection.d - (global.counter * global.spacing)];
                    var newSecondEntryPos = [global.secondSection.a, global.secondSection.b - 20 - (global.counter * global.spacing), global.secondSection.c, global.secondSection.d - 20 - (global.counter * global.spacing)];

                    // Calculate the bottom boundary of the second section's entry field
                    var newSecondSectionBottom = newSecondEntryPos[1];

                    if (newSecondSectionBottom < 0) { // If the second section's entry field's bottom boundary is below the page height
                        global.doc.newPage();
                        currentPage += 1; // move to the new page index
                        newSecondTitlePos = [global.secondSection.a, global.pageHeight - 40, global.secondSection.c, global.pageHeight - 20];
                        newSecondEntryPos = [global.secondSection.a, global.pageHeight - 60, global.secondSection.c, global.pageHeight - 40];
                    }

                    var secondTitleField = global.doc.getField("SecondSectionTitle");
                    if (secondTitleField != null) {
                        secondTitleField.rect = newSecondTitlePos;
                        secondTitleField.page = currentPage;
                    } else {
                        console.println("Second title field not found");
                    }

                    var secondEntryField = global.doc.getField("SecondEntryField");
                    if (secondEntryField != null) {
                        secondEntryField.rect = newSecondEntryPos;
                        secondEntryField.page = currentPage;
                    } else {
                        console.println("Second entry field not found");
                    }

                    console.println("Second section adjusted successfully.");
                } catch (e) {
                    console.println("Error adjusting second section: " + e);
                }
            }
        );

        console.println("duplicateSection, deleteLastSection, and adjustSecondSection functions added successfully.");
    } catch (e) {
        console.println("Error: " + e);
    }
}
test2();

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
Explorer ,
May 27, 2024 May 27, 2024

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.

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 ,
May 28, 2024 May 28, 2024

Copy link to clipboard

Copied

So are you running this code on an LCD 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
Community Expert ,
May 28, 2024 May 28, 2024

Copy link to clipboard

Copied

In Adobe Acrobat the script works without any error message.

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
Explorer ,
May 28, 2024 May 28, 2024

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 

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 ,
May 28, 2024 May 28, 2024

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".

 

 

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
Explorer ,
May 29, 2024 May 29, 2024

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. 

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 ,
May 29, 2024 May 29, 2024

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. 

 

 

 

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
Explorer ,
Jun 17, 2024 Jun 17, 2024

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,




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 ,
Jun 17, 2024 Jun 17, 2024

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. 

 

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
Explorer ,
Jun 18, 2024 Jun 18, 2024

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

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 ,
Jun 18, 2024 Jun 18, 2024

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. 

 

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
Explorer ,
Jun 21, 2024 Jun 21, 2024

Copy link to clipboard

Copied

LATEST

thank you

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