C# WPF Webbrowser在更改设置后打印,无需用户交互
我有一个从WebBrowser控件打印到标签打印机的应用程序。其思想是,当我们打印HTML文档时,它会根据各种设置(如纸张大小和方向)进行打印。当我这样做时,任何事情都可以正常工作:C# WPF Webbrowser在更改设置后打印,无需用户交互,c#,wpf,printing,browser,C#,Wpf,Printing,Browser,我有一个从WebBrowser控件打印到标签打印机的应用程序。其思想是,当我们打印HTML文档时,它会根据各种设置(如纸张大小和方向)进行打印。当我这样做时,任何事情都可以正常工作: public Boolean DrawHTML(String HTML) { try { // Load HTML document as a string //using (StringReader reader = new StringReader(HTML)) using (S
public Boolean DrawHTML(String HTML)
{
try
{
// Load HTML document as a string
//using (StringReader reader = new StringReader(HTML))
using (StreamReader reader = new StreamReader(HTML))
{
// Navigate to HTML document string
Web_Document.Navigate(reader.ReadToEnd());
}
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + ":\nFile:\n\n" + HTML);
return false;
}
}
public Boolean Print(String PrinterName, String PrintJobName, Boolean IsLandscape, int LabelCount,double LabelWidth, double LabelHeight)
{
try
{
PrintQueueCollection _Printers = new PrintServer().GetPrintQueues(new[] {
EnumeratedPrintQueueTypes.Local, EnumeratedPrintQueueTypes.Connections});
foreach (PrintQueue Q in _Printers)
{
if (Q.FullName == PrinterName)
{
Q.UserPrintTicket.CopyCount = LabelCount;
if (IsLandscape)
{
Q.UserPrintTicket.PageOrientation = PageOrientation.Landscape;
}
else
{
Q.UserPrintTicket.PageOrientation = PageOrientation.Portrait;
}
Q.UserPrintTicket.PageBorderless = PageBorderless.Borderless;
double W = ((LabelWidth / 25.4) * 96);
double H = ((LabelHeight / 25.4) * 96);
Q.UserPrintTicket.PageMediaSize = new PageMediaSize(PageMediaSizeName.Unknown, W, H);
var writer = PrintQueue.CreateXpsDocumentWriter(Q);
writer.Write(Web_Document, Q.UserPrintTicket);
return true;
}
}
//Printer Not Found
return false;
}
catch (Exception)
{
//problem occured
return false;
}
}
#endregion
}
除3个问题外,所有问题都有效
try
{
var doc = Web_Document.Document as mshtml.IHTMLDocument2;
const string keyName = @"Software\Microsoft\Internet Explorer\PageSetup";
using (var key = Registry.CurrentUser.OpenSubKey(keyName, true))
{
if (key != null)
{
var oldFooter = key.GetValue("footer");
var oldHeader = key.GetValue("header");
key.SetValue("footer", "");
key.SetValue("header", "");
doc.execCommand("Print", true, null);
key.SetValue("footer", oldFooter);
key.SetValue("header", oldHeader);
}
}
}
catch (Exception ex)
{
Log.Error(ex.Message,ex);
}
这将很好地打印,没有页眉和页脚