Example #1
0
        /// <summary>
        /// Compare two word documnet
        /// </summary>
        /// <param name="wordDocDataRef"></param>
        /// <param name="RefFormat"></param>
        /// <param name="wordDocDataComp"></param>
        /// <param name="CompFormat"></param>
        /// <returns></returns>
        public static XpsDocument CompareWordDocument(byte[] wordDocDataRef, OfficeDocuFormat RefFormat, byte[] wordDocDataComp, OfficeDocuFormat CompFormat)
        {
            string resDocPath = String.Empty;
            string pathRef    = String.Empty;
            string pathComp   = String.Empty;
            string pathRes    = String.Empty;
            string pathXps    = String.Empty;

            if (RefFormat == OfficeDocuFormat.docx)
            {
                pathRef = string.Concat(m_tempPoolPath, Guid.NewGuid().ToString(), ".docx");
            }
            else if (RefFormat == OfficeDocuFormat.doc)
            {
                pathRef = string.Concat(m_tempPoolPath, Guid.NewGuid().ToString(), ".doc");
            }
            else
            {
                throw new Exception("wrong format.");
            }

            if (CompFormat == OfficeDocuFormat.docx)
            {
                pathComp = string.Concat(m_tempPoolPath, Guid.NewGuid().ToString(), ".docx");
            }
            else if (CompFormat == OfficeDocuFormat.doc)
            {
                pathComp = string.Concat(m_tempPoolPath, Guid.NewGuid().ToString(), ".doc");
            }
            else
            {
                throw new Exception("wrong format.");
            }

            pathXps = string.Concat(m_tempPoolPath, Guid.NewGuid().ToString(), ".xps");
            pathRes = string.Concat(m_tempPoolPath, Guid.NewGuid().ToString(), ".docx");

            pathRef  = System.IO.Path.GetFullPath(pathRef);
            pathComp = System.IO.Path.GetFullPath(pathComp);
            pathXps  = System.IO.Path.GetFullPath(pathXps);
            pathRes  = System.IO.Path.GetFullPath(pathRes);

            try
            {
                using (FileStream fs = new FileStream(pathRef, FileMode.Create, FileAccess.ReadWrite))
                {
                    BinaryWriter bw = new BinaryWriter(fs);
                    bw.Write(wordDocDataRef);
                    bw.Close();
                }

                using (FileStream fs = new FileStream(pathComp, FileMode.Create, FileAccess.ReadWrite))
                {
                    BinaryWriter bw = new BinaryWriter(fs);
                    bw.Write(wordDocDataComp);
                    bw.Close();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            // Create a WordApplication and host word document
            Word.Application wordApp = new Microsoft.Office.Interop.Word.Application()
            {
                Visible = false
            };

            XpsDocument result = null;

            try
            {
                wordApp.Documents.Open(pathRef);

                // To Invisible the word document
                wordApp.Application.Visible = false;

                // Minimize the opened word document
                wordApp.WindowState = Word.WdWindowState.wdWindowStateMinimize;

                Word.Document doc = wordApp.ActiveDocument;
                doc.Compare(pathComp);
                doc.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
                doc = wordApp.ActiveDocument;
                doc.SaveAs(pathRes, Word.WdSaveFormat.wdFormatXMLDocument);
                doc.Close(Word.WdSaveOptions.wdDoNotSaveChanges);

                wordApp.Documents.Open(pathRes);
                doc = wordApp.ActiveDocument;
                doc.SaveAs(pathXps, Word.WdSaveFormat.wdFormatXPS);

                result = new XpsDocument(pathXps, FileAccess.Read);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                wordApp.Documents.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
                ((Word._Application)wordApp).Quit(Word.WdSaveOptions.wdDoNotSaveChanges);
            }

            return(result);
        }
Example #2
0
        /// <summary>
        ///  Show a Word document in DocumentViewer control.
        /// </summary>
        /// <param name="viewer">DocumentViewer</param>
        /// <param name="wordDoc">Word document data</param>
        /// <param name="format">Document format</param>
        public static void DisplayWordDocument(DocumentViewer viewer, byte[] wordDoc, OfficeDocuFormat format)
        {
            if (wordDoc == null)
            {
                return;
            }

            string wordDocxPath = String.Empty;

            if (format == OfficeDocuFormat.docx)
            {
                wordDocxPath = string.Concat(m_tempPoolPath, Guid.NewGuid().ToString(), ".docx");
            }
            else if (format == OfficeDocuFormat.doc)
            {
                wordDocxPath = string.Concat(m_tempPoolPath, Guid.NewGuid().ToString(), ".doc");
            }
            else
            {
                throw new Exception("wrong format.");
            }

            wordDocxPath = System.IO.Path.GetFullPath(wordDocxPath);

            try
            {
                using (FileStream fs = new FileStream(wordDocxPath, FileMode.Create, FileAccess.ReadWrite))
                {
                    BinaryWriter bw = new BinaryWriter(fs);
                    bw.Write(wordDoc);
                    bw.Close();
                }

                DisplayWordDocument(viewer, wordDocxPath);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }