Results 1 to 12 of 12

Thread: Embed Sumatra PDF Reader into a Form

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2016
    Posts
    35

    Embed Sumatra PDF Reader into a Form

    Hi there lads,
    I was wondering if it would be possible to embed Sumatra PDF reader into a given Form ?
    Code:
    https://www.sumatrapdfreader.org/free-pdf-reader.html
    I've searched fairly a lot but most examples are in C++ and to be quite honest I'm relatively new to all this

  2. #2

    Thread Starter
    Member
    Join Date
    Nov 2016
    Posts
    35

    Re: Embed Sumatra PDF Reader into a Form

    Would it be possible to run Sumatra PDF reader as a child process inside let's say a panel in my main form instead ?
    I'm just looking for the easiest way to view pdf files using my form without Adobe Acrobat Reader.
    iText Sharp isn't an option as it isn't meant to view pdf files but to generate some as far as I understand the lot.

  3. #3
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,739

    Re: Embed Sumatra PDF Reader into a Form

    I'm just looking for the easiest way to view pdf files using my form without Adobe Acrobat Reader.
    The link below will take you to a some code I posted to extract pdf page as images. You could the display the image in a picture box.

    http://www.vbforums.com/showthread.p...=1#post5130801
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  4. #4

    Thread Starter
    Member
    Join Date
    Nov 2016
    Posts
    35

    Re: Embed Sumatra PDF Reader into a Form

    Quote Originally Posted by kebo View Post
    The link below will take you to a some code I posted to extract pdf page as images. You could the display the image in a picture box.
    http://www.vbforums.com/showthread.p...=1#post5130801
    Yes indeed, I had already seen that but it relies on Ghostscript which is not installed by default. Images won't let me copy any text either.
    For the time beeing, pdf2htmlEX is serving me very well. I subsequently load the *.html file using the WebBrowser control.
    I'm just trying to find a way to load the pdf file without any conversion you see

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2016
    Posts
    35

    Re: Embed Sumatra PDF Reader into a Form

    Alright, getting near the end .....
    Based on this code, I've managed to make it work.
    Now, how can make SumatraPDF fit the panel upon start? (Process1 is running inside Panel1)
    I mean, I just want SumatraPDF to run maximized inside the panel without having to resize it myself.


    Last edited by kmlj; Jan 27th, 2017 at 06:20 AM.

  6. #6
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,739

    Re: Embed Sumatra PDF Reader into a Form

    Try using the ShowWindows api on the Sumatra window.

    http://www.pinvoke.net/default.aspx/user32.showwindow

    Code:
    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function ShowWindow(ByVal hwnd As IntPtr, ByVal nCmdShow As ShowWindowCommands) As Boolean
    End Function
    
    ShowWindow(sumatraWindowHandle, 3)' maximize=3
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  7. #7

    Thread Starter
    Member
    Join Date
    Nov 2016
    Posts
    35

    Re: Embed Sumatra PDF Reader into a Form

    Quote Originally Posted by kebo View Post
    Try using the ShowWindows api on the Sumatra window.
    Thanks for your prompt reply.

    This is the working code:
    Code:
    Imports System.Diagnostics
    Imports System.Runtime.InteropServices
    
    Public Partial Class MainForm
    <DllImport("user32.dll")> Public Shared Function SetParent(ByVal hwndChild As IntPtr, ByVal hwndNewParent As IntPtr) As Integer
    End Function
    
    Sub Button1Click(sender As Object, e As EventArgs)	
    Dim p As Process() = Process.GetProcessesByName("SumatraPDF.exe")
    Process1.StartInfo.FileName = ("e:\SumatraPDF\SumatraPDF.exe")
    Process1.Start()
    Do Until Process1.WaitForInputIdle = True
    Loop
    SetParent(Process1.MainWindowHandle, panel1.Handle)
    
    End Sub
    Now, if I use your code accordingly, this is what I've got:

    Name:  Error.jpg
Views: 1247
Size:  10.2 KB

  8. #8
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,739

    Re: Embed Sumatra PDF Reader into a Form

    Beware of copy and paste coding. Read and understand what you are coding.

    The ShowWindow declaration has an enumeration type in its signature that is not defined in your code. You can either create the enum from the information in the pInvoke link in post 6, or I think you can change the declaration from

    Code:
    ByVal nCmdShow As ShowWindowCommands
    to
    Code:
    ByVal nCmdShow As Int32
    You also have to change sumatraWindowHandle to the correct window handle.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  9. #9

    Thread Starter
    Member
    Join Date
    Nov 2016
    Posts
    35

    Re: Embed Sumatra PDF Reader into a Form

    No, no, not all
    I'm trying to understand what's going on but I'm just a beginner you see .....
    I'll try that, thanks.
    While we're still at it, I'm trying to pass an argument to SumatraPDF.exe. Let's say, I click on button1 and I want Sumatra to open file 1.pdf right away, no need for the open file dialogue.
    This works natively, but it doesn't inside the panel as it says "the system cannot find the file"
    Code:
    SumatraPDF.exe 1.pdf

  10. #10
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,739

    Re: Embed Sumatra PDF Reader into a Form

    You need to supply the file name as an Argument in Process.StartInfo

    https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  11. #11

    Thread Starter
    Member
    Join Date
    Nov 2016
    Posts
    35

    Re: Embed Sumatra PDF Reader into a Form

    Quote Originally Posted by kebo View Post
    You need to supply the file name as an Argument in Process.StartInfo
    https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx
    Thank you very much

  12. #12

    Thread Starter
    Member
    Join Date
    Nov 2016
    Posts
    35

    Re: Embed Sumatra PDF Reader into a Form

    I've ended up adapting another piece of code to my needs ...
    Code:
    Declare Auto Function SetParent Lib "user32.dll" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer
    Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    
    
    Private Const WM_SYSCOMMAND As Integer = &H112
    Private Const SC_MAXIMIZE As Integer = &HF030
    Dim proc As Process
    
    Sub Button1Click(sender As Object, e As EventArgs)
    		
    proc = Process.Start("file:///" & IO.Path.GetFullPath(".\SumatraPDF.exe") , "1.pdf")
    proc.WaitForInputIdle()
    
    SetParent(proc.MainWindowHandle, Panel1.Handle)
    SendMessage(proc.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
    
    End Sub
    This starts SumatraPDF maximized with file 1.pdf right away inside Panel1.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width