简体   繁体   English

以编程方式从vb.net中的水晶报表导出时如何设置密码为pdf文件?

[英]how to set password to pdf file while exporting from crystal report in vb.net programatically?

I have written a code which creates a pdf file exporting from Crystal Reports in VB.NET 2005. All the code is working fine and also PDF files are created ok, but I want to set a password to that PDF file programatically. 我编写了一个代码,用于创建从VB.NET 2005中的Crystal Reports导出的pdf文件。所有代码工作正常,PDF文件也可以创建,但我想以编程方式为该PDF文件设置密码。 Is there any solution? 有什么解决方案吗?

Below is my code to create PDF files while exporting from Crystal Reports 下面是我从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()

As far as I know, Crystal Reports can export to PDF format but can't put any password protection on the resulting file (see one of many similar posts here ). 据我所知,Crystal Reports可以导出为PDF格式,但不能对生成的文件进行任何密码保护(请参阅此处的许多类似帖子之一)。 There are third party tools that you can use to protect the resulting PDFs, but you can't do it during the export. 您可以使用第三方工具来保护生成的PDF,但在导出期间无法执行此操作。 There is one post I found that mentions a successful export with password protection, but after unsuccessfully trying to navigate the mentioned site I gave up. 有一篇文章我发现提到密码保护成功导出,但在尝试浏览上述网站失败后,我放弃了。 Check it out for yourself here and maybe you have more luck. 在这里自己检查一下,也许你有更多的运气。

Chris 克里斯

For people who are still searching for a solution, I have found a way to do it with the help of PdfSharp . 对于仍在寻找解决方案的人,我找到了一种在PdfSharp的帮助下完成这项工作的方法。 You can add pdfsharp to your project using Nuget Package Manager . 您可以使用Nuget Package Manager将pdfsharp添加到项目中。 Then just add the following code - 然后只需添加以下代码 -

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM