doc.addWatermarkFromFile() - Possible bug in Javascript implementation

Forum for the PDF-XChange Editor - Free and Licensed Versions

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
nuflux
User
Posts: 21
Joined: Tue May 24, 2016 11:59 pm
Location: New York, NY

doc.addWatermarkFromFile() - Possible bug in Javascript implementation

Post by nuflux »

Hi,

I created a simple javascript to flatten and scale an annotated document so that I can send it out to another party. The scaling is key as I usually annotate a resized (scaled up) version of the document and then shrink it back down once the annotations are added. Hence, I am using the doc.addWatermarkFromFile() method to scale down the page content and flatten in one fell swoop.

The script works great except that PDF X-Change seems to ignore the nSourcePage parameter when the doc.addWatermarkFromFile() method is called, so it always pulls only the first page from the source file instead of pulling page 2, page 3, and successive pages. This is true even when I put in a number instead of a variable. Is this perhaps a bug in PDF X-Change's implementation of the method, or am I doing something wrong?

Here is the code:

Code: Select all

var currentDoc = this;
for(var i=0;i<currentDoc.numPages;i++){
	if(!i){
		// Create new document
		var flatDoc = app.newDoc();
	}else
	{
		// Add page if document already created
		flatDoc.newPage(i);
	}
	flatDoc.addWatermarkFromFile({
		cDIPath: currentDoc.path,
		nSourcePage: i,
		nStart: i,
		nScale: -1
	});
}
ETA: I just ran the javascript in Acrobat DC Pro and it worked properly, so this does appear to be an issue with PDF X-Change's implementation of the method.

Thanks,
David
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17820
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: doc.addWatermarkFromFile() - Possible bug in Javascript implementation

Post by Tracker Supp-Stefan »

Hello David,

I have passed this to a colleague in the dev team who works on the JS implementation, and they will take a look soon!

Kind regards,
Stefan
User avatar
Ivan - Tracker Software
Site Admin
Posts: 3549
Joined: Thu Jul 08, 2004 10:36 pm
Location: Vancouver Island - Canada
Contact:

Re: doc.addWatermarkFromFile() - Possible bug in Javascript implementation

Post by Ivan - Tracker Software »

Yes, for some reason it was ignored. Fixed now. The fix will be included in the next build.
Tracker Software (Project Director)

When attaching files to any message - please ensure they are archived and posted as a .ZIP, .RAR or .7z format - or they will not be posted - thanks.
nuflux
User
Posts: 21
Joined: Tue May 24, 2016 11:59 pm
Location: New York, NY

Re: doc.addWatermarkFromFile() - Possible bug in Javascript implementation

Post by nuflux »

Terrific, thank you both!
User avatar
TrackerSupp-Daniel
Site Admin
Posts: 8436
Joined: Wed Jan 03, 2018 6:52 pm

doc.addWatermarkFromFile() - Possible bug in Javascript implementation

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
nuflux
User
Posts: 21
Joined: Tue May 24, 2016 11:59 pm
Location: New York, NY

Re: doc.addWatermarkFromFile() - Possible bug in Javascript implementation

Post by nuflux »

Hi guys,

I have an update and a follow-up question here. The update is that the doc.addWatermarkFromFile() method is now working properly after the implementation bug was fixed. Yay! Thanks again.

Now that I have gotten the script to work, I created a custom toolbar button to run the script in PDF X-Change. Below is the code that I have saved in the .js file in my "JavaScripts" folder. However, a curious thing is happening. The doc.newPage() method does not seem to work when the script is executed by pressing the button, so all the "watermarks" are being stamped on one page. I thought this might be a permissions issue, so I tried calling the function from within app.trustedFunction() and using app.beginPriv() to get around it (see code below). Still no dice. Any idea what I might be doing wrong?

Thanks,
David

Code: Select all

app.addToolButton({
    cName: "Package and Save",
    cLabel: "Package and Save",
    cExec: "flattenAndScale()",
    cTooltext: "Flatten, scale and save the document for distribution to third parties",
    cEnable: true,
    nPos: -1
});

flattenAndScale = app.trustedFunction(function()
{
	app.beginPriv();
	var currentDoc = this;
	for(var i=0;i<currentDoc.numPages;i++){
		if(!i){
			// Create new document
			var flatDoc = app.newDoc();
		}else
		{
			// Add page if document already created
			flatDoc.newPage(i);
		}
		flatDoc.addWatermarkFromFile({
			cDIPath: currentDoc.path,
			nSourcePage: i,
			nStart: i,
			nScale: -1
		});
	}
	app.endPriv();
});
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17820
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: doc.addWatermarkFromFile() - Possible bug in Javascript implementation

Post by Tracker Supp-Stefan »

Hello nuflux,

The new document does get created by your script - so the trusted context is working to some extent, but it would seem not fully, as the same script works perfectly fine from the console. However the console is obviously with different privileges.
I will ask our devs to take a look and advise further!

Kind regards,
Stefan
nuflux
User
Posts: 21
Joined: Tue May 24, 2016 11:59 pm
Location: New York, NY

Re: doc.addWatermarkFromFile() - Possible bug in Javascript implementation

Post by nuflux »

Thanks Stefan, I look forward to your further feedback!
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17820
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: doc.addWatermarkFromFile() - Possible bug in Javascript implementation

Post by Tracker Supp-Stefan »

Hello nuflux,

Over the weekend I got some feedback from one of our devs.
They told me that they will investigate the situation, and it might be a bug on our end that the trustedFunction works with newDoc() but does not work with newPage(). As soon as I have more information I will post again here!

Kind regards,
Stefan
nuflux
User
Posts: 21
Joined: Tue May 24, 2016 11:59 pm
Location: New York, NY

Re: doc.addWatermarkFromFile() - Possible bug in Javascript implementation

Post by nuflux »

Thanks for the update!
nuflux
User
Posts: 21
Joined: Tue May 24, 2016 11:59 pm
Location: New York, NY

Re: doc.addWatermarkFromFile() - Possible bug in Javascript implementation

Post by nuflux »

Hey Stefan, any update on this from the dev team?

Thanks,
David
User avatar
Ivan - Tracker Software
Site Admin
Posts: 3549
Joined: Thu Jul 08, 2004 10:36 pm
Location: Vancouver Island - Canada
Contact:

Re: doc.addWatermarkFromFile() - Possible bug in Javascript implementation

Post by Ivan - Tracker Software »

Fixed in the upcoming build.
Tracker Software (Project Director)

When attaching files to any message - please ensure they are archived and posted as a .ZIP, .RAR or .7z format - or they will not be posted - thanks.
nuflux
User
Posts: 21
Joined: Tue May 24, 2016 11:59 pm
Location: New York, NY

Re: doc.addWatermarkFromFile() - Possible bug in Javascript implementation

Post by nuflux »

Great news, thank you!
Post Reply