Skip to content

Commit 6589e4b

Browse files
authored
Merge pull request #62 from SyncfusionExamples/WPF-899019
899019 - How to show a custom password dialog box for the password protected document in PdfViewer
2 parents d195f12 + 451762e commit 6589e4b

16 files changed

+667
-0
lines changed

Custom-password-dialog-box/App.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
5+
</startup>
6+
</configuration>

Custom-password-dialog-box/App.xaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Application x:Class="CustomPasswordDialog.App"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:local="clr-namespace:CustomPasswordDialog"
5+
StartupUri="MainWindow.xaml">
6+
<Application.Resources>
7+
8+
</Application.Resources>
9+
</Application>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Windows;
2+
3+
namespace CustomPasswordDialog
4+
{
5+
/// <summary>
6+
/// Interaction logic for App.xaml
7+
/// </summary>
8+
public partial class App : Application
9+
{
10+
}
11+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{16097611-C1FC-42D6-B7EF-0321487CC62C}</ProjectGuid>
8+
<OutputType>WinExe</OutputType>
9+
<RootNamespace>CustomPasswordDialog</RootNamespace>
10+
<AssemblyName>CustomPasswordDialog</AssemblyName>
11+
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
14+
<WarningLevel>4</WarningLevel>
15+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
16+
<Deterministic>true</Deterministic>
17+
<TargetFrameworkProfile />
18+
</PropertyGroup>
19+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
20+
<PlatformTarget>AnyCPU</PlatformTarget>
21+
<DebugSymbols>true</DebugSymbols>
22+
<DebugType>full</DebugType>
23+
<Optimize>false</Optimize>
24+
<OutputPath>bin\Debug\</OutputPath>
25+
<DefineConstants>DEBUG;TRACE</DefineConstants>
26+
<ErrorReport>prompt</ErrorReport>
27+
<WarningLevel>4</WarningLevel>
28+
</PropertyGroup>
29+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
30+
<PlatformTarget>AnyCPU</PlatformTarget>
31+
<DebugType>pdbonly</DebugType>
32+
<Optimize>true</Optimize>
33+
<OutputPath>bin\Release\</OutputPath>
34+
<DefineConstants>TRACE</DefineConstants>
35+
<ErrorReport>prompt</ErrorReport>
36+
<WarningLevel>4</WarningLevel>
37+
</PropertyGroup>
38+
<ItemGroup>
39+
<PackageReference Include="Syncfusion.PdfViewer.WPF" Version="*" />
40+
<Reference Include="System" />
41+
<Reference Include="System.Data" />
42+
<Reference Include="System.Drawing" />
43+
<Reference Include="System.Xml" />
44+
<Reference Include="Microsoft.CSharp" />
45+
<Reference Include="System.Core" />
46+
<Reference Include="System.Xml.Linq" />
47+
<Reference Include="System.Data.DataSetExtensions" />
48+
<Reference Include="System.Net.Http" />
49+
<Reference Include="System.Xaml">
50+
<RequiredTargetFramework>4.0</RequiredTargetFramework>
51+
</Reference>
52+
<Reference Include="WindowsBase" />
53+
<Reference Include="PresentationCore" />
54+
<Reference Include="PresentationFramework" />
55+
</ItemGroup>
56+
<ItemGroup>
57+
<ApplicationDefinition Include="App.xaml">
58+
<Generator>MSBuild:Compile</Generator>
59+
<SubType>Designer</SubType>
60+
</ApplicationDefinition>
61+
<Page Include="MainWindow.xaml">
62+
<Generator>MSBuild:Compile</Generator>
63+
<SubType>Designer</SubType>
64+
</Page>
65+
<Compile Include="App.xaml.cs">
66+
<DependentUpon>App.xaml</DependentUpon>
67+
<SubType>Code</SubType>
68+
</Compile>
69+
<Compile Include="MainWindow.xaml.cs">
70+
<DependentUpon>MainWindow.xaml</DependentUpon>
71+
<SubType>Code</SubType>
72+
</Compile>
73+
</ItemGroup>
74+
<ItemGroup>
75+
<Compile Include="Properties\AssemblyInfo.cs">
76+
<SubType>Code</SubType>
77+
</Compile>
78+
<Compile Include="Properties\Resources.Designer.cs">
79+
<AutoGen>True</AutoGen>
80+
<DesignTime>True</DesignTime>
81+
<DependentUpon>Resources.resx</DependentUpon>
82+
</Compile>
83+
<Compile Include="Properties\Settings.Designer.cs">
84+
<AutoGen>True</AutoGen>
85+
<DependentUpon>Settings.settings</DependentUpon>
86+
<DesignTimeSharedInput>True</DesignTimeSharedInput>
87+
</Compile>
88+
<EmbeddedResource Include="Properties\Resources.resx">
89+
<Generator>ResXFileCodeGenerator</Generator>
90+
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
91+
</EmbeddedResource>
92+
<None Include="Properties\Settings.settings">
93+
<Generator>SettingsSingleFileGenerator</Generator>
94+
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
95+
</None>
96+
</ItemGroup>
97+
<ItemGroup>
98+
  <Page Include="PasswordDialog.xaml">
99+
    <Generator>MSBuild:Compile</Generator>
100+
    <SubType>Designer</SubType>
101+
  </Page>
102+
  <Compile Include="PasswordDialog.xaml.cs">
103+
    <DependentUpon>PasswordDialog.xaml</DependentUpon>
104+
  </Compile>
105+
</ItemGroup>
106+
107+
<ItemGroup>
108+
<None Include="App.config" />
109+
</ItemGroup>
110+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
111+
</Project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35707.178 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CustomPasswordDialog", "CustomPasswordDialog.csproj", "{16097611-C1FC-42D6-B7EF-0321487CC62C}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{16097611-C1FC-42D6-B7EF-0321487CC62C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{16097611-C1FC-42D6-B7EF-0321487CC62C}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{16097611-C1FC-42D6-B7EF-0321487CC62C}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{16097611-C1FC-42D6-B7EF-0321487CC62C}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>WinExe</OutputType>
5+
<TargetFramework>net8.0-windows</TargetFramework>
6+
<Nullable>enable</Nullable>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<UseWPF>true</UseWPF>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.PdfViewer.WPF" Version="*" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35707.178 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CustomPasswordDialog_NET", "CustomPasswordDialog_NET.csproj", "{34EA9804-D9B3-463E-A25A-886A99DD457B}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{34EA9804-D9B3-463E-A25A-886A99DD457B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{34EA9804-D9B3-463E-A25A-886A99DD457B}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{34EA9804-D9B3-463E-A25A-886A99DD457B}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{34EA9804-D9B3-463E-A25A-886A99DD457B}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<Window x:Class="CustomPasswordDialog.MainWindow"
2+
xmlns:PdfViewer="clr-namespace:Syncfusion.Windows.PdfViewer;assembly=Syncfusion.PdfViewer.WPF"
3+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
7+
xmlns:local="clr-namespace:CustomPasswordDialog"
8+
mc:Ignorable="d"
9+
Title="MainWindow" WindowState="Maximized">
10+
<Grid>
11+
<Grid.RowDefinitions>
12+
<RowDefinition Height="40"/>
13+
<RowDefinition Height="*"/>
14+
</Grid.RowDefinitions>
15+
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Grid.Row="0">
16+
<Button x:Name="openPDFbutton"
17+
Content="Open PDF"
18+
Width="70"
19+
Margin="5"
20+
Click="openPDFbutton_Click"/>
21+
</StackPanel>
22+
<PdfViewer:PdfViewerControl
23+
x:Name="pdfViewer"
24+
Grid.Row="1"
25+
GetDocumentPassword="pdfViewer_GetDocumentPassword"
26+
ErrorOccurred="pdfViewer_ErrorOccurred"/>
27+
</Grid>
28+
</Window>
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
using Microsoft.Win32;
2+
using Syncfusion.Windows.PdfViewer;
3+
using System;
4+
using System.IO;
5+
using System.Security;
6+
using System.Windows;
7+
using System.Windows.Controls.Primitives;
8+
using System.Windows.Controls;
9+
10+
namespace CustomPasswordDialog
11+
{
12+
/// <summary>
13+
/// Interaction logic for MainWindow.xaml
14+
/// </summary>
15+
public partial class MainWindow : Window
16+
{
17+
string preLoadPDF;
18+
PasswordDialog passwordDialog;
19+
public MainWindow()
20+
{
21+
InitializeComponent();
22+
pdfViewer.Loaded += PdfViewer_Loaded;
23+
}
24+
25+
private void PdfViewer_Loaded(object sender, RoutedEventArgs e)
26+
{
27+
DocumentToolbar toolbar = pdfViewer.Template.FindName("PART_Toolbar", pdfViewer) as DocumentToolbar;
28+
ToggleButton FileButton = (ToggleButton)toolbar.Template.FindName("PART_FileToggleButton", toolbar);
29+
30+
//Iterating the File Context menu and hides the Open button
31+
ContextMenu FileContextMenu = FileButton.ContextMenu;
32+
foreach (MenuItem FileMenuItem in FileContextMenu.Items)
33+
{
34+
if (FileMenuItem.Name == "PART_OpenMenuItem")
35+
FileMenuItem.Visibility = Visibility.Collapsed;
36+
}
37+
}
38+
39+
private void openPDFbutton_Click(object sender, RoutedEventArgs e)
40+
{
41+
GetPDFfromLocalStorage();
42+
}
43+
44+
private void GetPDFfromLocalStorage()
45+
{
46+
try
47+
{
48+
var openPdfDialog = new OpenFileDialog()
49+
{
50+
DefaultExt = "pdf",
51+
Filter = "Pdf files|*.pdf",
52+
Title = "Search a PDF",
53+
FilterIndex = 1,
54+
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
55+
};
56+
if (openPdfDialog.ShowDialog()==true && File.Exists(openPdfDialog.FileName))
57+
{
58+
preLoadPDF = openPdfDialog.FileName;
59+
60+
pdfViewer.ReferencePath = AppDomain.CurrentDomain.BaseDirectory;
61+
pdfViewer.Load(preLoadPDF);
62+
}
63+
}
64+
catch (System.Exception ex)
65+
{
66+
Console.WriteLine("Unexpected error" + ex.Message);
67+
}
68+
}
69+
/// <summary>
70+
/// Occurs every time when you try to open a password protected PDF file in run-time
71+
/// </summary>
72+
private void pdfViewer_GetDocumentPassword(object sender, Syncfusion.Windows.PdfViewer.GetDocumentPasswordEventArgs e)
73+
{
74+
//Initiating the added Custom password dialog box
75+
passwordDialog = new PasswordDialog();
76+
passwordDialog.ShowDialog();
77+
if (passwordDialog.DialogResult == true && !string.IsNullOrEmpty(passwordDialog.password))
78+
{
79+
SecureString secureString = ConvertToSecureString(passwordDialog.password);
80+
e.Password = secureString;
81+
}
82+
//Hnadling the GetDocumentPasssword event so that the internal event did not execute
83+
e.Handled = true;
84+
}
85+
86+
/// <summary>
87+
/// Triggered whenever the wrong password is entered for the PDF
88+
/// </summary>
89+
private void pdfViewer_ErrorOccurred(object sender, Syncfusion.Windows.PdfViewer.ErrorOccurredEventArgs args)
90+
{
91+
if (passwordDialog.DialogResult == true && args.Message == "Can't open an encrypted document. The password is invalid.")
92+
{
93+
MessageBox.Show(args.Message);
94+
//Reloads the same document whenever entered the wrong password until correct one is given
95+
pdfViewer.Load(preLoadPDF);
96+
}
97+
}
98+
/// <summary>
99+
/// Converts the recieved string to a SecureString object
100+
/// </summary>
101+
/// <param name="secureString">Password of the document in string format</param>
102+
/// <returns>Converted SecureString</returns>
103+
104+
private SecureString ConvertToSecureString(string secureString)
105+
{
106+
SecureString passsword = new SecureString();
107+
foreach (char c in secureString)
108+
{
109+
passsword.AppendChar(c);
110+
}
111+
passsword.MakeReadOnly();
112+
return passsword;
113+
}
114+
}
115+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Window x:Class="CustomPasswordDialog.PasswordDialog"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
xmlns:local="clr-namespace:CustomPasswordDialog"
7+
mc:Ignorable="d"
8+
Title="PasswordDialog" Height="200" Width="400" WindowStartupLocation="CenterScreen">
9+
<Grid Margin="10">
10+
<Grid.RowDefinitions>
11+
<RowDefinition Height="*" />
12+
<RowDefinition Height="100" />
13+
</Grid.RowDefinitions>
14+
<Grid.ColumnDefinitions>
15+
<ColumnDefinition Width="auto" />
16+
<ColumnDefinition Width="*" />
17+
</Grid.ColumnDefinitions>
18+
<TextBlock Text="Enter Your Password:" FontSize="14" Margin="5,0,0,0"/>
19+
<PasswordBox x:Name="paswordInput" Grid.Row="0" Grid.Column="1" Height="20" Width="100" Margin="20,0,0,20" HorizontalAlignment="Center"/>
20+
<StackPanel Grid.Row="1" Grid.Column="1" Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,10,0,0">
21+
<Button Content="OK" Width="75" Height="30" Margin="5" Click="OkButton_Click"/>
22+
<Button Content="Cancel" Width="75" Height="30" Margin="5" Click="CancelButton_Click"/>
23+
</StackPanel>
24+
25+
</Grid>
26+
</Window>

0 commit comments

Comments
 (0)