ComponentOne PdfViewer for WPF and Silverlight
In This Topic
    Encrypted Files
    In This Topic

    You can read and show an encrypted file with PdfViewer by using a password. This topic provides basic information about passwords for a PDF file as well as how to read the encrypted PDF files with C1PdfViewer.

    PDF File passwords

    The PDF file can read encrypted files with the User password in C1PDFViewer. Whereas in C1PDF the PDF file can read and write encrypted files with the User and Owner passwords.

    The owner password is used to prevent people from doing the following: printing, copying, or modifying text, adding or deleting pages in your PDF file. Once the owner password has been applied you will not be allowed to do the following: printing, content copying, and page extraction.

    The user password is used to prevent people from opening or viewing your PDF. using an owner and user password by using the LoadDocument(pdfStream, password) method to render encrypted pdf.

    Creating a Password Protected File

    To create a password protected file, use the following code:

    VB
    Copy Code
      Private Sub _btnCreate_Click(sender As Object, e As RoutedEventArgs)
     Dim dlg = New SaveFileDialog()
     dlg.Filter = "Pdf files (*.pdf)|*.pdf"
     If dlg.ShowDialog().Value Then
      Using stream = dlg.OpenFile()
       Dim pdf = New C1.Silverlight.Pdf.C1PdfDocument()
       pdf.Security.UserPassword = "thePassword"
       pdf.DrawElement(_contentGrid, pdf.PageRectangle, C1.Silverlight.Pdf.ContentAlignment.MiddleCenter, Stretch.Uniform)
       pdf.Save(stream)
       MessageBox.Show("Protected pdf saved. Password is 'thePassword'.")
      End Using
     End If
    End Sub
    
    C#
    Copy Code
      void _btnCreate_Click(object sender, RoutedEventArgs e)
            {
                var dlg = new SaveFileDialog();
                dlg.Filter = "Pdf files (*.pdf)|*.pdf";
                if (dlg.ShowDialog().Value)
                {
                    using (var stream = dlg.OpenFile())
                    {
                        var pdf = new C1.Silverlight.Pdf.C1PdfDocument();
                        pdf.Security.UserPassword = "thePassword";
                        pdf.DrawElement(_contentGrid, pdf.PageRectangle, C1.Silverlight.Pdf.ContentAlignment.MiddleCenter, Stretch.Uniform);
                        pdf.Save(stream);
                        MessageBox.Show("Protected pdf saved. Password is 'thePassword'.");
                    }
                }
            }
    

     

    Opening an Existing PDF File

    To open an existing PDF file, complete the following:

    VB
    Copy Code
     ' open an existing PDF file
    Private Sub _btnOpen_Click(sender As Object, e As RoutedEventArgs)
     Dim dlg = New OpenFileDialog()
     dlg.Filter = "Pdf files (*.pdf)|*.pdf"
     If dlg.ShowDialog().Value Then
      Dim ms = New System.IO.MemoryStream()
      Using stream = dlg.File.OpenRead()
       stream.CopyTo(ms)
      End Using
      LoadProtectedDocument(ms, Nothing)
     End If
    End Sub
    
    CS
    Copy Code
      // open an existing PDF file
            void _btnOpen_Click(object sender, RoutedEventArgs e)
            {
                var dlg = new OpenFileDialog();
                dlg.Filter = "Pdf files (*.pdf)|*.pdf";
                if (dlg.ShowDialog().Value)
                {
                    var ms = new System.IO.MemoryStream();
                    using (var stream = dlg.File.OpenRead())
                    {
                        stream.CopyTo(ms);
                    }
                    LoadProtectedDocument(ms, null);
                }
            }
    

    Loading Password Protected PDF Documents

    To load password protected PDF documents use the LoadDocument and LoadProtectedDocument methods like the following:

    VB
    Copy Code
      ' loads password-protected Pdf documents.
    ' calling this method with null for a password will open unprotected pdf files.
    ' if the pdf file is password-protected, an exeption will be thrown and caught,
    ' the user will be prompted for the actual password, and the method will call
    ' itself recursively.
    Private Sub LoadProtectedDocument(stream As System.IO.MemoryStream, password As String)
     Try
      stream.Position = 0
      _viewer.LoadDocument(stream, password)
     Catch x As Exception
      'if (x.Message.IndexOf("password") > -1)
      '{
      Dim msg = "This file seems to be password-protected." & vbCr & vbLf & "Please provide the password and try again."
      C1.Silverlight.C1PromptBox.Show(msg, "Enter Password", Function(text, result)
      If result = MessageBoxResult.OK Then
       ' try again using the password provided by the user
       LoadProtectedDocument(stream, text)
      End If
       '}
       'else
       '{
       '    throw;
       '}
    End Function)
     End Try
    End Sub
    
    C#
    Copy Code
      // loads password-protected Pdf documents.
            // calling this method with null for a password will open unprotected pdf files.
            // if the pdf file is password-protected, an exeption will be thrown and caught,
            // the user will be prompted for the actual password, and the method will call
            // itself recursively.
            void LoadProtectedDocument(System.IO.MemoryStream stream, string password)
            {
                try
                {
                    stream.Position = 0;
                    _viewer.LoadDocument(stream, password);
                }
                catch (Exception x)
                {
                    //if (x.Message.IndexOf("password") > -1)
                    //{
                        var msg = "This file seems to be password-protected.\r\nPlease provide the password and try again.";
                        C1.Silverlight.C1PromptBox.Show(msg, "Enter Password", (text, result) =>
                            {
                                if (result == MessageBoxResult.OK)
                                {
                                    // try again using the password provided by the user
                                    LoadProtectedDocument(stream, text);
                                }
                            });
                    //}
                    //else
                    //{
                    //    throw;
                    //}
                }
            }