Export with a password

Contents[Hide]

1. Overview

This sample shows how to use an extension to hook into the server event for export completed. The extension created in the sample subscribes to the ExportCompleted event, and adds a password to PDF documents that are exported.

PDF in a browser with a password.
PDF in a browser with a password.

2. Getting started

The focus of this sample is to highlight the steps necessary to add a password to a PDF export. For more information about creating an extension and hooking into server events, see the Hook into Dundas BI server events sample.

The following prerequisites must be installed on your computer:

  • Visual Studio 2013 or higher.
  • Microsoft .NET Framework 4.5.2
  • Dundas BI Server

2.1. Downloading file

To download the Export Completed Sample solution click here.

2.2. Extracting sample to SDK folder

This sample is designed to automatically publish the extension to your Dundas BI instance. First, you must extract the ExportCompletedSample.zip to the SDK folder within the instance. To extract the ExportCompletedSample.zip do the following:

  1. Find the SDK folder for your instance. It is located at [instance root]\sdk.
  2. Extract the ExportCompletedSample.zip to the SDK folder.
  3. You should see the following result:

    Samples added to SDK folder
    Samples added to SDK folder

    With the following structure:

    [instance root]
         •sdk
              •Samples
                   •ExportCompletedSample

    Important
    If the sample is not in the exact folder, it will not work correctly.

    2.3. Opening Solution

    To open the Visual Studio solution, do the following:

    1. Right click on the Microsoft Visual Studio shortcut and click run as administrator.
    2. Click File Menu and then Open Solution.
    3. Double click the solution located at: [instance root]\sdk\Samples\ExportCompletedSample\ExportCompletedSample.sln

    Note
    If you build the solution, it will be published to the instance automatically.

    3. Creating an application setting for the passwords

    The following example defines two new application settings to use as the user and owner passwords for the PDF documents:

    using Dundas.BI;
    using Dundas.BI.Configuration;
    using Dundas.BI.Data.DataRetrieval;
    using Dundas.BI.Export;
    using Dundas.BI.Extensibility;
    
       ...
    
    /// <summary>
    /// Called after the extension package is loaded during engine startup.
    /// </summary>
    public override void OnLoaded()
    {
    	// Create application settings to store the passwords.
    	CreatePasswordSettings();
    }
    
    private void CreatePasswordSettings()
    {
    	// Get the application configuration service.
    	IAppConfigService appConfigService = 
                   Engine.Current.GetService<IAppConfigService>();
    
    	// Create the properties for the user and owner passwords for the PDF document.
    
    	AppSettingProperties appSettingProperties;
    
    	appSettingProperties = new AppSettingProperties(
    		PdfUserPasswordSettingId,
    		"PdfUserPassword",
    		ExportCompletedExtensionPackageInfo.ModuleId,
    		typeof(string)
    		);
    
    	appSettingProperties.CategoryName = "CustomExtension";
    	appSettingProperties.IsEncrypted = true;
    	appSettingProperties.IsPassword = true;
    	appSettingProperties.DefaultValue = "1234";
    
    	appConfigService.RegisterSetting(appSettingProperties);
    
    	appSettingProperties = new AppSettingProperties(
    		PdfOwnerPasswordSettingId,
    		"PdfOwnerPassword",
    		ExportCompletedExtensionPackageInfo.ModuleId,
    		typeof(string)
    		);
    	appSettingProperties.CategoryName = "CustomExtension";
    	appSettingProperties.IsEncrypted = true;
    	appSettingProperties.IsPassword = true;
    	appSettingProperties.DefaultValue = "1234";
    
    	appConfigService.RegisterSetting(appSettingProperties);
    }
    
    

    4. Hooking into the ExportCompleted event

    The following example uses IEventHookService to hook into the ExportCompleted event, and then adds a password to PDF exports:

    using Dundas.BI;
    using Dundas.BI.Configuration;
    using Dundas.BI.Data.DataRetrieval;
    using Dundas.BI.Export;
    using Dundas.BI.Extensibility;
    
       ...
    
    /// <summary>
    /// Called after the extension package is loaded during engine startup.
    /// </summary>
    public override void OnLoaded()
    {
    	// Create application settings to store the passwords.
    	CreatePasswordSettings();
    
    	IEventHookService eventHookService =
    		Engine.Current.GetService<IEventHookService>();
    
    	// Hooks up event handler when a file system entry is created.
    	eventHookService.ExportCompleted += eventHookService_ExportCompleted;
    }
    
    private void eventHookService_ExportCompleted(
          object sender, Dundas.BI.Export.ExportCompletedEventArgs e)
    {
    	ExportResult result = e.ExportResult;
    
    	if (e.ExportRequest.ProviderId.Equals(
                   Dundas.BI.Export.StandardExportProviders.StandardExportProviderIds.Pdf
               )
            )
    	{
    		// The file is stored in.
    		if (e.ExportResult.Content == null && result.BlobId.HasValue)
    		{
    			IExportService exportService = Engine.Current.GetService<IExportService>();
    			result = exportService.GetExportBlobResult(result.BlobId.Value);
    		}
    
    		// Write the file back to disk to be able to manipulate it.
    		string tempFilePath =
    			Path.Combine(
    				Path.GetTempPath(),
    				Guid.NewGuid() + (".pdf")
    			);
    
    		using (FileStream tempFile = File.Create(tempFilePath))
    		{
    			result.Content.CopyTo(tempFile);
    		}
    
    		// Open an existing document.
    		PdfDocument document = PdfReader.Open(tempFilePath);
    
    		PdfSecuritySettings securitySettings = document.SecuritySettings;
    
    		// Get the application configuration service.
    		IAppConfigService appConfigService = Engine.Current.GetService<IAppConfigService>();
    
    		// Setting one of the passwords automatically 
                    // sets the security level to PdfDocumentSecurityLevel.Encrypted128Bit.
    		securitySettings.UserPassword = appConfigService.GetString(PdfUserPasswordSettingId);
    		securitySettings.OwnerPassword = appConfigService.GetString(PdfOwnerPasswordSettingId);
    
    		// Don't use 40 bit encryption unless needed for compatibility.
    		//securitySettings.DocumentSecurityLevel = PdfDocumentSecurityLevel.Encrypted40Bit;
    
    		// Restrict some rights.
    		securitySettings.PermitAccessibilityExtractContent = false;
    		securitySettings.PermitAnnotations = false;
    		securitySettings.PermitAssembleDocument = false;
    		securitySettings.PermitExtractContent = false;
    		securitySettings.PermitFormsFill = true;
    		securitySettings.PermitFullQualityPrint = true;
    		securitySettings.PermitModifyDocument = false;
    		securitySettings.PermitPrint = true;
    
    		// Save the document.
    		document.Save(tempFilePath);
    
    		// Set the UpdatedExportContentFileInfo to the new temp file path.
    		e.UpdatedExportContentFileInfo = new System.IO.FileInfo(tempFilePath);
    	}
    }
    
    

    Important
    The most important part of the code above is setting the UpdatedExportContentFileInfo to the modified file using e.UpdatedExportContentFileInfo = new System.IO.FileInfo(tempFilePath).

    5. See Also

Dundas Data Visualization, Inc.
500-250 Ferrand Drive
Toronto, ON, Canada
M3C 3G8

North America: 1.800.463.1492
International: 1.416.467.5100

Dundas Support Hours: 7am-6pm, ET, Mon-Fri