WebBrowser control: How to overwrite URL property - c#

This is a bit special:
I'm loading some HTML in string form into the WebBrowser control via the DocumentText property.
Now when I print the page with a PDF printer (Bullzip PDF Printer in my case) it always takes the URL as document name which is "about:blank".
Is there any way to change this to another value by either changing the URL property (it's read-only) or by changing the print behaviour to use another text rather than the URL as filename?

There are a couple of options that I am aware of though my knowledge on this subject is a bit dated.
The first is to change the computer settings in Internet Options. Regardless of whether this is done by hand or through a registry change script, it is simple for the developer, but obviously not the best approach from the deployment or compatibility angles.
The next approach is to develop a custom print template. This is not fun, but is probably the most professional approach. As much as I would love to include all of the information needed on this approach in this post, it is too much to cover. Here is a good overview and good luck Googling from there.
My experience with this involved printing from the MS WebControl component used in a .NET 1.1 Winforms application to the Web Super Goo PDF converter component. Your mileage may vary.

You can change the header and footer such that the URL property is not printed.

Try to Navigate to a URL, then cancel that via DWebBrowserEvents::BeforeNavigate

Related

Embed a PDF into a WPF application

I'm trying to embed/display a PDF in a WPF application. So far, I've tried those solutions, without success:
Display the PDF in a WindowsFormsHost hosting an AxAcroPdf
control, similarly to what's shown here. The problem is that my application sets AllowsTransparency
= True to create a style similar to Modern UI, but
that doesn't blend well with a WindowsFormsHost (the control
becomes invisible).
Display the PDF in a WebBrowser control. The problem is the same.
Set AllowsTransparency = False, but this causes a sluggish feeling in the application. Since I use WPF purposedly to enhance the look and feel of our business applications to the benefit of our end-users, this can't be a solution.
Use a second window with AllowsTransparency = False to display the WindowsFormsHost, and hack it to make it look like a child control of the main window, as it's described here. However, I don't like the code-behind approach since I'm using MVVM.
Find a native PDF control for WPF. However, I only found a couple of commercial ones and that's not an option right now.
What I need is to be able to:
Display a PDF or its representation (i.e. an image or a conversion to another format) in a WPF application.
Keep my style visually intact and fluid (AllowsTransparency must stay True).
Use an approach respecting the principles of MVVM (preferably no code-behind).
Include it in my application for free (for commercial usage).
I'm totally open to hand-made solutions, open-source libraries and even completely different approaches.
I have two solution for this:
Open your .pdf file and then print as an .xps (also you must be able for doint this in code), then this file you can embded this file in your app, and show it as a xps document. See this: Documents in WPF - MSDN - Microsoft (XPS)
To use a free library, I'm not very sure if this allows show pdf, but it generate them, you can take a look at EO-Pdf.
Hope this tips helps to solve the problem.
If you're opened to Open-Source solutions, I would recommend GhostScript. You can convert the PDF (with decent quality, for the most part) to individual image files of most any format you might want to work with.
The other option is to convert the PDF to HTML using pdf2htmlEX, but it will currently only compile for Linux.
I use both of the above solutions in several applications on both Linux and Windows. The advantage to the HTML way is that the text can be copied and pasted. The advantage of the GhostScript way is that the images might be more portable (smaller).
As with any open-source solution, you need to be aware of the terms of the license under which each product is released, and how that may impact your final result.
There is a good solution that I used before is to use CefSharp. It's the Chrome browser engine that supports previewing PDF documents.
Visit quick start page.
It is recommended to visit Troubleshooting page to set appropriate settings if you have problems.
If you want to show PDF files on your local drives you should also use these settings for the browser and then use file:/// protocol:
CefSharp.BrowserSettings browserSettings = new CefSharp.BrowserSettings();
browserSettings.FileAccessFromFileUrls = CefSharp.CefState.Enabled;
browserSettings.UniversalAccessFromFileUrls = CefSharp.CefState.Enabled;
browserSettings.TextAreaResize = CefSharp.CefState.Enabled;
my_bowser.BrowserSettings = browserSettings;

