• C#, VB.NET如何加密PDF文档


    在日常工作中,人们通常通过加密PDF文档的方式来保护PDF文档。不管是公司还是个人,使用PDF加密术来设置一些权限是必不可少的。为了使PDF文档既可读又不能被未授权的用户所更改,一份PDF文档往往需要两个密码:所有者密码和用户密码。本文我将给大家分享如何使用一个免费版PDF组件—Free Spire.PDF,以C#/VB.NET编程的方式来快速地加密PDF文档。

    这个免费版的PDF组件是由E-iceblue公司开发的,它可以通过设置所有者密码和用户密码来加密PDF文档。所有者密码可以完全访问PDF文档,例如重置密码和权限;用户密码虽然可以允许用户打开对应的PDF文档,但也会受制于所有者设置的一些权限。

    在加密方案中,命名空间Spire.PDFDocument.Security下的PDFSecurity类的实例对象用来设置所有者密码和用户密码。

    如果您对该组件感兴趣,可以从官网下载,组件下载安装后,再加载您的PDF文档,然后就可以保护它了。

    接下来我将介绍如何以C#/VB.NET编程的方式来加密PDF文档:

    步骤1:新建一个PDF文档对象(因为我没有现有的PDF文档,所以就新建了一个)

    [C#]
    
          PdfDocument doc = new PdfDocument();

    步骤2:通过“Spire.Pdf.Security.PdfEncryptionKeySize”的枚举值来设置密钥长度。密钥长度有3种可用的类型:Key128Bit, Key256Bit 和 Key40Bit,您可以使用其中的任意一种。

    [C#]
    
           doc.Security.KeySize = PdfEncryptionKeySize.Key256Bit;

    步骤3:通过设置所有者密码和用户密码来加密PDF文档。注意:您所设置的密钥长度不能超过可用的密钥长度。

    [C#]
    
           doc.Security.OwnerPassword = "e-iceblue";
           doc.Security.UserPassword = "pdfcomponent";
    
     


    步骤4:指定用户密码的访问权限。在此方案中,有9种可用的权限,请查看下图:

                                        

    [C#]
    
           doc.Security.Permissions = PdfPermissionsFlags.Print | PdfPermissionsFlags.CopyContent;

    步骤5:保存文档

    [C#]
    
          doc.SaveToFile("result.pdf",FileFormat.PDF);


    项目运行后,当你打开这个加密的PDF文档时就需要输入密码了。请看下面的效果截图:

       

    C#完整代码:

    using Spire.Pdf;
    using Spire.Pdf.Security;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace __encryption
    {
        class Program
        {
            static void Main(string[] args)
            {
               PdfDocument doc = new PdfDocument();
               doc.Security.KeySize = PdfEncryptionKeySize.Key256Bit;
               doc.Security.OwnerPassword = "e-iceblue";
               doc.Security.UserPassword = "pdfcomponent";
               doc.Security.Permissions = PdfPermissionsFlags.Print | PdfPermissionsFlags.CopyContent;
               doc.SaveToFile("result.pdf", FileFormat.PDF);
            }
        }
    }

    VB.NET完整代码:

    Imports Spire.Pdf
    Imports Spire.Pdf.Security
    Imports System.Collections.Generic
    Imports System.Linq
    Imports System.Text
    
    Namespace __encryption
        Class Program
            Private Shared Sub Main(args As String())
                Dim doc As New PdfDocument()
                doc.Security.KeySize = PdfEncryptionKeySize.Key256Bit
                doc.Security.OwnerPassword = "e-iceblue"
                doc.Security.UserPassword = "pdfcomponent"
                doc.Security.Permissions = PdfPermissionsFlags.Print Or PdfPermissionsFlags.CopyContent
                doc.SaveToFile("result.pdf", FileFormat.PDF)
            End Sub
        End Class
    End Namespace
    
    


    希望这篇文章能给您带来一定的帮助。感谢您的浏览。

    关于C# 加密、解密PDF的视频教程,可点击链接地址查看:

    http://v.youku.com/v_show/id_XNDAyNzg3MTk5Ng==.html?spm=a2hzp.8244740.0.0

  • 相关阅读:
    Spring注解运行时抛出null
    关于apache服务器加载so的报错
    apache apr的编译和引用
    FreeSWITCH在会议室中持续播放音频文件
    64位FreeSWITCH编译安装(版本1.4.20)
    Spring整合Tiles
    eclipse启动报错eclipse failed to create the java virutal machine
    菜鸟新闻2--设置沉浸式状态栏的三种方法
    OkHttp3源码详解(三) 拦截器
    Android N(API level 24.)废弃了Html.fromHtml(String)
  • 原文地址:https://www.cnblogs.com/Yesi/p/6668529.html
Copyright © 2020-2023  润新知