C # encrypt and decrypt PDF documents (based on spire.cloud.sdk for. Net)

Time:2021-5-13

Spire.cloud.sdk for. Net provides the interface pdfsecurityapi, which can be used to encrypt and decrypt PDF documents. This article will demonstrate the specific encryption and decryption methods through C # code.

 

Tools:

  • Spire.Cloud.SDK for .NET
  • Visual Studio

 

Necessary steps:

Step 1:dllFile acquisition and importPass in the programNuget search download, import all DLLs directly.

The import effect is as follows:

 

 

 

Step 2:App IDAnd key acquisition.Create an app in the “my app” section to get the app ID and app key.

Step 3:Source document upload.In the document management section, upload the source document. Here you can create a folder and store documents in it. When no folder is created, the source document and result document are directly saved in the root directory. In this example, two folders are created to store source documents and result documents( Cloud platform provides free 10000 calls and 2G document memory)

 

C# Code examples

[example]1】EncryptionPDFfile

using System;
using Spire.Cloud.Pdf.Sdk.Client;
using Spire.Cloud.Pdf.Sdk.Api;
using System.IO;
using System.Collections.Generic;

namespace Encryt
{
    class Program
    {   
        //Configure account information
        static String appId = "App ID";
        static String appKey = "App Key";
        static String baseUrl = "https://api.e-iceblue.cn";
        static Configuration PdfConfiguration = new Configuration(appId, appKey, baseUrl);
        static PdfSecurityApi PdfSecurityApi = new PdfSecurityApi(PdfConfiguration);
        static void Main(string[] args)
        {
            string name = "sample.pdf";// Source document
            string destFilePath = "pdfsecurity/Encrypt.pdf";// Result document path (store the result document in the pdfsecurity folder)
            string userPassword = "123";// Set user password     
            string ownerPassword = "321";// Set owner password
            string keySize = "Key40Bit";// Set keysize (null if not required)
            List permissionsFlags = new List();// Set permissionsflags (null if not required)
            permissionsFlags.Add("Print");
            string folder = "input";// Source document folder
            string password = null;// Source document password
            string storage = null;

            //Call method to encrypt document
            PdfSecurityApi.EncryptDocumentInStorage(name,destFilePath,userPassword,ownerPassword,keySize,permissionsFlags,folder,storage,password);
        }        
    }
}

When the generated document is opened, you need to enter a password.

Document encryption result:

 

[example]2DecryptionPDFfile

Here, the encrypted PDF generated above is the test document.

using System;
using Spire.Cloud.Pdf.Sdk.Client;
using Spire.Cloud.Pdf.Sdk.Api;

namespace Decrypt
{
    class Program
    {
        //Configure account information
        static String appId = "App ID";
        static String appKey = "App Key";
        static String baseUrl = "https://api.e-iceblue.cn";
        static Configuration PdfConfiguration = new Configuration(appId, appKey, baseUrl);

        static PdfSecurityApi PdfSecurityApi = new PdfSecurityApi(PdfConfiguration);
        static void Main(string[] args)
        {
            string name = "Encrypt.pdf";// Source document
            string destFilePath = "pdfsecurity/Decrypt.pdf";// Path of result document (pdfsecurity is the folder of result document)
            string password = "321";// Document password (ownerpassword is required here)
            string folder = "pdfsecurity";// Source document folder
            string storage = null;

            //Call the method to decrypt the document
           PdfSecurityApi.DecryptDocumentInStorage(name,destFilePath,password,folder,storage);
        }     
    }
}

The generated document will no longer be password protected.

 

(end)

Recommended Today

Looking for frustration 1.0

I believe you have a basic understanding of trust in yesterday’s article. Today we will give a complete introduction to trust. Why choose rust It’s a language that gives everyone the ability to build reliable and efficient software. You can’t write unsafe code here (unsafe block is not in the scope of discussion). Most of […]