I've been working on a project that automatically prints HTML invoices. It's been particularly challenging as I've needed to keep the CSS formatting.
Following advice, I've found myself using IHTMLDocument2 to carry out my printing. I'm supposed to be able to:
mshtml.IHTMLDocument2 doc = new mshtml.HTMLDocument() as mshtml.IHTMLDocument2;
doc.write(htmlContent); //htmlContent is a string of HTML
doc.execCommand("PRINT", false, null);
The second argument specifies whether to produce the Printer Options box, but it doesn't help. I've read that using PRINT will always result in a dialog box - however I haven't been able to find an alternative.
Any ideas?
According MSDN:
Print
Opens the print dialog box so the user can print the current page.
So I think there is no way to workaround this behaviour. Try to use other classes for your application or open the print dialog box during printing.
You should note that you must provide true as secord parameter (showUI [in, optional]) in execCommand method.
Related
My goal is to load up a file (htm file) in a WebBrowser control on my form. Once I have the htm file loaded I want to print it with the default printer settings.
Currently I can do everything above. The one thing that I can NOT do is give the print job a name. It just uses the default "file:///C:/Users/blah/blah/myfilename.htm" for the title of the print job. This is what I want to change.
Is this possible via the WebBrowser control or via the print spooler (changing the job title after the fact)?
According to the MSDN on WebBrowser.Print()...
Prints the document currently displayed in the WebBrowser control using the current print and page settings.
So, it is a matter of changing the WebBrowser print settings. According to Microsoft, that is not allowed without changing the registry on the fly.
As a potential starting point for a workaround, there is a way to get the default PrinterSettings and set the PrintFileName like so:
PrinterSettings settings = new PrinterSettings();
settings.PrintFileName = "YourFileNameHere";
If there were a way to then assign those printer settings to your WebBrowser object, that'd be swell! Sadly, that cannot be done. But, you may be able to use the above code to engineer another solution, likely outside the scope of WebBrowser.
This is the best way i fount to print an HTML from a string
void PrintString(string strHTMLText)
{
WebBrowser wbPrintString = new WebBrowser() { DocumentText = string.Empty };
wbPrintString.Document.Write(strHTMLText);
wbPrintString.Document.Title = "Type The Header You Want Here";
Microsoft.Win32.RegistryKey rgkySetting = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Internet Explorer\\PageSetup", true);
rgkySetting.SetValue("footer", "Type THe Footer You Want Here");
rgkySetting.Close();
wbPrintString.Parent = this;
wbPrintString.ShowPrintPreviewDialog();
wbPrintString.Dispose();
}
i am trying to create a contract (document) that I can print like a normal word document.
I have a form which is my 'entry form') where i can type in details such as customer name, amount,date of contract etc. these information is then saved in ms access.
i have another form (which i call 'contract doc'). it has labels in it and the label holds the information i typed in my 'entry form'. I assigned one label to get the values that i entered in the textbox in the entry form.
contractdoc.contract_date_label.Text = contract_date_tb.Text;
contractdoc.deposit_label.Text = deposit_tb.Text;
contractdoc.customer_name_label.Text = customer_name_tb.Text;
i also added labels and typed the rest of the documents body and position them in the 'contract doc form ' to look like an actual contract.
but i dont know how to print it like a document. i iused :
printForm1.Print();
but what happens is, it asks me to save in xls format and only a messege box that says:"printing page 1 of document" with a cancel button.
I hope you can help. thank you in advance
I presume from the name printForm1, that you're using the PrintForm class provided as part of the Visual Basic PowerPack.
In that case, you need to make sure that the PrintAction property is set appropriately. To print to a physical printer, you need to set it to PrintAction.PrintToPrinter. Depending on what printer is configured as the system default, you might also need to set the PrinterSettings property. Then when you call the Print method, it will print to the correct printer.
It's probably prompting you to save the file in XLS format because your system default printer is a virtual one that generates XLS files.
I want to implement a continious printing of document.Please see image belowalt text http://www.freeimagehosting.net/uploads/c87bde9396.png
When I choose the checkboxes then click the print button, all the information of a particular applicant will be printed.Take note that each document was printed on a new page. I found a soluton from this forum http://bytes.com/topic/asp-net/answers/861073-print-multiple-files-continuously
But I need an additional resources about this to implement the best solution for this functionality.Any ideas?
I believe that the best approach for this problem is to open a popup window with the contents using page break when necessary. This way you have better control over what is printed and you can give user a chance to review what is going to be printed if you like or call window.print on ready.
An existing process uses the Adobe Acrobat COM object AFormAutLib to open and fill form items. There are over 500 forms, and they all have a form field of type Button at the top. The method AFormAutLib.setButtonIcon is used to set the path of another PDF file to be used as the image on the button.
I am looking for an alternative. I have looked at iTextSharp, activePDF Tookit, and others, but have been unable to find anything that can replace this functionality.
Thanks in advance.
The solution was to use activePDF Toolkit in a different way...
APToolkitNET.FieldInfo myFI = aTK.FieldInfo(x.Key.ToString(),1);
aTK.PrintImage(logoPath, myFI.Left, myFI.Bottom, myFI.Width, myFI.Height, true, 1);
aTK.DeleteFormField(x.Key.ToString());
The button had the right location and dimensions, so the FieldInfo class is used to get those values. Then PrintImage is called with the path to the image and the locations, before the button is deleted.
Here's what I am trying to do:
Select text from a webpage I pulled up using my web browser control.After clicking a button while this text is still selected I would like a message box to pop-up displaying the text that was highlighted by the user. How do I get this functionality to work in my wpf application?
I think I'm on the right track using mshtml but I get an error that says:
Error HRESULT E_FAIL has been returned from a call to a COM component.
This error will happen even when I try something small on the document like changing the title.
The code is below:
IHTMLDocument2 doc = (IHTMLDocument2)this.webBookText.Document;
doc.title = "l";
Well, for starters it would be a lot simpler to use WebBrowser than mshtml (note that you can still host WebBrowser in WPF) - this will certainly let you do simple things a lot easier:
webBook.Document.Title = "foo";
However, I can't see anything there that would let you work with selections very easily...
You can get the selected element with .Document.ActiveElement, but this is the entire element - not the selected portion.
Figured it out that error was because this wasn't in my form class