Display HTML in a silverlight application

I need to display HTML in my silverlight application and cannot find a way of doing it. I cannot use the web browser control as it needs to be able to run in or out of a browser.
Does anyone know of a good way to do this, because all I can think of doing at the moment is running replace methods on the text to just replace the tags with C# equivalents eg(<br /> to \n).
The way I do it is to check if the application is running inside the browser and change the means of display accordingly. If running inside the browser, I overlay the application with an IFrame, as I describe in this article: http://www.silverlightshow.net/items/Building-a-Silverlight-Line-Of-Business-Application-Part-6.aspx. Otherwise, I use the WebBrowser control. I have a control which does this all for you in the source code that accompanies my book, which is downloadable from the Apress website here: http://www.apress.com/book/downloadfile/4638.
Hope this helps...
Chris
I believe what you are looking for is HTML Bridge.
Edit I'm am actually now unsure if you'll still have access to javascript if you're running this OOB. I'm going to look into this some more and will further update. I'll still leave the answer up though for reference.
Second Edit Here is what I've found. HTML Bridge is disabled when you run silverlight out of browser. This disables access to the HTML DOM as well as Javascript. However, according to a comment on this site:
HTML Bridge is not available when you first install a OOB app. But you CAN force it if you modify the index.html in the folder where the app is installed just adding the enablehtmlaccess parameter.
It works!
You can even create dynamic HTML elements using the well-known methods of the HtmlPage class. You can even open a new browser window with the Navigate() method and its "_blank" parameter.
Keep in mind this information was posted about SL 3. Its possible that this may have changed, but I doubt it. So it seems that what you may want to do is build a script into the startup of your SL app that detects whether or not your app is running out of browser. If it is then you may want to have some script to call that can modify this file for you.
There recently was a similar question.
I posted a link there to an implementation that parses and displays HTML inline in Silverlight. Of course, it will work only with simple HTML, but maybe you can expand it to your needs.

Programmatically changing the destination printer for a WinForms WebBrowser control

I'm trying to use an invisible WebBrowser control to print a very simple HTML document. Our application requires that we be able to print several documents this way, and that they all can be sent to different printers. Unfortunately, I haven't been very successful in making the output go to the right printer.
The way it works right now is that before printing a document, the application determines which printer is to receive it, and sets the default printer accordingly. To do this it uses SetDefaultPrinter() imported from WinSpool.drv. If I step the code in debug mode I can clearly see that the default printer changes (and this change is reflected in the control panel UI), but the WebBrowser still insists on using the original default printer.
The MSDN documentation, from what I've seen, doesn't really provide a solution for this scenario. I would greatly appreciate some input on how I can accomplish this programmatically.
Given what you've said, perhaps if you restart the process which contains the web browser control (or the process which is the web browser control), after you change the default printer? That's the kind of thing I see happening here, for example.
I suppose it would be possible to fork off a background process that does the actual printing, but I'm really hoping for a simpler solution.
Forking was my first thought towards a probably-simplest solution.
Some other alternatives are as follows.
1). IE, which the webbrowser control is wrapping, exposes APIs via ActiveX. One of its/those APIs might let you specify the destination printer.
2). Some executables (I don't know about IE) have printto entries in the registry. For example, Acrobat Reader has an entry whose value is as follows:
""C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe"" /t "%1" "%2" "%3" "%4"
That's used for specifying the syntax of the command-line which you can use to print and specify a (non-default) printer. You can also Google for printto, see e.g. PrintTo command in the ShellExecute.
3). I have implemented an HTML control for .NET of my own, which doesn't depend on IE. You say that your HTML (and CSS I presume) are simple, so perhaps I can render it, either out of the box or with only a little extra development effort. I don't support printing, but printing is quite easy for a user control to implement. Getting me to implement that for you would cost you several hundred but, who knows, maybe it's worth it to you. It would be quite a light-weight solution, and perhaps well supported. You could email me if you want to discuss that further.
4). You might also find other controls, similar to mine, more or less famous/expensive; or other applications, e.g. OpenOffice etc etc.
5). You could consider converting the HTML (somehow) to another format (e.g. PDF) for which you have an application which gives you better support for printing.
I've had the exact same problem, and incorporated this control instead of the standard .NET WebBrowser to work around it.
This works on .NET 3.5, if not before
this.webBrowser1.ShowPrintDialog();

