Skip to main content

Code samples for PDF Security

Secure PDFs with Pdftools SDK

This product has been replaced by the Pdftools SDK.

With PDF Security, you can digitally sign multiple PDF documents using online signing services and local certificates. Here you'll find some of some examples of how to integrate the code in your development.

Encryption

Decrypt a PDF document

1// Create secure object
2pSecure = PdfSecureCreateObject();
3
4// Decrypt input file
5if (!PdfSecureOpen(pSecure, szInputPath, szPassword))
6{
7    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
8    iReturnValue = 1;
9    goto cleanup;
10}
11
12// Save file
13if (!PdfSecureSaveAs(pSecure, szOutputPath, _T(""), _T(""), ePermNoEncryption, 0, _T(""), _T("")))
14{
15    _tprintf(_T("Unable to save document %s. %s (ErrorCode: 0x%08x).\n"), szOutputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
16    iReturnValue = 1;
17    goto cleanup;
18}
19
20// Cleanup
21PdfSecureClose(pSecure);
22
Download code sample
1// Create secure object
2using (Secure secure = new Secure())
3{
4    // Decrypt input file
5    if (!secure.Open(inputPath, password))
6        throw new Exception(String.Format("Input file {0} cannot be opened. " + 
7            "{1} (ErrorCode: 0x{2:x}).", inputPath, secure.ErrorMessage, secure.ErrorCode));
8
9    // Save file
10    if (!secure.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption, 0, "", ""))
11        throw new Exception(String.Format("Unable to save document {0}. {1} (ErrorCode: 0x{2:x}).",
12            outputPath, secure.ErrorMessage, secure.ErrorCode));
13
14    // Cleanup
15    secure.Close();
16}
17
Download code sample
1// Create secure object
2secure = new Secure();
3
4// Decrypt input file 
5if (!secure.open(inputPath, password))
6    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).", 
7            inputPath, secure.getErrorMessage(), secure.getErrorCode()));
8
9// Save file
10if (!secure.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption, 0, "", ""))
11    throw new IOException(String.format("Unable to save the document %s. %s (ErrorCode: 0x%08x).", 
12            outputPath, secure.getErrorMessage(), secure.getErrorCode()));
13
14// Cleanup
15secure.close();
16
Download code sample

Encrypt a PDF document

1// Create secure object
2pSecure = PdfSecureCreateObject();
3
4// Decrypt input file
5if (!PdfSecureOpen(pSecure, szInputPath, _T("")))
6{
7    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
8    iReturnValue = 1;
9    goto cleanup;
10}
11
12// Enable encryption
13PdfSecureSetForceEncryption(pSecure, 1);
14
15// Encyrpt file
16if (!PdfSecureSaveAs(pSecure, szOutputPath, szUserPassword, szOwnerPassword, ePermDigitalPrint | ePermPrint, 128, _T("V2"), _T("V2")))
17{
18    _tprintf(_T("Unable to encrypt document %s. %s (ErrorCode: 0x%08x).\n"), szOutputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
19    iReturnValue = 1;
20    goto cleanup;
21}
22
23// Cleanup
24PdfSecureClose(pSecure);
25
Download code sample
1// Create secure object
2using (Secure secure = new Secure())
3{
4    // Open input file
5    if (!secure.Open(inputPath, ""))
6        throw new Exception(String.Format("Input file {0} cannot be opened. " + 
7            "{1} (ErrorCode: 0x{2:x}).", inputPath, secure.ErrorMessage, secure.ErrorCode));
8
9    // enable encryption 
10    secure.ForceEncryption = true;
11
12    // Encrypt file
13    if (!secure.SaveAs(outputPath, userPassword, ownerPassword, PDFPermission.ePermDigitalPrint | 
14        PDFPermission.ePermPrint, 128, "V2", "V2"))
15        throw new Exception(String.Format("Unable to encrypt document {0}. " + 
16            "{1} (ErrorCode: 0x{2:x}).", outputPath, secure.ErrorMessage, secure.ErrorCode));
17
18    // Cleanup
19    secure.Close();
20}
21
Download code sample
1// Create secure object
2secure = new Secure();
3
4// Open input file 
5if (!secure.open(inputPath, ""))
6    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
7            inputPath, secure.getErrorMessage(), secure.getErrorCode()));
8
9// enable encryption
10secure.setForceEncryption(true);
11
12// Encrypt file
13if (!secure.saveAs(outputPath, userPassword, ownerPassword, 
14        NativeLibrary.PERMISSION.ePermDigitalPrint | NativeLibrary.PERMISSION.ePermPrint, 
15        128, "V2", "V2"))
16    throw new IOException(String.format("Unable to save the document %s. %s (ErrorCode: 0x%08x).",
17            outputPath, secure.getErrorMessage(), secure.getErrorCode()));
18
19// Cleanup
20secure.close();
21
Download code sample

Signing

Mass signing of PDF documents

1using (SessionPool pool = new SessionPool())
2{
3    List<Task> allTasks = new List<Task>();
4    foreach (string inputPath in Directory.GetFiles(inputDir, "*.pdf", SearchOption.AllDirectories))
5    {
6        // Create output directory if it doesn't exist
7        string outputPath = inputPath.Replace(inputDir, outputDir);
8        Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
9
10        // Start signing task
11        Task t = Task.Factory.StartNew(() => Sign(pool, inputPath, outputPath, certificate));
12        allTasks.Add(t);
13    }
14    Task.WaitAll(allTasks.ToArray());
15}
16
1static void Sign(SessionPool pool, string inputPath, string outputPath, string certificate)
2{
3    Secure secure = null;
4    try
5    {
6        // Get session from pool
7        secure = pool.GetSession();
8
9        // Open input file
10        if (!secure.Open(inputPath, ""))
11            throw new Exception(String.Format("Input file {0} cannot be opened. {1} (ErrorCode: 0x{2:x}).",
12                inputPath, secure.ErrorMessage, secure.ErrorCode));
13
14        // Create signature object
15        using (Signature signature = new Signature())
16        {
17            signature.Name = certificate;
18            secure.AddSignature(signature);
19
20            // Sign document
21            if (!secure.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption, 0, "", ""))
22                throw new Exception(String.Format("Unable to sign document {0}. {1} (ErrorCode: 0x{2:x}).",
23                    inputPath, secure.ErrorMessage, secure.ErrorCode));
24        }
25
26        secure.Close();
27        Console.WriteLine("Document {0} signed successfully.", inputPath);
28    }
29    catch (Exception ex)
30    {
31        Console.WriteLine(ex.Message);
32    }
33    finally
34    {
35        if (secure != null)
36            pool.PutSession(secure);
37    }
38}
1class SessionPool : IDisposable
2{
3    private ConcurrentBag<Secure> sessions = new ConcurrentBag<Secure>();
4
5    public Secure GetSession()
6    {
7        Secure session;
8        if (sessions.TryTake(out session))
9            return session;
10        else
11            session = new Secure();
12
13        if (!session.BeginSession(""))
14        {
15            session.Dispose();
16            throw new Exception(String.Format("Unable to connect to windows cryptographic provider. " + 
17                "{0} (ErrorCode: 0x{1:x}).", session.ErrorMessage, session.ErrorCode));
18        }
19        return session;
20    }
21
22    public void PutSession(Secure session)
23    {
24        sessions.Add(session);
25    }
26
27    public void Dispose()
28    {
29        try
30        {
31            int sessionCount = 0;
32            Secure session;
33            while (sessions.TryTake(out session))
34            {
35                session.EndSession();
36                session.Dispose();
37                sessionCount++;
38            }
39            Console.WriteLine("Closed {0} sessions.", sessionCount);
40        }
41        finally
42        {
43            Secure.Terminate();
44        }
45    }
46}
Download code sample

Sign a PDF using Windows Cryptographic Provider

1// Create secure object
2pSecure = PdfSecureCreateObject();
3
4// Open input file
5if (!PdfSecureOpen(pSecure, szInputPath, _T("")))
6{
7    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
8    iReturnValue = 1;
9    goto cleanup;
10}
11
12// Begin session with Windows Cryptographic Provider
13if (!PdfSecureBeginSession(pSecure, _T("")))
14{
15    _tprintf(_T("Unable to connect to Windows Cryptographic Provider. %s (ErrorCode: 0x%08x).\n"), PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
16    iReturnValue = 1;
17    goto cleanup;
18}
19
20// Create signature object
21pSignature = PdfSignatureCreateObject();
22
23PdfSignatureSetName(pSignature, szCertificate);
24PdfSecureAddSignature(pSecure, pSignature);
25
26// Sign document
27if (!PdfSecureSaveAs(pSecure, szOutputPath, _T(""), _T(""), ePermNoEncryption, 0, _T(""), _T("")))
28{
29    _tprintf(_T("Unable to sign document %s. %s (ErrorCode: 0x%08x).\n"), szOutputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
30    iReturnValue = 1;
31    goto cleanup;
32}
33
34// Cleanup
35PdfSecureClose(pSecure);
36
Download code sample
1// Create secure object
2using (Secure secure = new Secure())
3{
4    // Open input file
5    if (!secure.Open(inputPath, ""))
6        throw new Exception(String.Format("Input file {0} cannot be opened. " + 
7            "{1} (ErrorCode: 0x{2:x}).", inputPath, secure.ErrorMessage, secure.ErrorCode));
8
9    // Begin session with Windows Cryptographic Provider
10    if (!secure.BeginSession(""))
11        throw new Exception(String.Format("Unable to connect to Windows Cryptographic Provider. " + 
12            "{0} (ErrorCode: 0x{1:x}).", secure.ErrorMessage, secure.ErrorCode));
13
14    // Create signature object
15    using (Signature signature = new Signature())
16    {
17        signature.Name = certificate;
18        secure.AddSignature(signature);
19
20        // Sign document
21        if (!secure.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption, 0, "", ""))
22            throw new Exception(String.Format("Unable to sign document {0}. " + 
23                "{1} (ErrorCode: 0x{2:x}).", outputPath, secure.ErrorMessage, secure.ErrorCode));
24    }
25
26    // Cleanup
27    secure.Close();
28    secure.EndSession();
29}
30
Download code sample
1// Create secure object
2secure = new Secure();
3
4// Open input file 
5if (!secure.open(inputPath, ""))
6    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).", 
7            inputPath, secure.getErrorMessage(), secure.getErrorCode()));
8
9// Begin session with Windows Cryptographic Provider
10if (!secure.beginSession("")) 
11    throw new IOException(String.format("Unable to connect to Windows Cryptographic Provider. " + 
12            "%s (ErrorCode: 0x%08x).", secure.getErrorMessage(), secure.getErrorCode()));
13
14// Create signature object
15signature = new Signature();
16
17signature.setName(certificate);
18secure.addSignature(signature);
19
20// Sign document
21if (!secure.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption, 0, "", ""))
22    throw new IOException(String.format("Unable to sign document %s. %s (ErrorCode: 0x%08x).", 
23            outputPath, secure.getErrorMessage(), secure.getErrorCode()));
24
25// Cleanup
26secure.close();
27secure.endSession();
28
Download code sample

Sign a PDF using DigiCert-QuoVadis sealsign

1// Create secure object
2pSecure = PdfSecureCreateObject();
3
4// Open input file
5if (!PdfSecureOpen(pSecure, szInputPath, _T("")))
6{
7    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
8    iReturnValue = 1;
9    goto cleanup;
10}
11
12// Required: unique name of the accountspecified on the server.
13PdfSecureSetSessionPropertyString(pSecure, _T("Identity"), _T("Rigora"));
14// Required: identifies the signature specifications by a unique name.
15PdfSecureSetSessionPropertyString(pSecure, _T("Profile"), _T("Default"));
16// Required: password which secures the access to the account.
17PdfSecureSetSessionPropertyString(pSecure, _T("secret"), _T("NeE=EKEd33FeCk70"));
18// Required: helps to separate access and to create better statistics.
19PdfSecureSetSessionPropertyString(pSecure, _T("clientId"), _T("3949-4929-3179-2818"));
20// Required: activates the signing key.
21PdfSecureSetSessionPropertyString(pSecure, _T("pin"), _T("123456"));
22// Optional: default value "SHA-256"
23PdfSecureSetSessionPropertyString(pSecure, _T("MessageDigestAlgorithm"), _T("SHA-256"));
24
25// Begin session using DigiCert-QuoVadis Sealsign (demo version)
26if (!PdfSecureBeginSession(pSecure, _T("https://services.sealsignportal.com/sealsign/ws/BrokerClient")))
27{
28    _tprintf(_T("Unable to connect to DigiCert-QuoVadis Sealsign. %s (ErrorCode: 0x%08x).\n"), PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
29    iReturnValue = 1;
30    goto cleanup;
31}
32
33// Create signature object
34pSignature = PdfSignatureCreateObject();
35
36// Add signature
37// Required, name of the signer
38PdfSignatureSetName(pSignature, _T("Rigora"));
39PdfSecureAddSignature(pSecure, pSignature);
40
41// Sign document
42if (!PdfSecureSaveAs(pSecure, szOutputPath, _T(""), _T(""), ePermNoEncryption, 0, _T(""), _T("")))
43{
44    _tprintf(_T("Unable to sign document %s. %s (ErrorCode: 0x%08x).\n"), szOutputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
45    iReturnValue = 1;
46    goto cleanup;
47}
48
49// Cleanup
50PdfSecureClose(pSecure);
51PdfSecureEndSession(pSecure);
52
Download code sample
1// Create secure object
2using (Secure secure = new Secure())
3{
4    // Open input file
5    if (!secure.Open(inputPath, ""))
6        throw new Exception(String.Format("Input file {0} cannot be opened. " + 
7            "{1} (ErrorCode: 0x{2:x}).", inputPath, secure.ErrorMessage, secure.ErrorCode));
8
9    // Required: unique name of the accountspecified on the server.
10    secure.SetSessionPropertyString("Identity", "Rigora");
11    // Required: identifies the signature specifications by a unique name.
12    secure.SetSessionPropertyString("Profile", "Default");
13    // Required: password which secures the access to the account.
14    secure.SetSessionPropertyString("secret", "NeE=EKEd33FeCk70");
15    // Required: helps to separate access and to create better statistics.
16    secure.SetSessionPropertyString("clientId", "3949-4929-3179-2818");
17    // Required: activates the signing key.
18    secure.SetSessionPropertyString("pin", "123456");
19    // Optional: default value "SHA-256"
20    secure.SetSessionPropertyString("MessageDigestAlgorithm", "SHA-256");
21
22    // Begin session using DigiCert-QuoVadis Sealsign (demo version)
23    if (!secure.BeginSession(@"https://services.sealsignportal.com/sealsign/ws/BrokerClient"))
24        throw new Exception(String.Format("Unable to establish connection to DigiCert-QuoVadis Sealsign. " +
25            "{0} (ErrorCode: 0x{1:x}).", secure.ErrorMessage, secure.ErrorCode));
26
27    // Add signature
28    using (Signature signature = new Signature())
29    {
30        // Required, name of the signer
31        signature.Name = "Rigora";
32        secure.AddSignature(signature);
33    }
34
35    // Sign document
36    if (!secure.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption, 0, "", ""))
37        throw new Exception(String.Format("Unable to sign document {0}. {1} (ErrorCode: 0x{2:x}).", 
38            outputPath, secure.ErrorMessage, secure.ErrorCode));
39
40    // Cleanup
41    secure.Close();
42    secure.EndSession();
43}
44
Download code sample
1// Create secure object
2secure = new Secure();
3
4// Open input file 
5if (!secure.open(inputPath, ""))
6    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
7            inputPath, secure.getErrorMessage(), secure.getErrorCode()));
8
9// Required: unique name of the account specified on the server.
10secure.setSessionPropertyString("Identity", "Rigora");
11// Required: identifies the signature specifications by a unique name.
12secure.setSessionPropertyString("Profile", "Default");
13// Required: password which secures the access to the account.
14secure.setSessionPropertyString("secret", "NeE=EKEd33FeCk70");
15// Required: helps to separate access and to create better statistics.
16secure.setSessionPropertyString("clientId", "3949-4929-3179-2818");
17// Required: activates the signing key.
18secure.setSessionPropertyString("pin", "123456");
19// Optional: default value "SHA-256"
20secure.setSessionPropertyString("MessageDigestAlgorithm", "SHA-256");
21
22// Begin session using DigiCert-QuoVadis Sealsign (demo version)
23if (!secure.beginSession("https://services.sealsignportal.com/sealsign/ws/BrokerClient"))
24    throw new IOException(String.format("Unable to establish connection to DigiCert-QuoVadis Sealsign. " + 
25            "%s (ErrorCode: 0x%08x).", secure.getErrorMessage(), secure.getErrorCode()));
26
27// Add signature
28signature = new Signature();
29
30// Required, name of the signer
31signature.setName("Rigora");
32secure.addSignature(signature);
33
34// Sign document
35if (!secure.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption, 0, "", ""))
36    throw new IOException(String.format("Unable to sign document %s. %s (ErrorCode: 0x%08x).", 
37            outputPath, secure.getErrorMessage(), secure.getErrorCode()));
38
39// Cleanup
40secure.close();
41secure.endSession();
42
Download code sample

