我正在使用C#开发一个项目。为此,我想将.doc格式转换为.txt格式并将其发送到printer.Now我正在使用以下代码。但它不会将字格式转换为txt格式。我该怎么办?
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
{
System.Windows.Forms.RichTextBox rtBox = new System.Windows.Forms.RichTextBox();
//string rtfText = System.IO.File.ReadAllText(fileName);
//rtBox.Rtf = rtfText;
//string plainText = rtBox.Text;
content = System.IO.File.ReadAllText(fileName);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
//Show handeled Exceptions
}
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();
Thread.Sleep(3000);
MessageBox.Show("Uploded file has sent to Printer");
}
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());
}
答案 0 :(得分:0)
你可以试试这个:
application.Documents.Open(fileName);
application.ActiveDocument.SaveAs2(fileName, WdSaveFormat.wdFormatText);
这会将Word文档保存到文本文件中。请注意,您可能会丢失Word对象等信息,但我想您已经知道了。