C# 如何将上载的.doc格式更改为.txt格式?
我正在使用C开发一个项目。为此,我想将.doc格式转换为.txt格式并将其发送到打印机。现在我使用以下代码。但它没有将word格式转换为txt格式。我怎么做C# 如何将上载的.doc格式更改为.txt格式?,c#,winforms,printing,ms-word,C#,Winforms,Printing,Ms Word,我正在使用C开发一个项目。为此,我想将.doc格式转换为.txt格式并将其发送到打印机。现在我使用以下代码。但它没有将word格式转换为txt格式。我怎么做 OpenFileDialog ofd = new OpenFileDialog(); DialogResult result = ofd.ShowDialog(); if (result == DialogResult.OK) // Test 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
{
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());
}
您可以尝试以下方法:
application.Documents.Open(fileName);
application.ActiveDocument.SaveAs2(fileName, WdSaveFormat.wdFormatText);
这将把Word文档保存到文本文件中。请注意,您可能会丢失诸如Word对象之类的信息,但我想您已经知道了。您是否尝试过,Path.ChangeExtensionfileName.txt;?