Sign a PDF using GlobalSign Digital Signing Service

1// Create secure object
2pSecure = PdfSecureCreateObject();
3
4// Open input file
5if (!PdfSecureOpen(pSecure, szInputPath, _T("")))
6{
7    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
8    iReturnValue = 1;
9    goto cleanup;
10}
11
12// Required: insert your account credentials’ key parameter for the login request
13PdfSecureSetSessionPropertyString(pSecure, _T("api_key"), _T("****fill in****"));
14// Required: insert your account credentials’ secret parameter for the login request
15PdfSecureSetSessionPropertyString(pSecure, _T("api_secret"), _T("****fill in****"));
16// Required: parameter to create the signing certificate
17PdfSecureSetSessionPropertyString(pSecure, _T("Identity"), _T("{ }"));
18// Required: insert byte array of SSL client certificate in PKCS#12 Format (.p12, .pfx)
19PdfSecureSetSessionPropertyString(pSecure, _T("SSLClientCertificate"), _T("****fill in****"));
20// Optional: password to decrypt the private key of theSSL client certificate
21PdfSecureSetSessionPropertyString(pSecure, _T("SSLClientCertificatePassword"), _T("****fill in****"));
22// Recommended: path to server's SSL certificate or its issuer (CA) certificate.
23PdfSecureSetSessionPropertyString(pSecure, _T("SSLServerCertificate"), _T("globalsign-root-ca.cer"));
24
25// Begin session using GlobalSign Digital Signing Service
26if (!PdfSecureBeginSession(pSecure, _T("https://emea.api.dss.globalsign.com:8443/v2")))
27{
28    _tprintf(_T("Unable to connect to GlobalSign Digital Signing Service. %s (ErrorCode: 0x%08x).\n"), PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
29    iReturnValue = 1;
30    goto cleanup;
31}
32
33// Create signature object
34pSignature = PdfSignatureCreateObject();
35
36// Add signature
37// Required, name of the signer
38PdfSignatureSetName(pSignature, szSignatureName);
39PdfSignatureSetTimeStampURL(pSignature, _T("urn:ietf:rfc:3161"));
40
41PdfSecureAddSignature(pSecure, pSignature);
42
43// Sign document
44if (!PdfSecureSaveAs(pSecure, szOutputPath, _T(""), _T(""), ePermNoEncryption, 0, _T(""), _T("")))
45{
46    _tprintf(_T("Unable to sign document %s. %s (ErrorCode: 0x%08x).\n"), szOutputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
47    iReturnValue = 1;
48    goto cleanup;
49}
50
51// Cleanup
52PdfSecureClose(pSecure);
53PdfSecureEndSession(pSecure);
54
Download code sample
1// Create secure object
2using (Secure secure = new Secure())
3{
4    // Open input file
5    if (!secure.Open(inputPath, ""))
6        throw new Exception(String.Format("Input file {0} cannot be opened. " + 
7            "{1} (ErrorCode: 0x{2:x}).", inputPath, secure.ErrorMessage, secure.ErrorCode));
8
9    // Required: insert your account credentials’ key parameter for the login request
10    secure.SetSessionPropertyString("api_key", "****fill in****");
11    // Required: insert your account credentials’ secret parameter for the login request
12    secure.SetSessionPropertyString("api_secret", "****fill in****");
13    // Required: parameter to create the signing certificate
14    secure.SetSessionPropertyString("Identity", "{ }");
15    // Required: insert byte array of SSL client certificate in PKCS#12 Format (.p12, .pfx)
16    secure.SetSessionPropertyBytes("SSLClientCertificate", File.ReadAllBytes("****fill in****"));
17    // Optional: password to decrypt the private key of theSSL client certificate
18    secure.SetSessionPropertyString("SSLClientCertificatePassword", "****fill in****");
19    // Recommended: The server's SSL certificate or its issuer (CA) certificate.
20    secure.SetSessionPropertyBytes("SSLServerCertificate", File.ReadAllBytes("globalsign-root-ca.cer"));
21
22    // Begin session using GlobalSign Digital Signing Service
23    if (!secure.BeginSession("https://emea.api.dss.globalsign.com:8443/v2"))
24        throw new Exception(String.Format("Unable to establish connection to GlobalSign Digital " + 
25            "Signing Service. {0} (ErrorCode: 0x{1:x}).", secure.ErrorMessage, secure.ErrorCode));
26
27    // Add signature
28    using (Signature signature = new Signature())
29    {
30        // Required, name of the signer
31        signature.Name = signatureName;
32        signature.TimeStampURL = "urn:ietf:rfc:3161";
33        secure.AddSignature(signature);
34    }
35
36    // Sign document
37    if (!secure.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption, 0, "", ""))
38        throw new Exception(String.Format("Unable to sign document {0}. {1} (ErrorCode: 0x{2:x}).",
39            outputPath, secure.ErrorMessage, secure.ErrorCode));
40
41    // Cleanup
42    secure.Close();
43    secure.EndSession();
44}
45
Download code sample
1// Create secure object
2secure = new Secure();
3
4// Open input file 
5if (!secure.open(inputPath, ""))
6    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
7            inputPath, secure.getErrorMessage(), secure.getErrorCode()));
8
9// Required: insert your account credentials' key parameter for the login request
10secure.setSessionPropertyString("api_key", "*****fill in*****");
11// Required: insert your account credentials' secret parameter for the login request
12secure.setSessionPropertyString("api_secret", "*****fill in*****");
13// Required: parameter to create the signing certificate
14secure.setSessionPropertyString("Identity", "{ }");
15// Required: insert byte array of SSL client certificate in PKCS#12 Format (.p12, .pfx)
16secure.setSessionPropertyBytes("SSLClientCertificate", 
17        Files.readAllBytes(Paths.get("*****fil in*****")));
18// Optional: password to decrypt the private key of theSSL client certificate
19secure.setSessionPropertyString("SSLClientCertificatePassword", "*****fill in*****");
20// Recommended: server's SSL certificate or its issuer (CA) certificate.
21secure.setSessionPropertyBytes("SSLServerCertificate", 
22    Files.readAllBytes(Paths.get("globalsign-root-ca.cer")));
23
24// Begin session using GlobalSign Digital Signing Service
25if (!secure.beginSession("https://emea.api.dss.globalsign.com:8443/v2"))
26    throw new IOException(String.format("Unable to establish connection to GlobalSign Digital " + 
27            "Signing Service. %s (ErrorCode: 0x%08x).", secure.getErrorMessage(), 
28            secure.getErrorCode()));
29
30// Add signature
31signature = new Signature();
32
33// Required, name of the signer
34signature.setName(signatureName);
35signature.setTimeStampURL("urn:ietf:rfc:3161");
36secure.addSignature(signature);
37
38// Sign document
39if (!secure.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption, 0, "", ""))
40    throw new IOException(String.format("Unable to sign document %s. %s (ErrorCode: 0x%08x).", 
41            outputPath, secure.getErrorMessage(), secure.getErrorCode()));
42
43// Cleanup
44secure.close();
45secure.endSession();
46
Download code sample

Sign a PDF using myBica Digital Signing Service

1// Create secure object
2pSecure = PdfSecureCreateObject();
3
4// Open input file
5if (!PdfSecureOpen(pSecure, szInputPath, _T("")))
6{
7    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
8    iReturnValue = 1;
9    goto cleanup;
10}
11
12// Required: insert missing value ‹customer name›:‹key identity›
13PdfSecureSetSessionPropertyString(pSecure, _T("Identity"), _T("****fill in****"));
14// Required: set DSSProfile
15PdfSecureSetSessionPropertyString(pSecure, _T("DSSProfile"), _T("http://www.pdf-tools.com/dss/profile/pades/1.0"));
16// Required: insert byte array of SSL client certificate in PKCS#12 Format (.p12, .pfx)
17PdfSecureSetSessionPropertyString(pSecure, _T("SSLClientCertificate"), _T("****fill in****"));
18// Optional: insert password to decrypt the private key of the SSL client certificate
19PdfSecureSetSessionPropertyString(pSecure, _T("SSLClientCertificatePassword"), _T("****fill in****"));
20// Recommended: Path to server's SSL certificate or its issuer (CA) certificate.
21PdfSecureSetSessionPropertyString(pSecure, _T("SSLServerCertificate"), _T("swisssign-root-ca.cer"));
22// Recommended, insert any string that can be used to track the request, e.g. an UUID
23PdfSecureSetSessionPropertyString(pSecure, _T("RequestID"), _T("1"));
24
25// Begin session using myBica Digital Signing Service
26if (!PdfSecureBeginSession(pSecure, _T("https://sign.mybica.ch/DS/DS")))
27{
28    _tprintf(_T("Unable to connect to myBica Digital Signing Service. %s (ErrorCode: 0x%08x).\n"), PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
29    iReturnValue = 1;
30    goto cleanup;
31}
32
33// Create signature object
34pSignature = PdfSignatureCreateObject();
35
36// Add signature
37// Required, name of the signer
38PdfSignatureSetName(pSignature, _T("****fill in****"));
39PdfSignatureSetTimeStampURL(pSignature, _T("urn:ietf:rfc:3161"));
40PdfSecureAddSignature(pSecure, pSignature);
41
42// Sign document
43if (!PdfSecureSaveAs(pSecure, szOutputPath, _T(""), _T(""), ePermNoEncryption, 0, _T(""), _T("")))
44{
45    _tprintf(_T("Unable to sign document %s. %s (ErrorCode: 0x%08x).\n"), szOutputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
46    iReturnValue = 1;
47    goto cleanup;
48}
49
50// Cleanup
51PdfSecureClose(pSecure);
52PdfSecureEndSession(pSecure);
53
Download code sample
1// Create secure object
2using (Secure secure = new Secure())
3{
4    // Open input file
5    if (!secure.Open(inputPath, ""))
6        throw new Exception(String.Format("Input file {0} cannot be opened. " + 
7            "{1} (ErrorCode: 0x{2:x}).", inputPath, secure.ErrorMessage, secure.ErrorCode));
8
9    // Required: set identity of the signing certificate
10    secure.SetSessionPropertyString("Identity", "****fill in****");
11    // Required: set DSSProfile
12    secure.SetSessionPropertyString("DSSProfile", "http://www.pdf-tools.com/dss/profile/pades/1.0");
13    // Required: insert byte array of SSL client certificate in PKCS#12 Format (.p12, .pfx)
14    secure.SetSessionPropertyBytes("SSLClientCertificate", File.ReadAllBytes("****fill in****"));
15    // Optional: set password to decrypt the private key of the SSL client certificate
16    secure.SetSessionPropertyString("SSLClientCertificatePassword", "****fill in****");
17    // Recommended: Path to server's SSL certificate or its issuer (CA) certificate.
18    secure.SetSessionPropertyString("SSLServerCertificate", "swisssign-root-ca.cer");
19
20    // Create a Request-ID 
21    Guid requestId = Guid.NewGuid();
22    secure.SetSessionPropertyString("RequestID", requestId.ToString("D"));
23
24    // Begin session using myBica Digital Signing Service
25    if (!secure.BeginSession(@"https://sign.mybica.ch/DS/DS"))
26        throw new Exception(String.Format("Unable to establish connection to myBica Digital " + 
27            "Signing Service. {0} (ErrorCode: 0x{1:x}).", secure.ErrorMessage, secure.ErrorCode));
28
29    // Add signature
30    using (Signature signature = new Signature())
31    {
32        // Required, name of signer
33        signature.Name = "****fill in****";
34        signature.TimeStampURL = "urn:ietf:rfc:3161";
35        secure.AddSignature(signature);
36    }
37
38    // Sign document
39    if (!secure.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption, 0, "", ""))
40        throw new Exception(String.Format("Unable to sign document {0}. {1} (ErrorCode: 0x{2:x}, " +
41            "Request-ID: {3}).", outputPath, secure.ErrorMessage, secure.ErrorCode, requestId));
42
43    // Cleanup
44    secure.Close();
45    secure.EndSession();
46}
47
Download code sample
1// Create secure object
2secure = new Secure();
3
4// Open input file 
5if (!secure.open(inputPath, ""))
6    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
7            inputPath, secure.getErrorMessage(), secure.getErrorCode()));
8
9// Required: set identity of the signing certificate
10secure.setSessionPropertyString("Identity", "*****fill in*****");
11// Required: set DSSProfile
12secure.setSessionPropertyString("DSSProfile", "http://www.pdf-tools.com/dss/profile/pades/1.0");
13// Required: insert byte array of SSL client certificate in PKCS#12 Format (.p12, .pfx)
14secure.setSessionPropertyBytes("SSLClientCertificate", 
15        Files.readAllBytes(Paths.get("*****fill in*****")));
16// Optional: set password to decrypt the private key of the SSL client certificate
17secure.setSessionPropertyString("SSLClientCertificatePassword", "*****fill in*****");
18// Recommended: Path to server's SSL certificate or its issuer (CA) certificate.
19secure.setSessionPropertyString("SSLServerCertificate", "swisssign-root-ca.cer");
20
21// Create a Request-ID
22UUID requestId = UUID.randomUUID();
23secure.setSessionPropertyString("RequestID", requestId.toString());
24
25// Begin session using myBica Digital Signing Service
26if (!secure.beginSession("https://sign.mybica.ch/DS/DS"))
27    throw new IOException(String.format("Unable to establish connection to myBica Digital" +
28            "Signing Service. %s (ErrorCode: 0x%08x).", secure.getErrorMessage(), 
29            secure.getErrorCode()));
30
31// Add signature
32signature = new Signature();
33
34// Required, name of the signer
35signature.setName("****fill in****");
36signature.setTimeStampURL("urn:ietf:rfc:3161");
37secure.addSignature(signature);
38
39// Sign document
40if (!secure.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption, 0, "", ""))
41    throw new IOException(String.format("Unable to sign document %s. %s (ErrorCode: 0x%08x, " + 
42            "Request-ID: %s).", outputPath, secure.getErrorMessage(), secure.getErrorCode(), 
43            requestId));
44
45// Cleanup
46secure.close();
47secure.endSession();
48
Download code sample

