on my PC this is works fine, but on some PCs the file qould not open, the WebBrowser displays an error, and the file opens in the default PDF program instead of WebBrowser.
My code:
Uri GuideURI = new Uri(String.Format("file:///{0}/../PDFs/" + link + ".pdf", Directory.GetCurrentDirectory()));
PDF_Web_Browser.Navigate(GuideURI);
One way to resolve this issue might be to not rely on the PC's PDF reader software.
You can use MuPDF as a library to extract the text from PDF and maybe write the content of it in XML format, then navigate to the file.
If you don't want to go this far, you can show an error message when trying to display a PDF file on a PC that doesn't have the required features to open it in the WebBrowser (source).
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
string url = e.Url.ToString();
if (url.StartsWith("res://ieframe.dll/navcancl.htm") && url.EndsWith("pdf"))
{
e.Cancel = true;
MessageBox.Show("Cannot open PDF!");
}
}
Or you can even make a mix of those. Just in case the WebBroswer can't open the PDF file, you can write a message like "PDF addon not detected" and then display the XML file generated with the help of MuPDF library.
Maybe its because WebBrowser uses engine of Interneet Explorer. If that person doesn't have installed extension for that, or have older version of IE, he dont be able to open PDF in WebBrowser.
Related
The scenario is that a mail is sent to an inbox. Attached to the mail is a html file which the user clicks to open the page in a browser. They then click a link on the webpage which opens a PDF file online.
Now, what I want to achieve programmatically with c# is to save the attached html file on disk, open the file, find the link, click it and save the file that opens to disk.
I have gotten as far as programmatically open the email and save the attached html file to disk. But now I'm sort of stuck at opening the file programmatically.
I've gotten as far as creating a FileWebRequest to open the file but I don't know how to find the link ("a" tag, only on in the whole page) and programmatically click it (in c#) so the PDF opens so I can download it and save to disk.
What needs to be done after the filewebrequest?
FileWebRequest req = (FileWebRequest)WebRequest.Create(pathToHtmlFile);
FileWebResponse res = (FileWebResponse)req.GetResponse();
// What now..?
At first you should extract the PDF URL using RegEx from html content and then download it using WebClient :
private static string FindPdfFileDownloadLink(string htmlContent)
{
return Regex.Match(htmlContent, #"^(https?:\/\/)?www\.([\da-z\.-]+)\.([a-z\.]{2,6})\/[\w \.-]+?\.pdf$").Value;
}
public static int Main(string[] args)
{
string htmlContent = File.ReadAllText("1.html");
string pdfUrl = FindPdfFileDownloadLink(htmlContent);
using (WebClient wClient = new WebClient())
{
wClient.DownloadFile(pdfUrl, #"1.pdf");
}
Console.Read();
return 0;
}
if you want to really click on link for any reason you can load the html in a hidden web browser and find the element that you want and click on it.
To load the content into WebBrowser control :
webBrowser1.Navigate(#"1.html");
and to find and click on element :
HtmlElement link = webBrowser.Document.GetElementByID("link_id_58547")
link.InvokeMember("Click")
I'm trying to retrieve data from a webpage but I cannot do it by making a web request and parsing the resulting html file because the actual text that I'm trying to retrieve is not in the html file! I imagine that this text is pulled using some script and for that reason it's not in the html file. For all I know I'm looking at the wrong data, but assuming that my theory is correct, is there a straightforward way to retrieve whatever text is displayed by the browser (Firefox or IE) rather than attempt to fetch the text from the html file?
Assuming you are referring to text that has been generated using Javascript in the browser.
You can use PhantomJS to achieve this: http://phantomjs.org/
It is essentially a headless browser that will process Javascript.
You may need to run this as ane xternal program but Im sure you can do that through C#
Your other option would be to open the web page in a WebBrowser object which should execute the scripts, and then you can get the HtmlDocument object and go from there.
Take a look at this example...
private void test()
{
WebBrowser wBrowser1 = new WebBrowser();
wBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wBrowser1_DocumentCompleted);
wBrowser1.Url = new Uri("Web Page URL");
}
void wBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlDocument document = (sender as WebBrowser).Document;
// get elements and values accordingly.
}
What's the best method for printing a directory of HTML files in landscape orientation? I don't mind showing a print dialog or not. I've tried several solutions (exhausting Google & StackOverflow) which either print the HTML as a string, or can't print in landscape.
I'm using a .NET 2.0 Win Forms project to create HTML reports. Now I need to send them to the printer spool.
Thanks
Update :
After understanding the requirement correctly following can be achieved
//The example is using WebBrowser Control Version 4.0.0.0 .NET Component
//MSDN : http://msdn.microsoft.com/en-us/library/2te2y1x6(v=vs.80).aspx
//Example 1 : You can print html string using the Web Browser Control
string htmlString = "<html><head><title>Printing from Win forms - Web Browser Control</title></head><body><h1>Hello World....</h1></body></html>";
webBrowser1.DocumentText = htmlString;
//Example 2 : Print file or URL using the Web Browser Control
webBrowser1.Url = new Uri("http://www.stackoverflow.com/faq");
//Call Print function or Print Dialog
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(PrintFile);
private void PrintFile(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//You can setup page e.g. Orientation to Landscape and choose one of the Print options below
(WebBrowser)sender).ShowPageSetupDialog();
// Print the document now that it is fully loaded.
((WebBrowser)sender).Print();
//OR
(WebBrowser)sender).ShowPrintDialog();
//OR Even better setup print options and then Print
(WebBrowser)sender).ShowPrintPreviewDialog();
// Dispose the WebBrowser now that the task is complete.
((WebBrowser)sender).Dispose();
}
I'm trying to display a local PDF file in a WebBrowser-Control. I didn't want to use the Adobe-Libraries, because they don't support 64-bit. Now I already have the code to display a PDF, but only if it is not on the local HDD. When I right-clicked on the WebBrowser-Control and displayed the SourceCode of the HTML, I saved it as an HTML-File to check, if the HTML-Code is correctly working. Well, it works.
My window only consists of a maximized WebControl. I think the problem are the Security Settings of the local Internet Explorer. I read that a custom IInternetSecurityManager could solve the problem, but I don't know how to implement it... :/
I'm using C# with .NET Framework 4.0
Here is my code:
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
string url = "C:/test.pdf";
string html = "<!-- saved from url=(0014)about:internet -->\n<html>\n<body>\n<embed src=\"" + url + "\" width=\"100%\" height=\"100%\"/>\n</body>\n</html>";
webBrowser.NavigateToString(html); // System.Windows.Controls.WebBrowser
}
I the "saved from URL" part does only work if I directly open the HTML-Code in IE, so please tell me what to do, to get this code work... Maybe you have a better solution for my problem. Thanks for your help!
Regards,
Chris
Just use
webBrowser.Navigate("file:///" + url);
I want to generate a PDF file on server side, and then in response want to send that file (buffer,fileName- whatever may work) and show a print dialog to ask user to print the generated PDF file.
I tried something like below. But it does not trigger window.print() dialog.
public static void ForcedPrint(HttpResponse response, byte[] buffer, string fileName, string fileExtension) {
response.Clear();
response.Buffer=true;
response.Write("<script>window.print();</script>");
response.Charset="";
response.Cache.SetCacheability(HttpCacheability.NoCache);
response.ContentType="application/pdf";
response.BinaryWrite(buffer);
response.Flush();
response.End();
}
Can someone please help me with this?
The feature i am looking for is that i should be able to create PDF file on server, and in response user should get a dialog to print the generated file.
Thanks in advance.
As far as I'm aware you cannot generate a print command to the browser on the server. The most you can do is generate the javascript which will make it pop up with a print dialog (window.print()) but that wouldnt help you with what you're trying to do.
Just speculating but you may try generating a page with an iframe that points to the PDF file, and in the base page have the javascript that tells the iframe to print?
Hope this helps,
Darko
You need to embed the PDF in the HTML document, say in a div called thePDF and in JavaScript code in the document, you need to invoke
thePDF.printWithDialog()
The dialog that appears will be the Adobe Reader plugin's print dialog rather than the browser's print dialog; this will allow selection of pages etc. before printing.