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

Dynamically place a Static signature script

New Here ,
May 02, 2023 May 02, 2023

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;

TOPICS
Acrobat SDK and JavaScript , Windows
2.4K
Translate
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

correct answers 2 Correct answers

Community Expert , May 09, 2023 May 09, 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
...
Translate
Community Expert , May 18, 2023 May 18, 2023

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);
	
...
Translate
Community Expert ,
May 03, 2023 May 03, 2023

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

Translate
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
New Here ,
May 03, 2023 May 03, 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.

Translate
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 09, 2023 May 09, 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;
	}
}

@+

Translate
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
New Here ,
May 09, 2023 May 09, 2023

Thank you! I will test it out and report back. I really appreciate the help!

Translate
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
New Here ,
May 09, 2023 May 09, 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.

Translate
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 09, 2023 May 09, 2023

What kind of debug have you done?  Are any error messages displayed in the Console window? 

Do you even know if the "addWatermarkFromFile" code is ever run?  

 

This script has some complexity and every step needs to be looked at individually to ensure it is operating as intended. I would suggest running this code from inside the Console in pieces.  

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
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 09, 2023 May 09, 2023

With this script, the "Signature.pdf" file must be located in the same folder as the file you must sign.
Could you share a screenshot of the place where the signature must be applyed like the one below.

Capture d’écran 2023-05-09 à 19.31.19.png

FYI, the signature above has been done with my script!

@+

Translate
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
New Here ,
May 09, 2023 May 09, 2023

Here's a screenshot of what the signature line appears as. There's another signature line for the client, which we don't sign, but it is the same phrase - SIGNATURE OF: FirstName Last Name

Translate
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 09, 2023 May 09, 2023

So, you must write:

 

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

 

Capture d’écran 2023-05-09 à 20.35.18.png

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

@+

Translate
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
New Here ,
May 09, 2023 May 09, 2023

@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!

Translate
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 18, 2023 May 18, 2023

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.

@+

Translate
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
New Here ,
May 19, 2023 May 19, 2023
LATEST

@bebarthWorked beautifully! Thank you very much for your assistance!

Translate
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