Sign a PDF using PKCS#11 Provider

1// Create secure object
2pSecure = PdfSecureCreateObject();
3
4// Open input file
5if (!PdfSecureOpen(pSecure, szInputPath, _T("")))
6{
7    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
8    iReturnValue = 1;
9    goto cleanup;
10}
11
12// Begin session using PKCS#11
13// General: "PathToDll;SlotId;Pin"
14if (!PdfSecureBeginSession(pSecure, "****fill in****"))
15{
16    _tprintf(_T("Unable to connect to PKCS#11. %s (ErrorCode: 0x%08x).\n"), PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
17    iReturnValue = 1;
18    goto cleanup;
19}
20
21// Create signature object
22pSignature = PdfSignatureCreateObject();
23PdfSignatureSetName(pSignature, szCertificate);
24PdfSecureAddSignature(pSecure, pSignature);
25
26// Sign document
27if (!PdfSecureSaveAs(pSecure, szOutputPath, _T(""), _T(""), ePermNoEncryption, 0, _T(""), _T("")))
28{
29    _tprintf(_T("Unable to sign document %s. %s (ErrorCode: 0x%08x).\n"), szOutputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
30    iReturnValue = 1;
31    goto cleanup;
32}
33
34// Cleanup
35PdfSecureClose(pSecure);
36
Download code sample
1// Create secure object
2using (Secure secure = new Secure())
3{
4    // Open input file
5    if (!secure.Open(inputPath, ""))
6        throw new Exception(String.Format("Input file {0} cannot be opened. " + 
7            "{1} (ErrorCode: 0x{2:x}).", inputPath, secure.ErrorMessage, secure.ErrorCode));
8
9    // Begin session using PKCS#11
10    // General: "PathToDll;SlotId;Pin"
11    if (!secure.BeginSession("****fill in****")) 
12        throw new Exception(String.Format("Unable to connect to PKCS#11. {0} (ErrorCode: 0x{1:x}).",
13            secure.ErrorMessage, secure.ErrorCode));
14
15    // Create signature object
16    using (Signature signature = new Signature())
17    {
18        signature.Name = certificate;
19        secure.AddSignature(signature);
20
21        // Sign document
22        if (!secure.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption, 0, "", ""))
23            throw new Exception(String.Format("Unable to sign document {0}. " + 
24                "{1} (ErrorCode: 0x{2:x}).", outputPath, secure.ErrorMessage, secure.ErrorCode));
25    }
26
27    // Cleanup
28    secure.Close();
29    secure.EndSession();
30}
31
Download code sample
1// Create secure object
2secure = new Secure();
3
4// Open input file 
5if (!secure.open(inputPath, ""))
6    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
7            inputPath, secure.getErrorMessage(), secure.getErrorCode()));
8
9// Begin session using PKCS#11
10// General: "PathToDll;SlotId;Pin"
11if (!secure.beginSession("*****fill in*****"))
12    throw new IOException(String.format("Unable to connect to PKCS#11. %s (ErrorCode: 0x%08x).",
13            secure.getErrorMessage(), secure.getErrorCode()));
14
15// Create signature object 
16signature = new Signature();
17
18signature.setName(certificate);
19secure.addSignature(signature);
20
21// Sign document
22if (!secure.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption, 0, "", ""))
23    throw new IOException(String.format("Unable to sign document %s. %s (ErrorCode: 0x%08x).",
24            outputPath, secure.getErrorMessage(), secure.getErrorCode()));
25
26// Cleanup
27secure.close();
28secure.endSession();
29
Download code sample

Sign PDF using Swisscom All-in Signing Service

1// Create secure object
2pSecure = PdfSecureCreateObject();
3
4// Open input file
5if (!PdfSecureOpen(pSecure, szInputPath, _T("")))
6{
7    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
8    iReturnValue = 1;
9    goto cleanup;
10}
11
12// Required: insert missing value ‹customer name›:‹key identity›
13PdfSecureSetSessionPropertyString(pSecure, _T("Identity"), _T("****fill in****"));
14// Required: set DSSProfile
15PdfSecureSetSessionPropertyString(pSecure, _T("DSSProfile"), _T("http://ais.swisscom.ch/1.0"));
16// Required: insert byte array of SSL client certificate in PKCS#12 Format (.p12, .pfx)
17PdfSecureSetSessionPropertyString(pSecure, _T("SSLClientCertificate"), _T("****fill in****"));
18// Optional: insert password to decrypt the private key of the SSL client certificate
19PdfSecureSetSessionPropertyString(pSecure, _T("SSLClientCertificatePassword"), _T("****fill in****"));
20// Recommended: Path to server's SSL certificate or its issuer (CA) certificate.
21PdfSecureSetSessionPropertyString(pSecure, _T("SSLServerCertificate"), _T("ais.swisscom-root-ca.cer"));
22// Recommended, insert any string that can be used to track the request
23PdfSecureSetSessionPropertyString(pSecure, _T("RequestID"), _T("AE57F021-C0EB-4AE0-8E5E-67FB93E5BC7F"));
24
25// Begin session using Swisscom All-in Signing Service
26if (!PdfSecureBeginSession(pSecure, _T("https://ais.swisscom.com/AIS-Server/rs/v1.0/sign")))
27{
28    _tprintf(_T("Unable to connect to Swisscom All-in Signing Service. %s (ErrorCode: 0x%08x).\n"), PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
29    iReturnValue = 1;
30    goto cleanup;
31}
32
33// Create signature object
34pSignature = PdfSignatureCreateObject();
35
36// Add signature
37// Required, name of the signer
38PdfSignatureSetName(pSignature, _T("****fill in****"));
39PdfSecureAddSignature(pSecure, pSignature);
40
41// Sign document
42if (!PdfSecureSaveAs(pSecure, szOutputPath, _T(""), _T(""), ePermNoEncryption, 0, _T(""), _T("")))
43{
44    _tprintf(_T("Unable to sign document %s. %s (ErrorCode: 0x%08x).\n"), szOutputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
45    iReturnValue = 1;
46    goto cleanup;
47}
48
49// Cleanup
50PdfSecureClose(pSecure);
51PdfSecureEndSession(pSecure);
52
Download code sample
1// Create secure object
2using (Secure secure = new Secure())
3{
4    // Open input file
5    if (!secure.Open(inputPath, ""))
6        throw new Exception(String.Format("Input file {0} cannot be opened. " + 
7            "{1} (ErrorCode: 0x{2:x}).", inputPath, secure.ErrorMessage, secure.ErrorCode));
8
9    // Required: insert missing value ‹customer name›:‹key identity›
10    secure.SetSessionPropertyString("Identity", "****fill in****");
11    // Required: set DSSProfile
12    secure.SetSessionPropertyString("DSSProfile", "http://ais.swisscom.ch/1.0");
13    // Required: insert byte array of SSL client certificate in PKCS#12 Format (.p12, .pfx)
14    secure.SetSessionPropertyBytes("SSLClientCertificate", File.ReadAllBytes(@"****fill in****"));
15    // Optional: insert password to decrypt the private key of the SSL client certificate
16    secure.SetSessionPropertyString("SSLClientCertificatePassword", "****fill in****");
17    // Recommended: set server's SSL certificate or its issuer (CA) certificate.
18    secure.SetSessionPropertyBytes("SSLServerCertificate", File.ReadAllBytes("ais.swisscom-root-ca.cer"));
19
20    // Create a Request-ID 
21    Guid requestId = Guid.NewGuid();
22    secure.SetSessionPropertyString("RequestID", requestId.ToString("D"));
23
24    // Begin session using Swisscom All-in Signing Service
25    if (!secure.BeginSession(@"https://ais.swisscom.com/AIS-Server/rs/v1.0/sign"))
26        throw new Exception(String.Format("Unable to establish connection to Swisscom All-in " + 
27            "Signing Service. {0} (ErrorCode: 0x{1:x}).", secure.ErrorMessage, secure.ErrorCode));
28
29    // Add signature
30    using (Signature signature = new Signature())
31    {
32        // Required, name of the signer
33        signature.Name = "****fill in****";
34        secure.AddSignature(signature);
35    }
36
37    // Sign document
38    if (!secure.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption, 0, "", ""))
39        throw new Exception(String.Format("Unable to sign document {0}. {1} (ErrorCode: 0x{2:x}, " +
40            "Request-ID: {3}).", outputPath, secure.ErrorMessage, secure.ErrorCode, requestId));
41
42    // Cleanup
43    secure.Close();
44    secure.EndSession();                   
45}
46
Download code sample
1// Create secure object
2secure = new Secure();
3
4// Open input file 
5if (!secure.open(inputPath, ""))
6    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
7            inputPath, secure.getErrorMessage(), secure.getErrorCode()));
8
9// Required: insert missing value ‹customer name›:‹key identity›
10secure.setSessionPropertyString("Identity", "*****fill in****");
11// Required: set DSSProfile
12secure.setSessionPropertyString("DSSProfile", "http://ais.swisscom.ch/1.0");
13// Required: insert byte array of SSL client certificate in PKCS#12 Format (.p12, .pfx)
14secure.setSessionPropertyBytes("SSLClientCertificate", 
15        Files.readAllBytes(Paths.get("*****fill in*****")));
16// Optional: insert password to decrypt the private key of the SSL client certificate
17secure.setSessionPropertyString("SSLClientCertificatePassword", "*****fill in*****");
18// Recommended: set server's SSL certificate or its issuer (CA) certificate.
19secure.setSessionPropertyBytes("SSLServerCertificate",
20        Files.readAllBytes(Paths.get("ais.swisscom-root-ca.cer")));
21
22// Create a Request-ID
23UUID requestId = UUID.randomUUID();
24secure.setSessionPropertyString("RequestID", requestId.toString());
25
26// Begin session using Swisscom All-in Signing Service
27if (!secure.beginSession("https://ais.swisscom.com/AIS-Server/rs/v1.0/sign"))
28    throw new IOException(String.format("Unable to establish connection to Swisscom All-in " + 
29            "Signing Service. %s (ErrorCode: 0x%08x).", secure.getErrorMessage(), 
30            secure.getErrorCode()));
31
32// Add signature
33signature = new Signature();
34
35// Required, name of the signer
36signature.setName("****fill in****");
37secure.addSignature(signature);
38
39// Sign document
40if (!secure.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption, 0, "", ""))
41    throw new IOException(String.format("Unable to sign document %s. %s (ErrorCode: 0x%08x, " + 
42            "Request-ID: %s).", outputPath, secure.getErrorMessage(), secure.getErrorCode(), 
43            requestId));
44
45// Cleanup
46secure.close();
47secure.endSession();
48
Download code sample

Sign a PDF using SwissSign SuisseID Signing Service

