DocumentViewerHelper.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Windows.Forms;
  7. using System.Windows.Xps.Packaging;
  8. using Microsoft.Office.Interop.Word;
  9. using Word = Microsoft.Office.Interop.Word;
  10. namespace Core.Mes.Client.Comm.Office
  11. {
  12. /// <summary>
  13. /// DocumentViewer tool
  14. /// </summary>
  15. public class DocumentViewerHelper
  16. {
  17. /// <summary>
  18. /// Convert the word document to xps document
  19. /// </summary>
  20. /// <param name="wordFilename">Word document Path</param>
  21. /// <param name="xpsFilename">Xps document Path</param>
  22. /// <returns></returns>
  23. public static XpsDocument ConvertWordToXps(string wordFilename, string xpsFilename)
  24. {
  25. // Create a WordApplication and host word document
  26. Word.Application wordApp = new Word.Application();
  27. try
  28. {
  29. object objMissing = System.Reflection.Missing.Value;
  30. object wordName = wordFilename;
  31. object xpsName = xpsFilename;
  32. wordApp.Documents.Open(ref wordName, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing);
  33. // To Invisible the word document
  34. wordApp.Application.Visible = false;
  35. wordApp.Visible = false;
  36. // Minimize the opened word document
  37. wordApp.WindowState = WdWindowState.wdWindowStateMinimize;
  38. Document doc = wordApp.ActiveDocument;
  39. object saveFormat = Word.WdSaveFormat.wdFormatXPS;
  40. doc.ExportAsFixedFormat(xpsFilename, WdExportFormat.wdExportFormatXPS, false, WdExportOptimizeFor.wdExportOptimizeForPrint, WdExportRange.wdExportAllDocument, 0, 0, WdExportItem.wdExportDocumentContent, true, true, WdExportCreateBookmarks.wdExportCreateWordBookmarks, true, true, false, ref objMissing);
  41. //doc.SaveAs(ref xpsName, ref saveFormat, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing);
  42. XpsDocument xpsDocument = new XpsDocument(xpsFilename, FileAccess.Read);
  43. return xpsDocument;
  44. }
  45. catch (Exception ex)
  46. {
  47. MessageBox.Show("Error occurs, The error message is " + ex.ToString());
  48. return null;
  49. }
  50. finally
  51. {
  52. object saveOption = WdSaveOptions.wdDoNotSaveChanges;
  53. object originalFormat = WdOriginalFormat.wdWordDocument;
  54. object routeDocument = false;
  55. wordApp.Documents.Close(ref saveOption, ref originalFormat, ref routeDocument);
  56. ((_Application)wordApp).Quit(ref saveOption, ref originalFormat, ref routeDocument);
  57. }
  58. }
  59. }
  60. }