Displaying a local PDF file in WPF with WebBrowser-Control - c#

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);

Related

Display PDF in WPF WebBrowser

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.

Firefox: Image does not load on aspx code

I just use simple code to load image in asp.net image control, it works fine in IE but firefox does't show image. the line of code is:
string path = "F:\\Image\\";
string img = "header-firefox.PNG";
Image1.ImageUrl = path + img;
and the error is:
The address wasn't understood
Firefox doesn't know how to open this address, because the protocol (f) isn't associated with any program.
even file:///F:/Image/header-firefox.PNG path is not working in code even.
Do you mean that is the path you see in the HTML? I understand that the backslashes are escaped in C# but surely in the HTML the path should be "\\192.172.60.05\Users\4133.Png"?
Have you tried navigating to \192.172.60.05\Users\4133.Png directly in Firefox?

is there a straightforward way to retrieve text that is rendered by the browser but is not hard-coded in the actual html file?

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.
}

Load image from URL to pictureBox

I'm trying to load several images from URLs to a pictureBox.
My problem is that "pictureBox.Load(URL)" or "pictureBox.ImageLocation = URL" doesn't work.
I don't know why, I think it is because the URL doesn't have an extension like .jpg.
private void button3_Click(object sender, EventArgs e)
{
string URL = "https://internetmarke.deutschepost.de/internetmarke/franking/image/view/1403556118.do";
pictureBox1.ImageLocation = URL;
}
The URL works in Firefox and the picture is shown. But in IE it doesn't work and I think that's the same reason why it's not working in .NET.
IE says "unknown filetype" and wants to download a "1403556118.do" file.
In C# I only get a red X in the pictureBox.
When I first try to load it in IE, it works in the pictureBox (IE cache?)
Does somebody knows another possibility to load this images to a pictureBox?
EDITED: Added sample code.
Today I've tested the code on three different computers and with different Internet connections; Home DSL, Company DSL and a UMTS/3G Surf-Stick. All without any proxies and also tested without virusscan.
In every single scenario it didn't work, same as I wrote in my first post.
After I accessed some of the URLs in Firefox or IE, the images of these URLs showed up in my application. All others remained a red X.
Is there any different (old-school^^) method to load these images, like downloading the HTTP-Stream into a byte array and then copy this into pictureBox.Image or something?
Dino
pictureBox1.ImageLocation = "http://www.micoequipment.com/products/large/MF_260_l.jpg"; //should work
Make sure that image is accessible via web browser (test it before). Also, please make sure that you are calling correct picture box :)
It works for me.
#Andrew:
pictureBox1.ImageLocation = "http://www.micoequipment.com/products/large/MF_260_l.jpg";
This Works!
pictureBox1.ImageLocation = "https://internetmarke.deutschepost.de/internetmarke/franking/image/view/1403556118.do";
This doesn't work!
Your link and my link both work in Firefox. But my link doesn't work in IE and it doesn't work in .NET and in the pictureBox.
This should work since you're loading an image from a remote URL:
pictureBox1.Load(URL);

Displaying image that was previously uploaded to server

For a project I'm working I would like the user (admin) to be able to change the picture on the page he is currently on. I managed to upload the image to "the server" using interlink. This basicly uploads it to a given folder on a server, in my case being: Interlink/Uploads.
But now I don't really know how can I tell my website to replace the source of the image that is currently shown with the source of the uploaded image.
Another something I would like to do is create a simple image gallery with all the images in that folder, once again I don't know how to do this.
I hope somebody can help me, Thanks.
Thomas
Edit: Just so clarify, the application is written in silverlight (XAML, C#). I apologise for any inconvenience.
I take it that the "Silverlight" portion of this question is related only to Interlink (your file uploader), and not to the page itself, which I presume is straight HTML.
If that's the case, you've got several options for changing the local image. The simplest way is simply to wait until you know that your file upload has finished (presumably Interlink has some way of notifying you that this is the case), and then run something like this bit of JavaScript:
<script type='text/javascript'>
function changeImage(newImageSource) {
document.getElementById('myTargetImage').setAttribute('src', newImageSource);
}
</script>
As far as displaying a simple image gallery with all the images in the folder, my recommendation would be to look into one of the numerous jquery plugins that handle this sort of thing, e.g.:
http://www.1stwebdesigner.com/css/fresh-jquery-image-gallery-display-solutions/
EDIT: Silverlight Options
You basically have the same options, except you're doing them in C# instead of JavaScript. For instance, when Interlink tells you that the new image has been uploaded, run this:
string imageName = "something.jpeg";
var ub = new UriBuilder(HtmlPage.Document.DocumentUri);
ub.Path = "/Interlink/Uploads/" + imageName;
img.Source = new BitmapImage(ub.Uri);
And for an image carousel, something like this:
http://3dimagecarousel.codeplex.com/
You'll just need to provide the URL's of all the images. The easiest way to do that is probably to expose a web service method that lists them all.

Categories

Resources