1// Create secure object
2pSecure = PdfSecureCreateObject();
3
4// Open input file
5if (!PdfSecureOpen(pSecure, szInputPath, _T("")))
6{
7    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
8    iReturnValue = 1;
9    goto cleanup;
10}
11
12// Required: SAML token issued by the SuisseID Identity Provider (IDP)
13PdfSecureSetSessionPropertyString(pSecure, _T("SAMLToken"), szSAMLToken));
14// Required: insert byte array of SSL client certificate in PKCS#12 Format (.p12, .pfx)
15PdfSecureSetSessionPropertyString(pSecure, _T("SSLClientCertificate"), _T("****fill in****"));
16// Optional: password to decrypt the private key of the SSL client certificate
17PdfSecureSetSessionPropertyString(pSecure, _T("SSLClientCertificatePassword"), _T("****fill in****"));
18// Recommended: Path to server's SSL certificate or its issuer (CA) certificate.
19PdfSecureSetSessionPropertyString(pSecure, _T("SSLServerCertificate"), _T("swisssign-root-ca.cer"));
20
21// Begin session using SwissSign SuisseID Signing Service
22if (!PdfSecureBeginSession(pSecure, _T("https://sig.post.ch/sigaas/?SuisseID")))
23{
24    _tprintf(_T("Unable to establish connection to SwissSign Suisse ID Signing Service. %s (ErrorCode: 0x%08x).\n"), PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
25    iReturnValue = 1;
26    goto cleanup;
27}
28
29// Create signature object
30pSignature = PdfSignatureCreateObject();
31
32// Add signature and time-stamp
33// Required, name of the signer
34PdfSignatureSetName(pSignature, szSignatureName);
35PdfSignatureSetTimeStampURL(pSignature, _T("http://tsa.swisssign.net"));
36PdfSignatureSetReason(pSignature, _T("Document reviewed!"));
37PdfSignatureSetText1(pSignature, (_T("\t10,44 %s"), PdfSignatureGetName(pSignature)));
38PdfSignatureSetFontSize1(pSignature, 15);
39PdfSignatureSetFontSize2(pSignature, 6);
40PdfSignatureSetImageFileName(pSignature, _T("DigitalSignature.jpg"));
41PdfSignatureSetPageNo(pSignature, 1);
42PdfSignatureSetRect(pSignature, &rect);
43PdfSecureAddSignature(pSecure, pSignature);
44
45// Sign document
46if (!PdfSecureSaveAs(pSecure, szOutputPath, _T(""), _T(""), ePermNoEncryption, 0, _T(""), _T("")))
47{
48    _tprintf(_T("Unable to sign document %s. %s (ErrorCode: 0x%08x).\n"), szOutputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
49    iReturnValue = 1;
50    goto cleanup;
51}
52
53// Cleanup
54PdfSecureClose(pSecure);
55PdfSecureEndSession(pSecure);
56
Download code sample
1// Create secure object
2using (Secure secure = new Secure())
3{
4    // Open input file
5    if (!secure.Open(inputPath, ""))
6        throw new Exception(String.Format("Input file {0} cannot be opened. " + 
7            "{1} (ErrorCode: 0x{2:x}).", inputPath, secure.ErrorMessage, secure.ErrorCode));
8
9    // Required: SAML token issued by the SuisseID Identity Provider (IDP)
10    secure.SetSessionPropertyString("SAMLToken", samlToken);
11    // Required: insert byte array of SSL client certificate in PKCS#12 Format (.p12, .pfx)
12    secure.SetSessionPropertyBytes("SSLClientCertificate", File.ReadAllBytes("****fill in****"));
13    // Optional: password to decrypt the private key of the SSL client certificate
14    secure.SetSessionPropertyString("SSLClientCertificatePassword", "****fill in****");
15    // Recommended: Path to server's SSL certificate or its issuer (CA) certificate.
16    secure.SetSessionPropertyString("SSLServerCertificate", "swisssign-root-ca.cer");
17
18    // Begin session using SwissSign SuisseID Signing Service
19    if (!secure.BeginSession("https://sig.post.ch/sigaas/?SuisseID"))
20        throw new Exception(String.Format("Unable to establish connection to SwissSign SuisseID " + 
21            "Signing Service. {0} (ErrorCode: 0x{1:x}).", secure.ErrorMessage, secure.ErrorCode));
22
23    // Add signature and time-stamp
24    using (Signature signature = new Signature())
25    {
26        // Required, name of signer
27        signature.Name = signatureName;
28        signature.TimeStampURL = "http://tsa.swisssign.net";
29        signature.Reason = "Document reviewed!";
30        signature.Text1 = "\t10,44 " + signature.Name;
31        signature.FontSize1 = 15;
32        signature.FontSize2 = 6;
33        signature.ImageFileName = "DigitalSignature.jpg";
34        signature.PageNo = 1;
35        signature.Rect = new PDFRect(10.0f, 10.0f, 145.0f, 86.0f);
36
37        secure.AddSignature(signature);
38    }
39
40    // Sign document
41    if (!secure.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption, 0, "", ""))
42        throw new Exception(String.Format("Unable to sign document {0}. {1} (ErrorCode: 0x{2:x}).",
43            outputPath, secure.ErrorMessage, secure.ErrorCode));
44
45    // Cleanup
46    secure.Close();
47    secure.EndSession();
48}
49
Download code sample
1// Create secure object
2secure = new Secure();
3
4// Open input file 
5if (!secure.open(inputPath, ""))
6    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).", 
7            inputPath, secure.getErrorMessage(), secure.getErrorCode()));
8
9// Required: SAML token issued by the SuisseID Identity Provider (IDP)
10secure.setSessionPropertyString("SAMLToken", samlToken);
11// Required: insert byte array of SSL client certificate in PKCS#12 Format (.p12, .pfx)
12secure.setSessionPropertyBytes("SSLClientCertificate", 
13        Files.readAllBytes(Paths.get("****fill in****")));
14// Optional: password to decrypt the private key of the SSL client certificate
15secure.setSessionPropertyString("SSLClientCertificatePassword", "****fill in****");
16// Recommended: Path to server's SSL certificate or its issuer (CA) certificate.
17secure.setSessionPropertyString("SSLServerCertificate", "swisssign-root-ca.cer");
18
19// Begin session using SwissSign SuisseID Signing Service
20if (!secure.beginSession("https://sig.post.ch/sigaas/?SuisseID"))
21    throw new IOException(String.format("Unable to establish connection to SwissSign SuisseID " + 
22            "Signing Service. %s (ErrorCode: 0x%08x).", secure.getErrorMessage(), 
23            secure.getErrorCode()));
24
25// Add signature and time-stamp
26signature = new Signature();
27
28// Required, name of the signer
29signature.setName(signatureName);
30signature.setTimeStampURL("http://tsa.swisssign.net");
31signature.setReason("Document reviewed!");
32signature.setText1("\t10,44" + signature.getName());
33signature.setFontSize1(15);
34signature.setFontSize2(6);
35signature.setImageFileName("DigitalSignature.jpg");
36signature.setPageNo(1);
37signature.setRect(new float[]{10.0f, 10.0f, 145.0f, 86.0f});
38
39secure.addSignature(signature);
40
41// Sign document
42if (!secure.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption, 0, "", ""))
43    throw new IOException(String.format("Unable to sign document %s. %s (ErrorCode: 0x%08x).", 
44            outputPath, secure.getErrorMessage(), secure.getErrorCode()));
45
46// Cleanup
47secure.close();
48secure.endSession();
49
Download code sample

Create visual appearance of a signed PDF document

1// Create secure object
2pSecure = PdfSecureCreateObject();
3
4// Open input file
5if (!PdfSecureOpen(pSecure, szInputPath, _T("")))
6{
7    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
8    iReturnValue = 1;
9    goto cleanup;
10}
11
12// Begin session with Windows Cryptographic Provider
13if (!PdfSecureBeginSession(pSecure, _T("")))
14{
15    _tprintf(_T("Unable to connect to Cryptographic Provider. %s (ErrorCode: 0x%08x).\n"), PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
16    iReturnValue = 1;
17    goto cleanup;
18}
19
20// Create signature object
21pSignature = PdfSignatureCreateObject();
22
23// Get current time
24time(&iTime);
25szCurrentTime = localtime(&iTime);
26_tcsftime(szTimeBuffer, 26, _T("%d.%m.%Y %H:%M:%S"), szCurrentTime);
27
28PdfSignatureSetName(pSignature, szCertificate);
29PdfSignatureSetReason(pSignature, szReason);
30_stprintf(szCerBuff, _T("\t10,47 %s"), szCertificate);
31PdfSignatureSetText1(pSignature, szCerBuff);
32_stprintf(szTextBuffer, _T("\n Reason: %s \n Date: %s"), PdfSignatureGetReason(pSignature), szTimeBuffer);
33PdfSignatureSetText2(pSignature, szTextBuffer);
34PdfSignatureSetFontSize1(pSignature, 10);
35PdfSignatureSetFontSize2(pSignature, 6);
36PdfSignatureSetImageFileName(pSignature, "DigitalSignature.jpg");
37PdfSignatureSetPageNo(pSignature, 1);
38PdfSignatureSetRect(pSignature, &rect);
39
40PdfSecureAddSignature(pSecure, pSignature);
41
42// Sign document and imprint visual appearance of signature
43if (!PdfSecureSaveAs(pSecure, szOutputPath, _T(""), _T(""), ePermNoEncryption, 0, _T(""), _T("")))
44{
45    _tprintf(_T("Unable to sign document %s. %s (ErrorCode: 0x%08x).\n"), szOutputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
46    iReturnValue = 1;
47    goto cleanup;
48}
49
50// Cleanup
51PdfSecureClose(pSecure);
52PdfSecureEndSession(pSecure);
53
Download code sample
1// Create secure object
2using (Secure secure = new Secure())
3{
4    // Open input file
5    if (!secure.Open(inputPath, ""))
6        throw new Exception(String.Format("Input file {0} cannot be opened. " + 
7            "{1} (ErrorCode: 0x{2:x}).", inputPath, secure.ErrorMessage, secure.ErrorCode));
8
9    // Begin session using Windows Cryptographic Provider
10    if (!secure.BeginSession(""))
11        throw new Exception(String.Format("Unable to connect to Windows Cryptographic Provider. " + 
12            "{0} (ErrorCode: 0x{1:x}).", secure.ErrorMessage, secure.ErrorCode));
13
14    // Create signature object
15    using (Signature signature = new Signature())
16    {
17        signature.Name = certificate;
18        signature.Reason = reason;
19        signature.Text1 = "\t10,47 " + signature.Name;
20        signature.Text2 = "\n Reason: " + signature.Reason + "\n Date: " + DateTime.Now;
21        signature.FontSize1 = 10;
22        signature.FontSize2 = 6;
23        signature.ImageFileName = "DigitalSignature.jpg";
24        signature.PageNo = 1;
25        signature.Rect = new PDFRect(10.0f, 10.0f, 145.0f, 86.0f);
26
27        secure.AddSignature(signature);
28
29        // Sign document and imprint visual appearance of signature
30        if (!secure.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption, 0, "", ""))
31            throw new Exception(String.Format("Unable to sign document {0}. " + 
32                "{1} (ErrorCode: 0x{2:x}).", outputPath, secure.ErrorMessage, secure.ErrorCode));
33    }
34
35    // Cleanup
36    secure.Close();
37    secure.EndSession();                   
38}
39
Download code sample
1// Create signature object
2secure = new Secure();
3
4// Open input file 
5if (!secure.open(inputPath, ""))
6    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
7            inputPath, secure.getErrorMessage(), secure.getErrorCode()));
8
9// Begin session with Windows Cryptographic Provider
10if (!secure.beginSession(""))
11    throw new IOException(String.format("Unable to connect to Cryptographic Provider." + 
12            "%s (ErrorCode: 0x%08x).", secure.getErrorMessage(), secure.getErrorCode()));
13
14// Create signature object
15signature = new Signature();
16
17signature.setName(certificate);
18signature.setReason(reason);
19signature.setText1("\t10,47 " + signature.getName());
20signature.setText2("\n Reason: " + signature.getReason() + "\n Date: " + 
21        new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(Calendar.getInstance().getTime()));
22signature.setFontSize1(10);
23signature.setFontSize2(6);
24signature.setImageFileName("DigitalSignature.jpg");
25signature.setPageNo(1);
26signature.setRect(new float[]{10.0f, 10.0f, 145.0f, 86.0f});
27
28secure.addSignature(signature);
29
30// Sign document and imprint visual appearance of signature
31if (!secure.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption, 128, "V2", "V2"))
32    throw new IOException(String.format("Unable to sign document %s. %s (ErrorCode: 0x%08x).", 
33            outputPath, secure.getErrorMessage(), secure.getErrorCode()));
34
35// Cleanup
36secure.close();
37secure.endSession();
38
Download code sample

Time-stamping

Put time-stamp on a PDF using Windows Cryptographic Provider

1// Create secure object
2pSecure = PdfSecureCreateObject();
3
4// Open input file
5if (!PdfSecureOpen(pSecure, szInputPath, _T("")))
6{
7    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
8    iReturnValue = 1;
9    goto cleanup;
10}
11
12// Create signature object
13pTimeStamp = PdfSignatureCreateObject();
14
15// Add time-stamp
16PdfSignatureSetTimeStampURL(pTimeStamp, _T("http://tsa.swisssign.net"));
17PdfSecureAddTimeStampSignature(pSecure, pTimeStamp);
18
19// Put time-stamp to document
20if (!PdfSecureSaveAs(pSecure, szOutputPath, _T(""), _T(""), ePermNoEncryption, 0, _T(""), _T("")))
21{
22    _tprintf(_T("Unable to add time-stamp to document %s. %s (ErrorCode: 0x%08x).\n"), szOutputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
23    iReturnValue = 1;
24    goto cleanup;
25}
26
27// Cleanup
28PdfSecureClose(pSecure);
29
Download code sample
1// Create secure object
2using (Secure secure = new Secure())
3{
4    // Open input file
5    if (!secure.Open(inputPath, ""))
6        throw new Exception(String.Format("Input file {0} cannot be opened. " + 
7            "{1} (ErrorCode: 0x{2:x}).", inputPath, secure.ErrorMessage, secure.ErrorCode));
8
9    // Add time-stamp
10    using (Signature timeStamp = new Signature())
11    {
12        timeStamp.TimeStampURL = "http://tsa.swisssign.net";
13        secure.AddTimeStampSignature(timeStamp);
14    }
15
16    // Put time-stamp to document
17    if (!secure.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption, 0, "", ""))
18        throw new Exception(String.Format("Unable to add time-stamp to document {0}. " + 
19            "{1} (ErrorCode: 0x{2:x}).", outputPath, secure.ErrorMessage, secure.ErrorCode));
20
21    // Cleanup
22    secure.Close();
23}
24
Download code sample
1// Create secure object
2secure = new Secure();
3
4// Open input file 
5if (!secure.open(inputPath, ""))
6    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
7        inputPath, secure.getErrorMessage(), secure.getErrorCode()));
8
9// Add time-stamp
10timeStamp = new Signature();
11
12timeStamp.setTimeStampURL("http://tsa.swisssign.net");
13secure.addTimeStampSignature(timeStamp);
14
15// Put time-stamp to document
16if (!secure.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption, 0, "", ""))
17    throw new IOException(String.format("Unable to add time-stamp to document %s. " + 
18        "%s (ErrorCode: 0x%08x).", outputPath, secure.getErrorMessage(), secure.getErrorCode()));
19
20// Cleanup
21secure.close();
22
Download code sample

