wpf中word文件转xps文件报转换后的xps文件路径找不到

aspnetSky 2016-10-25 05:08:29
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文件么
...全文
267 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
aspnetSky 2016-10-26
  • 打赏
  • 举报
回复
方法没有问题,思路要正确,转换成功后,将xps存储到本地,然后用wpf自带的控件来显示。 生成本地文件xps借用利用xps虚拟打印机实现。 安装了.NetFrameWork3.5之后,默认会在系统中安装XPS虚拟打印机,我们将其设置为默认打印机。 Microsoft XPS Document Writer word文档打印为xps public void PrintWord(string wordfile) { oWord.ApplicationClass word = new oWord.ApplicationClass(); Type wordType = word.GetType(); //打开WORD文档 oWord.Documents docs = word.Documents; Type docsType = docs.GetType(); object objDocName = wordfile; oWord.Document doc = (oWord.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { objDocName, true, true }); //打印输出到指定文件 //可以使用 doc.PrintOut();方法,次方法调用中的参数设置较繁琐,建议使用 Type.InvokeMember 来调用时可以不用将PrintOut的参数设置全,只设置4个主要参数 Type docType = doc.GetType(); object printFileName = wordfile + ".xps"; docType.InvokeMember("PrintOut", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { false, false, oWord.WdPrintOutRange.wdPrintAllDocument, printFileName }); //退出WORD wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null); }
aspnetSky 2016-10-25
  • 打赏
  • 举报
回复
或求大神们给一个别的方法也行,功能只是在wpf中提供一个上传word或excel文件的预览~目前 应用的控件是wpf中DocumentViewer控件
aspnetSky 2016-10-25
  • 打赏
  • 举报
回复
另外也应用了网上的提供的一个类,OfficeToXps, 核心代码: private static OfficeToXpsConversionResult ConvertFromWord(string sourceFilePath, ref string resultFilePath) { object pSourceDocPath = sourceFilePath; string pExportFilePath = string.IsNullOrEmpty(resultFilePath) ? GetTempXpsFilePath() : resultFilePath; try { var pExportFormat = Word.WdExportFormat.wdExportFormatXPS; bool pOpenAfterExport = false; var pExportOptimizeFor = Word.WdExportOptimizeFor.wdExportOptimizeForOnScreen; var pExportRange = Word.WdExportRange.wdExportAllDocument; int pStartPage = 0; int pEndPage = 0; var pExportItem = Word.WdExportItem.wdExportDocumentContent; var pIncludeDocProps = true; var pKeepIRM = true; var pCreateBookmarks = Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks; var pDocStructureTags = true; var pBitmapMissingFonts = true; var pUseISO19005_1 = false; Word.Application wordApplication = null; Word.Document wordDocument = null; try { wordApplication = new Word.Application(); } catch (Exception exc) { return new OfficeToXpsConversionResult(ConversionResult.ErrorUnableToInitializeOfficeApp, "Word", exc); } try { try { wordDocument = wordApplication.Documents.Open(ref pSourceDocPath); } catch (Exception exc) { return new OfficeToXpsConversionResult(ConversionResult.ErrorUnableToOpenOfficeFile, exc.Message, exc); } if (wordDocument != null) { try { wordDocument.ExportAsFixedFormat( pExportFilePath, pExportFormat, pOpenAfterExport, pExportOptimizeFor, pExportRange, pStartPage, pEndPage, pExportItem, pIncludeDocProps, pKeepIRM, pCreateBookmarks, pDocStructureTags, pBitmapMissingFonts, pUseISO19005_1 ); } catch (Exception exc) { return new OfficeToXpsConversionResult(ConversionResult.ErrorUnableToExportToXps, "Word", exc); } } else { return new OfficeToXpsConversionResult(ConversionResult.ErrorUnableToOpenOfficeFile); } } finally { // Close and release the Document object. if (wordDocument != null) { wordDocument.Close(); wordDocument = null; } // Quit Word and release the ApplicationClass object. if (wordApplication != null) { wordApplication.Quit(); wordApplication = null; } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } } catch (Exception exc) { return new OfficeToXpsConversionResult(ConversionResult.ErrorUnableToAccessOfficeInterop, "Word", exc); } resultFilePath = pExportFilePath; return new OfficeToXpsConversionResult(ConversionResult.OK, pExportFilePath); } 同样是转换换时找不到xps路径

62,266

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