1

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?

Below is my code to create PDF files while exporting from Crystal Reports

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()
4

2 に答える 2

2

まだ解決策を探している人のために、 の助けを借りてそれを行う方法を見つけましたPdfSharpNuget 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);
于 2013-11-04T11:59:29.323 に答える