Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.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
如何在没有任何UI的情况下从C#打印文档_C#_Printing_Processstartinfo - Fatal编程技术网

如何在没有任何UI的情况下从C#打印文档

如何在没有任何UI的情况下从C#打印文档,c#,printing,processstartinfo,C#,Printing,Processstartinfo,我想从C#打印一个文档,但不需要任何UI。文档应使用C#以静默方式打印 我尝试了使用Verb=“Print”处理StartInfo,但显示了打印文档的UI 感谢您的任何帮助。也许您会有所帮助:) 请注意,我认为这在.net Core中不起作用,因为System.Drawing命名空间尚未完全移植。( 这在UWP应用程序中也不起作用,因为它们使用不同的API打印东西 我没有带打印机来测试这一点,但下面是来自的代码示例 使用系统; 使用System.IO; 使用系统图; 使用系统、绘图、打印; 使

我想从C#打印一个文档,但不需要任何UI。文档应使用C#以静默方式打印

我尝试了使用
Verb=“Print”
处理StartInfo,但显示了打印文档的UI

感谢您的任何帮助。

也许您会有所帮助:)

请注意,我认为这在.net Core中不起作用,因为System.Drawing命名空间尚未完全移植。(

这在UWP应用程序中也不起作用,因为它们使用不同的API打印东西

我没有带打印机来测试这一点,但下面是来自的代码示例

使用系统;
使用System.IO;
使用系统图;
使用系统、绘图、打印;
使用System.Windows.Forms;
公共类打印示例
{
私有字体打印字体;
私人StreamReader StreamTopPrint;
静态字符串文件路径;
公共打印示例()
{
印刷();
}
//将为每个要打印的页面引发PrintPage事件。
私有void pd_PrintPage(对象发送方,PrintPageEventArgs ev)
{
float linesPerPage=0;
浮动yPos=0;
整数计数=0;
浮动左边距=ev.MarginBounds.Left;
浮动顶部边距=ev.MarginBounds.Top;
字符串行=null;
//计算每页的行数。
linesPerPage=ev.MarginBounds.Height/
printFont.GetHeight(ev.Graphics);
//迭代文件,打印每一行。
而(计数
只是澄清一下-你真的想在物理打印机上打印文档吗?可能与你的目标重复:Winforms、WPF、ASP..?始终正确标记你的问题!-还有:我们谈论的是什么类型的文档?Word、pdf、文本???@Tobiasheel我希望使用打印机打印文档。根据你提供的链接,如果我使用
ProcessStartInfo
打印一个文档,它将显示一个我不想显示的UI。@t我需要通用解决方案。对于word、pdf、文本、图像、xml、ppt、xls等文档。
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;

public class PrintingExample 
{
   private Font printFont;
   private StreamReader streamToPrint;
   static string filePath;


   public PrintingExample() 
   {
       Printing();
   }

   // The PrintPage event is raised for each page to be printed.
   private void pd_PrintPage(object sender, PrintPageEventArgs ev) 
   {
       float linesPerPage = 0;
       float yPos =  0;
       int count = 0;
       float leftMargin = ev.MarginBounds.Left;
       float topMargin = ev.MarginBounds.Top;
       String line=null;

       // Calculate the number of lines per page.
       linesPerPage = ev.MarginBounds.Height  / 
          printFont.GetHeight(ev.Graphics) ;

       // Iterate over the file, printing each line.
       while (count < linesPerPage && 
          ((line=streamToPrint.ReadLine()) != null)) 
       {
          yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
          ev.Graphics.DrawString (line, printFont, Brushes.Black, 
             leftMargin, yPos, new StringFormat());
          count++;
       }

       // If more lines exist, print another page.
       if (line != null) 
          ev.HasMorePages = true;
       else 
          ev.HasMorePages = false;
   }

   // Print the file.
   public void Printing()
   {
       try 
       {
          streamToPrint = new StreamReader (filePath);
          try 
          {
             printFont = new Font("Arial", 10);
             PrintDocument pd = new PrintDocument(); 
             pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
             // Print the document.
             pd.Print();
          } 
          finally 
          {
             streamToPrint.Close() ;
          }
      } 
      catch(Exception ex) 
      { 
          MessageBox.Show(ex.Message);
      }
   }

   // This is the main entry point for the application.
   public static void Main(string[] args) 
   {
      string sampleName = Environment.GetCommandLineArgs()[0];
      if(args.Length != 1)
      {
         Console.WriteLine("Usage: " + sampleName +" <file path>");
         return;
      }
      filePath = args[0];
      new PrintingExample();
   }
}