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)

Please ask...

Although we really tried hard, there are always questions left open. But perhaps somebody else had already put the same question?
Please check!
  • Is it possible to encrypt with certificate using the AES algorithm? Could you provide a sample?
    Currently that is not possible. But in case you really need it you can send a feature request to info@aloaha.com
    Answered 23.04.2010
  • Is it possible to encrypt with certificate using the AES algorithm? Could you provide a sample?
  • Is it possible to restrict all permissions (including content copying and page extraction) on a pdf that has been encrypted using a certificate? Could you provide sample code?
  • Hi,

    It is possible to use the api to decrypt pdf files? Or the api is only for encrypt?

If not, please do not hesitate to send us your question.
We would be glad to answer it.
PDF SDKPDF Signature APICertificate PropertiesSample CodeText StampsPDF AttachmentsImage 2 PDFPDF to Multipage TIFFAppend PDFInsert Blank PageRender ImageStamp PDF with PDFDraw PDF as LetterheadApply LetterheadPDF BackgroundsAdd NotesURL HotspotSecure PDF ViewerPDF OptimizerPDF Encryption APIc# crypto sample40/128 Bit encryptionLDAP/Active Directory EncryptionLDAP encryption SamplePDF Printing APIPDF BookmarksPDF BarcodingMerge PDF DocumentsSample APIsResize PDFCrop PDF PagePDF InformationPDF2TXTNormalize PagesDirect PDF ConversionPDF Form Saver SDKSmart Card APIAloaha Web ServicesAloaha Print MonitorAloaha Remote ConfigurationWebDAVLDAP ClientProgramming Office MacrosIIS Event Sinks