Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports System.Windows.Forms
Imports System.IO
Imports ColorCode
Imports SelectPdf

Namespace SelectPdf.Samples
    Partial Public Class PdfConverterSecurity
        Inherits Form

        Private Sub BtnCreatePdf_Click(sender As Object, e As EventArgs) Handles BtnCreatePdf.Click
            Dim renderingEngine As RenderingEngine = CType([Enum].Parse(GetType(RenderingEngine), DdlRenderingEngine.SelectedItem.ToString(), True), RenderingEngine)
            Cursor = Cursors.WaitCursor
            Dim file As String = "Document.pdf"

            Try
                ' read parameters from the webpage
                Dim url As String = TxtUrl.Text

                Dim userPassword As String = TxtUserPassword.Text.Trim()
                Dim ownerPassword As String = TxtOwnerPassword.Text.Trim()

                Dim canAssembleDocument As Boolean = ChkCanAssembleDocument.Checked
                Dim canCopyContent As Boolean = ChkCanCopyContent.Checked
                Dim canEditAnnotations As Boolean = ChkCanEditAnnotations.Checked
                Dim canEditContent As Boolean = ChkCanEditContent.Checked
                Dim canFillFormFields As Boolean = ChkCanFillFormFields.Checked
                Dim canPrint As Boolean = ChkCanPrint.Checked

                ' instantiate a html to pdf converter object
                Dim converter As New HtmlToPdf()
                converter.Options.RenderingEngine = renderingEngine

                ' set document passwords
                If Not String.IsNullOrEmpty(userPassword) Then
                    converter.Options.SecurityOptions.UserPassword = userPassword
                End If
                If Not String.IsNullOrEmpty(ownerPassword) Then
                    converter.Options.SecurityOptions.OwnerPassword = ownerPassword
                End If

                'set document permissions
                converter.Options.SecurityOptions.CanAssembleDocument = canAssembleDocument
                converter.Options.SecurityOptions.CanCopyContent = canCopyContent
                converter.Options.SecurityOptions.CanEditAnnotations = canEditAnnotations
                converter.Options.SecurityOptions.CanEditContent = canEditContent
                converter.Options.SecurityOptions.CanFillFormFields = canFillFormFields
                converter.Options.SecurityOptions.CanPrint = canPrint

                ' create a new pdf document converting an url
                Dim doc As PdfDocument = converter.ConvertUrl(url)

                ' save pdf document
                doc.Save(file)

                ' close pdf document
                doc.Close()
            Catch ex As Exception
                MessageBox.Show([String].Format("Error: {0}", ex.Message), "Error", _
                                MessageBoxButtons.OK, MessageBoxIcon.[Error])
                Return
            Finally
                Cursor = Cursors.Arrow
            End Try

            ' open generated pdf
            Try
                System.Diagnostics.Process.Start(file)
            Catch
                MessageBox.Show("Could not open generated pdf document", "Error", _
                                MessageBoxButtons.OK, MessageBoxIcon.[Error])
            End Try
        End Sub
        Public Sub New()
            InitializeComponent()
            Try
                ' display highlighted sample code
                Dim code As String = Helper.ReadSourceCode("PdfConverterSecurity.vb")
                WBSampleCode.DocumentText = New CodeColorizer().Colorize(code, Languages.VbDotNet)
            Catch
                WBSampleCode.DocumentText = "Sample code not available"
            End Try
        End Sub

    End Class
End Namespace
