locked
Embedding rtf and pdf files into Visual Basic 2010

    Question

  • I would like to embed an RTF file that can be chosen with the OpenFileDialog control into my form. Where should I start looking for the easiest solution?
    Friday, December 17, 2010 12:54 PM

Answers

  • By far, the quickest and easiest way to show a PDF in a Windows Forms application is by using the Web Browser control (because IE can already host the pdf viewer automatically).

    Place a WebBrowser control and a Button on Form1 in a new project and try this code:

      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dlg As New OpenFileDialog
        dlg.Filter = "PDF Files|*.pdf"
        If dlg.ShowDialog = Windows.Forms.DialogResult.OK Then
          WebBrowser1.Navigate(dlg.FileName)
        End If
      End Sub
    
    Per your original question, the RichTextBox control can easily display RTF files using it's LoadFile() method.
    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
    • Proposed as answer by QQLove Thursday, December 23, 2010 3:17 AM
    • Marked as answer by Qw3rtyPoiuy Thursday, December 23, 2010 8:48 AM
    Wednesday, December 22, 2010 3:39 PM
    Moderator

All replies

  • Do you want to open a pdf file in your application? then try to use this code

    AdobeReader.src=openFileDialogctrl.FileName



    Please mark the replies as answers if they help. Kumar
    Friday, December 17, 2010 1:08 PM
  • Thanks for your help, but could you point me in the right direction? I'm a little confused as to which control I should use and where I would put your code. Sorry, I'm new here.

     

    I imagine I would have an "OpenFileDialog" control, a "Button" control, and a control similar to "PictureBox."

    I heard that support for pdfs was limited, so I decided that if I couldn't find a solution to embed PDFs, I'd turn my PDFs into RTF and embed those instead. 

    Friday, December 17, 2010 1:17 PM
  • if you want to preview your pdf file inside your application you need pdfviewer control. You can find this control in your Toolbox, if it is not available, right click on your toolbox then select choose items, a new control dialog box appear from there go to com components tab, select adobe pdf reader (this will be available only if you installed adobe acrobat reader) from the list. Then press ok, control will appear in your toolbox. now just drag this control in to your form.

    Here i am recommending you to create two form one for opening a pdf file and another one for pdf viewer.

    In the first form use the OpenfileDialog Control and Preview Button, in the preview button click event call the pdf viewer form 

    like :

    pdffilepathvariable = openFileDialogctrl.FileName 
    Dim frmOobj as Form 
    frmObj = New PdfViewerForm 
    frmObj.MdiParent = Me 
    frmObj.Show()

    In the PDF Viewer form load event use the bellow code

    AdobeReader.src=pdffilepathvariable
    
    hope this will help you

     

     


    Please mark the replies as answers if they help. Kumar
    • Proposed as answer by QQLove Wednesday, December 22, 2010 2:09 AM
    Friday, December 17, 2010 1:45 PM
  • I got an error when I tried to add the new control

    "Failed to create component 'AxHost'. The error message follows: 'System.Runtime.InteropServices.COMException (0x80004005): A wrapper assembly is not registered for this type library.

    at

    System.Windows.Forms.Design.DocumentDesigner.AxToolboxItem.CreateComponentsCore(IDesignerHost host)

    at

    System.Drawing.Design.ToolboxItem.CreateComponentsCore(IDesignerHost host, IDictionary defaultValues)

    at

    System.Drawing.Design.ToolboxItem.CreateComponents(IDesignerHost host, IDictionary defaultValues)

    at

    System.Windows.Forms.Design.OleDragDrophandler.CreateTool(ToolboxItem tool, Control parent, Int32 x, Int32 y, Int32 width, Int32 height, Boolean hasLocation, Boolean hasSize, ToolboxSnapDragDropEventArgs e)'

     

     

    Any suggestions on fixing this or any idea where I should look?  

    Friday, December 17, 2010 2:37 PM
  • Can you tell me which version you of Visual Studio you are using also Adobe Acrobat installed version, for my visual studio (2008) there is no problem. Also follow this link, which may help you

    http://forums.adobe.com/message/1158945

     


    Please mark the replies as answers if they help. Kumar
    Friday, December 17, 2010 2:55 PM
  • Freshly installed Microsoft Visual Basic 2010 Express and I have both Adobe Acrobat 9 Pro Extended and Adobe Reader 9.3.1 installed. 
    Friday, December 17, 2010 3:04 PM
  • Sorry i never tried with Acrobat 9, I am using Acrobat 7 and its working for, If possible just try to uninstall the current version and install acrobat 7 Profession (its only a suggestion).
    Please mark the replies as answers if they help. Kumar
    Saturday, December 18, 2010 8:18 AM
  • Hello Qw3rtyPoiuy,

     

            Welcome to MSDN Forum.

     

            I don't know why your project happen so many errors. Because I follow your steps and some codes and my project is running with no errors.  So, I guess something is wrong. Could you check your code carefully or maybe you can send your code on this web site? Waiting for your reply!

     

    Best Regards

     Rocky

    • Edited by QQLove Thursday, December 23, 2010 2:06 AM
    Wednesday, December 22, 2010 2:25 AM
  • I didn't run any code, I couldn't even get the control to work. I ended up just sticking screenshots of the pdf into an rtf document and figuring out how to use the rtfbox control. 
    Wednesday, December 22, 2010 9:43 AM
  • By far, the quickest and easiest way to show a PDF in a Windows Forms application is by using the Web Browser control (because IE can already host the pdf viewer automatically).

    Place a WebBrowser control and a Button on Form1 in a new project and try this code:

      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dlg As New OpenFileDialog
        dlg.Filter = "PDF Files|*.pdf"
        If dlg.ShowDialog = Windows.Forms.DialogResult.OK Then
          WebBrowser1.Navigate(dlg.FileName)
        End If
      End Sub
    
    Per your original question, the RichTextBox control can easily display RTF files using it's LoadFile() method.
    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
    • Proposed as answer by QQLove Thursday, December 23, 2010 3:17 AM
    • Marked as answer by Qw3rtyPoiuy Thursday, December 23, 2010 8:48 AM
    Wednesday, December 22, 2010 3:39 PM
    Moderator
  • i used this code Reed Kimble suggested and it worked fine. however i need the button to open a specific file. how can i achieve this?

    regards

    dezyne

    Wednesday, April 11, 2012 4:03 AM