Put time-stamp on a PDF using GlobalSign Digital Signing Service

1// Create secure object
2pSecure = PdfSecureCreateObject();
3
4// Open input file
5if (!PdfSecureOpen(pSecure, szInputPath, _T("")))
6{
7    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
8    iReturnValue = 1;
9    goto cleanup;
10}
11
12// Required: insert your account credentials' key parameter for the login request
13PdfSecureSetSessionPropertyString(pSecure, _T("api_key"), _T("****fill in****"));
14// Required: insert your account credentials' secret parameter for the login request
15PdfSecureSetSessionPropertyString(pSecure, _T("api_secret"), _T("****fill in****"));
16// Required: parameter to create the signing certificate
17PdfSecureSetSessionPropertyString(pSecure, _T("Identity"), _T("{ }"));
18// Required: insert byte array of SSL client certificate in PKCS#12 Format (.p12, .pfx)
19PdfSecureSetSessionPropertyString(pSecure, _T("SSLClientCertificate"), _T("****fill in****"));
20// Optional: password to decrypt the private key of theSSL client certificate
21PdfSecureSetSessionPropertyString(pSecure, _T("SSLClientCertificatePassword"), _T("****fill in****"));
22// Recommended: Path to server's SSL certificate or its issuer (CA) certificate.
23PdfSecureSetSessionPropertyString(pSecure, _T("SSLServerCertificate"), _T("globalsign-root-ca.cer"));
24
25// Begin session using GlobalSign Digital Signing Service
26if (!PdfSecureBeginSession(pSecure, _T("https://emea.api.dss.globalsign.com:8443/v2")))
27{
28    _tprintf(_T("Unable to establish connection to GlobalSign Digital Signing Service. %s (ErrorCode: 0x%08x).\n"), PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
29    iReturnValue = 1;
30    goto cleanup;
31}
32
33// Create signature object
34pTimeStamp = PdfSignatureCreateObject();
35
36// Add time-stamp
37PdfSignatureSetTimeStampURL(pTimeStamp, _T("urn:ietf:rfc:3161"));
38PdfSecureAddTimeStampSignature(pSecure, pTimeStamp);
39
40// Save output document
41if (!PdfSecureSaveAs(pSecure, szOutputPath, _T(""), _T(""), ePermNoEncryption, 0, _T(""), _T("")))
42{
43    _tprintf(_T("Unable to add time-stamp to document %s. %s (ErrorCode: 0x%08x).\n"), szOutputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
44    iReturnValue = 1;
45    goto cleanup;
46}
47
48// Cleanup
49PdfSecureClose(pSecure);
50PdfSecureEndSession(pSecure);
51
Download code sample
1// Create secure object
2using (Secure secure = new Secure())
3{
4    // Open input file
5    if (!secure.Open(inputPath, ""))
6        throw new Exception(String.Format("Input file {0} cannot be opened. " + 
7            "{1} (ErrorCode: 0x{2:x}).", inputPath, secure.ErrorMessage, secure.ErrorCode));
8
9    // Required: insert your account credentials’ key parameter for the login request
10    secure.SetSessionPropertyString("api_key", "****fill in****");
11    // Required: insert your account credentials’ secret parameter for the login request
12    secure.SetSessionPropertyString("api_secret", "****fill in****");
13    // Required: parameter to create the signing certificate
14    secure.SetSessionPropertyString("Identity", "{ }");
15    // Required: insert byte array of SSL client certificate in PKCS#12 Format (.p12, .pfx)
16    secure.SetSessionPropertyBytes("SSLClientCertificate", File.ReadAllBytes("****fill in****"));
17    // Optional: password to decrypt the private key of theSSL client certificate
18    secure.SetSessionPropertyString("SSLClientCertificatePassword", "****fill in****");
19    // Recommended: server's SSL certificate or its issuer (CA) certificate.
20    secure.SetSessionPropertyBytes("SSLServerCertificate", File.ReadAllBytes("globalsign-root-ca.cer"));
21
22    // Begin session using GlobalSign Digital Signing Service
23    if (!secure.BeginSession("https://emea.api.dss.globalsign.com:8443/v2"))
24        throw new Exception(String.Format("Unable to establish connection to GlobalSign Digital " + 
25            "Signing Service. {0} (ErrorCode: 0x{1:x}).", secure.ErrorMessage, secure.ErrorCode));
26
27    // Add time-stamp
28    using (Signature signature = new Signature())
29    {
30        signature.TimeStampURL = "urn:ietf:rfc:3161";
31        secure.AddTimeStampSignature(signature);
32    }
33
34    // Save output document
35    if (!secure.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption, 0, "", ""))
36        throw new Exception(String.Format("Unable to add time-stamp to document {0}. " + 
37            "{1} (ErrorCode: 0x{2:x}).", outputPath, secure.ErrorMessage, secure.ErrorCode));
38
39    // Cleanup
40    secure.Close();
41    secure.EndSession();
42}
43
Download code sample
1// Create secure object
2secure = new Secure();
3
4// Open input file 
5if (!secure.open(inputPath, ""))
6    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).", 
7        inputPath, secure.getErrorMessage(), secure.getErrorCode()));
8
9// Required: insert your account credentials' key parameter for the login request
10secure.setSessionPropertyString("api_key", "****fill in****");
11// Required: insert your account credentials' secret parameter for the login request
12secure.setSessionPropertyString("api_secret", "****fill in****");
13// Required: parameter to create the signing certificate
14secure.setSessionPropertyString("Identity", "{ }"); 
15// Required: insert byte array of SSL client certificate in PKCS#12 Format (.p12, .pfx)
16secure.setSessionPropertyBytes("SSLClientCertificate", 
17    Files.readAllBytes(Paths.get("****fill in****")));
18// Optional: password to decrypt the private key of the SSL client certificate
19secure.setSessionPropertyString("SSLClientCertificatePassword", "****fill in****");
20// Recommended: server's SSL certificate or its issuer (CA) certificate.
21secure.setSessionPropertyBytes("SSLServerCertificate", 
22    Files.readAllBytes(Paths.get("globalsign-root-ca.cer")));
23
24// Begin session using GlobalSign Digital Signing Service
25if (!secure.beginSession("https://emea.api.dss.globalsign.com:8443/v2"))
26    throw new IOException(String.format("Unable to establish connection to GlobalSign Digital " +
27       "Signing Service. %s (ErrorCode: 0x%08x).", secure.getErrorMessage(), 
28       secure.getErrorCode()));
29
30// Add time-stamp
31signature = new Signature();
32
33signature.setTimeStampURL("urn:ietf:rfc:3161");
34secure.addTimeStampSignature(signature);
35
36// Save output document
37if (!secure.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption, 0, "", ""))
38    throw new IOException(String.format("Unable to add time-stamp to document %s. " +
39        "%s (ErrorCode: 0x%08x).", outputPath, secure.getErrorMessage(), secure.getErrorCode()));
40
41// Cleanup
42secure.close();
43
Download code sample

Put time-stamp on a PDF using myBica Digital Signing Service

1// Create secure object
2pSecure = PdfSecureCreateObject();
3
4// Open input file
5if (!PdfSecureOpen(pSecure, szInputPath, _T("")))
6{
7    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
8    iReturnValue = 1;
9    goto cleanup;
10}
11
12// Required: insert missing value ‹customer name›:‹key identity›
13PdfSecureSetSessionPropertyString(pSecure, _T("Identity"), _T("****fill in****"));
14// Required: set DSSProfile
15PdfSecureSetSessionPropertyString(pSecure, _T("DSSProfile"), _T("http://www.pdf-tools.com/dss/profile/pades/1.0"));
16// Required: insert byte array of SSL client certificate in PKCS#12 Format (.p12, .pfx)
17PdfSecureSetSessionPropertyString(pSecure, _T("SSLClientCertificate"), _T("****fill in****"));
18// Optional: insert password to decrypt the private key of the SSL client certificate
19PdfSecureSetSessionPropertyString(pSecure, _T("SSLClientCertificatePassword"), _T("****fill in****"));
20// Recommended: Path to server's SSL certificate or its issuer (CA) certificate.
21PdfSecureSetSessionPropertyString(pSecure, _T("SSLServerCertificate"), _T("swisssign-root-ca.cer"));
22// Recommended, insert any string that can be used to track the request, e.g. an UUID
23PdfSecureSetSessionPropertyString(pSecure, _T("RequestID"), _T("1"));
24
25// Begin session using myBica Digital Signing Service
26if (!PdfSecureBeginSession(pSecure, _T("https://sign.mybica.ch/DS/DS")))
27{
28    _tprintf(_T("Unable to establish connection to myBica Digital Signing Service. %s (ErrorCode: 0x%08x).\n"), PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
29    iReturnValue = 1;
30    goto cleanup;
31}
32
33// Create signature object
34pTimeStamp = PdfSignatureCreateObject();
35
36// Add time-stamp
37PdfSignatureSetTimeStampURL(pTimeStamp, _T("http://tsa.swisssign.net"));
38PdfSecureAddTimeStampSignature(pSecure, pTimeStamp);
39
40// Save document
41if (!PdfSecureSaveAs(pSecure, szOutputPath, _T(""), _T(""), ePermNoEncryption, 0, _T(""), _T("")))
42{
43    _tprintf(_T("Unable to put time-stamp to document %s. %s (ErrorCode: 0x%08x).\n"), szOutputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
44    iReturnValue = 1;
45    goto cleanup;
46}
47
48// Cleanup
49PdfSecureClose(pSecure);
50PdfSecureEndSession(pSecure);
51
Download code sample
1// Create secure object
2using (Secure secure = new Secure())
3{
4    // Open input file
5    if (!secure.Open(inputPath, ""))
6        throw new Exception(String.Format("Input file {0} cannot be opened. " + 
7            "{1} (ErrorCode: 0x{2:x}).", inputPath, secure.ErrorMessage, secure.ErrorCode));
8
9    // Required: identity of the signing certificate
10    secure.SetSessionPropertyString("Identity", "****fill in****");
11    // Required: set DSSProfile
12    secure.SetSessionPropertyString("DSSProfile", "http://www.pdf-tools.com/dss/profile/pades/1.0");
13    // Required: insert byte array of SSL client certificate in PKCS#12 Format (.p12, .pfx)
14    secure.SetSessionPropertyBytes("SSLClientCertificate", File.ReadAllBytes("****fill in****"));
15    // Optional: password to decrypt the private key of the SSL client certificate
16    secure.SetSessionPropertyString("SSLClientCertificatePassword", "****fill in****");
17    // Recommended: Path to server's SSL certificate or its issuer (CA) certificate.
18    secure.SetSessionPropertyString("SSLServerCertificate", "swisssign-root-ca.cer");
19
20    // Create a Request-ID 
21    Guid requestId = Guid.NewGuid();
22    secure.SetSessionPropertyString("RequestID", requestId.ToString("D"));
23
24    // Begin session using myBica Digital Signing Service
25    if (!secure.BeginSession(@"https://sign.mybica.ch/DS/DS"))
26        throw new Exception(String.Format("Unable to establish connection to myBica Digital " + 
27            "Signing Service. {0} (ErrorCode: 0x{1:x}).", secure.ErrorMessage, secure.ErrorCode));
28
29    // Add time-stamp
30    using (Signature signature = new Signature())
31    {
32        signature.TimeStampURL = "http://tsa.swisssign.net";
33        secure.AddTimeStampSignature(signature);
34    }
35
36    // Save document
37    if (!secure.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption, 0, "", ""))
38        throw new Exception(String.Format("Unable to put time-stamp to document {0}. " + 
39            "{1} (ErrorCode: 0x{2:x}, Request-ID: {3}).", outputPath, secure.ErrorMessage, 
40            secure.ErrorCode, requestId));
41
42    // Cleanup
43    secure.Close();
44    secure.EndSession();
45}
46
Download code sample
1// Create secure object
2secure = new Secure();
3
4// Open input file 
5if (!secure.open(inputPath, ""))
6    throw new IOException(String.format("Input file %s cannot be opened. " + 
7            "%s (ErrorCode: 0x%08x).", inputPath, secure.getErrorMessage(), secure.getErrorCode()));
8
9// Required: identity of the signing certificate
10secure.setSessionPropertyString("Identity", "****fill in****");
11// Required: set DSSProfile
12secure.setSessionPropertyString("DSSProfile", "http://www.pdf-tools.com/dss/profile/pades/1.0");
13// Required: insert byte array of SSL client certificate in PKCS#12 Format (.p12, .pfx)
14secure.setSessionPropertyBytes("SSLClientCertificate", 
15        Files.readAllBytes(Paths.get("*****fill in*****")));
16// Optional: insert password to decrypt the private key of the SSL client certificate
17secure.setSessionPropertyString("SSLClientCertificatePassword", "****fill in****");
18// Recommended: Path to server's SSL certificate or its issuer (CA) certificate.
19secure.setSessionPropertyString("SSLServerCertificate", "swisssign-root-ca.cer"); 
20
21// Create a Request-ID
22UUID requestId = UUID.randomUUID();
23secure.setSessionPropertyString("RequestID", requestId.toString());
24
25// Begin session using myBica Digital Signing Service
26if (!secure.beginSession("https://sign.mybica.ch/DS/DS"))
27    throw new IOException(String.format("Unable to establish connection to myBica Digital " +
28            "Service. %s (ErrorCode: 0x%08x).", secure.getErrorMessage(), secure.getErrorCode()));
29
30// Add time-stamp
31signature = new Signature();
32
33signature.setTimeStampURL("http://tsa.swisssign.net");
34secure.addTimeStampSignature(signature);
35
36// Sign document and put time-stamp
37if (!secure.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption, 0, "", ""))
38    throw new IOException(String.format("Unable to put time-stamp to document %s. " + 
39            "%s (ErrorCode: 0x%08x, Request-ID: %s).", outputPath, secure.getErrorMessage(), 
40            secure.getErrorCode(), requestId));
41
42// Cleanup
43secure.close();
44secure.endSession();
45
Download code sample

Put time-stamp on a PDF using PKCS#11 Provider

