Up to now i am using following code. Using btnUpload I can upload the document. Using btnPrint I can send the document to printer.when I upload notepad it prints well.But when I upload word document it give me Print document also. But it contains not letters.Some kind of symbols with few letterers.How can solve this?
到目前为止,我使用以下代码。使用btnUpload我可以上传文件。使用btnPrint我可以将文件发送到打印机。当我上传记事本时它打印得很好。但是当我上传word文档时它也给我打印文件。但它不包含字母。一些符号很少的字符。如何解决这个问题呢?
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());
}
2 个解决方案
#1
1
You just can't print a binary file as it were a plain text, in order to print Word Documents you have to use Microsoft.Office.Interop.
您无法打印二进制文件,因为它是纯文本,为了打印Word文档,您必须使用Microsoft.Office.Interop。
#2
1
Make sure you refer Microsoft.Office.Interop;
确保你引用了Microsoft.Office.Interop;
Word.Application _app = new Word.Application();
Word.Document doc = _app.Documents.Open(refer fileName, ...);
doc.PrintOut(/* refer options */);
#1
1
You just can't print a binary file as it were a plain text, in order to print Word Documents you have to use Microsoft.Office.Interop.
您无法打印二进制文件,因为它是纯文本,为了打印Word文档,您必须使用Microsoft.Office.Interop。
#2
1
Make sure you refer Microsoft.Office.Interop;
确保你引用了Microsoft.Office.Interop;
Word.Application _app = new Word.Application();
Word.Document doc = _app.Documents.Open(refer fileName, ...);
doc.PrintOut(/* refer options */);