wpf中word文件转xps文件报转换后的xps文件路径找不到
wpf中word文件转xps文件报转换后的xps文件路径找不到,求大神们帮看看!
上传事件:
private void button1_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.CheckFileExists = true;
dlg.Filter = "Office Files|*.doc;*.docx;*.xls;*.xlsx";;
if ((bool)dlg.ShowDialog(this))
{
string filePath = dlg.FileName;
docViewer.Document = ConvertWordToXPS(filePath).GetFixedDocumentSequence();
docViewer.FitToWidth();
}
}
转换方法:
private XpsDocument ConvertWordToXPS(string wordDocName)
{
FileInfo fi=new FileInfo(wordDocName);
XpsDocument result = null;
string xpsDocName = wordDocName;
xpsDocName = xpsDocName.Replace(".docx", ".xps").Replace(".doc", ".xps");
Microsoft.Office.Interop.Word.Application wordApplication = new Microsoft.Office.Interop.Word.Application();
try
{
if (!File.Exists(xpsDocName))
{
wordApplication.Documents.Add(wordDocName);
Document doc = wordApplication.ActiveDocument;
doc.ExportAsFixedFormat(xpsDocName, WdExportFormat.wdExportFormatXPS, false, WdExportOptimizeFor.wdExportOptimizeForPrint, WdExportRange.wdExportAllDocument, 0, 0, WdExportItem.wdExportDocumentContent, true, true, WdExportCreateBookmarks.wdExportCreateHeadingBookmarks, true, true, false, Type.Missing);
result = new XpsDocument(xpsDocName, System.IO.FileAccess.Read);
}
if (File.Exists(xpsDocName))
{
result = new XpsDocument(xpsDocName, FileAccess.Read);
}
}
catch (Exception ex)
{
string error = ex.Message;
wordApplication.Quit(WdSaveOptions.wdDoNotSaveChanges);
}
wordApplication.Quit(WdSaveOptions.wdDoNotSaveChanges);
return result;
}
在这这段代码中, result = new XpsDocument(xpsDocName, System.IO.FileAccess.Read); result = new XpsDocument(xpsDocName, System.IO.FileAccess.Read); 的xpsDocName是转换后的xps路径,这种转换后是要指定路径真生成xps文件么