• 循序渐进学习iTextSharp之十一(对文档进行加密)


    在前面的循序渐进学习iTextSharp的讲解中讲到,在打开文档前可以对文档的元数据进行设置,并且可以设置文档第一页的水印、页眉和页脚。在这节中我们主要讲解如何在打开文档前对文档进行加密。要实现在打开文档前对文档进行加密则要调用iTextSharp中setEncryption函数,函数原形如下。

    public void setEncryption(boolean strength, String userPassword, String ownerPassword, int permissions);

    参数strength是用来设置是40位加密还是128位加密。通过以下两个常量来设置

    PdfWriter.STRENGTH40BITS: 40 位

    PdfWriter.STRENGTH128BITS: 128 位 (只能够适用于Acrobat Reader 5.0以上) 

    参数userPassword和参数ownerPassword可以为空(null)或0长度的字符串,如果ownerPassword为空或0长度的字符串,则ownerPassword被一个随机字符串取代。

    参数permissions设置是通过以下常量的或运算来实现的

    PdfWriter.AllowPrinting : 允许打印

    PdfWriter.AllowModifyContents :    允许修改内容

    PdfWriter.AllowCopy :              允许拷贝

    PdfWriter.AllowModifyAnnotations :       允许修改注释

    PdfWriter.AllowFillIn :              允许填充表单

    PdfWriter.AllowScreenReaders :    允许提取内容用于辅助工具

    PdfWriter.AllowAssembly :              允许文档组合

    PdfWriter.AllowDegradedPrinting :       允许低清晰度打印

    这个函数的示例如下面两个程序所示

    示例1

    using System;
    using System.IO;

    using iTextSharp.text;
    using iTextSharp.text.pdf;

    public class Chap0109 {

          public static void Main() {
                   Console.WriteLine("Chapter 1 example 9: encryption 40 bits");
          
                   Document document = new Document(PageSize.A4, 50, 50, 50, 50);
                   try {
                          PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap0109.pdf", FileMode.Create));
                          writer.setEncryption(PdfWriter.STRENGTH40BITS, null, null, PdfWriter.AllowCopy);
                          document.Open();
                          document.Add(new Paragraph("This document is Top Secret!"));
                          document.Close();
                   }
                   catch (Exception de) {
                          Console.WriteLine(de.StackTrace);
                   }
          }
    }
    其中调用
    writer.setEncryption(PdfWriter.STRENGTH40BITS, null, null, PdfWriter.AllowCopy);

    的结果如Chap0109.pdf所示, 你可以打开文档不需要任何密码,但是你不能打印、修改文档,但允许拷贝文档。

    示例2

    using System;
    using System.IO;

    using iTextSharp.text;
    using iTextSharp.text.pdf;

    public class Chap0110 {

          public static void Main() {       
                   Console.WriteLine("Chapter 1 example 10: encryption 128 bits");
          
                   Document document = new Document(PageSize.A4, 50, 50, 50, 50);
                   try {
                          PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap0110.pdf", FileMode.Create));
                      writer.setEncryption(PdfWriter.STRENGTH128BITS,"userpass", "ownerpass", PdfWriter.AllowCopy | PdfWriter.AllowPrinting);
                          document.Open();
                          document.Add(new Paragraph("This document is Top Secret!"));
                          document.Close();
                   }
                   catch (Exception de) {
                          Console.Error.WriteLine(de.StackTrace);
                   }
          }
    }
    其中调用
    writer.setEncryption(PdfWriter.STRENGTH128BITS, "userpass", "ownerpass", PdfWriter.AllowCopy | PdfWriter.AllowPrinting);

    的结果如Chap0110.pdf 所示当你打开这个文档的时候,要求你输入密码 (键入'userpass'). 

    因为在加密的时候已经允许了打印所以你打开这个文档后可以打印这个文档.
  • 相关阅读:
    第二周学习总结
    第一周学习进度总结
    淘宝网质量属性分析
    软件架构师如何工作
    寒假学习记录第十六天
    寒假学习记录第十五天
    寒假学习记录第十四天
    寒假学习记录第十三天
    三.NFS存储服务
    二.Rsync备份服务
  • 原文地址:https://www.cnblogs.com/OwenWu/p/2094835.html
Copyright © 2020-2023  润新知