Friday, January 2, 2015

Adobe JavaScript to sign PDF files

Think that you need to sign a PDF file and add the signed date, you need a quick method to add a digital signature and some text with the date in the PDF file.
To avoid using Acrobat or Photoshop manually to import the image signature and the text label for the date, could be great to make a little script to sign and add variables with the current date.

If you work with Adobe CSx, you will find some smaple scripts on path that will allow you code a litle script to open source PDF file, import a external image with your signature and add labels to source PDF with current date or some else:
<install path>/Adobe Photoshop CSx/scripting/samples/JavaScript...
You will find next files:

Lets Go to see my Script:

( I edit sample javascript file "OpenDocument.jsx" for make mi own one)

// Copyright 2002-2007. Adobe Systems, Incorporated. All rights reserved. // Open a Photoshop document located in the Photoshop samples folder on the Photoshop CD. // You must first create a File object to pass into the open method.
// enable double clicking from the Macintosh Finder or the Windows Explorer #target photoshop // in case we double clicked the file app.bringToFront(); // on localized builds we pull the $$$/Strings from a .dat file, see documentation for more details $.localize = true; // units for move and scale imported image var strtRulerUnits = app.preferences.rulerUnits; var strtTypeUnits = app.preferences.typeUnits; app.preferences.rulerUnits = Units.INCHES; app.preferences.typeUnits = TypeUnits.POINTS; // Display File Open Dialog filePath = File.openDialog(); var docRef = open( File( filePath )); // suppress all dialogs app.displayDialogs = DialogModes.NO; var textColor = new SolidColor; = 0; = 0; = 0; // LAYER 1 var doc = open( File("<path to signature image>") ); //select all activeDocument.selection.selectAll() //copy image activeDocument.selection.copy(); //close that document without saving app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); // select the source image activeDocument = docRef; //paste app.activeDocument.paste(); // move var startRulerUnits = app.preferences.rulerUnits; app.preferences.rulerUnits = Units.PIXELS; var cm = 100;//activeDocument.resolution/2.45; var LB = activeDocument.activeLayer.bounds;
activeDocument.activeLayer.translate(-200,1930); app.preferences.rulerUnits = startRulerUnits; // LAYER 2 var myDate = new Date(); myDate = myDate.getDate()+"/"+(myDate.getMonth()+1)+"/"+ myDate.getFullYear(); var newTextLayer = docRef.artLayers.add(); newTextLayer.kind = LayerKind.TEXT; newTextLayer.textItem.contents = myDate; newTextLayer.textItem.position = Array(0.15, 8.6); newTextLayer.textItem.size = 18; newTextLayer.textItem.color = textColor; // SAVE NEW PDF SIGNED var savefname = "<path to new signed pdf files>";
str = filePath.toString(); saveas = str.substr(1,str.length-5) + "signed"; filename = saveas.split("/"); longi = filename.length - 1; savefname = savefname + filename[longi]; var pngFile = new File(savefname ); pngSaveOptions = new PDFSaveOptions(); pngSaveOptions.embedColorProfile = true; pngSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE; pngSaveOptions.matte = MatteType.NONE; pngSaveOptions.quality = 1; activeDocument.saveAs(pngFile, pngSaveOptions, false, Extension.LOWERCASE); app.preferences.rulerUnits = strtRulerUnits; app.preferences.typeUnits = strtTypeUnits;
docRef = null; textColor = null; newTextLayer = null; app.activeDocument.close()

Sources needed:

No comments:

Post a Comment