如何在以编程方式从 vb.net 中的水晶报告导出时将密码设置为 pdf 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19292681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to set password to pdf file while exporting from crystal report in vb.net programatically?
提问by Amol Padalkar
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. Is there any solution?
我已经编写了一个代码,它创建了一个从 VB.NET 2005 中的 Crystal Reports 导出的 pdf 文件。所有代码都运行良好,而且 PDF 文件也可以正常创建,但我想以编程方式为该 PDF 文件设置密码。有什么解决办法吗?
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()
回答by campagnolo_1
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). There are third party tools that you can use to protect the resulting PDFs, but you can't do it during the export. 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 hereand maybe you have more luck.
据我所知,Crystal Reports 可以导出为 PDF 格式,但不能对生成的文件设置任何密码保护(请参阅此处的许多类似帖子之一)。您可以使用第三方工具来保护生成的 PDF,但在导出过程中不能这样做。我发现有一篇文章提到使用密码保护成功导出,但在尝试浏览提到的网站失败后,我放弃了。在这里亲自检查一下,也许你会有更多的运气。
Chris
克里斯
回答by th1rdey3
For people who are still searching for a solution, I have found a way to do it with the help of PdfSharp. You can add pdfsharp to your project using Nuget Package Manager. Then just add the following code -
对于仍在寻找解决方案的人,我找到了一种在PdfSharp. 您可以使用Nuget Package Manager将 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);

