C# Convert Excel, Word- and PowerPoint-Documents to XPS

You can easily automate the office apps to convert office documents to the XPS-Format. With Microsoft Word you can as well convert RTF, HTML and TXT Documents to the XPS Format.

You need to reference the following Assemblies:

Microsoft.Office.Interop.Excel
Microsoft.Office.Interop.PowerPoint
Microsoft.Office.Interop.Word
Office

 

        private Microsoft.Office.Interop.Word._Application wordApp;
        private Microsoft.Office.Interop.Excel._Application excelApp;
        private Microsoft.Office.Interop.PowerPoint._Application ppApp;

        public Converter()
        {
            // Initialize Office Apps in the beginning because it could take a while to load them into the memory
            wordApp = new Microsoft.Office.Interop.Word.Application();
            wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
            excelApp = new Microsoft.Office.Interop.Excel.Application();
            excelApp.DisplayAlerts = false;
            ppApp = new Microsoft.Office.Interop.PowerPoint.Application();
            ppApp.DisplayAlerts = PpAlertLevel.ppAlertsNone;
        } 

       ///
        /// It uses word to save the specified input file in the xps format to the specified destination file
        /// 
        private void ConvertWordToXPSDoc(string wordDocName, string xpsDocName)
        {
            wordApp.Documents.Add(wordDocName);

            _Document doc = wordApp.ActiveDocument;

            try
            {
                doc.ExportAsFixedFormat(xpsDocName, WdExportFormat.wdExportFormatXPS);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }
            finally
            {
                doc.Close(WdSaveOptions.wdDoNotSaveChanges);
            }
        }

        ///
        /// It uses excel to save the specified input file in the xps format to the specified destination file.
        /// It considers all the worksheets.
        /// 
        private void ConvertExcelToXPSDoc(string excelDocName, string xpsDocName)
        {
            excelApp.Workbooks.Add(excelDocName);

            Microsoft.Office.Interop.Excel.Workbook wb = excelApp.ActiveWorkbook;

            try
            {
                wb.ExportAsFixedFormat(XlFixedFormatType.xlTypeXPS, xpsDocName);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }
            finally
            {
                wb.Close(false);
            }
        }

        ///
        /// It uses powerpoint to save the specified input file in the xps format to the specified destination file.
        /// It considers all the slides but no notes.
        /// 
        private void ConvertPowerPointToXPSDoc(string ppDocName, string xpsDocName)
        {
            ppApp.Presentations.Open(ppDocName, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);

            Microsoft.Office.Interop.PowerPoint.Presentation p = ppApp.Presentations[ppApp.Presentations.Count];

            try
            {
                p.ExportAsFixedFormat(xpsDocName, PpFixedFormatType.ppFixedFormatTypeXPS);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }
            finally
            {
                p.Close();
            }
        }

Leave a comment

Your email address will not be published. Required fields are marked *