C# Aloaha PDF Crypto Sample
Our customer Matthew Ciantar supplied us with an excellent C# sample package. This sample demonstrates how to use our Aloaha APIs in C#. Please see the source code below. At the bottom of this page you can also download the files.
This sample will work only with a valid evaluation key. Please contact
aloaha@wrocklage.de for such a key!
/*
* This command line applications uses Aloaha PDF Crypter
* to encrypt a PDF File with a given Public Key (Cer File).
*
* Author: Matthew Ciantar
* Date: 2006/01/20
*
* License for the Aloaha PDF Crypter is required
* Evaluation of API works without valid License
*
**/
using System;
using System.IO;
using System.Reflection;
namespace PDFCrypterTool
{
class Crypter
{
static void Main(string[] args)
{
if (args.Length == 3)
{
// Store Arguments in Local Variables
string CertificateFilePath = args[0];
string PDFSourceFilePath = args[1];
string PDFDestinationFilePath = args[2];
// Check existance of Certificate and Source File
if (File.Exists(CertificateFilePath) && File.Exists(PDFSourceFilePath))
{
Type aloahapdf = Type.GetTypeFromProgID("aloahapdf.edit");
object aloahapdfLateBound = Activator.CreateInstance(aloahapdf);
object[] Certificate = new object[1];
Certificate[0] = CertificateFilePath;
aloahapdf.InvokeMember("add_recipient", BindingFlags.Default | BindingFlags.InvokeMethod,
null, aloahapdfLateBound, Certificate );
object[] SourceFileName = new object[1];
SourceFileName[0] = PDFSourceFilePath;
object[] OriginalPDFString = new object[1];
OriginalPDFString[0] = (string)aloahapdf.InvokeMember("ReadStringFromFile", BindingFlags.Default | BindingFlags.InvokeMethod,
null, aloahapdfLateBound, SourceFileName);
string PDFString = (string)aloahapdf.InvokeMember("encrypt_pdf_string", BindingFlags.Default | BindingFlags.InvokeMethod,
null, aloahapdfLateBound, OriginalPDFString);
if (null != PDFString)
{
// Save file if encryption was succesfully
string DestinationFileName = PDFDestinationFilePath;
bool noOverwrite = false;
object[] WriteFileParams = new object[3];
WriteFileParams[0] = PDFDestinationFilePath;
WriteFileParams[1] = PDFString;
WriteFileParams[2] = noOverwrite;
aloahapdf.InvokeMember("WriteStringToFile", BindingFlags.Default | BindingFlags.InvokeMethod,
null, aloahapdfLateBound, WriteFileParams);
}
else
{
Console.Write("Failed to Encrypt file with Public Key.");
}
}
else
{
Console.Write("Invalid Files sepcified.");
Environment.ExitCode = -1;
}
}
else
{
PrintUsage();
Environment.ExitCode = -1;
}
}
public static void PrintUsage()
{
Console.Write("Usage: " + System.Environment.GetCommandLineArgs()[0] + " [Certificate File] [Soruce PDF] [Encrypted PDF]");
}
}
}
Encrypt_PDF.cs (2,51 KB)
Executable.zip (61,80 KB)
Aloaha Software / Knowledge Base / PDF SDK / PDF Encryption API / c# crypto sample