Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF Webbrowser在更改设置后打印,无需用户交互_C#_Wpf_Printing_Browser - Fatal编程技术网

C# WPF Webbrowser在更改设置后打印,无需用户交互

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

我有一个从WebBrowser控件打印到标签打印机的应用程序。其思想是,当我们打印HTML文档时,它会根据各种设置(如纸张大小和方向)进行打印。当我这样做时,任何事情都可以正常工作:

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个问题外,所有问题都有效

  • 如果HTML正文不包含scroll=“no”滚动条,则会出现滚动条,这是正常的,但也会出现打印条,这是不正常的

  • 印刷真的很差。非常模糊。我认为在创建视觉效果时会发生这种情况

  • 如果托管框架小于html内容,则只打印可见内容。我想打印全部内容

  • 有没有关于我如何做到这一点的建议

    谢谢使用此代码:

    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);
    }
    
    这将很好地打印,没有页眉和页脚