我编写了一个编写文件的尖锐控制台应用程序,现在我要打印它。我在搜索时看到了很多例子,但其中很多都以表格和按钮开头,我不希望用户按任何按钮,因为我已经知道文件,字体和大小,我只想打印文档。我找到的每个应用程序都是一个Windows窗体应用程序,你能告诉我如何从控制台应用程序调用它吗?
如果问题有点基础,我很抱歉,我刚开始学习。
答案 0 :(得分:0)
您可以使用PrintDocument对象
这是来自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();
}
}
答案 1 :(得分:0)
确保将System.Drawing
引用(System.Drawing.dll)添加到项目中。
using System.Drawing;
using System.Drawing.Printing;
namespace PrintingFromConsole
{
class Program
{
static void Main(string[] args)
{
PrintDocument doc = new PrintDocument();
doc.PrintPage += new PrintPageEventHandler(doc_PrintPage);
doc.Print();
}
private static void doc_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawString("text", SystemFonts.DefaultFont, Brushes.Black, new PointF(100f, 100f));
e.HasMorePages = false;
}
}
}
我已使用Microsoft XPS Document Writer
和Bullzip PDF Printer
测试了此代码 - 两者都按预期工作。
答案 2 :(得分:0)
string fl = Application.StartupPath + "\\BarCode.txt";
string StrFileName = "Barcode.txt";
int intFN;
intFN = FileSystem.FreeFile();
FileSystem.FileOpen(intFN, fl, OpenMode.Output, OpenAccess.Default,OpenShare.Default, 1024);
// Write Text In File FileSystem.PrintLine(intFN, "TEST");
FileSystem.FileClose(intFN);
Microsoft.VisualBasic.Interaction.Shell(Application.StartupPath + "\\PRINT.BAT " + pStrFileName, AppWinStyle.Hide, false, -1);
现在创建新的批处理文件iePRINT.BAT并粘贴下面的命令: -
Type %1>\\ "PRINTER PATH WITHOUT QUOTES"
将此命令写入记事本并另存为PRINT.BAT文件 运行应用程序