Friday, May 30, 2008

Fun with javascript

I was working with javascript and found myself in weird situation. I banged my head several minutes to resolve the issue which would have resolved in a second.
Try to follow below steps:

Create one html page and call this javascript


function fnCall(){
for(x = 0; x < 10; x++){
fnCall1();
alert(x);
}
}

function fnCall1(){
for(x = 0; x < 10; x++){
//your code
}

}

If you go through below snippet of code you must be wondering why alert is coming only once, when it is expected to come 10 times

function fnCall(){
for(x = 0; x < 10; x++){
fnCall1();
alert(x);
}
}

function fnCall1(){
for(x = 0; x < 10; x++){
//your code
}

Above behavior is normal because x is global. If the variable doesn’t have var javascript treats it as a global variable.
Since the x doesn’t have var the variable is global. When fnCall1 is called, loop increments that value of x to 10 making fnCall to run only once.

To resolve the issue, put var in front of x

for(var x = 0; x < 10; x++)

Sunday, May 18, 2008

Add digital signature in pdf using c#

Few days back, one of our client wanted to add digital signature in pdf using window application. Windows application which will run after scheduled time need to add the the signature. Windows application was written in c#.

To achieve above goal, Adobe Acrobat professional and Adobe SDK is required.

Before writing c# code we need to following configuration setting

copy sdkAddSignature.js from Adobe\Acrobat 8 SDK\Version2\JavaScriptSupport\samples\outsidepdf\addsignature to adobeprofession javascript folder

copy DrTest.pft from Adobe\Acrobat 8 SDK\Version2\JavaScriptSupport\samples\outsidepdf\addsignature to C:\ (anywhere you like to)

Add reference of Interop.Acrobat.dll from Acrobat SDK

we can modify the location of signature on pdf or even hide it modifying the AddSignatureField function in sdkAddSignature.js.

Here is the final c# code which will add signature on pdf

Type AcrobatCAcroAppType;
AcrobatCAcroAppType = Type.GetTypeFromProgID("AcroExch.app");
Acrobat.CAcroApp gapp = (Acrobat.CAcroApp)Activator.CreateInstance(AcrobatCAcroAppType);

Type AcrobatPDDocType;
AcrobatPDDocType = Type.GetTypeFromProgID("AcroExch.PDDoc");
Acrobat.CAcroPDDoc gpddoc = (Acrobat.CAcroPDDoc)Activator.CreateInstance(AcrobatPDDocType);

object jso ;

if(gpddoc.Open("c:\\s.pdf"))
{
jso = gpddoc.GetJSObject();
object[] param = new object[1];
param[0] = "testpassword";
object con = jso.GetType().InvokeMember("SetUserPassword",
BindingFlags.InvokeMethod , null, jso, param);

param[0] = "C:\\DrTest.pfx";

con = jso.GetType().InvokeMember("SetUserDigitalIDPath",
BindingFlags.InvokeMethod , null, jso, param);

param[0] = jso;

con = jso.GetType().InvokeMember("AddSignature",
BindingFlags.InvokeMethod , null, jso, param);

Happy Coding

:)

Site Meter