Public Class Form1

    ' Make sure that you have registered (regsvr32) the ActiveX
    ' and added it as a reference to this project (Project > Add Reference > COM)

    Dim pdf As PDFlibAX.PDFlibrary
    Dim ClassName = "PDFlibAX.PDFlibrary"

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        pdf = CreateObject(ClassName)
    End Sub
    Private Sub btnDrawText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDrawText.Click
        ' This function draws text onto a document

        ' Setup the parameters
        Dim fileName As String = "drawText.pdf"

        ' Set origin co-ordinates to top left of page
        pdf.SetOrigin(1)

        ' Call the DrawText function
        pdf.DrawText(100, 100, "Hello world from C# and the PDF Library ActiveX")

        ' Save the changes to the document on the local disk (in the debug folder)
        pdf.SaveToFile(fileName)

        ' Open PDF automatically
        System.Diagnostics.Process.Start(fileName)
    End Sub

    Private Sub btnDrawHTMLText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDrawHTMLText.Click
        ' This function draws text using HTML tags onto a document

        ' Setup the parameters
        Dim fileName As String = "drawHTMLText.pdf"

        ' Create a new document in memory with one blank page
        Dim docID As Integer = pdf.NewDocument()

        ' Call the DrawHTMLText function and specify the necessary settings.
        pdf.DrawHTMLText(40, 760, 200, "Here is some <b>bold</b>, <i>italic</i> and <u>underlined</u> text")
        pdf.DrawHTMLText(40, 730, 200, "<ul><li>This</li><li>is</li><li>a</li><li>bullet</li><li>list</li></ul>")

        ' Save the changes to the document on the local disk (in the debug folder)
        pdf.SaveToFile(fileName)

        ' Remove document from memory
        pdf.RemoveDocument(docID)

        ' Open PDF automatically
        System.Diagnostics.Process.Start(fileName)
    End Sub

    Private Sub btnGetPageText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetPageText.Click
        ' This function extracts text from a page in a document

        ' Setup the parameters
        Dim fileName As String = "sample1.pdf"

        ' Create a new document in memory with one blank page
        Dim docID As Integer = pdf.NewDocument()

        ' Load the document that you want to extract text from into memory
        pdf.LoadFromFile(fileName, "")

        ' Select the page that you want to extract text from (first page in 
        ' a document is always 1)

        pdf.SelectPage(1)

        ' Specify the variable that the text should be added to and call
        ' the GetPageText function (with the appropriate option selected)
        Dim pageText As String = pdf.GetPageText(1)

        ' Display the extracted text in a message box
        MessageBox.Show(pageText)

        ' Remove document from memory
        pdf.RemoveDocument(docID)
    End Sub

    Private Sub btnExtractPages_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExtractPages.Click
        ' This function extracts pages from a document

        ' Setup the parameters
        Dim fileName As String = "sample1.pdf"
        Dim StartPage As Integer = 1
        Dim PageCount As Integer = 1
        Dim OutputFile As String = "sample1_extracted.pdf"

        ' Load the document that you want to extract text from into memory
        Dim docID As Integer = pdf.LoadFromFile(fileName, "")

        ' The ExtractPages requires two paramters. The first paramter
        ' tells the library where to start extracting pages from and
        ' the second paramter tells the library how many pages from the
        ' starting point should be extracted.
        Dim extractPageSuccess As Integer = pdf.ExtractPages(StartPage, PageCount)

        ' You can find out if the extraction process was successfull by 
        ' checking the returned value of the ExtractPages function. If 
        ' it returns 0 then it means the page(s) could not be extracted, 
        ' other wise it will return a new document ID.
        If extractPageSuccess = 0 Then
            MessageBox.Show("Page(s) could not be extracted.")
        Else
            MessageBox.Show("Page(s) successfully extracted.")
            ' First check to see if the extraction process worked
            ' and if it did, then save the results to a new file.
            pdf.SaveToFile(OutputFile)
            System.Diagnostics.Process.Start(OutputFile)
        End If

        ' Remove document from memory
        pdf.RemoveDocument(docID)
    End Sub

    Private Sub btnPrintDocument_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintDocument.Click
        ' This function prints a document

        ' Setup general parameters
        Dim fileName As String = "sample1.pdf"

        ' Load the document that you want to print into memory
        Dim docID As Integer = pdf.LoadFromFile(fileName, "")

        ' Set the paramters for the PrintDocument function
        Dim PrinterName As String = pdf.GetDefaultPrinterName()
        Dim StartPage As Integer = 1
        ' Set first page number
        Dim EndPage As Integer = pdf.PageCount()
        ' Set last page number
        ' Set the paramters for the PrintOptions function
        Dim PageScaling As Integer = 0
        Dim AutoRotateCenter As Integer = 0
        Dim Title As String = fileName

        ' Assign the options for printing to an int variable
        Dim Options As Integer = pdf.PrintOptions(PageScaling, AutoRotateCenter, Title)

        ' Print the document with the required paramters and options specified
        pdf.PrintDocument(PrinterName, StartPage, EndPage, Options)

        ' Remove document from memory
        pdf.RemoveDocument(docID)
    End Sub

    Private Sub btnGetPrinterNames_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetPrinterNames.Click
        ' This function retrieves a list of all the local printers
        ' Setup general parameters

        ' Assign all  of the printers to a string variable
        Dim printers As String = pdf.GetPrinterNames()

        ' Display the printer names in a message box
        MessageBox.Show(printers)
    End Sub

    Private Sub btnDARenderPageToVariant_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDARenderPageToVariant.Click
        ' This function renders a page in a PDF to a variant which can be used
        ' to display the PDF in a window

        ' Setup general parameters
        Dim fileName As String = "sample1.pdf"
        Dim password As String = ""
        Dim pageNumber As Integer = 1

        ' Open the file, get a file handle
        Dim fileHandle As Integer = pdf.DAOpenFile(fileName, password)

        ' Get a reference to the first page
        Dim pageReference As Integer = pdf.DAFindPage(fileHandle, pageNumber)

        ' Height and width in points (1/72 inch)
        Dim pageHeight As Double = pdf.DAGetPageHeight(fileHandle, pageReference)
        Dim pageWidth As Double = pdf.DAGetPageWidth(fileHandle, pageReference)

        ' Work out the smallest DPI needed to fit the entire
        ' page into the PictureBox
        Dim horizontalDPI As Integer = Convert.ToInt32((PictureBox1.Width / pageHeight) * 72)
        Dim verticalDPI As Integer = Convert.ToInt32((PictureBox1.Height / pageHeight) * 72)
        Dim smallestDPI As Integer
        If horizontalDPI < verticalDPI Then
            smallestDPI = horizontalDPI
        Else
            smallestDPI = verticalDPI
        End If

        ' Render the page to BMP format into the byte array
        Dim bitmapData As Byte() = DirectCast(pdf.DARenderPageToVariant(fileHandle, pageReference, 0, smallestDPI), Byte())

        ' Put the byte array into a stream
        Dim stream As New System.IO.MemoryStream(bitmapData)

        ' Create a new Image from the stream
        PictureBox1.Image = Image.FromStream(stream)

        ' Close the file
        pdf.DACloseFile(fileHandle)
    End Sub

    Private Sub btnNewOptionalContentGroup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewOptionalContentGroup.Click
        ' This function creates a new Optional Content Group (known as a Layer in Acrobat)

        ' Setup general parameters
        Dim OutputFileName As String = "OCG1.pdf"

        ' Load the document that you want to print into memory
        Dim docID As Integer = pdf.NewDocument()

        ' Create the new Optional Content Group and assign it a name
        Dim OCG1 As Integer = pdf.NewOptionalContentGroup("OCG 1")

        Dim ContentStream As Integer = pdf.NewContentStream()
        pdf.SelectContentStream(ContentStream)
        pdf.DrawText(100, 750, "This text belongs to OCG 1.")
        pdf.SetContentStreamOptional(OCG1)
        pdf.SetOptionalContentGroupVisible(OCG1, 1)

        ' Save the new OCG to the specified file on the local disk
        pdf.SaveToFile(OutputFileName)

        ' Remove document from memory
        pdf.RemoveDocument(docID)

        System.Diagnostics.Process.Start(OutputFileName)
    End Sub

    Private Sub btnOptionalContentGroupCount_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOptionalContentGroupCount.Click
        ' This function counts the number of Optional Content Groups (aka Layers) in a document

        ' Setup general parameters
        Dim FileName As String = "OCG1.pdf"

        ' Load the document that you want to print into memory
        ' and assign the Document ID to an int variable
        Dim docID As Integer = pdf.LoadFromFile(FileName, "")

        ' Select the document using the Document ID
        pdf.SelectDocument(docID)

        ' Initiate the OptionalContentGroupCount function
        ' and assign the returned value to an int variable
        Dim OCGs As Integer = pdf.OptionalContentGroupCount()

        ' Convert the int value to a string
        Dim CountOCGs As String = Convert.ToString(OCGs)

        ' Display the number of OCGs from the file in the message box
        MessageBox.Show(CountOCGs)

        ' Remove document from memory
        pdf.RemoveDocument(docID)
    End Sub

    Private Sub btnNewOutline_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewOutline.Click
        ' This function creates a new outline (aka bookmark) in a document

        ' Setup general parameters
        Dim InputFileName As String = "sample1.pdf"
        Dim OutputFileName As String = "NewOutline.pdf"

        ' Load the document into memory
        Dim docID As Integer = pdf.LoadFromFile(InputFileName, "")

        ' Get the page height value for the position paramter in the NewOutline function
        Dim pageHeight As Double = pdf.PageHeight()

        ' Initiate the NewOutline function and set paramters
        Dim nOutline As Integer = pdf.NewOutline(0, "New Bookmark", 1, pageHeight)

        ' Check the value returned from the NewOutline function to see
        ' if it was successful.
        If nOutline = 0 Then
            MessageBox.Show("Sorry, the bookmark could not be added.")
        Else
            ' Save the new outline to the document.
            pdf.SaveToFile(OutputFileName)
            System.Diagnostics.Process.Start(OutputFileName)
        End If

        ' Remove document from memory
        pdf.RemoveDocument(docID)
    End Sub

    Private Sub btnOutlineCount_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOutlineCount.Click
        ' This function counts the number of outlines (aka bookmarks) in a document

        ' Setup general parameters
        Dim FileName As String = "NewOutline.pdf"

        ' Load the document into memory
        Dim docID As Integer = pdf.LoadFromFile(FileName, "")
        pdf.SelectDocument(docID)
        Dim numberOfOutlines As Integer = pdf.OutlineCount()
        Dim Outlines As String = Convert.ToString(numberOfOutlines)
        MessageBox.Show(Outlines)

        ' Remove document from memory
        pdf.RemoveDocument(docID)
    End Sub

    Private Sub btnMergeDocument_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMergeDocument.Click
        ' This function merges two documents together

        ' Setup general parameters
        Dim firstDoc As String = "sample1.pdf"
        Dim secondDoc As String = "sample2.pdf"
        Dim DestFileName As String = "merged_samples.pdf"

        ' Load the document into memory
        pdf.LoadFromFile(firstDoc, "")
        Dim MainDoc As Integer = pdf.SelectedDocument()
        pdf.LoadFromFile(secondDoc, "")
        Dim Append As Integer = pdf.SelectedDocument()

        pdf.SelectDocument(MainDoc)
        pdf.MergeDocument(Append)

        pdf.SaveToFile(DestFileName)
        System.Diagnostics.Process.Start(DestFileName)

        ' Remove document from memory
        pdf.RemoveDocument(MainDoc)
    End Sub

    Private Sub btnMergeFileList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMergeFileList.Click
        ' This function merges a list of documents together
        ' Setup general parameters
        Dim ListName As String = "Sample"
        Dim firstFile As String = "sample1.pdf"
        Dim secondFile As String = "sample2.pdf"
        Dim OutputFile As String = "MergeFileListSample.pdf"

        ' Add the first file to the file list
        pdf.AddToFileList(ListName, firstFile)

        ' Add the second file to the file list
        pdf.AddToFileList(ListName, secondFile)

        ' Merge all files in the file list and output a new file
        pdf.MergeFileList(ListName, OutputFile)

        System.Diagnostics.Process.Start(OutputFile)
    End Sub

    Private Sub btnGetInformation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetInformation.Click
        ' This function retrieves document information
        ' Setup general parameters
        Dim FileName As String = "sample1.pdf"

        ' Load and select document
        Dim DocumentID As Integer = pdf.LoadFromFile(FileName, "")
        pdf.SelectDocument(DocumentID)

        ' Retrieve information from document
        Dim PDFversion As String = pdf.GetInformation(0)
        Dim Author As String = pdf.GetInformation(1)
        Dim Title As String = pdf.GetInformation(2)

        ' Display retrieved information
        Dim infoArray As String() = {PDFversion, Author, Title}
        For Each info As String In infoArray
            MessageBox.Show(info)
        Next
    End Sub

    Private Sub btnFindImages_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFindImages.Click
        ' This function counts the number of images in a document
        ' Setup general parameters
        Dim FileName As String = "..\..\..\Images.pdf"

        ' Load and select document
        Dim DocumentID As Integer = pdf.LoadFromFile(FileName, "")
        pdf.SelectDocument(DocumentID)

        ' Find all images in selected document
        Dim images As Integer = pdf.FindImages()

        ' Show number of images found in documents
        Dim numberOfImages As String = Convert.ToString(images)

        MessageBox.Show(numberOfImages)

        ' Remove the document from memory
        pdf.RemoveDocument(DocumentID)
    End Sub
End Class