1// Create secure object
2pSecure = PdfSecureCreateObject();
3
4// Open input file
5if (!PdfSecureOpen(pSecure, szInputPath, _T("")))
6{
7    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
8    iReturnValue = 1;
9    goto cleanup;
10}
11
12// Create signature object
13pTimeStamp = PdfSignatureCreateObject();
14
15// Add time-stamp
16// Begin session using PKCS#11
17// General: "PathToDll;SlotId;Pin"
18PdfSignatureSetProvider(pTimeStamp, _T("****fill in****"));
19PdfSignatureSetTimeStampURL(pTimeStamp, _T("http://tsa.swisssign.net"));
20PdfSecureAddSignature(pSecure, pTimeStamp);
21
22// Put time-stamp to document
23if (!PdfSecureSaveAs(pSecure, szOutputPath, _T(""), _T(""), ePermNoEncryption, 0, _T(""), _T("")))
24{
25    _tprintf(_T("Unable to add time-stamp to document %s. %s (ErrorCode: 0x%08x).\n"), szOutputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
26    iReturnValue = 1;
27    goto cleanup;
28}
29
30// Cleanup
31PdfSecureClose(pSecure);
32
Download code sample
1// Create secure object
2using (Secure secure = new Secure())
3{
4    // Open input file
5    if (!secure.Open(inputPath, ""))
6        throw new Exception(String.Format("Input file {0} cannot be opened. " + 
7            "{1} (ErrorCode: 0x{2:x}).", inputPath, secure.ErrorMessage, secure.ErrorCode));
8
9    // Add time-stamp
10    using (Signature timeStamp = new Signature())
11    {
12        // Begin session using PKCS#11
13        // General: "PathToDll;SlotId;Pin"
14        timeStamp.Provider = "****fill in****";
15        timeStamp.TimeStampURL = "http://tsa.swisssign.net";
16        secure.AddTimeStampSignature(timeStamp);
17    }
18
19    // Put time-stamp to document
20    if (!secure.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption, 0, "", ""))
21        throw new Exception(String.Format("Unable to add time-stamp to document {0}. " + 
22            "{1} (ErrorCode: 0x{2:x}).", outputPath, secure.ErrorMessage, secure.ErrorCode));
23
24    // Cleanup
25    secure.Close();                   
26}
27
Download code sample
1// Create secure object
2secure = new Secure();
3
4// Open input file 
5if (!secure.open(inputPath, ""))
6    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
7        inputPath, secure.getErrorMessage(), secure.getErrorCode()));
8
9// Add time-stamp
10timeStamp = new Signature();
11
12// Begin session using PKCS#11
13// General: "PathToDll;SlotId;Pin"
14timeStamp.setProvider("*****fill in*****");
15timeStamp.setTimeStampURL("http://tsa.swisssign.net");
16secure.addTimeStampSignature(timeStamp);
17
18// Put time-stamp to document
19if (!secure.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption, 0, "", ""))
20    throw new IOException(String.format("Unable to add time-stamp to document %s. " +
21        "%s (ErrorCode: 0x%08x).", outputPath, secure.getErrorMessage(), secure.getErrorCode()));
22
23// Cleanup
24secure.close();
25
Download code sample

Put time-stamp on a PDF using Swisscom All-in Signing Service

1// Create secure object
2pSecure = PdfSecureCreateObject();
3
4// Open input file
5if (!PdfSecureOpen(pSecure, szInputPath, _T("")))
6{
7    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
8    iReturnValue = 1;
9    goto cleanup;
10}
11
12// Required: insert missing value ‹customer name›:‹key identity›
13PdfSecureSetSessionPropertyString(pSecure, _T("Identity"), _T("****fill in****"));
14// Required: set DSSProfile
15PdfSecureSetSessionPropertyString(pSecure, _T("DSSProfile"), _T("http://ais.swisscom.ch/1.0"));
16// Required: insert byte array of SSL client certificate in PKCS#12 Format (.p12, .pfx)
17PdfSecureSetSessionPropertyString(pSecure, _T("SSLClientCertificate"), _T("****fill in****"));
18// Optional: insert password to decrypt the private key of the SSL client certificate
19PdfSecureSetSessionPropertyString(pSecure, _T("SSLClientCertificatePassword"), _T("****fill in****"));
20// Recommended: Path to server's SSL certificate or its issuer (CA) certificate.
21PdfSecureSetSessionPropertyString(pSecure, _T("SSLServerCertificate"), _T("ais.swisscom-root-ca.cer"));
22// Recommended, insert any string that can be used to track the request
23PdfSecureSetSessionPropertyString(pSecure, _T("RequestID"), _T("AE57F021-C0EB-4AE0-8E5E-67FB93E5BC7F"));
24
25// Begin session using Swisscom All-in Signing Service
26if (!PdfSecureBeginSession(pSecure, _T("https://ais.swisscom.com/AIS-Server/rs/v1.0/sign")))
27{
28    _tprintf(_T("Unable to establish connection to Swisscom All-in Signing Service. %s (ErrorCode: 0x%08x).\n"), PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
29    iReturnValue = 1;
30    goto cleanup;
31}
32
33// Create signature object
34pTimeStamp = PdfSignatureCreateObject();
35
36// Add time-stamp
37// Required, name of the signer
38PdfSignatureSetTimeStampURL(pTimeStamp, _T("urn:ietf:rfc:3161"));
39PdfSecureAddTimeStampSignature(pSecure, pTimeStamp);
40
41// Sign document and put time-stamp
42if (!PdfSecureSaveAs(pSecure, szOutputPath, _T(""), _T(""), ePermNoEncryption, 0, _T(""), _T("")))
43{
44    _tprintf(_T("Unable to sign document %s and to put a time-stamp. %s (ErrorCode: 0x%08x).\n"), szOutputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
45    iReturnValue = 1;
46    goto cleanup;
47}
48
49// Cleanup
50PdfSecureClose(pSecure);
51PdfSecureEndSession(pSecure);
52
Download code sample
1// Create secure object
2using (Secure secure = new Secure())
3{
4    // Open input file
5    if (!secure.Open(inputPath, ""))
6        throw new Exception(String.Format("Input file {0} cannot be opened. " + 
7            "{1} (ErrorCode: 0x{2:x}).", inputPath, secure.ErrorMessage, secure.ErrorCode));
8
9    // Required: insert missing value ‹customer name›:‹key identity›
10    secure.SetSessionPropertyString("Identity", "****fill in****");
11    // Required: set DSSProfile
12    secure.SetSessionPropertyString("DSSProfile", "http://ais.swisscom.ch/1.0");
13    // Required: insert byte array of SSL client certificate in PKCS#12 Format (.p12, .pfx)
14    secure.SetSessionPropertyBytes("SSLClientCertificate", File.ReadAllBytes("****fill in****"));
15    // Optional: insert password to decrypt the private key of the SSL client certificate
16    secure.SetSessionPropertyString("SSLClientCertificatePassword", "****fill in****");
17    // Recommended: set server's SSL certificate or its issuer (CA) certificate.
18    secure.SetSessionPropertyBytes("SSLServerCertificate", File.ReadAllBytes("ais.swisscom-root-ca.cer"));
19
20    // Create a Request-ID 
21    Guid requestId = Guid.NewGuid();
22    secure.SetSessionPropertyString("RequestID", requestId.ToString("D"));
23
24    // Begin session using Swisscom All-in Signing Service
25    if (!secure.BeginSession(@"https://ais.swisscom.com/AIS-Server/rs/v1.0/sign"))
26        throw new Exception(String.Format("Unable to establish connection to Swisscom All-in " + 
27            "Signing Service. {0} (ErrorCode: 0x{1:x}).", secure.ErrorMessage, secure.ErrorCode));
28
29    // Add time-stamp
30    using (Signature timeStamp = new Signature())
31    {
32        // Required, name of the signer
33        timeStamp.TimeStampURL = "urn:ietf:rfc:3161";
34        secure.AddTimeStampSignature(timeStamp);
35    }
36
37    // Sign document and put time-stamp
38    if (!secure.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption, 0, "", ""))
39        throw new Exception(String.Format("Unable to sign document {0} and to put a time-stamp. " +
40            "{1} (ErrorCode: 0x{2:x}, Request-ID: {3}).", outputPath, secure.ErrorMessage, 
41            secure.ErrorCode, requestId));
42
43    // Cleanup
44    secure.Close();
45    secure.EndSession();
46}
47
Download code sample
1// Create secure object
2secure = new Secure();
3
4// Open input file 
5if (!secure.open(inputPath, ""))
6    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
7            inputPath, secure.getErrorMessage(), secure.getErrorCode()));
8
9// Required: insert missing value <customer name>:<key identity>
10secure.setSessionPropertyString("Identity", "****fill in****");
11// Required: set DSSProfile
12secure.setSessionPropertyString("DSSProfile", "http://ais.swisscom.ch/1.0");
13// Required: insert byte array of SSL client certificate in PKCS#12 Format (.p12, .pfx)
14secure.setSessionPropertyBytes("SSLClientCertificate", 
15        Files.readAllBytes(Paths.get("*****fill in*****")));
16// Optional: insert password to decrypt the private key of the SSL client certificate
17secure.setSessionPropertyString("SSLClientCertificatePassword", "****fill in****");
18// Recommended: set server's SSL certificate or its issuer (CA) certificate.
19secure.setSessionPropertyBytes("SSLServerCertificate",
20        Files.readAllBytes(Paths.get("ais.swisscom-root-ca.cer")));
21
22// Create a Request-ID
23UUID requestId = UUID.randomUUID();
24secure.setSessionPropertyString("RequestID", requestId.toString());
25
26// Begin session using Swisscom All-in Signing Service
27if (!secure.beginSession("https://ais.swisscom.com/AIS-Server/rs/v1.0/sign"))
28    throw new IOException(String.format("Unable to establish connection to Swisscom All-in Signing "
29            + "Service. %s (ErrorCode: 0x%08x).", secure.getErrorMessage(), secure.getErrorCode()));
30
31// Add time-stamp
32timeStamp = new Signature();
33
34// Required, name of the signer
35timeStamp.setTimeStampURL("urn:ietf:rfc:3161");
36secure.addTimeStampSignature(timeStamp);
37
38// Sign document and put time-stamp
39if (!secure.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption, 0, "", ""))
40    throw new IOException(String.format("Unable to sign document %s and to put a time-stamp. " + 
41            "%s (ErrorCode: 0x%08x, Request-ID: %s).", outputPath, secure.getErrorMessage(), 
42            secure.getErrorCode(), requestId));
43
44// Cleanup
45secure.close();
46secure.endSession();
47
Download code sample

PAdES

Create PAdES-B-LTA signature on a PDF document

1// Create secure object
2pSecure = PdfSecureCreateObject();
3
4// Open input file
5if (!PdfSecureOpen(pSecure, szInputPath, _T("")))
6{
7    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
8    iReturnValue = 1;
9    goto cleanup;
10}
11
12// Begin session using PKCS#11 or Windows Cryptographic Provider
13// General for PKCS#11: "PathToDll;SloId;Pin"
14if (!PdfSecureBeginSession(pSecure, "****fill in****"))
15{
16    _tprintf(_T("Unable to connect to selected cryptographic provider. %s (ErrorCode: 0x%08x).\n"), PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
17    iReturnValue = 1;
18    goto cleanup;
19}
20
21// Add validation information for every signature
22for (int i = 0; i < PdfSecureGetSignatureCount(pSecure); i++)
23{
24    pSignature = PdfSecureGetSignature(pSecure, i);
25    if (PdfSignatureGetHasSignature(pSignature) && !PdfSecureAddValidationInformation(pSecure, pSignature))
26    {
27        _tprintf(_T("Error adding validation information to \"%s \": %s\n"), PdfSignatureGetName(pSignature), PdfSecureGetErrorMessage(pSecure));
28        iReturnValue = 1;
29        goto cleanup;
30    }
31    PdfSignatureDestroyObject(pSignature);
32}
33
34// Create signature object
35pTimeStamp = PdfSignatureCreateObject();
36
37// Add time-stamp
38PdfSignatureSetTimeStampURL(pTimeStamp, _T("http://tsa.swisssign.net"));
39PdfSecureAddTimeStampSignature(pSecure, pTimeStamp);
40
41// Create PAdES-B-LTA compliant document
42if (!PdfSecureSaveAs(pSecure, szOutputPath, _T(""), _T(""), ePermNoEncryption, 0, _T(""), _T("")))
43{
44    _tprintf(_T("Error creating PAdES-B-LTE signature for %s. %s (ErrorCode: 0x%08x).\n"), szOutputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
45    iReturnValue = 1;
46    goto cleanup;
47}
48
49// Cleanup
50PdfSecureClose(pSecure);
51PdfSecureEndSession(pSecure);
52
Download code sample
1// Create secure object
2using (Secure secure = new Secure())
3{
4    // Open input file
5    if (!secure.Open(inputPath, ""))
6        throw new Exception(String.Format("Input file {0} cannot be opened. " + 
7            "{1} (ErrorCode: 0x{2:x}).", inputPath, secure.ErrorMessage, secure.ErrorCode));
8
9    // Begin session using PKCS#11 or Windows Cryptographic Provider
10    // General for PKCS#11: "PathToDll;SloId;Pin"
11    if (!secure.BeginSession("****fill in****"))
12        throw new Exception(String.Format("Unable to connect to selected cryptographic provider." + 
13            " {0} (ErrorCode: 0x{1:x}).", secure.ErrorMessage, secure.ErrorCode));
14
15    // Add validation information for every signature
16    for (int i = 0; i < secure.SignatureCount; i++)
17    {
18        using (Signature signature = secure.GetSignature(i))
19        {
20            if (signature.HasSignature && !secure.AddValidationInformation(signature))
21                throw new Exception("Error adding validation information to \"" + signature.Name + 
22                    "\": " + secure.ErrorMessage);
23        }
24    }
25
26    // Add time-stamp
27    using (Signature timeStamp = new Signature())
28    {
29        timeStamp.TimeStampURL = "http://tsa.swisssign.net";
30        secure.AddTimeStampSignature(timeStamp);
31    }
32
33    // Create PAdES-B-LTA compliant document
34    if (!secure.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption, 0, "", ""))
35        throw new Exception(String.Format("Error creating PAdES-B-LTA signature for {0}. " + 
36            "{1} (ErrorCode: 0x{2:x}).", outputPath, secure.ErrorMessage, secure.ErrorCode));
37
38    // Cleanup
39    secure.Close();
40    secure.EndSession();
41}
42
Download code sample
1// Create secure object
2secure = new Secure();
3
4// Open input file 
5if (!secure.open(inputPath, ""))
6    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).", 
7            inputPath, secure.getErrorMessage(), secure.getErrorCode()));
8
9// Begin session using PKCS#11 or Windows Cryptographic Provider
10// General for PKCS#11: "PathToDll;SloId;Pin"
11if (!secure.beginSession("*****fill in*****")) 
12    throw new IOException(String.format("Unable to connect to selected cryptographic provider. " + 
13            "%s (ErrorCode: 0x%08x).", secure.getErrorMessage(), secure.getErrorCode()));
14
15// Add validation information for every signature
16for (int i=0; i < secure.getSignatureCount(); i++)
17{
18    Signature signature = secure.getSignature(i);
19    if (signature.getHasSignature() && !secure.addValidationInformation(signature))
20        throw new IOException("Error adding validation information to \"" + signature.getName() + 
21                "\": " + secure.getErrorMessage());
22    signature.destroyObject();
23}
24
25// Add time-stamp 
26timeStamp = new Signature();
27timeStamp.setTimeStampURL("http://tsa.swisssign.net");
28secure.addTimeStampSignature(timeStamp);
29
30// Create PAdES-B-LTA compliant document
31if (!secure.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption, 0, "", ""))
32    throw new IOException(String.format("Error creating PAdES-B-LTA signature for %s. %s " + 
33            "(ErrorCode: 0x%08x).", outputPath, secure.getErrorMessage(), secure.getErrorCode()));
34
35// Cleanup
36secure.close();
37secure.endSession();
38
Download code sample

