我測試了幾種打印文檔的方案,第一個方案測試過程中發現打印的都是亂碼,後來我發現,word文檔好像不能以流的方式讀取,這個還有待研究。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using Spire.Doc;
namespace PrintTestPro
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private System.Drawing.Font printFont;
private StreamReader streamToPrint;
private void button1_Click(object sender, EventArgs e)
{
//這種方式打印,打印不了word文件,我不確定word能不能以流的方式讀取
try
{
//streamToPrint = new StreamReader("D:\\Git\\wordTemplateTest\\wordTemplateTest\\Template\\myfile.doc", Encoding.Default);
streamToPrint = new StreamReader("E:\\MyFile.txt", Encoding.Default);
try
{
printFont = new System.Drawing.Font("Arial", 10);
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
////跳出打印對話框,提供打印參數可視化設置,如選擇哪個打印機打印此文檔等
//PrintDialog pdg = new PrintDialog();
//pdg.Document = pd;
//if (DialogResult.OK == pdg.ShowDialog()) //如果確認,將會覆蓋所有的打印參數設置
//{
// //打印預覽
// PrintPreviewDialog ppd = new PrintPreviewDialog();
// ppd.Document = pd;
// if (DialogResult.OK == ppd.ShowDialog())
// {
// pd.Print(); //打印
// }
//}
pd.Print(); //打印
}
finally
{
streamToPrint.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
// 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);
// Print each line of the file.
while (count < linesPerPage &&((line = streamToPrint.ReadLine()) != null))
{
//Console.WriteLine(line);
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;
}
private void button2_Click(object sender, EventArgs e)
{
wordOperate();
}
public static void wordOperate()
{
Object oMissing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document wordDoc = new Microsoft.Office.Interop.Word.Document();
try
{
//Set no alerts as printing in background.
wordApp.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone;
//Set the printer.默認打印機
//wordApp.ActivePrinter = printerName;
wordDoc = wordApp.Documents.Open("D:\\Git\\wordTemplateTest\\wordTemplateTest\\Template\\myfile.doc");
wordDoc.Activate();
//設置打印紙張
wordDoc.PageSetup.PaperSize = WdPaperSize.wdPaperA5;
//wordDoc.PageSetup.PaperSize = WdPaperSize.wdPaperA4;
wordDoc.PrintOut(); //打印
}
catch (Exception ex)
{
}
finally
{
wordDoc.Close(SaveChanges: false);
wordApp.Application.Quit();
}
}
private void button3_Click(object sender, EventArgs e)
{
//第三方庫封裝的word操作庫
// 創建一個Documnet類對象
Spire.Doc.Document doc = new Spire.Doc.Document();
// 加載需要打印的Word文檔
doc.LoadFromFile(@"D:\\Git\\wordTemplateTest\\wordTemplateTest\\Template\\myfile.doc");
// 實例化System.Windows.Forms.PrintDialog對象
PrintDialog dialog = new PrintDialog();
dialog.AllowPrintToFile = true;
dialog.AllowCurrentPage = true;
dialog.AllowSomePages = true;
dialog.UseEXDialog = true;
// 關聯doc.PrintDialog屬性和PrintDialog對象
doc.PrintDialog = dialog;
// 後臺打印
PrintDocument printDoc = doc.PrintDocument;
// printDoc.Print();
// 顯示打印對話框並打印
if (dialog.ShowDialog() == DialogResult.OK)
{
printDoc.Print();
}
}
private void button4_Click(object sender, EventArgs e)
{
//這種方式也可以實現打印word文檔
using (PrintDialog pd = new PrintDialog())
{
pd.ShowDialog();
ProcessStartInfo info = new ProcessStartInfo("D:\\閘機人證覈查系統客戶端使用說明 - V1.0.doc");
info.Verb = "PrintTo";
info.Arguments = pd.PrinterSettings.PrinterName;
info.UseShellExecute = true;
info.CreateNoWindow = false;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(info);
}
}
}
}