Caputuring a Silverlight Bing Map as an image

The WriteableBitmap class that was introduced in Silverlight 3 does not allow images to be rendered from a different domain for security reasons and hence it is impossibe to output an image of the Bing Map.
Does anyone have any suggestions for a workaround? I just want to capture a snapshot of the image including the controls and shapes I have drawn on the overlying canvas
Thanks
Paul
There is a workaround - the bing maps silverlight control allows you to specify a custom configuration. In this configuration, you can specify your own URLs for images, and make them so that they are not cross-domain, and hence not "insecure".
However, there is a bug in Silverlight which causes the workaround to fail. See here: https://connect.microsoft.com/VisualStudio/feedback/details/628962/
The sample attached to the bug report in the link above shows how to specify a custom map control configuration.
Did you try Bing's Imagery Service? See
http://msdn.microsoft.com/en-us/library/cc966738.aspx
It would be shame if something that is inplace for good security reasons could then be worked around, wouldn't it?
It may be that your true goal might be achievable using some other approach but you don't state what that is.
For example, you might want to generate some simple image that can then be hosted on a web site to be downloaded. A possible workaround would be to use the Silveright print API to print the desired content. Use a PDF generating printer driver you could then upload the generated PDF to the site
Of course this example may be far from your purpose. State your intent and a more lateral work around may be offered or at least a definitive, "no you can't".

Printing from the web at client side

How can we print using print document so that we can control the printer paper size?
It must be controlled after web
hosting is done without the print
dialog popup or javascript popup.
It should print on the client's printer
after webhosting without the print
dialog option and setting the paper
size.
It should be done in C# .net
As a user I don't want that when I enter a site someone to change my printer's settings or to take control over my printer.
For security reasons I hope this is not possible.
Your best option is to generate PDF-files with the appropriate siez in it. A printer should choose the correct page-size based on that unless the user manually changes it in the print-dialog.
Cirrcumventing the print-dialog is a security-restriction, ActiveX or a firefox-extension is your only alternative then, which might be okay on an intranet-based webapplication.
The only way that I've been able to have this amount of control over the printer client side is to create an ActiveX component that would handling the printing interface and go from there. It's not cross-broswer not the cleanest solution; however, it's the only way that I've been able to manage paper size. I looked into a CSS #print options but was not able to find one that could truly control the size.
The only other possibility is to look to see what printing capabilities something like Flash or Silverlight have. I'm not familiar enough to say if they do or don't have what you're looking for; however, it may be another option.
I believe a Java applet has the ability to set paper size if you grant it the proper privileges. I think your users will almost certainly have to jump through hoops for this to work...
Maybe too late but I'm adding for future reference; you could use a kind of download/print manager utility. Imagine the torrent magnet links, you click a link and a .torrent file opened by torrent client, it reads the necessary info and do the rest at client site.
So you have to write a little utility which one recognizes these 'print jobs' from your web site. You click a [Print Invoice] link and browser downloads a unique .print file and the utility captures it.
The .print file has two parts: 1. Header and 2. The file (pdf/jpg/any). Header contains predefined printer/paper size/orientation/etc or just asks for user input. And then prints the file from 2. part/body.
You may also queue the print jobs and manage them as well.
You have to install that utility; define a protocol like http:// or ftp:// to capture print links from the utility. And set printers/papers/etc for specific jobs like invoices/color printings/etc.
These will be what I'll do when I port our winforms app to web. Plus If you are on a LAN environment then you would print the things from the web server to network printers.
I have looked into this before, and the only way to force things like portrait/landscape etc, is to get the user to install an ActiveX control (or similar).
Javascript or CSS alone cannot do it.

Categories

Resources