【问题标题】:how to set password to pdf file while exporting from crystal report in vb.net programatically?如何以编程方式从 vb.net 中的水晶报表导出时将密码设置为 pdf 文件?
【发布时间】:2013-10-10 09:58:39
【问题描述】:

我编写了一个代码,它创建了一个从 VB.NET 2005 中的 Crystal Reports 导出的 pdf 文件。 所有代码都运行良好,并且 PDF 文件也可以创建,但我想以编程方式为该 PDF 文件设置密码。 有什么解决办法吗?

以下是我在从 Crystal Reports 导出时创建 PDF 文件的代码

Dim CrExportOptions As ExportOptions
Dim CrDiskFileDestinationOptions As New DiskFileDestinationOptions()
Dim CrFormatTypeOptions As New PdfRtfWordFormatOptions()

CrDiskFileDestinationOptions.DiskFileName = "D:\PDFFiles\" & fileName

CrFormatTypeOptions.FirstPageNumber = 1 ' Start Page in the Report

CrFormatTypeOptions.LastPageNumber = 10 ' End Page in the Report

CrFormatTypeOptions.UsePageRange = True


CrExportOptions = CrReport.ExportOptions

With CrExportOptions

    .ExportDestinationType = ExportDestinationType.DiskFile
    .ExportFormatType = ExportFormatType.PortableDocFormat

    .DestinationOptions = CrDiskFileDestinationOptions

    .FormatOptions = CrFormatTypeOptions

End With
CrReport.Export()

【问题讨论】:

    标签: vb.net crystal-reports


    【解决方案1】:

    据我所知,Crystal Reports 可以导出为 PDF 格式,但无法对生成的文件设置任何密码保护(请参阅许多类似帖子中的一篇 here)。您可以使用第三方工具来保护生成的 PDF,但您不能在导出期间执行此操作。 我发现有一篇文章提到了使用密码保护成功导出,但在尝试导航提到的站点失败后我放弃了。亲自检查一下here,也许你有更多的运气。

    克里斯

    【讨论】:

    • ok as Crystal Report 导出时无法设置密码,但我们可以在硬盘上创建文件后设置密码。
    • 如果你搜索一下,你会发现像这样的第三方工具:pdfsharp.com
    • 这里有一个link 显示如何使用 pdfsharp 设置密码
    【解决方案2】:

    对于仍在寻找解决方案的人,我在PdfSharp 的帮助下找到了一种方法。您可以使用 Nuget 包管理器 将 pdfsharp 添加到您的项目中。然后只需添加以下代码 -

    System.IO.Stream st = CrReport.ExportToStream(ExportFormatType.PortableDocFormat);
    PdfDocument document = PdfReader.Open(st);
    
    PdfSecuritySettings securitySettings = document.SecuritySettings;
    
    // Setting one of the passwords automatically sets the security level to 
    // PdfDocumentSecurityLevel.Encrypted128Bit.
    securitySettings.UserPassword = "user";
    securitySettings.OwnerPassword = "owner";
    
    // Don´t use 40 bit encryption unless needed for compatibility reasons
    //securitySettings.DocumentSecurityLevel = PdfDocumentSecurityLevel.Encrypted40Bit;
    
    // Restrict some rights.            
    securitySettings.PermitAccessibilityExtractContent = false;
    securitySettings.PermitAnnotations = false;
    securitySettings.PermitAssembleDocument = false;
    securitySettings.PermitExtractContent = false;
    securitySettings.PermitFormsFill = true;
    securitySettings.PermitFullQualityPrint = false;
    securitySettings.PermitModifyDocument = true;
    securitySettings.PermitPrint = false;
    
    // Save the document...
    document.Save(filename);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-20
      相关资源
      最近更新 更多