Saving PDF to file using webbrowser in vb.net - vb.net

I have a dynamically generated PDF that I wish to download to the harddrive. The file download is started by the vb.net code:
WebBrowser1.Navigate("javascript:fnSubmitPDFForm('modalDivpdf')")
and I currently get the normal internet explorer 'File Download' window with the Open Save Cancel options. I would like to specifiy a folder and filename and automatically download the PDF. Is this possible?
Here is a better example of a link that induces the 'File Download' window. I'd like to automatically save the PDF:
WebBrowser1.Navigate("http://www2.hungryhorse.co.uk/index.php/download_file/view/838/71/")
Thanks

You can neither automatically download the file nor specify a path. You can however give a suggested filename:
context.Response.ContentType = "application/octet-stream"
context.Response.AddHeader("content-disposition", "attachment; filename=""" & "yourFilename.pdf & """")
context.Response.TransmitFile(actualFile)
Where yourFilename.pdf is the suggested filename and actualFile is the full path of the file to send.

I know this is an old thread, but I found it yesterday trying to solve the same problem. May sources say that it is not possible to do this, but it IS possible with a workaround. There are many reasons why this is needed; some wisecracks in other forums resort to "but why do you need it".
I must add that the solution below is not the most elegant, but perhaps it is the simplest, and it allowed me to solve the problem in a couple of hours.
There are two steps:
This will cause links to PDFs to automatically trigger the dialog asking if you want to save the file (with a default to "Cancel"). I did it by installing and then removing Adobe Reader. I know that there must be an elegant way around it by looking at some key in the registry, or by configuring the web browser, but I run out of time with this problem.
Send key strokes to the pop-up dialogs.
The code looks like:
Public Sub YourFunction()
'...
WebBrowser1.Navigate("https://... your URL")
Wait(2)
' Accept question to save
SendKeys.SendWait("{LEFT}")
Wait(0.5)
SendKeys.SendWait("{ENTER}")
Wait(0.5)
' Actually save the file
SendKeys.SendWait("{ENTER}")
Wait(0.5)
'...
End Sub
The function "Wait" is needed to allow time to the WebBrowser control to actually navigate to the target URL; this might be different in your case. The code is :
Public Sub Wait(ByVal seconds As Double)
Static start As Date
start = Now()
Do While Now() < start.AddSeconds(seconds)
System.Windows.Forms.Application.DoEvents()
Loop
End Sub
It works. I just downloaded 1,700 PDFs (for my own purpose, not going to explain it). It took a little bit of time, though.

Related

Create Document in the Folder the EXE is in (VB)

I'm starting to play around with FileStream to make a text document. When you do this, you have to clarify a path. Is there a way to create the text document in the folder the EXE file is in?
(I'm asking this because this program is meant to be downloaded, so I think I can't clarify a path specific to my computer)
Thank you!
You're right, you can't bake a path into your program that is specific to your computer because then it won't work on the user's computer
Jimi makes the wise point that often programs are installed to C:\Program Files or similar and it's not automatically possible to write to subfolders in there - you'll have to get into asking the user for permission (Elevation) .. headache
Better to decide what you want the path for:
If you need a temporary path to e.g. download something to then throw it away you can call Path.GetTempFilename() or Path.GetTempPath() - the former creates a 0 byte file with a random name in the user's temp folder, and returns the path. The latter gives you the path to the temp folder so you can create your own file
If the file is to have some permanence, such as the user saving his work, you should ask the user for it. SaveFileDialog and FolderBrowserDialog are two things you can drop on your windows form and then call ShowDialog() on to show the uer a UI where they pick a path. After they OK or Cancel, you check if they OK'd or Cancel and proceed using the dialog's Filename or SelectedPath respectively (if they OK'd)
When you're writing your files it's easier not to use FileStream unless you really need to seek in the file etc. Easier to just:
System.IO.File.WriteAllText(path here, contents here)
If you have to write the contents of a string variable to a file
The best way to create a text file, would be to use CreateText method. It will create a file besides the executable program file. You can go the following way.
Dim sw as StreamWriter = File.CreateText("myfile.txt")
Dim str as String = "Your text"
sw.Write(str)
sw.Flush()
sw.Close()

VB.Net Run.exe with Form button

I'm pretty new to VB so I need a bit of help with some coding.
I made a program that should download a certain file to what ever directory the user wants, but now i want my program to run that certain file but i don't know how to do it since the user used his/her own directory.
Like i want the program to run that certain file no matter where its saved, how do i do that?
I do know this code:
System.Diagnostics.Process.Start("C:\")
but this doesnt really work since i have to put in a directory before i use the program.
You can get the path of the SFD by using filename.
Dim Path as String
Path = SaveFileDialog.FileName
Then run the file.
System.Diagnostics.Process.Start(Path)
Found another way of doing it,
I just used:
file.open(textbox1.text)
And made it take the text forom the the other textbox the user browsed the file to

TeXnicCenter - open as PDF

I have just spent some time installing TeXnicCenter and tried to configure it properly. I'm almost there now! The only issue I have is that every time I compile the document I'm working on as a PDF document, the document doesn't open directly in Adobe Reader X. Instead I only get the "opening screen" of Adobe Reader X to pop up, meaning that I have to choose "Open file" and navigate to the proper folder where the document is saved to view it. This is frustrating and time-consuming - especially if I want to check my progress often. Is there any way I can configure this so that Adobe Reader X automatically opens the file I'm compiling, without me having to navigate to the proper folder?
Any help will be truly appreciated!
Since this question is TeX related, you might have better luck posting in http://tex.stackexchange.com, StackExchange's TeX site.
You should look into this question, pay special attention to the first two answers, they should work just fine.
Cheers.
In Adobe Reader X, go to Edit -> Preferences, there in category general there should be a checkbox Enable protected mode at startup. Uncheck this, exit adobe and try again. Does it work now?

Photoshop Scripting with Applescript -- will not open images without a prompt

tell application "Adobe Photoshop CS5.1"
set myFile to (choose file) as string
open file myFile
end tell
This results in 2 separate dialogs opening asking me what file to open.... It doesn't matter what I select first, it asks a second time and opens the second.
What I want to open is a PDF, so I really want:
tell application "Adobe Photoshop CS5.1"
set myFile to (choose file) as string
open file myFile as PDF with options {class:PDF open options, mode:CMYK, resolution:300, use antialias:true, page:1} showing dialogs never
end tell
Result:
Error: General Photoshop error occurred. This functionality may not be available in this version of Photoshop
All right a more direct approach:
tell application "Adobe Photoshop CS5.1"
set myFilePath to alias "other: PREPRESS SAVE:GAMES:3Sudoku:20120213pzsud-v:sudoku 0213.pdf"
with timeout of 300 seconds
open myFilePath as PDF with options {class:PDF open options, mode:CMYK, resolution:300, use antialias:true, page:1}
end timeout
end tell
The file exists or the setting of the alias would fail. So what do I get?
"Error: -43. Adobe Photoshop CS5.1 got an error: File some object wasn’t found."
I had asked over on the Adobe forums, with no response. The full code I'm really trying to use is there if you're interested. I reduced it to basics to figure out what the problem is.
Is there a setting somewhere that I am missing? I'm not sure what is wrong, the example out of the
There are horrible issues with The Photoshop 5 dictionary. One idea is to try and run it in 32-bit mode which 'solves' a lot of issues. Please digest further in http://forums.adobe.com/message/2822670#2822670?tstart=0#2822670
Might be a CS5 issue, but I honestly don't know.
This worked fine for me, but with CS4:
tell application "Adobe Photoshop CS4"
set myFile to (choose file) as alias
with timeout of 300 seconds
open myFile as PDF with options {class:PDF open options, mode:CMYK, resolution:300, use antialias:true, page:1}
end timeout
end tell
The only real difference (that I can see) is that I alias the chosen file, and call open ... - not open file ...

VBA Zip File error

I am using code similar to the below to add some files to a zip folder:
Set oApp = CreateObject("Shell.Application")
oApp.Namespace(CVar(strDest)).CopyHere CVar(strSource)
This works fine most of the time, but sometimes I get an error:
"(Compressed (zipped) Folders Error) Cannot create output file". This error is raised asynchronously outside of my VBA code and as such I cannot trap it to take remedial action.
If I enter break mode and step back to:
oApp.Namespace(CVar(strDest)).CopyHere CVar(strSource)
then the file is added correctly.
I am guessing that there is some kind of lock either on the compressed folder or the source file that is causing this problem, but I am unsure as to how to check this. I should note that the files are being added are pdf files created from Microsoft Access 2007 and we are using the fully qualified paths and the code runs as follows creating up to 10 pdfs per zip file:
Create Zip
Run this loop:
For Each ReportToRun
CreatePdf ' using DoCmd.OutputTo acOutputReport, "rptHame", acFormatPDF, strReportName
AddToZip
Next
Any idea how to either fix this or trap the Cannot create output file error?
Thanks
I've very successfully used the open souurce Info Zip DLLs. See Compression DLLs, OCXs, etc for links and sample VB code.
Two thoughts, neither likely to fix the issue:
Have you tried explicitly providing FALSE as the next argument, i.e., the AutoStart argument? The help file say it defaults to FALSE, but maybe it's opening the PDFs after generating them, so that they are still open?
add a SLEEP of a couple of seconds (see Make code go to Sleep for the code).
alternatively, loop through all your reports and generate all of them, then start a new loop to copy each one into the zip file, rather than generate/copy for each report. Having a couple of seconds sleep between the two loops might not hurt (assuming that the problem is that the asynchronous PDF generation process is not fully complete at the point the copy is initiated).