到目前为止,我正在使用以下代码。使用btnUpload我可以上传文件。使用btnPrint我可以将文档发送到打印机。当我上传记事本时,它打印效果良好。但是当我上传单词文档时,它也会给我打印文档。但它包含的不是字母。一些符号很少的出租车。如何解决这个问题?使用C#代码打印文档
String content = "";
private void btnUpload_Click(object sender, EventArgs e)
{
int numberOfPages = 0;
string fileName;
// Show the dialog and get result.
OpenFileDialog ofd = new OpenFileDialog();
DialogResult result = ofd.ShowDialog();
if (result == DialogResult.OK) // Test result.
{
fileName = ofd.FileName;
var application = new Microsoft.Office.Interop.Word.Application();
//read all text into content
try
{
content = System.IO.File.ReadAllText(fileName);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
var document = application.Documents.Open(@fileName);
numberOfPages = document.ComputeStatistics(WdStatistic.wdStatisticPages, false);
MessageBox.Show("Total Pages"+ numberOfPages);
}
}
private void btnPrint_Click(object sender, EventArgs e)
{
PrintDialog printDlg = new PrintDialog();
PrintDocument printDoc = new PrintDocument();
printDoc.DocumentName = "fileName";
printDlg.Document = printDoc;
printDlg.AllowSelection = true;
printDlg.AllowSomePages = true;
//Call ShowDialog
if (printDlg.ShowDialog() == DialogResult.OK)
{
printDoc.PrintPage += new PrintPageEventHandler(pd_PrintPage);
printDoc.Print();
}
}
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
ev.Graphics.DrawString(content, new System.Drawing.Font(new FontFamily("Times new Roman"), 12f), Brushes.Black,
ev.MarginBounds.Left, 0, new StringFormat());
}
我不知道答案,但我的第一个猜测是你不能打印一个文本文件(=二进制文件)就像一个简单的文本文件......你的打印机不知道如何处理一个文件。 –
我不明白你想达到什么目的。如果你想打印Word文档,不能直接调用document.Printout();? –