advertisement

Find Code  Advanced Search   

Free Newsletters:   
browse free vb code
Submit Code
ASP,  HTML, and XML
Database
Dates  and Math
Files  and Directories
Forms  and Controls
Lists,  Collections, and Arrays
Miscellaneous
Multimedia/Games
Office/VBA
Network/Internet
Registry
Screen/Graphics
String  Manipulation
System/API
Windows  2000/XP
VB.NET/ASP.NET



advertisement
Adobe Acrobat Automation With VB and Windows API

Author: Anonymous
Category: System/API
Type: Applications
Difficulty: Advanced

Version Compatibility:  Visual Basic 6   Visual Basic.NET  

More information: Have you ever wanted to include Acrobat in your VB application? That is, run Acrobat within a VB form or component? Then this is for you. I wanted to share this because Acrobat automation requires some very archaic and unconventional coding, if all you know is Visual Basic. This is a one form application. I leave it to your skills to integrate the code into a component.

This code has been viewed 100505 times.

Instructions: Copy the declarations and code below and paste directly into your VB project.


Declarations:

    Structure Rect
        Dim Left As Integer
        Dim Top As Integer
        Dim Right As Integer
        Dim Bottom As Integer
    End Structure

    Declare Function GetWindow Lib "user32" (ByVal hwnd As Integer, ByVal wCmd As Integer) As Integer
    Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Integer, ByVal hWndInsertAfter As Integer, ByVal x As Integer, _
        ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer
    Declare Function GetClientRect Lib "user32" (ByVal hwnd As Integer, ByRef lpRect As Rect) As Integer
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
    Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Integer, ByVal hWndNewParent As Integer) As Integer
    Private Declare Function GetWindowRect Lib "user32" (ByVal Hwnd As Integer, ByRef lpRect As Form1.Rect) As Integer
    Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Integer) As Integer
    Private Declare Function MoveWindow Lib "user32" (ByVal Hwnd As Integer, ByVal x As Integer, ByVal y As Integer, _
        ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal bRepaint As Integer) As Integer

    Private AcroHandle As Integer
    Private AcroXApp As Acrobat.CAcroApp
    Private AcroXAVDoc As Acrobat.CAcroAVDoc
    Private AcroXPDDoc As Acrobat.CAcroPDDoc
    Private FormApp As AFORMAUTLib.AFormApp
    Private AcroForm As AFORMAUTLib.Fields
    Private Field As AFORMAUTLib.Field

Code:

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim FileName As String
        AcroXApp = CType(CreateObject("AcroExch.App"), Acrobat.CAcroApp)
        'Removing toolbar buttons from the user interface
        With AcroXApp
            .ToolButtonRemove("Save")
            .ToolButtonRemove("Open")
            .ToolButtonRemove("AcroForm:WidgetTool")
        End With
        AcroXAVDoc = CType(CreateObject("AcroExch.AVDoc"), Acrobat.CAcroAVDoc)
        FileName = "C:\'Your path and pdf file name'"
        AcroXAVDoc.Open(FileName, "Acrobat")
        AcroHandle = FindWindow(vbNullString, "Adobe Acrobat Professional - [Acrobat]")
        SetParent(AcroHandle, Panel1.Handle.ToInt32) 'put Acrobat in winform panel.  The panel is docked to all four sides of the form.  In VB6 you need to resize the panel with the form
        'Show in winform (not in a panel) alternative to above:
        'AcroXAVDoc.OpenInWindowEx(FileName, Me.Panel1.Handle.ToInt32, 1, 1, 0, 0, 2, 0, 0, 0)
        'I've found this is not as flexible
        'The following is to fill Acrobat fields with data
        AcroXPDDoc = CType(AcroXAVDoc.GetPDDoc, Acrobat.CAcroPDDoc)
        FormApp = CType(CreateObject("AFormAut.App"), AFORMAUTLib.AFormApp)
        AcroForm = CType(FormApp.Fields, AFORMAUTLib.Fields)
        For Each Field In AcroForm
            Field.Value = "This is a test of how well this works, and if I have this right, I can be happy."
        Next
        AcroXPDDoc.ClearFlags(&H1) 'Keep Acrobat from querying a file save when component closes
        SetAcroSize(AcroHandle, Panel1)
        AcroXApp.Show()

    End Sub

    Private Sub SetAcroSize(ByRef lngHwnd As Integer, ByRef AcroPanel As System.Windows.Forms.Panel)

        'Hide the title bar and menus from the user
        'and resize Acrobat with the resizing of the form
        Dim lngX As Integer 'acro left position
        Dim lngY As Integer 'acro top position
        Dim lngW As Integer 'acro width
        Dim lngH As Integer 'acro height
        Dim AppRect As Form1.Rect
        GetWindowRect(AcroPanel.Handle.ToInt32, AppRect)
        lngX = -GetSystemMetrics(32)
        lngY = -GetSystemMetrics(33) - 37
        lngW = AppRect.Right - AppRect.Left - lngX * 2
        lngH = AppRect.Bottom - AppRect.Top - lngY * 2 - 37
        MoveWindow(lngHwnd, lngX, lngY, lngW, lngH, 1)

    End Sub

    Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize

        SetAcroSize(AcroHandle, Panel1)

    End Sub

    Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed

        'If acrobat is not properly closed, you end up with the acrobat.exe process continuing to run, 
        'but Acrobat will not be visible.  This will baffle you to no end until you "end process" 
        'with Windows task manager while testing your app.
        AcroXApp.Hide()
        SetParent(AcroHandle, 0)
        AcroXAVDoc.Close(0)
        AcroXApp.CloseAllDocs()
        AcroXApp.Exit()

    End Sub

Text Boxes

Sponsored Links