Skip to main content
Participating Frequently
May 2, 2023
Answered

Dynamically place a Static signature script

  • May 2, 2023
  • 2 replies
  • 2772 views

I have multiple PDFs requiring an e-signature (not digital). However, the signature line isn't consistent across all PDFs. I need to search the PDF for a specific phrase, then insert the e-signature above that phrase. I have created a Action to watermark the signature, but since the signature line isn't consistent, this doesn't work. I understand this will take scripting to do and I've tried to find answers, however, every script I've found is applied to a static location and not a dynamic location. This script looks extremely promising (though it uses a digital signature, not an e-signature). Here's what I have so far:

var phrase = "Signature of: Name";
var currentPage = this.pageNum;
var found = this.getPageNthWord(currentPage, 0, phrase);

if (found.length > 0) {
var bbox = this.getPageNthWordQuads(currentPage, 0)[found[0]];
var x = bbox[0];
var y = bbox[1];

var signatureImage = this.addWatermarkFromFile("FILE LOCATION", { opacity: 1, onTop: true, bUseAsBackground: false, nRotation: 0 });
signatureImage.page = currentPage;
signatureImage.display = display.visible;
signatureImage.rect = [x, y - 20, x + 60, y];
signatureImage.borderWidth = 0;

This topic has been closed for replies.
Correct answer bebarth

@bebarthit worked! I had changed it to SIGNATURE and OF, but added more conditions that broke it. Apologies for my ignorance getting in the way of your beautiful coding. Thank you for your help!


Hi,

Use this new script if you want to add the signature every other time when it meets "SIGNATURE OF".

var found=0;
for (var p=0; p<this.numPages; p++) {
	var numWords=this.getPageNumWords(p);
	for (var i=0; i<numWords-2; i++) {
		if (this.getPageNthWord(p,i,true)=="SIGNATURE" && this.getPageNthWord(p,i+1,true)=="OF") {
			found++;
			if (found%2!=0) {
				var q=this.getPageNthWordQuads(p,i+2);
				var m=(new Matrix2D).fromRotated(this,p);
				var mInv=m.invert();
				var r=mInv.transform(q);
				var r=r.toString();
				var r=r.split(",");
				this.addWatermarkFromFile({
					cDIPath: this.path.substring(0,this.path.length-this.documentFileName.length)+"Signature.pdf",
					nSourcePage: 0,
					nStart: p,
					nEnd: p,
					bOnTop: false,
					bOnScreen: true,
					bOnPrint: true,
					nHorizAlign: app.constants.align.left,
					nVertAlign: app.constants.align.bottom,
					nHorizValue: r[0],
					nVertValue: r[1],
					bPercentage: false,
					nScale: 0.85,
					bFixedPrint: false,
					nRotation: 0,
					nOpacity: 0.75
				});
			}
		}
	}
}
if (found) this.saveAs(this.path.replace(/.pdf$/i," \(With Signature\).pdf"));

Let me know.

@+

2 replies

bebarth
Community Expert
Community Expert
May 9, 2023

Hi,
Sorry if I'm late for this answer.
Here is a script you can use for doing what you are looking for. It places the "Signature.pdf" above the top of the name. You will have to move it a bit if you wish. let me know if you need help.

var found=0;
for (var p=0; p<this.numPages; p++) {
	var numWords=this.getPageNumWords(p);
	for (var i=0; i<numWords-2; i++) {
		if (this.getPageNthWord(p,i,true)=="Signature" && this.getPageNthWord(p,i+1,true)=="of") {
			var q=this.getPageNthWordQuads(p,i+2);
			var m=(new Matrix2D).fromRotated(this,p);
			var mInv=m.invert();
			var r=mInv.transform(q);
			var r=r.toString();
			var r=r.split(",");
			this.addWatermarkFromFile({
				cDIPath: this.path.substring(0,this.path.length-this.documentFileName.length)+"Signature.pdf",
				nSourcePage: 0,
				nStart: p,
				nEnd: p,
				bOnTop: false,
				bOnScreen: true,
				bOnPrint: true,
				nHorizAlign: app.constants.align.left,
				nVertAlign: app.constants.align.bottom,
				nHorizValue: r[0],
				nVertValue: r[1],
				bPercentage: false,
				nScale: 0.85,
				bFixedPrint: false,
				nRotation: 0,
				nOpacity: 0.75
			});
			found++;
			break;
		}
	}
	if (found) {
		this.saveAs(this.path.replace(/.pdf$/i," \(With Signature\).pdf"));
		break;
	}
}

@+

Participating Frequently
May 9, 2023

Unfortunately, the signature is not appearing. I named my file Signature.pdf and placed it in the Watermark folder. I also placed Signature.pdf in the location of the PDFs that need the signature added. Additionally, I created a watermark and named it Signature, still no luck.

Participating Frequently
May 9, 2023

So, you must write:

 

...
if (this.getPageNthWord(p,i,true)=="SIGNATURE" && this.getPageNthWord(p,i+1,true)=="OF") {
...

 

If that doesn't still work, let me know if you have a message in the console window.

@+


@bebarthit worked! I had changed it to SIGNATURE and OF, but added more conditions that broke it. Apologies for my ignorance getting in the way of your beautiful coding. Thank you for your help!

try67
Community Expert
Community Expert
May 3, 2023

None of these properties apply to a watermark. Are you trying to add a signature field, or a watermark? It's not clear.

Participating Frequently
May 3, 2023

I understand that my code doesn't apply the watermark or stamp of an e-signature. I do not want to create a signature field. I have to identify the correct coordinates first, then apply the watermark or stamp of the e-signature slightly above the identified coordinates. This is to automate signing multiple PDFs at one time.

 

The length of the PDF will vary (sometimes it's only 3 pages and sometimes it's 7 pages or more), the signature needs to be applied on page 1 and then for longer documents page 4, 8, etc. That's why I have to find where the signature line is since it moves up/down slightly on each page of the PDF and on different PDFs.

 

Also, I already tried using the Action Wizard to apply the signature as a watermark. That's how I discovered that the signature line isn't at the same coordinates for all of the PDFs to which I will apply the signature.