[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 52605
  • Last Modified:

Opening a PDF web file in VB.net form.

Hello Everyone,

Here is my problem. I would like to check if a PDF file exists in a website. Then if it does to send that PDF file to my default printer. I am writing all the code in VB.net. This is what I have so far.

Public Function PdfCheck()
  Dim URL As String = "http://www.vecernjilist.hr/ARHIVA/2004/03/25/040325-11.pdf"
  Dim PDFDoc As Acrobat.CAcroPDDoc
  Try
    PDFDoc.Open(URL)
  Catch e As Exception
    MessageBox.Show(e.Message)
  End Try

‘Add printing code here..

  PDFDoc.Close()
End Function

Now my problem is that the “PDFDoc.Open(URL)” line is for opening a file on the local computer. What is the syntax for open a PDF from the web?

If anyone has ever done something like this and may give me some code I can use, I will increase points
0
DaneJurkovic
Asked:
DaneJurkovic
  • 9
  • 6
1 Solution
 
DaneJurkovicAuthor Commented:
UPDATE:

This works...


    Sub PdfCheck(ByVal URL As String)
        Dim AcroApp As Acrobat.CAcroApp
        Dim PDDoc As Acrobat.CAcroPDDoc
        Dim avDoc As Acrobat.CAcroAVDoc

        AcroApp = CreateObject("AcroExch.App")
        PDDoc = CreateObject("AcroExch.PDDoc")

        If PDDoc.Open(URL) Then
            AcroApp.Show()
            avDoc = PDDoc.OpenAVDoc("")
        Else
            MsgBox("Unable to open the PDF-file", vbInformation)
        End If

        avDoc = Nothing
        PDDoc = Nothing
        AcroApp = Nothing
    End Sub

But, only for a "local" file on the computer. i am looking for a way to open up a PDF from the web.

Thanks...
0
 
DaneJurkovicAuthor Commented:
Anyone?
0
 
PsychotextCommented:
I think the best way of doing it in this case is to buffer the pdf to your local machine / server and then send the buffer to Acrobat.  I'm not sure of the code right now, but i'll have a look.

By the way, this assumes that there isn't a method for using a remote file!  I really dont know but this will help if there isn't.
0
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
PsychotextCommented:
Try this... it's not the best code in the world, but it should work.  You'll need to give ASPNET write access to where you want the file (In this case the dir that the aspx file is in.).  You can do the same with buffering to avoid saving the file.  It still seems unlikely that the adobe object can't access urls though.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim strURL As String = "http://www.adobe.com/products/acrobat/pdfs/docsforms.pdf"

        PdfCheck(strURL)
    End Sub

    Sub PdfCheck(ByVal strURL As String)
        Dim AcroApp As Acrobat.CAcroApp
        Dim PDDoc As Acrobat.CAcroPDDoc
        Dim avDoc As Acrobat.CAcroAVDoc
        Dim wc As New WebClient()
        Dim strLocalName As String = Server.MapPath(".") & "\" & GetFileName(strURL)

        wc.DownloadFile(strURL, strLocalName)

        AcroApp = CreateObject("AcroExch.App")
        PDDoc = CreateObject("AcroExch.PDDoc")

        If PDDoc.Open(strLocalName) Then
            AcroApp.Show()
            avDoc = PDDoc.OpenAVDoc("")
        Else
            MsgBox("Unable to open the PDF-file", vbInformation)
        End If

        avDoc = Nothing
        PDDoc = Nothing
        AcroApp = Nothing
    End Sub

    Function GetFileName(ByVal URL As String) As String
        Dim strPathArray() = Split(URL, "/")

        GetFileName = strPathArray(UBound(strPathArray))
    End Function
0
 
DaneJurkovicAuthor Commented:
Psychotext,

You just about have what I got so far. Good work!!! This is my code so far......



Private Sub cmdGetPdf_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdScanDL.Click
    Dim ThisURL As String = "http://www.adobe.com/products/acrobat/pdfs/docsforms.pdf"
    Dim PDFFolder As String = "c:\TempPDF\"
    Dim LocalPdfSave As String = PDFFolder &  "LocalPdf.pdf"
    Dim DeleteFiles() As String = System.IO.Directory.GetFiles(PDFFolderDL)
    Dim DeleteThisFile As String

    For Each DeleteThisFile In DeleteFiles
        System.IO.File.Delete(DeleteThisFile)
    Next

    If PdfCheck(ThisURL, LocalPdfSave) Then
        MessageBox.Show("Done Importing PDF Files", "Import", MessageBoxButtons.OK, MessageBoxIcon.Information)
        ViewThisPDF(LocalPdfSave)
    Else
        MessageBox.Show("Error in Importing PDF files!!!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End If
End Sub

Public Function PdfCheck(ByVal URL As String, ByVal localPdfFile As String) As Boolean
    Dim wc As New WebClient
    Dim LocalError As Boolean = True

    Try
        wc.DownloadFile(URL, localPdfFile)
    Catch e As Exception
        LocalError = False
    End Try

    Return LocalError
End Function

Private Sub ViewThisPDF(ByVal ViewThisNow As String)
    Dim AcroApp As Acrobat.CAcroApp
    Dim PDDoc As Acrobat.CAcroPDDoc
    Dim avDoc As Acrobat.CAcroAVDoc

    AcroApp = CreateObject("AcroExch.App")
    PDDoc = CreateObject("AcroExch.PDDoc")

    If PDDoc.Open(ViewPDFNow) Then
        'PDDoc.Magnification(200)
        AcroApp.Show()
        AcroApp.Maximize(100)
        avDoc = PDDoc.OpenAVDoc("")
    Else
        MsgBox("Unable to open the PDF file", vbInformation)
    End If

    avDoc = Nothing
    PDDoc = Nothing
    AcroApp = Nothing
End Sub


What I am trying to figure out now is setting the magnification (zoom) to "200%". Any thoughts??
0
 
PsychotextCommented:
In this file:

http://www.pdfimage.com/pdf_ps/acroole/AcroOLE.pdf

You have to use the "AcroExch.AVPageView.ZoomTo" method to control the magnification.
0
 
PsychotextCommented:
Sorry, that should have read "Found in this file:"
0
 
DaneJurkovicAuthor Commented:
This is exactly my problem now. I can not figure out how to implement the "AcroExch.AVPageView.ZoomTo" method in my code (from above) to control the magnification of the page after it opens. Mine you this is not a web app, this is all windows forms.



The link you sent had this in it for the “ZoomTo” method.

ZoomTo
    BOOL ZoomTo(short nType, short nScale);
    Description: Zooms to a specified magnification.
    Parameters: nType
                    Zoom type
                nScale
                    The desired zoom factor
    Return Value: true or false


Many thanks for your help so far. You’re doing great!!! In helping me.
0
 
PsychotextCommented:
Sorry, I dont think I'm going to be able to help on this bit because I dont have the right components.  My assumption would be that you have to instantiate the AcroExch.AVPageView object and then use the ZoomTo method on it.  (Much as you have with the PDDoc)
0
 
PsychotextCommented:
Do you still need assistance on this question?  I think your original question has been answered, but if you need any more help, just post.
0
 
DaneJurkovicAuthor Commented:
I still can not figure out how to implement the "AcroExch.AVPageView.ZoomTo" method.
0
 
PsychotextCommented:
Sorted, I coded the entire thing myself because I was interested.  Here is the part of the code that will open the document and zoom.  I assume you can fit this in with your code above (Just need to ?

    Private Sub ViewThisPDF(ByVal ViewThisNow As String)
        Dim AcroExchApp As Object
        Dim AcroExchAVDoc As Object
        Dim AcroExchAVPageView As Acrobat.CAcroAVPageView

        AcroExchApp = CreateObject("AcroExch.App")
        AcroExchAVDoc = CreateObject("AcroExch.AVDoc")

        If (AcroExchAVDoc.Open(ViewThisNow, "Text for window title") = False) Then
            MsgBox("Could not open file")
            Exit Sub
        End If

        AcroExchAVPageView = AcroExchAVDoc.GetAVPageView

        'I have absolutely no idea what the options for the zoom type param are, but 0 works
        AcroExchAVPageView.ZoomTo(0, 200)

        AcroExchApp.Show()
        AcroExchApp.Maximize(100)

        ' Close Acrobat when done
        'AcroExchApp.Exit()

        ' Cleanup the Acrobat objects when done
        'AcroExchApp = Nothing
        'AcroExchAVDoc = Nothing
        'AcroExchPDDocSource = Nothing
    End Sub
0
 
PsychotextCommented:
Top line should have read: (Just need to replace your sub of the same name).
0
 
DaneJurkovicAuthor Commented:
Well done Psychotext!!!
0
 
PsychotextCommented:
No problem, was a good little challenge.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 9
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now