Create PAdES-B-T signature on PDF document

1// Create secure object
2pSecure = PdfSecureCreateObject();
3
4// Open input file
5if (!PdfSecureOpen(pSecure, szInputPath, _T("")))
6{
7    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
8    iReturnValue = 1;
9    goto cleanup;
10}
11
12// Begin session using PKCS#11 or Windows Cryptographic Provider
13// General for PKCS#11: "PathToDll;SloId;Pin"
14if (!PdfSecureBeginSession(pSecure, "****fill in****"))
15{
16    _tprintf(_T("Unable to connect to selected cryptographic provider. %s (ErrorCode: 0x%08x).\n"), PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
17    iReturnValue = 1;
18    goto cleanup;
19}
20
21// Create signature object
22pSignature = PdfSignatureCreateObject();
23
24PdfSignatureSetName(pSignature, szCertificate);
25PdfSignatureSetSubFilter(pSignature, _T("ETSI.CAdES.detached"));
26PdfSignatureSetTimeStampURL(pSignature, _T("http://tsa.swisssign.net"));
27PdfSecureAddSignature(pSecure, pSignature);
28
29// Create PAdES-B-T compliant document
30if (!PdfSecureSaveAs(pSecure, szOutputPath, _T(""), _T(""), ePermNoEncryption, 0, _T(""), _T("")))
31{
32    _tprintf(_T("Error creating PAdES-B-T signature for %s. %s (ErrorCode: 0x%08x).\n"), szOutputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
33    iReturnValue = 1;
34    goto cleanup;
35}
36
37// Cleanup
38PdfSecureClose(pSecure);
39PdfSecureEndSession(pSecure);
40
Download code sample
1// Create secure object
2using (Secure secure = new Secure())
3{
4    // Open input file
5    if (!secure.Open(inputPath, ""))
6        throw new Exception(String.Format("Input file {0} cannot be opened. " + 
7            "{1} (ErrorCode: 0x{2:x}).", inputPath, secure.ErrorMessage, secure.ErrorCode));
8
9    // Begin session using PKCS#11 or Windows Cryptographic Provider
10    // General for PKCS#11: "PathToDll;SlotId;Pin"
11    if (!secure.BeginSession("****fill in****")) 
12        throw new Exception(String.Format("Unable to connect to selected cryptographic provider. " +
13            "{0} (ErrorCode: 0x{1:x}).", secure.ErrorMessage, secure.ErrorCode));
14
15    // Create signature object
16    using (Signature signature = new Signature())
17    {
18        signature.Name = certificate;
19        signature.SubFilter = "ETSI.CAdES.detached";
20        signature.TimeStampURL = "http://tsa.swisssign.net";
21        secure.AddSignature(signature);
22
23        // Create PAdES-B-T compliant document
24        if (!secure.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption, 0, "", ""))
25            throw new Exception(String.Format("Error creating PAdES-B-T signature for {0}. " + 
26                "{1} (ErrorCode: 0x{2:x}).", outputPath, secure.ErrorMessage, secure.ErrorCode));
27    }
28
29    // Cleanup
30    secure.Close();
31    secure.EndSession();                    
32}
33
Download code sample
1// Create secure object
2secure = new Secure();
3
4// Open input file 
5if (!secure.open(inputPath, ""))
6    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).", 
7            inputPath, secure.getErrorMessage(), secure.getErrorCode()));
8
9// Begin session using PKCS#11 or Windows Cryptographic Provider
10// General for PKCS#11: "PathToDll;SloId;Pin"
11if (!secure.beginSession("*****fill in*****")) 
12    throw new IOException(String.format("Unable to connect to selected cryptographic provider. " + 
13            "%s (ErrorCode: 0x%08x).", secure.getErrorMessage(), secure.getErrorCode()));
14
15// Create signature object
16signature = new Signature();
17
18signature.setName(certificate);
19signature.setSubFilter("ETSI.CAdES.detached");
20signature.setTimeStampURL("http://tsa.swisssign.net");
21secure.addSignature(signature);
22
23// Create PAdES-B-T compliant document
24if (!secure.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption, 0, "", ""))
25    throw new IOException(String.format("Error creating PAdES-B-T signature for %s. %s " +
26            "(ErrorCode: 0x%08x).", outputPath, secure.getErrorMessage(), secure.getErrorCode()));
27
28// Cleanup
29secure.close();
30secure.endSession();
31
Download code sample

Signatures Validation

Validate digital signatures

1// Create secure object
2pSecure = PdfSecureCreateObject();
3
4// Open input file
5if (!PdfSecureOpen(pSecure, szInputPath, _T("")))
6{
7    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
8    iReturnValue = 1;
9    goto cleanup;
10}
11
12// Begin session with Windows Cryptographic Provider
13if (!PdfSecureBeginSession(pSecure, _T("")))
14{
15    _tprintf(_T("Unable to connect to Cryptographic Provider. %s (ErrorCode: 0x%08x).\n"), PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
16    iReturnValue = 1;
17    goto cleanup;
18}
19
20// Iterate through all signatures
21int iLatestSignedRevision = -1;
22for (int iSigNo = 0; iSigNo < PdfSecureGetSignatureCount(pSecure); iSigNo++)
23{
24    pSignature = PdfSecureGetSignature(pSecure, iSigNo);
25
26    _tprintf(_T("  %d: "), iSigNo + 1);
27
28    if (PdfSignatureGetHasSignature(pSignature))
29    {
30        // Validate signature
31        bool bOk = PdfSecureValidateSignature(pSecure, pSignature);
32
33        // Process dependent checks
34        switch (PdfSecureGetErrorCode(pSecure))
35        {
36        case SIG_VAL_W_ISSUERCERT:
37        case SIG_VAL_W_TSP:
38        case SIG_VAL_W_TSPCERT:
39        case SIG_VAL_W_NOTRUSTCHAIN:
40        case SIG_VAL_W_PADES:
41            bOk = false;
42            break;
43        }
44
45        _tcscpy(szIssuer, PdfSignatureGetIssuer(pSignature));
46        _tprintf(_T("Signed by \"%s\" of \"%s\" on %s, "), PdfSignatureGetName(pSignature), szIssuer, PdfSignatureGetDate(pSignature));
47
48        if (bOk)
49        {
50            // Signature is valid
51            _tprintf(_T("Signature is valid.\n"));
52        }
53        else
54        {
55            // Signature is invalid
56            _tprintf(_T("%s\n"), PdfSecureGetErrorMessage(pSecure));
57        }
58
59        if (PdfSignatureGetRevision(pSignature) > iLatestSignedRevision)
60            iLatestSignedRevision = PdfSignatureGetRevision(pSignature);
61    }
62    else
63    {
64        // Field is not signed yet
65        _tprintf(_T("Field \"%s\" not signed.\n"), PdfSignatureGetFieldName(pSignature));
66    }
67}
68PdfSignatureDestroyObject(pSignature);
69
70// Check if securement has been modified after last signature
71if (iLatestSignedRevision != -1 && iLatestSignedRevision < PdfSecureGetRevisionCount(pSecure))
72{
73    _tprintf(_T("Document hast been updated after last signature.\n"));
74    iReturnValue = 1;
75    goto cleanup;
76}
77
78// Cleanup
79PdfSecureClose(pSecure);
80PdfSecureEndSession(pSecure);
81
Download code sample
1// Create secure object
2using (Secure secure = new Secure())
3{
4    // Open input file
5    if (!secure.Open(inputPath, ""))
6        throw new Exception(String.Format("Input file {0} cannot be opened. " + 
7            "{1} (ErrorCode: 0x{2:x}).", inputPath, secure.ErrorMessage, secure.ErrorCode));
8
9    // Begin session using Windows Cryptographic Provider
10    if (!secure.BeginSession(""))
11        throw new Exception(String.Format("Unable to connect to Windows Cryptographic Provider. " + 
12            "{0} (ErrorCode: 0x{1:x}).", secure.ErrorMessage, secure.ErrorCode));
13
14    // Iterate through all signatures
15    int latestSignedRevision = -1;
16    for (int sigNo = 0; sigNo < secure.SignatureCount; sigNo++)
17    {
18        using (Signature signature = secure.GetSignature(sigNo))
19        {
20            Console.Write("  {0}: ", sigNo + 1);
21            if (signature.HasSignature)
22            {
23                // Validate signature
24                bool ok = secure.ValidateSignature(signature);
25
26                // Process dependent checks
27                switch (secure.ErrorCode)
28                {
29                    case PDFErrorCode.SIG_VAL_W_ISSUERCERT:
30                    case PDFErrorCode.SIG_VAL_W_TSP:
31                    case PDFErrorCode.SIG_VAL_W_TSPCERT:
32                    case PDFErrorCode.SIG_VAL_W_NOTRUSTCHAIN:
33                    case PDFErrorCode.SIG_VAL_W_PADES:
34                        ok = false;
35                        break;
36                }
37
38                Console.Write("Signed by \"{0}\" of \"{1}\" on {2}, ", signature.Name, 
39                    signature.Issuer, signature.Date);
40                if (ok)
41                {
42                    // Signature is valid
43                    Console.WriteLine("Signature is valid.");
44                }
45                else
46                {
47                    // Signature is invalid
48                    Console.WriteLine(secure.ErrorMessage);
49                }
50
51                if (signature.Revision > latestSignedRevision)
52                    latestSignedRevision = signature.Revision;
53            }
54            else
55            {
56                // Field is not signed yet
57                Console.WriteLine("Field \"{0}\" not signed.", signature.FieldName);
58            }
59        }
60    }
61
62    // Check if securement has been modified after last signature
63    if (latestSignedRevision != -1 && latestSignedRevision < secure.RevisionCount - 1)
64        Console.WriteLine("Document has been updated after last signature");
65
66    // Cleanup
67    secure.Close();
68    secure.EndSession();                   
69}
70
Download code sample
1// Create secure object
2secure = new Secure();
3
4// Open input file 
5if (!secure.open(inputPath, ""))
6    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
7            inputPath, secure.getErrorMessage(), secure.getErrorCode()));
8
9// Begin session with Windows Cryptographic Provider
10if (!secure.beginSession(""))
11    throw new IOException(String.format("Unable to connect to Cryptographic Provider. " +
12            "%s (ErrorCode: 0x%08x).", secure.getErrorMessage(), secure.getErrorCode()));
13
14// Iterate through all signatures
15int latestSignedRevision = -1;
16for (int sigNo = 0; sigNo < secure.getSignatureCount(); sigNo++)
17{
18    Signature signature = secure.getSignature(sigNo);
19    try
20    {
21        System.out.printf("  %d: ", sigNo + 1);
22        if (signature.getHasSignature())
23        {
24            // Validate signature
25            boolean ok = secure.validateSignature(signature);
26
27            // Process dependent checks
28            switch (secure.getErrorCode())
29            {
30            case NativeLibrary.ERRORCODE.SIG_VAL_W_ISSUERCERT:
31            case NativeLibrary.ERRORCODE.SIG_VAL_W_TSP:
32            case NativeLibrary.ERRORCODE.SIG_VAL_W_TSPCERT:
33            case NativeLibrary.ERRORCODE.SIG_VAL_W_NOTRUSTCHAIN:
34            case NativeLibrary.ERRORCODE.SIG_VAL_W_PADES:
35                ok = false;
36                break;
37            }
38
39            System.out.printf("Signed by \"%s\" of \"%s\" on %s, ", signature.getName(), 
40                    signature.getIssuer(), signature.getDate());
41            if (ok)
42            {
43                // Signature is valid
44                System.out.println("Signature is valid.");
45            }
46            else
47            {
48                // Signature is invalid
49                System.out.println(secure.getErrorMessage());
50            }
51
52            if (signature.getRevision() > latestSignedRevision)
53                latestSignedRevision = signature.getRevision();
54        }
55        else
56        {
57            // Field is not signed yet
58            System.out.printf("Field \"%s\" not signed.\n", signature.getFieldName());
59        }
60    }                
61    finally
62    {
63        // Clean up 
64        if (signature != null)
65            signature.destroyObject();
66    }
67}
68
69// Check if document has been modified after last signature
70if (latestSignedRevision != -1 && latestSignedRevision < secure.getRevisionCount() - 1)
71    System.out.println("Document has been updated after last signature");
72
73// Cleanup
74secure.close();
75secure.endSession();
76
Download code sample

Stamping

Revise draft document by setting a stamp

