My pluggin is not working anymore with last update

This forum is for plugins used in the PDF-XChange Editor only.

Moderators: TrackerSupp-Daniel, Tracker Support, Paul - Tracker Supp, Vasyl-Tracker Dev Team, Chris - Tracker Supp, Sean - Tracker, Ivan - Tracker Software, Tracker Supp-Stefan

Post Reply
nbmani
User
Posts: 17
Joined: Tue Feb 14, 2023 11:53 am

My pluggin is not working anymore with last update

Post by nbmani »

Hello.

I developed a plugin where I can extract and create a new file PDF considering a word founded but with last update is not working anymore.

Can anyone help me? Thanks
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17670
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: My pluggin is not working anymore with last update

Post by Tracker Supp-Stefan »

Hello nbmani,

Most likely some of the methods you are using require elevated privileges (and we were not enforcing those correctly in the past).
You will likely need to create trustedFunction in which to execute your code and it will work once again.
Please take a look e.g. here on how to declare such a trusted function:
viewtopic.php?p=170799#p170799

Kind regards,
Stefan
nbmani
User
Posts: 17
Joined: Tue Feb 14, 2023 11:53 am

Re: My pluggin is not working anymore with last update

Post by nbmani »

I tried to do what you suggested but still having the same error: "Security prevents to use this object.."
User avatar
TrackerSupp-Daniel
Site Admin
Posts: 8278
Joined: Wed Jan 03, 2018 6:52 pm

Re: My pluggin is not working anymore with last update

Post by TrackerSupp-Daniel »

Hello, nbmani

Would it be possible for you to send us a sample of your plugin, so that we can have the dev team take a look at see what it may be?

Kind regards,
Dan McIntyre - Support Technician
Tracker Software Products (Canada) LTD

+++++++++++++++++++++++++++++++++++
Our Web site domain and email address has changed as of 26/10/2023.
https://www.pdf-xchange.com
Support@pdf-xchange.com
nbmani
User
Posts: 17
Joined: Tue Feb 14, 2023 11:53 am

Re: My pluggin is not working anymore with last update

Post by nbmani »

Sure.

Where should I send a sample?
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17670
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: My pluggin is not working anymore with last update

Post by Tracker Supp-Stefan »

Hello nbmani,

Please e-mail that to support@pdf-xchange.com with a link back to this topic also included in the mail, and we would be happy to take a look for you!

Kind regards,
Stefan
nbmani
User
Posts: 17
Joined: Tue Feb 14, 2023 11:53 am

Re: My pluggin is not working anymore with last update

Post by nbmani »

Thank you, already sent a sample.

Thank you for your help
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17670
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: My pluggin is not working anymore with last update

Post by Tracker Supp-Stefan »

Hello nbmani,

Thanks! We got your sample code, and will ask one of our colleagues from the dev team to take a look. We will post an update as soon as we have any additional news!

Kind regards,
Stefan
nbmani
User
Posts: 17
Joined: Tue Feb 14, 2023 11:53 am

Re: My pluggin is not working anymore with last update

Post by nbmani »

Hi.

I did not receive any feedback so I tried to do on my own. I have the version 10.2.1.385 and still having having the same error "An error occured: NotAllowedError: Security settings prevent access to this property or method."

What I'm doing wrong?

Code: Select all

app.addToolButton({
    cName: "ExtractPagesByKeywords",
    cLabel: "Extract Pages by Keywords to one PDF file",
    cExec: "extractPagesByKeywords()",
    cTooltext: "Extract pages containing specific keywords",
    cEnable: true,
    nPos: -1
});

