I have a WPF application where I use a document viewer. I also start printing programmatically with documentviewer.Print(); However, when that is pressed it brings up the screen with the Windows printers and makes the user have to click "OK" again on that screen to start. Is there a way to avoid the confirmation and make documentviewer.Print(); immediately start the print job on the default Windows printer?
All you need is the default print queue, which you can get via
var pq = LocalPrintServer.GetDefaultPrintQueue()
From this, you can create an XpsDocumentWriter:
var writer = PrintQueue.CreateXpsDocumentWriter(pq);
Now, you can get the DocumentPaginator from your DocumentViewer via the Document property, which returns an IDocumentPaginatorSource that has a DocumentPaginator property:
var paginator = documentviewer.Document.DocumentPaginator;
and you can send that right to the XpsDocumentWriter's Write method:
writer.Write(paginator);
Simple, isn't it?
Related
I have used WebBrowser control in xaml like below.
<WebBrowser x:Name="pdfviewer"/>
Also refer below for back end code
this.pdfviewer.Source = new Uri(#"C:\Users\VMH8COB\Desktop\UpdateRequestFormV2.pdf");
To hide the toobar I have used below code.
this.pdfviewer.Source = new Uri(#"C:\Users\VMH8COB\Desktop\UpdateRequestFormV2.pdf#toolbar=0");
It is working but if the escape key is pressed then adobe default side tool par will display. I want to disable print and save button else hide tool bar in all case.
I have searched for solution more than one week, but still I didn't get it.
Kindly help me on this.
Thanks.
I can't seem to find a good way to print .htm files in c# using .net 4.0, visual studio 2010 and windows forms. When i tried to print it directly, it printed the raw html data instead of printing the "page" itself.
The only way i know to print it, is to use a WebBrowser control. When i print the document, it doesn't print colors and the page isn't printed correctly. For example, the edges are not drawn and so on.
Code for Web Browser :
public void Print()
{
// Create a WebBrowser instance.
WebBrowser webBrowserForPrinting = new WebBrowser();
// Add an event handler that prints the document after it loads.
webBrowserForPrinting.DocumentCompleted +=
new WebBrowserDocumentCompletedEventHandler(PrintDocument);
// Set the Url property to load the document.
webBrowserForPrinting.Url = new Uri(Core.textLog);
}
private void PrintDocument(object sender, WebBrowserDocumentCompletedEventArgs e)
{
((WebBrowser)sender).ShowPrintDialog();
//// Print the document now that it is fully loaded.
//((WebBrowser)sender).Print();
//// Dispose the WebBrowser now that the task is complete.
((WebBrowser)sender).Dispose();
}
What can i do?
Thank you!
Printing web pages will forever be the bane of your existence. There just isn't a solution out there that prints HTML directly to your printer really, really well. And even if you do find a program that does it well, it's only a matter of time until you try to print a page with some unsupported formatting, in which case you're right back where you started.
What we do is print HTML to a pdf file with a program called wkhtmltopdf. Then we open it in Acrobat (which has excellent printing support) and print from there. I can't say enough good things about wkhtmltopdf. It's command line driven, and its super, super fast. Best of all, its free. It has a companion program called wkhtmltoimage that will print to most popular image formats, too (bmp, jpg, png, etc).
After downloading/installing the program, you can run a quick test by going to your command prompt, navigating to the install folder, and typing:
wkhtmltopdf "http://YouWebAddress.com" "C:/YourSaveLocation.pdf"
It also has a ton of command line switches that give you greater control over the outputs (headers, footers, page numbering, etc etc).
Ok, as i said, problem was that edge are not drawn and neither are the backgrounds.
Here is how i solved it.
Hashtable values = new Hashtable();
values.Add("margin_left", "0.1");
values.Add("margin_right", "0.1");
values.Add("margin_top", "0.1");
values.Add("margin_bottom", "0.1");
values.Add("Print_Background", "yes");
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(#"Software\Microsoft\Internet Explorer\PageSetup", true))
{
if (key == null) return;
foreach (DictionaryEntry item in values)
{
string value = (string)key.GetValue(item.Key.ToString());
if (value != item.Value.ToString())
{
key.SetValue(item.Key.ToString(), item.Value);
}
}
}
So before i print, i go to regedit, change the values, and the document gets printed perfectly. Hope this helps other people that have the same problem when printing from webbrowser control in windows forms.
i want to know how i can open a browser to a specific web page and then fill out some of the content of the boxes on that page.
My idea is for someone to be able to order a particular item from our internal ordering system. The barcodes for these items are what will populate the fields on the page i want to open.
I no i can open a new instance of ie using Process.Start("IEXPLORE.EXE", url); howver how do i get a handle on that exact ie instance window so i can begin to add the required data to the fields?
Is this even possible?
Thanks very much
WatiN should help with this. I've generally used it for acceptance testing of web apps, but the principle is the same. Open a browser instance, reference stuff in the DOM, manipulate form elements, etc.
In addition to WatiN (as was suggested in another answer), you might consider a load testing package like Web Performance Load Tester. They have a free version that lets you run up to 10 virtual users at a time, which will perform scripted actions.
Another option would be to use a standard WebBrowser object to load your website. The WebBrowser object allows you to access and alter certain web parts. Below is sample code that automatically searches Bing:
private void LoadPage()
{
WebBrowser webBrowser1 = new WebBrowser();
webBrowser1.Navigate("http://www.bing.com");
//Wait for document to load...
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
//Set the text of the search input
HtmlElement txtTextField = webBrowser1.Document.GetElementById("sb_form_q");
txtTextField.InnerText = "My test text";
//Perform a click on the search button
HtmlElement btnSubmit = webBrowser1.Document.GetElementById("sb_form_go");
btnSubmit.InvokeMember("click");
}
Currently, I'm printing the contents of a WPF WebBrowser like so:
mshtml.IHTMLDocument2 doc = WebBrowser.Document as mshtml.IHTMLDocument2;
doc.execCommand("Print", true, null);
My HTML content has tables with background colors. Currently, when I print the content, the background colors do not print -- everything is solid white. Is there a way to tell the WebBrowser to print the background colors as well?
Also, this still causes a print dialog to pop up. Does anyone know what the command is to print the contents dialog-less?
Thanks a lot!
Assuming you're using 'SHDocVw.WebBrowser', you can use the ExecWB command. To print without the dialog, use the OLECMDEXECOPT_PROMPTUSER (1) constant. You can also pass an IE print template (just an HTML file) for more control over how the page is displayed.
It's something like this (taken from this MSDN question)
browser.ExecWB(SHDocVw.OLECMDID.OLECMDID_PRINT,
SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER,
"print_template.html", ref nullObject);
As for the background, it appears to be one of the options you can specify in the print template's LayoutRect. All print dialog settings are stored in the registry, but a print template is preferable because it won't change system-wide settings.
I'm building a c# app that displays a print preview (of a document) and then asks the user(s) to 'sign' the document via a InkPicture control. I've got no problems extracting the Bitmap from the inkpicture control and applying it to the PrintDocument (I do this earlier in the process before the print preview and paint those images to the printdocument) but the purpose of the print preview is to allow the user(s) to review the document as it would be printed and sign off on it.
I've tried resetting the document to a modified one
// MyDocumentType derives from PrintDocument and ipSignature is a
// user control derived from InkPicture that converts the ink to
// a gif
MyDocumentType doc = (MyDocumentType)ppcPreview.Document;
doc.AddSignature(ipSignature.Gif);
ppcPreview.Document = doc;
I've tried reconstructing the print preview control
MyDocumentType doc = (MyDocumentType)ppcPreview.Document;
doc.AddSignature(ipSignature.Gif);
ppcPreview = new PrintPreviewControl();
ppcPreview.Document = doc;
to no effect.
Invalidating the control after it's modified also does nothing.
I'm kind of stumped.
Did you try using the InvalidatePreview method instead of Invalidate?