1// Create secure object
2pSecure = PdfSecureCreateObject();
3
4// Open input file
5if (!PdfSecureOpen(pSecure, szInputPath, _T("")))
6{
7    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
8    iReturnValue = 1;
9    goto cleanup;
10}
11
12// Set revision stamp
13if (!PdfSecureAddStamps(pSecure, szStampPath))
14{
15    _tprintf(_T("Unable to add stamp file %s. %s (ErrorCode: 0x%08x).\n"), szStampPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
16    iReturnValue = 1;
17    goto cleanup;
18}
19
20// Begin session with Windows Cryptographic Provider
21if (!PdfSecureBeginSession(pSecure, _T("")))
22{
23    _tprintf(_T("Unable to connect to Cryptographic Provider. %s (ErrorCode: 0x%08x).\n"), PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
24    iReturnValue = 1;
25    goto cleanup;
26}
27
28// Create signature object
29pSignature = PdfSignatureCreateObject();
30
31PdfSignatureSetName(pSignature, szCertificate);
32PdfSecureAddSignature(pSecure, pSignature);
33
34// Add stamp to document and sign it
35if (!PdfSecureSaveAs(pSecure, szOutputPath, _T(""), _T(""), ePermNoEncryption, 0, _T(""), _T("")))
36{
37    _tprintf(_T("Unable to put a stamp to document %s and to sign it. %s (ErrorCode: 0x%08x).\n"), szOutputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
38    iReturnValue = 1;
39    goto cleanup;
40}
41
42// Cleanup
43PdfSecureClose(pSecure);
44PdfSecureEndSession(pSecure);
45
Download code sample
1// Create secure object
2using (Secure secure = new Secure())
3{
4    // Open input file
5    if (!secure.Open(inputPath, ""))
6        throw new Exception(String.Format("Input file {0} cannot be opened. " + 
7            "{1} (ErrorCode: 0x{2:x}).", inputPath, secure.ErrorMessage, secure.ErrorCode));
8
9    // Set revision stamp
10    if (!secure.AddStamps(stampPath))
11        throw new Exception(String.Format("Unable to add stamp file {0}. {1} (ErrorCode: 0x{2:x}).",
12            stampPath, secure.ErrorMessage, secure.ErrorCode));
13
14    // Begin session with windows cryptographic provider
15    if (!secure.BeginSession(""))
16        throw new Exception(String.Format("Unable to connect to Windows Cryptographic Provider. " + 
17            "{0} (ErrorCode: 0x{1:x}).", secure.ErrorMessage, secure.ErrorCode));
18
19    // Create signature object
20    using (Signature signature = new Signature())
21    {
22        signature.Name = certificate;
23        secure.AddSignature(signature);
24
25        // Add stamp to document and sign it
26        if (!secure.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption, 0, "", ""))
27            throw new Exception(String.Format("Unable to put a stamp to document {0} and " + 
28                "to sign it. {1} (ErrorCode: 0x{2:x}).", outputPath, secure.ErrorMessage, 
29                secure.ErrorCode));
30    }
31
32    // Cleanup
33    secure.Close();
34    secure.EndSession();
35}
36
Download code sample
1// Create secure object
2secure = new Secure();
3
4// Open input file 
5if (!secure.open(inputPath, ""))
6    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).", 
7            inputPath, secure.getErrorMessage(), secure.getErrorCode()));
8
9// Set revision stamp
10if (!secure.addStamps(stampPath))
11    throw new IOException(String.format("Unable to add stamp file %s. %s (ErrorCode: 0x%08x).", 
12            stampPath, secure.getErrorMessage(), secure.getErrorCode()));
13
14// Begin session with Windows Cryptographic Provider
15if (!secure.beginSession("")) 
16    throw new IOException(String.format("Unable to connect to Cryptographic Provider. " + 
17            "%s (ErrorCode: 0x%08x).", secure.getErrorMessage(), secure.getErrorCode()));
18
19// Create signature object
20signature = new Signature();
21
22signature.setName(certificate);
23secure.addSignature(signature);
24
25// Add stamp to document and sign it
26if (!secure.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption, 0, "", ""))
27    throw new IOException(String.format("Unable to put a stamp to document %s and to sign it. %s " +
28            "(ErrorCode: 0x%08x).", outputPath, secure.getErrorMessage(), secure.getErrorCode()));
29
30// Cleanup
31secure.close();
32secure.endSession();
33
Download code sample

Add watermark on a PDF document

1// Create secure object
2pSecure = PdfSecureCreateObject();
3
4// Decrypt input file
5if (!PdfSecureOpen(pSecure, szInputPath, _T("")))
6{
7    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
8    iReturnValue = 1;
9    goto cleanup;
10}
11
12// Create xml for watermark stamp
13pXmlString = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" \
14    "<pdfstamp xmlns=\"http://www.pdf-tools.com/pdfstamp/\">\n" \
15    "  <stamp page=\"all\" size=\"595 842\" align=\"center middle\" " \
16    "  scale=\"relToA4\" autoorientation=\"true\" type=\"foreground\">\n" \
17    "   <rotate angle=\"55\" origin=\"298 421\">\n" \
18    "     <text mode=\"stroke\" align=\"center middle\" position=\"298 421\"\n" \
19    "       font=\"Arial,Bold\" size=\"60\"\n" \
20    "       text=\"WATERMARK TEXT\"/>\n" \
21    "   </rotate>\n" \
22    "  </stamp>\n" \
23    "</pdfstamp>\n";
24
25// Set watermark from memory
26if (!PdfSecureAddStampsMem(pSecure, (void*) pXmlString, strlen(pXmlString)))
27{
28    _tprintf(_T("Unable to add watermark file. %s (ErrorCode: 0x%08x).\n"), PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
29    iReturnValue = 1;
30    goto cleanup;
31}
32
33// Imprint watermark on document
34if (!PdfSecureSaveAs(pSecure, szOutputPath, _T(""), _T(""), ePermNoEncryption, 0, _T(""), _T("")))
35{
36    _tprintf(_T("Unable to imprint watermark on document %s. %s (ErrorCode: 0x%08x).\n"), szOutputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
37    iReturnValue = 1;
38    goto cleanup;
39}
40
41// Cleanup
42PdfSecureClose(pSecure);
43
Download code sample
1// Create secure object
2using (Secure secure = new Secure())
3{
4    // Open input file
5    if (!secure.Open(inputPath, ""))
6        throw new Exception(String.Format("Input file {0} cannot be opened. " + 
7            "{1} (ErrorCode: 0x{2:x}).", inputPath, secure.ErrorMessage, secure.ErrorCode));
8
9    // Create xml for watermark stamp
10    string xmlString = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
11                       "<pdfstamp xmlns=\"http://www.pdf-tools.com/pdfstamp/\">\n" +
12                       "  <stamp page=\"all\" size=\"595 842\" align=\"center middle\" " +
13                       "       scale=\"relToA4\" autoorientation=\"true\" type=\"foreground\">\n" +
14                       "   <rotate angle=\"55\" origin=\"298 421\">\n" +
15                       "     <text mode=\"stroke\" align=\"center middle\" position=\"298 421\"\n" +
16                       "       font=\"Arial,Bold\" size=\"60\"\n" +
17                       "       text=\"WATERMARK TEXT\"/>\n" +
18                       "   </rotate>\n" +
19                       "  </stamp>\n" +
20                       "</pdfstamp>\n";
21
22    byte[] watermark = Encoding.UTF8.GetBytes(xmlString);
23
24    // Set watermark from memory
25    if (!secure.AddStampsMem(watermark))
26        throw new Exception(String.Format("Unable to add watermark file. {0} (ErrorCode: 0x{1:x}).",
27            secure.ErrorMessage, secure.ErrorCode));
28
29    // Imprint watermark on document
30    if (!secure.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption, 0, "", ""))
31        throw new Exception(String.Format("Unable to imprint watermark on document {0}. " + 
32            "{1} (ErrorCode: 0x{2:x}).", outputPath, secure.ErrorMessage, secure.ErrorCode));
33}
34
Download code sample
1// Create secure object
2secure = new Secure();
3
4// Open input file 
5if (!secure.open(inputPath, ""))
6    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
7            inputPath, secure.getErrorMessage(), secure.getErrorCode()));
8
9// Create xml for watermark stamp
10String xmlString = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
11                   "<pdfstamp xmlns=\"http://www.pdf-tools.com/pdfstamp/\">\n" +
12                   "  <stamp page=\"all\" size=\"595 842\" align=\"center middle\" " +
13                   "        scale=\"relToA4\" autoorientation=\"true\" type=\"foreground\">\n" +
14                   "    <rotate angle=\"55\" origin=\"298 421\">\n" +
15                   "      <text mode=\"stroke\" align=\"center middle\" position=\"298 421\"\n" +
16                   "        font=\"Arial,Bold\" size=\"60\"\n" +
17                   "        text=\"WATERMARK TEXT\"/>\n" +
18                   "    </rotate>\n" +
19                   "  </stamp>\n" +
20                   "</pdfstamp>\n"; 
21
22byte[] watermark = xmlString.getBytes(Charset.forName("UTF-8"));
23
24// Set watermark from memory
25if (!secure.addStampsMem(watermark))
26    throw new IOException(String.format("Unable to add watermark file. %s (ErrorCode: 0x%08x).", 
27            secure.getErrorMessage(), secure.getErrorCode()));
28
29// Imprint watermark on document
30if (!secure.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption, 0, "", ""))
31    throw new IOException(String.format("Unable to imprint watermark on document %s. %s " + 
32            "(ErrorCode: 0x%08x).", outputPath, secure.getErrorMessage(), secure.getErrorCode()));
33
Download code sample

In Memory

Sign PDF in memory

1// Create secure object
2pSecure = PdfSecureCreateObject();
3
4// Open input file
5if (!PdfSecureOpenMem(pSecure, pInputBuffer, nLength, _T("")))
6{
7    _tprintf(_T("Input buffer cannot be opened in-memory. %s (ErrorCode: 0x%08x).\n"), PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
8    iReturnValue = 1;
9    goto cleanup;
10}
11
12// Begin session with Windows Cryptographic Provider
13if (!PdfSecureBeginSession(pSecure, _T("")))
14{
15    _tprintf(_T("Unable to connect to Cryptographic Provider. %s (ErrorCode: 0x%08x).\n"), PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
16    iReturnValue = 1;
17    goto cleanup;
18}
19
20// Create signature object
21pSignature = PdfSignatureCreateObject();
22
23PdfSignatureSetName(pSignature, szCertificate);
24PdfSecureAddSignature(pSecure, pSignature);
25
26// Save output PDF as byte array
27if (!PdfSecureSaveInMemory(pSecure, _T(""), _T(""), ePermNoEncryption, 0, _T(""), _T("")))
28{
29    _tprintf(_T("Unable to sign document %s. %s (ErrorCode: 0x%08x).\n"), szOutputPath, PdfSecureGetErrorMessage(pSecure), PdfSecureGetErrorCode(pSecure));
30    iReturnValue = 1;
31    goto cleanup;
32}
33
34// Get output PDG as byte array
35pOutputBuffer = PdfSecureGetPdf(pSecure);
36if ((pData = _tfopen(szOutputPath, _T("wb"))) == NULL)
37{
38    _tprintf(_T("Output file %s cannot be created.\n"), szOutputPath);
39    iReturnValue = 1;
40    goto cleanup;
41}
42
43// Write bytes to output file
44fwrite(pOutputBuffer->m_pData, pOutputBuffer->m_nLength, 1, pData);
45fclose(pData);
46
47// Cleanup
48PdfSecureClose(pSecure);
49PdfSecureEndSession(pSecure);
50
Download code sample
1// Create secure object
2using (Secure secure = new Secure())
3{
4    // Open input file
5    if (!secure.OpenMem(inputBuffer, ""))
6        throw new Exception(String.Format("Input buffer cannot be opened in-memory. " +
7            "{0} (ErrorCode: 0x{1:x}).", secure.ErrorMessage, secure.ErrorCode));
8
9    // Begin session with Windows Cryptographic Provider
10    if (!secure.BeginSession(""))
11        throw new Exception(String.Format("Unable to connect to Windows Cryptographic Provider. " +
12            "{0} (ErrorCode: 0x{1:x}).", secure.ErrorMessage, secure.ErrorCode));
13
14    // Create signature object
15    using (Signature signature = new Signature())
16    {
17        signature.Name = certificate;
18        secure.AddSignature(signature);
19
20        // Save output buffer in-memory
21        if (!secure.SaveInMemory("", "", PDFPermission.ePermNoEncryption, 0, "", ""))
22            throw new Exception(String.Format("Unable to sign document {0}. " +
23                "{1} (ErrorCode: 0x{2:x}).", outputPath, secure.ErrorMessage, secure.ErrorCode));
24
25        // Get output PDF as byte array
26        byte[] outputBuffer = secure.GetPdf();
27        if (outputBuffer == null)
28            throw new Exception(String.Format("Output file %s cannot be created." , outputPath));
29
30        // Write bytes to output file
31        File.WriteAllBytes(outputPath, outputBuffer);
32    }
33    // Cleanup
34    secure.Close();
35    secure.EndSession();
36}
37
Download code sample
1// Create secure object
2secure = new Secure();
3
4// Open input file
5if (!secure.openMem(inputBuffer, ""))
6    throw new Exception(String.format("Input buffer cannot be opened in-memory. " + 
7            "%s (ErrorCode: 0x%08x).", secure.getErrorMessage(), secure.getErrorCode()));
8
9// Begin session with Windows Cryptographic Provider
10if (!secure.beginSession("")) 
11    throw new IOException(String.format("Unable to connect to Cryptographic Provider. " + 
12            "%s (ErrorCode: 0x%08x).", secure.getErrorMessage(), secure.getErrorCode()));
13
14// Create signature object
15signature = new Signature();
16
17signature.setName(certificate);
18secure.addSignature(signature);
19
20// Save output buffer in-memory
21if (!secure.saveInMemory("", "", NativeLibrary.PERMISSION.ePermNoEncryption, 0, "", ""))
22    throw new IOException(String.format("Unable to sign document %s. %s (ErrorCode: 0x%08x).", 
23            outputPath, secure.getErrorMessage(), secure.getErrorCode()));
24
25// Get output PDF as byte array
26byte[] outputBuffer = secure.getPdf();
27if (outputBuffer == null)
28    throw new IOException(String.format("Output file %s cannot be created.", outputPath));
29
30// Write bytes to output file
31Files.write(Paths.get(outputPath), outputBuffer, StandardOpenOption.CREATE_NEW);
32
33// Cleanup
34secure.close();
35secure.endSession();
36
Download code sample