-1
我想從C#打印文檔,但不想爲其創建任何用戶界面。應該使用C#靜默打印文檔。如何在沒有任何用戶界面的情況下從C#打印文檔
我試過ProcessStartInfo
與Verb = "Print"
,但顯示的是用於打印文檔的UI。
任何形式的幫助表示讚賞。
我想從C#打印文檔,但不想爲其創建任何用戶界面。應該使用C#靜默打印文檔。如何在沒有任何用戶界面的情況下從C#打印文檔
我試過ProcessStartInfo
與Verb = "Print"
,但顯示的是用於打印文檔的UI。
任何形式的幫助表示讚賞。
也許PrintDocument.Print Method可能幫助:)
請注意,我不認爲這將與.NET的核心工作爲System.Drawing命名空間尚未被完全移植過來。 (Source - GitHub corefx Issue #20325
也將這項工作在UWP的應用程序,因爲它們使用的承印物不同的API。
我沒有跟我一臺打印機,以測試這一點,但這裏是從 msdn代碼示例
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();
}
}
只是爲了澄清 - U真想物理打印機 –
的可能重複上打印文檔(https://開頭計算器?[我如何將文件發送文件到打印機,它打印?]。 com/questions/6103705/how-can-i-send-a-file-document-to-the-printer-and-it-it-print) –
你在做什麼:Winforms,WPF,ASP ..? __Always__正確標記您的問題! - 另外:我們在談論什麼類型的文件? Word,pdf,文本? – TaW