var extractPagesByKeywords = app.trustedFunction(function () {
    app.beginPriv();

    try {
        var doc = this;

        var searchText = app.trustedFunction(function () {
            app.beginPriv();
            var result = app.response("Enter the text to search for (not case sensitive), separated by commas:");
            app.endPriv();
            return result;
        })();

        if (!searchText) {
            app.alert("No keyword(s) inserted.");
            return;
        }

        var searchTextGroups = searchText.split(",").map(app.trustedFunction(function (group) {
            app.beginPriv();
            var result = group.trim().split(" ");
            app.endPriv();
            return result;
        }));

        var searchResults = new Set();

        for (var i = 0; i < doc.numPages; i++) {
            app.beginPriv(); // Start of the privileged section for the loop
            var numWords = doc.getPageNumWords(i);
            var pageText = "";

            for (var j = 0; j < numWords; j++) {
                var word = doc.getPageNthWord(i, j, true);
                pageText += word + " ";
            }
            app.endPriv(); // End of the privileged section for the loop

            searchTextGroups.forEach(app.trustedFunction(function (group) {
                app.beginPriv();
                var groupFound = group.every(function (keyword) {
                    return pageText.toLowerCase().includes(keyword.toLowerCase());
                });

                if (groupFound) {
                    searchResults.add(i);
                }
                app.endPriv();
            }));
        }

        if (searchResults.size > 0) {
            app.beginPriv();
            var outputPath = doc.path.replace(/\.pdf$/, '_' + 'extracted' + '.pdf');
            var newDoc = app.newDoc();

            searchResults.forEach(app.trustedFunction(function (pageIndex) {
                app.beginPriv();
                newDoc.insertPages({
                    nPage: newDoc.numPages - 1,
                    cPath: doc.path,
                    nStart: pageIndex,
                    nEnd: pageIndex
                });
                app.endPriv();
            }));

            newDoc.deletePages(0);
            newDoc.saveAs(outputPath);
            newDoc.closeDoc(true);
            app.endPriv();

            console.println("Pages extracted and saved to file: " + outputPath);
            app.alert("Process concluded. The pages were extracted and saved in the same folder as the original file: " + outputPath, 3);
        } else {
            console.println("Text not found.");
            app.alert("None of the words entered were found in the document.");
        }
    } catch (e) {
        console.println("An error occurred: " + e);
        app.alert("An error occurred: " + e);
    }

    app.endPriv();
});
User avatar
Roman - Tracker Supp
Site Admin
Posts: 302
Joined: Sun Nov 21, 2004 3:19 pm

Re: My pluggin is not working anymore with last update

Post by Roman - Tracker Supp »

Hi nbmani,
Please try this one:

Code: Select all

app.addToolButton({
    cName: "ExtractPagesByKeywords",
    cLabel: "Extract Pages by Keywords to one PDF file",
    cExec: "extractPagesByKeywords()",
    cTooltext: "Extract pages containing specific keywords",
    cEnable: true,
    nPos: -1
});

var extractPagesByKeywords = app.trustedFunction(function () {

    try {
        var doc = this;
        app.beginPriv();
        var searchText = app.response("Enter the text to search for (not case sensitive), separated by commas:");
        app.endPriv();
        if (!searchText) {
            app.alert("No keyword(s) inserted.");
            return;
        }

        var searchTextGroups = searchText.split(",").map(group => group.trim().split(" "));

        var searchResults = new Set();

        for (var i = 0; i < doc.numPages; i++) {
            var numWords = doc.getPageNumWords(i);
            var pageText = "";

            for (var j = 0; j < numWords; j++) {
                var word = doc.getPageNthWord(i, j, true);
                pageText += word + " ";
            }
            searchTextGroups.forEach(function (group) {
                var groupFound = group.every(function (keyword) {
                    return pageText.toLowerCase().includes(keyword.toLowerCase());
                });

                if (groupFound) {
                    searchResults.add(i);
                }
            });
        }
        if (searchResults.size > 0) {
            var outputPath = doc.path.replace(/\.pdf$/, '_' + 'extracted' + '.pdf');
            app.beginPriv();                
            var newDoc = app.newDoc();
            for (const pageIndex of searchResults) {
                newDoc.insertPages({
                    nPage: newDoc.numPages - 1,
                    cPath: doc.path,
                    nStart: pageIndex,
                    nEnd: pageIndex
                });
            }
            newDoc.deletePages(0);
            newDoc.saveAs(outputPath);
	    app.endPriv();   
            newDoc.closeDoc(true);
            console.println("Pages extracted and saved to file: " + outputPath);
            app.alert("Process concluded. The pages were extracted and saved in the same folder as the original file: " + outputPath, 3);
        } else {
            console.println("Text not found.");
            app.alert("None of the words entered were found in the document.");
        }
    } catch (e) {
        console.println("An error occurred: " + e);
        app.alert("An error occurred: " + e);
    }
});
nbmani
User
Posts: 17
Joined: Tue Feb 14, 2023 11:53 am

Re: My pluggin is not working anymore with last update

Post by nbmani »

It worked!! Many thanks!!!
User avatar
TrackerSupp-Daniel
Site Admin
Posts: 8278
Joined: Wed Jan 03, 2018 6:52 pm

My pluggin is not working anymore with last update

Post by TrackerSupp-Daniel »

:)
Dan McIntyre - Support Technician
Tracker Software Products (Canada) LTD

+++++++++++++++++++++++++++++++++++
Our Web site domain and email address has changed as of 26/10/2023.
https://www.pdf-xchange.com
Support@pdf-xchange.com
Post Reply