我编写了一个代码,用于创建从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()
答案 0 :(得分:2)
据我所知,Crystal Reports可以导出为PDF格式,但不能对生成的文件进行任何密码保护(请参阅许多类似帖子here之一)。您可以使用第三方工具来保护生成的PDF,但在导出期间无法执行此操作。 有一篇文章我发现提到密码保护成功导出,但在尝试浏览上述网站失败后,我放弃了。亲自检查here,也许你有更多的运气。
克里斯
答案 1 :(得分:2)
对于仍在寻找解决方案的人,我在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);