Thứ Hai, 15 tháng 11, 2010

Develop a File IO ActiveX control in Microsoft .NET

Development

1. Use this guide to create your own ActiveX control

Writing an ActiveX Control in .NET

2. Design your ActiveX as picture bellow:

image

3. Use following code behind

namespace FileIOActiveXControl
{
public partial class WriteToClientControl : UserControl
{
public WriteToClientControl()
{
InitializeComponent();
}

private void btnWrite_Click(object sender, EventArgs e)
{
WriteLog(@"c:\temp\log.txt", txtUserInput.Text);
}

public void WriteLog(string fileName, string content)
{
try
{
StreamWriter log = new StreamWriter(fileName, true);
log.WriteLine(content);
log.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}

4. Create TestActiveX.htm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<hr>
Please note that you MUST load this HTML document from your local IIS, or from a TRUSTED SITE or the control will not load properly on the page due to security policy.
<br>
To fix this:<br>
<li>copy the HTM file and the DLL into your c:\inetpub\wwwroot folder (or applicable IIS root directory)
<li>load the document in your browser with the url: http://localhost/TestActiveX.htm
<hr>
<font face=arial size=1>
<OBJECT id="fileIOActiveX" name="fileIOActiveX" classid="http:FileIOActiveXControl.dll#FileIOActiveXControl.WriteToClientControl" width="450" height="100" VIEWASTEXT>
</OBJECT>
</font>
<hr>
</body>
</script>
</html>

Deployment


1. Copy FileIOActiveXControl.dll and TestActiveX.htm to the same folder


2. Go to IIS to create a new web site and point to that folder


3. Use another computer to test your control.


If you control cannot load to page like picture bellow.


image


a. Try to add the site to Trusted Zone in IE


b. Make sure .dll and .htm in the same folder


If your control is loaded correctly, you will see:


image


4. Enter some text to the textbox then click Write To File button. You may see:


“Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.”


image


To fix this. You have to configure Code Access Security for Microsoft .NET on client. Run following command to fix this issue.


“C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\caspol.exe -quiet -machine -chggroup Trusted_Zone FullTrust”


image


And  (MANDATORY if your client is running OS 64 bit)


“C:\WINDOWS\Microsoft.NET\Framework64\v2.0.50727\caspol.exe -quiet -machine -chggroup Trusted_Zone FullTrust”


image


5. Stop IE –> reload the test page –> input some text –> click “Write To File” button again now. There will not have any error message appear. Check the log file from C:\temp\log.txt


Success.

Thứ Sáu, 5 tháng 11, 2010

Add Digital Signature to a PDF programmatically using Adobe SDK and Microsoft .NET

Requirement:

- Adobe Professional Installed

- Adobe SDK downloaded

- Microsoft Visual Studio

- Valid Certificate file (.pfx). It means the .pfx file not yet expired.

Setup environment:

- Copy sdkAddSignature.js from SDK to Acrobat\Javascripts folder

- Optional: Create Your Own Test Certificate

Code C#

            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("d:\\temp\\s.pdf"))
{
jso = gpddoc.GetJSObject();
object[] param = new object[1];

param[0] = "c:\\CATest.pfx";
object con = jso.GetType().InvokeMember("SetUserDigitalIDPath",
BindingFlags.InvokeMethod, null, jso, param);

param[0] = "testpassword";
con = jso.GetType().InvokeMember("SetUserPassword",
BindingFlags.InvokeMethod, null, jso, param);

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

Code VB:

        Dim gapp As Acrobat.CAcroApp
Dim gpddoc As Acrobat.CAcroPDDoc
Dim jso As Object
gapp = CreateObject("acroexch.app")
gpddoc = CreateObject("acroexch.pddoc")

If gpddoc.Open("d:\temp\s.pdf") Then
jso = gpddoc.GetJSObject()
jso.SetUserPassword("testpassword")
jso.SetUserDigitalIDPath("/C/CATest.pfx") //remember this path
jso.AddSignature(jso)
gapp.Show()
End If

basquang@hotmail.com