none
WebBrowser-Modul - How can I open a pdf-file which is not called *.pdf? RRS feed

  • Question

  • Hi,

    i got on my form a webbrowser-modul and i want to open a pdf-file!

    But I want only to open this pdf-file only in my form. So i changed the ending from "data.pdf" to "data.zyx".

    I know how to open it in Adobe-Reader, but with this code the Adobe-Reader show the file and not my form...

    Diagnostics.Process.Start("C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe", Application.StartupPath & "\" & "data.zyx")

    I´ve learned how to open a pdf-file in my webbrowser-modul :

    WebBrowser1.Navigate(Application.StartupPath & "\" & "data.pdf" & "#toolbar=0&navpanes=0&scrollbar=0")

    But when I change the ending to .zyx it doesn´t open the file.

    WebBrowser1.Navigate(Application.StartupPath & "\" & "data.zyx" & "#toolbar=0&navpanes=0&scrollbar=0")

    How can I open the file with ending .zyx in my webbrowser as pdf?

    Thanks and best regards

    witcher7

    Thursday, June 11, 2020 4:53 PM

Answers

  • You can use "Adobe Acrobat 7.0 Browser Control Type Library 1.0" for less code

    Otherwise, by defining it manually and by using "AtlAxWin" =>

    (change the string sPDFFile)

    Public Class Form1
        Public Enum HRESULT As Integer
            S_OK = 0
            S_FALSE = 1
            E_NOINTERFACE = &H80004002
            E_NOTIMPL = &H80004001
            E_FAIL = &H80004005
            E_UNEXPECTED = &H8000FFFF
            E_OUTOFMEMORY = &H8007000E
        End Enum
    
        <DllImport("Atl.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
        Public Shared Function AtlAxWinInit() As Boolean
        End Function
    
        <DllImport("Atl.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
        Public Shared Function AtlAxGetControl(ByVal h As IntPtr, <Out> <MarshalAs(UnmanagedType.IUnknown)> ByRef pp As Object) As Integer
        End Function
    
        <DllImport("Atl.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
        Public Shared Function AtlAxGetHost(ByVal h As IntPtr, <Out> <MarshalAs(UnmanagedType.IUnknown)> ByRef pp As Object) As Integer
        End Function
    
        <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Public Shared Function CreateWindowEx(ByVal dwExStyle As Integer, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Integer, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hWndParent As IntPtr, ByVal hMenu As IntPtr, ByVal hInstance As IntPtr, ByVal lpParam As IntPtr) As IntPtr
        End Function
    
        Public Const WS_OVERLAPPED = &H0
        Public Const WS_BORDER = &H800000
        Public Const WS_POPUP = &H80000000
        Public Const WS_CHILD = &H40000000
        Public Const WS_MINIMIZE = &H20000000
        Public Const WS_VISIBLE = &H10000000
        Public Const WS_DISABLED = &H8000000
    
        Public Shared Function GetWindowLong(hWnd As IntPtr, nIndex As Integer) As Integer
            If IntPtr.Size = 4 Then
                Return GetWindowLong32(hWnd, nIndex)
            End If
            Return GetWindowLongPtr64(hWnd, nIndex)
        End Function
    
        <DllImport("User32.dll", EntryPoint:="GetWindowLong", CharSet:=CharSet.Auto)>
        Private Shared Function GetWindowLong32(hWnd As IntPtr, nIndex As Integer) As Integer
        End Function
    
        <DllImport("User32.dll", EntryPoint:="GetWindowLongPtr", CharSet:=CharSet.Auto)>
        Private Shared Function GetWindowLongPtr64(hWnd As IntPtr, nIndex As Integer) As Integer
        End Function
    
        Public Const GWL_HINSTANCE = -6
    
        <ComImport()>
        <Guid("3b813ce7-7c10-4f84-ad06-9df76d97a9aa")>
        <InterfaceType(ComInterfaceType.InterfaceIsIDispatch)>
        Public Interface IAcroAXDocShim
            <DispId(1)>
            Property src() As String
    
            <DispId(30)>
            Property messageHandler() As Object
    
            <DispId(2)>
            Function LoadFile(<MarshalAs(UnmanagedType.BStr)> <[In]()> fileName As String) As Boolean
    
            <DispId(3)>
            Sub setShowToolbar(<[In]()> [On] As Boolean)
    
            <DispId(4)>
            Sub gotoFirstPage()
    
            <DispId(5)>
            Sub gotoLastPage()
    
            <DispId(6)>
            Sub gotoNextPage()
    
            <DispId(7)>
            Sub gotoPreviousPage()
    
            <DispId(8)>
            Sub setCurrentPage(<[In]()> n As Integer)
    
            <DispId(9)>
            Sub goForwardStack()
    
            <DispId(10)>
            Sub goBackwardStack()
    
            <DispId(11)>
            Sub setPageMode(<MarshalAs(UnmanagedType.BStr)> <[In]()> pageMode As String)
    
            <DispId(12)>
            Sub setLayoutMode(<MarshalAs(UnmanagedType.BStr)> <[In]()> layoutMode As String)
    
            <DispId(13)>
            Sub setNamedDest(<MarshalAs(UnmanagedType.BStr)> <[In]()> namedDest As String)
    
            <DispId(14)>
            Sub Print()
    
            <DispId(15)>
            Sub printWithDialog()
    
            <DispId(16)>
            Sub setZoom(<[In]()> percent As Single)
    
            <DispId(17)>
            Sub setZoomScroll(<[In]()> percent As Single, <[In]()> left As Single, <[In]()> top As Single)
    
            <DispId(18)>
            Sub setView(<MarshalAs(UnmanagedType.BStr)> <[In]()> viewMode As String)
    
            <DispId(19)>
            Sub setViewScroll(<MarshalAs(UnmanagedType.BStr)> <[In]()> viewMode As String, <[In]()> offset As Single)
    
            <DispId(20)>
            Sub setViewRect(<[In]()> left As Single, <[In]()> top As Single, <[In]()> width As Single, <[In]()> height As Single)
    
            <DispId(21)>
            Sub printPages(<[In]()> from As Integer, <[In]()> [to] As Integer)
    
            <DispId(22)>
            Sub printPagesFit(<[In]()> from As Integer, <[In]()> [to] As Integer, <[In]()> shrinkToFit As Boolean)
    
            <DispId(23)>
            Sub printAll()
    
            <DispId(24)>
            Sub printAllFit(<[In]()> shrinkToFit As Boolean)
    
            <DispId(25)>
            Sub setShowScrollbars(<[In]()> [On] As Boolean)
    
            <DispId(26)>
            Function GetVersions() As <MarshalAs(UnmanagedType.Struct)>
            Object
    
            <DispId(27)>
            Sub setCurrentHightlight(<[In]()> a As Integer, <[In]()> b As Integer, <[In]()> c As Integer, <[In]()> d As Integer)
    
            <DispId(28)>
            Sub setCurrentHighlight(<[In]()> a As Integer, <[In]()> b As Integer, <[In]()> c As Integer, <[In]()> d As Integer)
    
            <DispId(29)>
            Sub postMessage(<MarshalAs(UnmanagedType.Struct)> <[In]()> strArray As Object)
    
            <DispId(31)>
            Sub execCommand(<MarshalAs(UnmanagedType.Struct)> <[In]()> strArray As Object)
        End Interface
    
        Private hWndContainer As IntPtr = IntPtr.Zero
        Friend WithEvents Button1 As Button
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            If (AtlAxWinInit()) Then
                hWndContainer = CreateWindowEx(0, "AtlAxWin", "AcroPDF.PDF.1", WS_VISIBLE Or WS_CHILD Or WS_BORDER, 10, 10, 600, 400, Handle, CType(10, IntPtr), CType(GetWindowLong(Me.Handle, GWL_HINSTANCE), IntPtr), IntPtr.Zero)
            End If
            Me.Button1 = New System.Windows.Forms.Button()
            Me.Button1.Location = New System.Drawing.Point(264, 430)
            Me.Button1.Name = "Button1"
            Me.Button1.Size = New System.Drawing.Size(75, 23)
            Me.Button1.Text = "Load PDF"
            Me.Button1.UseVisualStyleBackColor = True
            Me.Controls.Add(Me.Button1)
            Me.ClientSize = New System.Drawing.Size(620, 475)
            CenterToScreen()
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim sPDFFile As String = "E:\Sources\AcroPDF\Welcome.xyz"
            Dim pUnknown As Object = Nothing
            Dim nRet As Integer = AtlAxGetControl(hWndContainer, pUnknown)
            If (nRet = 0) Then
                Dim pAcroAXDocShim As IAcroAXDocShim = Nothing
                pAcroAXDocShim = CType(pUnknown, IAcroAXDocShim)
                Dim sSrc As String
                Dim bRet As Boolean = False
                bRet = pAcroAXDocShim.LoadFile(sPDFFile)
                sSrc = pAcroAXDocShim.src
                pAcroAXDocShim.setShowToolbar(True)
                Marshal.ReleaseComObject(pAcroAXDocShim)
            End If
        End Sub
    End Class

    • Marked as answer by witcher7 Sunday, June 14, 2020 8:38 PM
    Thursday, June 11, 2020 8:15 PM

All replies

  • Read thru this post for associating a file extension.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/630ed1d9-73f1-4cc0-bc84-04f29cffc13b/what-registry-keys-are-responsible-for-file-extension-association?forum=netfxbcl


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, June 11, 2020 5:11 PM
    Moderator
  • I don´t want to register this extension in the registry...
    Thursday, June 11, 2020 6:14 PM
  • Did you consider a simple approach as well: copying the file to Windows’ temporary folder programmatically with extension changed to “.pdf”, then navigating to this temporary file? Later you can delete the file programmatically.  

    Thursday, June 11, 2020 6:17 PM
  • You can use IAcroAXDocShim.LoadFile

    (I just tested after having renamed the extension with .xyz, it loads the .pdf correctly)

    Thursday, June 11, 2020 6:31 PM
  • this sounds great. where can I get "IAcroAXDocShim"?
    Thursday, June 11, 2020 7:05 PM
  • You can use "Adobe Acrobat 7.0 Browser Control Type Library 1.0" for less code

    Otherwise, by defining it manually and by using "AtlAxWin" =>

    (change the string sPDFFile)

    Public Class Form1
        Public Enum HRESULT As Integer
            S_OK = 0
            S_FALSE = 1
            E_NOINTERFACE = &H80004002
            E_NOTIMPL = &H80004001
            E_FAIL = &H80004005
            E_UNEXPECTED = &H8000FFFF
            E_OUTOFMEMORY = &H8007000E
        End Enum
    
        <DllImport("Atl.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
        Public Shared Function AtlAxWinInit() As Boolean
        End Function
    
        <DllImport("Atl.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
        Public Shared Function AtlAxGetControl(ByVal h As IntPtr, <Out> <MarshalAs(UnmanagedType.IUnknown)> ByRef pp As Object) As Integer
        End Function
    
        <DllImport("Atl.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
        Public Shared Function AtlAxGetHost(ByVal h As IntPtr, <Out> <MarshalAs(UnmanagedType.IUnknown)> ByRef pp As Object) As Integer
        End Function
    
        <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Public Shared Function CreateWindowEx(ByVal dwExStyle As Integer, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Integer, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hWndParent As IntPtr, ByVal hMenu As IntPtr, ByVal hInstance As IntPtr, ByVal lpParam As IntPtr) As IntPtr
        End Function
    
        Public Const WS_OVERLAPPED = &H0
        Public Const WS_BORDER = &H800000
        Public Const WS_POPUP = &H80000000
        Public Const WS_CHILD = &H40000000
        Public Const WS_MINIMIZE = &H20000000
        Public Const WS_VISIBLE = &H10000000
        Public Const WS_DISABLED = &H8000000
    
        Public Shared Function GetWindowLong(hWnd As IntPtr, nIndex As Integer) As Integer
            If IntPtr.Size = 4 Then
                Return GetWindowLong32(hWnd, nIndex)
            End If
            Return GetWindowLongPtr64(hWnd, nIndex)
        End Function
    
        <DllImport("User32.dll", EntryPoint:="GetWindowLong", CharSet:=CharSet.Auto)>
        Private Shared Function GetWindowLong32(hWnd As IntPtr, nIndex As Integer) As Integer
        End Function
    
        <DllImport("User32.dll", EntryPoint:="GetWindowLongPtr", CharSet:=CharSet.Auto)>
        Private Shared Function GetWindowLongPtr64(hWnd As IntPtr, nIndex As Integer) As Integer
        End Function
    
        Public Const GWL_HINSTANCE = -6
    
        <ComImport()>
        <Guid("3b813ce7-7c10-4f84-ad06-9df76d97a9aa")>
        <InterfaceType(ComInterfaceType.InterfaceIsIDispatch)>
        Public Interface IAcroAXDocShim
            <DispId(1)>
            Property src() As String
    
            <DispId(30)>
            Property messageHandler() As Object
    
            <DispId(2)>
            Function LoadFile(<MarshalAs(UnmanagedType.BStr)> <[In]()> fileName As String) As Boolean
    
            <DispId(3)>
            Sub setShowToolbar(<[In]()> [On] As Boolean)
    
            <DispId(4)>
            Sub gotoFirstPage()
    
            <DispId(5)>
            Sub gotoLastPage()
    
            <DispId(6)>
            Sub gotoNextPage()
    
            <DispId(7)>
            Sub gotoPreviousPage()
    
            <DispId(8)>
            Sub setCurrentPage(<[In]()> n As Integer)
    
            <DispId(9)>
            Sub goForwardStack()
    
            <DispId(10)>
            Sub goBackwardStack()
    
            <DispId(11)>
            Sub setPageMode(<MarshalAs(UnmanagedType.BStr)> <[In]()> pageMode As String)
    
            <DispId(12)>
            Sub setLayoutMode(<MarshalAs(UnmanagedType.BStr)> <[In]()> layoutMode As String)
    
            <DispId(13)>
            Sub setNamedDest(<MarshalAs(UnmanagedType.BStr)> <[In]()> namedDest As String)
    
            <DispId(14)>
            Sub Print()
    
            <DispId(15)>
            Sub printWithDialog()
    
            <DispId(16)>
            Sub setZoom(<[In]()> percent As Single)
    
            <DispId(17)>
            Sub setZoomScroll(<[In]()> percent As Single, <[In]()> left As Single, <[In]()> top As Single)
    
            <DispId(18)>
            Sub setView(<MarshalAs(UnmanagedType.BStr)> <[In]()> viewMode As String)
    
            <DispId(19)>
            Sub setViewScroll(<MarshalAs(UnmanagedType.BStr)> <[In]()> viewMode As String, <[In]()> offset As Single)
    
            <DispId(20)>
            Sub setViewRect(<[In]()> left As Single, <[In]()> top As Single, <[In]()> width As Single, <[In]()> height As Single)
    
            <DispId(21)>
            Sub printPages(<[In]()> from As Integer, <[In]()> [to] As Integer)
    
            <DispId(22)>
            Sub printPagesFit(<[In]()> from As Integer, <[In]()> [to] As Integer, <[In]()> shrinkToFit As Boolean)
    
            <DispId(23)>
            Sub printAll()
    
            <DispId(24)>
            Sub printAllFit(<[In]()> shrinkToFit As Boolean)
    
            <DispId(25)>
            Sub setShowScrollbars(<[In]()> [On] As Boolean)
    
            <DispId(26)>
            Function GetVersions() As <MarshalAs(UnmanagedType.Struct)>
            Object
    
            <DispId(27)>
            Sub setCurrentHightlight(<[In]()> a As Integer, <[In]()> b As Integer, <[In]()> c As Integer, <[In]()> d As Integer)
    
            <DispId(28)>
            Sub setCurrentHighlight(<[In]()> a As Integer, <[In]()> b As Integer, <[In]()> c As Integer, <[In]()> d As Integer)
    
            <DispId(29)>
            Sub postMessage(<MarshalAs(UnmanagedType.Struct)> <[In]()> strArray As Object)
    
            <DispId(31)>
            Sub execCommand(<MarshalAs(UnmanagedType.Struct)> <[In]()> strArray As Object)
        End Interface
    
        Private hWndContainer As IntPtr = IntPtr.Zero
        Friend WithEvents Button1 As Button
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            If (AtlAxWinInit()) Then
                hWndContainer = CreateWindowEx(0, "AtlAxWin", "AcroPDF.PDF.1", WS_VISIBLE Or WS_CHILD Or WS_BORDER, 10, 10, 600, 400, Handle, CType(10, IntPtr), CType(GetWindowLong(Me.Handle, GWL_HINSTANCE), IntPtr), IntPtr.Zero)
            End If
            Me.Button1 = New System.Windows.Forms.Button()
            Me.Button1.Location = New System.Drawing.Point(264, 430)
            Me.Button1.Name = "Button1"
            Me.Button1.Size = New System.Drawing.Size(75, 23)
            Me.Button1.Text = "Load PDF"
            Me.Button1.UseVisualStyleBackColor = True
            Me.Controls.Add(Me.Button1)
            Me.ClientSize = New System.Drawing.Size(620, 475)
            CenterToScreen()
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim sPDFFile As String = "E:\Sources\AcroPDF\Welcome.xyz"
            Dim pUnknown As Object = Nothing
            Dim nRet As Integer = AtlAxGetControl(hWndContainer, pUnknown)
            If (nRet = 0) Then
                Dim pAcroAXDocShim As IAcroAXDocShim = Nothing
                pAcroAXDocShim = CType(pUnknown, IAcroAXDocShim)
                Dim sSrc As String
                Dim bRet As Boolean = False
                bRet = pAcroAXDocShim.LoadFile(sPDFFile)
                sSrc = pAcroAXDocShim.src
                pAcroAXDocShim.setShowToolbar(True)
                Marshal.ReleaseComObject(pAcroAXDocShim)
            End If
        End Sub
    End Class

    • Marked as answer by witcher7 Sunday, June 14, 2020 8:38 PM
    Thursday, June 11, 2020 8:15 PM
  • Yes, it works with "Adobe Acrobat 7.0 Browser Control Type Library 1.0, but there is one problem...

    I tried to turn off all things, like toolbar, scrollbar and bookmarks. But the floating toolbar with printing next page and so on, is still there.

    AxAcroPDF1.setShowToolbar(False)
            AxAcroPDF1.setShowScrollbars(False)
            AxAcroPDF1.setPageMode("none")
            AxAcroPDF1.LoadFile(Application.StartupPath & "\" & "data.zyx")

    Thats why I am asking to find a free simple PDF-viewer without help from adobe.

    Friday, June 12, 2020 9:57 PM
  • If you just want to display PDF pages, you can extract them and convert them to bitmaps with

    PdfPage.RenderToStreamAsync

    (

        Add reference to : C\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd
        Add reference to : C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\System.Runtime.WindowsRuntime.dll

    )

    Saturday, June 13, 2020 9:18 AM
  • You mean you want a solution to open a PDF file without that people see that it is an Adobe property?

    Create simple a program that creates document files with the same features as the Adobe Portable format. 


    Success
    Cor

    Saturday, June 13, 2020 12:17 PM