private static void Main()
        {
            var n      = DateTime.Now;
            var tempDi = new DirectoryInfo(string.Format("ExampleOutput-{0:00}-{1:00}-{2:00}-{3:00}{4:00}{5:00}", n.Year - 2000, n.Month, n.Day, n.Hour, n.Minute, n.Second));

            tempDi.Create();

            var originalWml             = new WmlDocument("../../Original.docx");
            var revisedDocumentInfoList = new List <WmlRevisedDocumentInfo>()
            {
                new WmlRevisedDocumentInfo()
                {
                    RevisedDocument = new WmlDocument("../../RevisedByBob.docx"),
                    Revisor         = "Bob",
                    Color           = Color.LightBlue,
                },
                new WmlRevisedDocumentInfo()
                {
                    RevisedDocument = new WmlDocument("../../RevisedByMary.docx"),
                    Revisor         = "Mary",
                    Color           = Color.LightYellow,
                },
            };
            var settings        = new WmlComparerSettings();
            var consolidatedWml = WmlComparer.Consolidate(
                originalWml,
                revisedDocumentInfoList,
                settings);

            consolidatedWml.SaveAs(Path.Combine(tempDi.FullName, "Consolidated.docx"));
        }
Esempio n. 2
0
        public void Sample2()
        {
            var originalWml             = new WmlDocument(GetFilePath("Sample2/Original.docx"));
            var revisedDocumentInfoList = new List <WmlRevisedDocumentInfo>()
            {
                new()
                {
                    RevisedDocument = new WmlDocument(GetFilePath("Sample2/RevisedByBob.docx")),
                    Revisor         = "Bob",
                    Color           = Color.LightBlue,
                },
                new()
                {
                    RevisedDocument = new WmlDocument(GetFilePath("Sample2/RevisedByMary.docx")),
                    Revisor         = "Mary",
                    Color           = Color.LightYellow,
                },
            };

            var settings        = new WmlComparerSettings();
            var consolidatedWml = WmlComparer.Consolidate(
                originalWml,
                revisedDocumentInfoList,
                settings);

            consolidatedWml.SaveAs(Path.Combine(TempDir, "Consolidated.docx"));
        }
        //[InlineData("", "")]
        //[InlineData("", "")]
        //[InlineData("", "")]
        //[InlineData("", "")]
        //[InlineData("", "")]
        //[InlineData("", "")]
        //[InlineData("", "")]
        //[InlineData("", "")]
        //[InlineData("", "")]


        public void WC002_Consolidate_Bulk_Test(string name1, string name2)
        {
            FileInfo source1Docx = new FileInfo(Path.Combine(TestUtil.SourceDir.FullName, name1));
            FileInfo source2Docx = new FileInfo(Path.Combine(TestUtil.SourceDir.FullName, name2));

            var source1CopiedToDestDocx = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, source1Docx.Name));
            var source2CopiedToDestDocx = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, source2Docx.Name));

            if (!source1CopiedToDestDocx.Exists)
            {
                File.Copy(source1Docx.FullName, source1CopiedToDestDocx.FullName);
            }
            if (!source2CopiedToDestDocx.Exists)
            {
                File.Copy(source2Docx.FullName, source2CopiedToDestDocx.FullName);
            }

            /************************************************************************************************************************/

            if (s_OpenWord)
            {
                FileInfo source1DocxForWord = new FileInfo(Path.Combine(TestUtil.SourceDir.FullName, name1));
                FileInfo source2DocxForWord = new FileInfo(Path.Combine(TestUtil.SourceDir.FullName, name2));

                var source1CopiedToDestDocxForWord = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, source1Docx.Name.Replace(".docx", "-For-Word.docx")));
                var source2CopiedToDestDocxForWord = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, source2Docx.Name.Replace(".docx", "-For-Word.docx")));
                if (!source1CopiedToDestDocxForWord.Exists)
                {
                    File.Copy(source1Docx.FullName, source1CopiedToDestDocxForWord.FullName);
                }
                if (!source2CopiedToDestDocxForWord.Exists)
                {
                    File.Copy(source2Docx.FullName, source2CopiedToDestDocxForWord.FullName);
                }

                FileInfo wordExe = new FileInfo(@"C:\Program Files (x86)\Microsoft Office\root\Office16\WINWORD.EXE");
                var      path    = new DirectoryInfo(@"C:\Users\Eric\Documents\WindowsPowerShellModules\Open-Xml-PowerTools\TestFiles");
                WordRunner.RunWord(wordExe, source2CopiedToDestDocxForWord);
                WordRunner.RunWord(wordExe, source1CopiedToDestDocxForWord);
            }

            /************************************************************************************************************************/

            var before = source1CopiedToDestDocx.Name.Replace(".docx", "");
            var after  = source2CopiedToDestDocx.Name.Replace(".docx", "");
            var docxWithRevisionsFi = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, before + "-COMPARE-" + after + ".docx"));
            var docxConsolidatedFi  = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, before + "-CONSOLIDATED-" + after + ".docx"));

            WmlDocument         source1Wml  = new WmlDocument(source1CopiedToDestDocx.FullName);
            WmlDocument         source2Wml  = new WmlDocument(source2CopiedToDestDocx.FullName);
            WmlComparerSettings settings    = new WmlComparerSettings();
            WmlDocument         comparedWml = WmlComparer.Compare(source1Wml, source2Wml, settings);

            comparedWml.SaveAs(docxWithRevisionsFi.FullName);

            List <WmlRevisedDocumentInfo> revisedDocInfo = new List <WmlRevisedDocumentInfo>()
            {
                new WmlRevisedDocumentInfo()
                {
                    RevisedDocument = source2Wml,
                    Color           = Color.LightBlue,
                    Revisor         = "Revised by Eric White",
                }
            };
            WmlDocument consolidatedWml = WmlComparer.Consolidate(
                source1Wml,
                revisedDocInfo,
                settings);

            consolidatedWml.SaveAs(docxConsolidatedFi.FullName);

            using (MemoryStream ms = new MemoryStream())
            {
                ms.Write(consolidatedWml.DocumentByteArray, 0, consolidatedWml.DocumentByteArray.Length);
                using (WordprocessingDocument wDoc = WordprocessingDocument.Open(ms, true))
                {
                    OpenXmlValidator validator = new OpenXmlValidator();
                    var errors = validator.Validate(wDoc).Where(e => !ExpectedErrors.Contains(e.Description));
                    if (errors.Count() > 0)
                    {
                        var ind = "  ";
                        var sb  = new StringBuilder();
                        foreach (var err in errors)
                        {
#if true
                            sb.Append("Error" + Environment.NewLine);
                            sb.Append(ind + "ErrorType: " + err.ErrorType.ToString() + Environment.NewLine);
                            sb.Append(ind + "Description: " + err.Description + Environment.NewLine);
                            sb.Append(ind + "Part: " + err.Part.Uri.ToString() + Environment.NewLine);
                            sb.Append(ind + "XPath: " + err.Path.XPath + Environment.NewLine);
#else
                            sb.Append("            \"" + err.Description + "\"," + Environment.NewLine);
#endif
                        }
                        var sbs = sb.ToString();
                        Assert.Equal("", sbs);
                    }
                }
            }

            /************************************************************************************************************************/

            if (s_OpenWord)
            {
                FileInfo wordExe = new FileInfo(@"C:\Program Files (x86)\Microsoft Office\root\Office16\WINWORD.EXE");
                WordRunner.RunWord(wordExe, docxConsolidatedFi);
            }

            /************************************************************************************************************************/
        }
        public void WC001_Consolidate(string originalName, string revisedDocumentsXml)
        {
            FileInfo originalDocx = new FileInfo(Path.Combine(TestUtil.SourceDir.FullName, originalName));

            var originalCopiedToDestDocx = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, originalDocx.Name));

            if (!originalCopiedToDestDocx.Exists)
            {
                File.Copy(originalDocx.FullName, originalCopiedToDestDocx.FullName);
            }

            var revisedDocumentsXElement = XElement.Parse(revisedDocumentsXml);
            var revisedDocumentsArray    = revisedDocumentsXElement
                                           .Elements()
                                           .Select(z =>
            {
                FileInfo revisedDocx        = new FileInfo(Path.Combine(TestUtil.SourceDir.FullName, z.Element("DocName").Value));
                var revisedCopiedToDestDocx = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, revisedDocx.Name));
                if (!revisedCopiedToDestDocx.Exists)
                {
                    File.Copy(revisedDocx.FullName, revisedCopiedToDestDocx.FullName);
                }
                return(new WmlRevisedDocumentInfo()
                {
                    RevisedDocument = new WmlDocument(revisedCopiedToDestDocx.FullName),
                    Color = Color.FromName(z.Element("Color").Value),
                    Revisor = z.Element("Revisor").Value,
                });
            })
                                           .ToList();

            var consolidatedDocxName   = originalCopiedToDestDocx.Name.Replace(".docx", "-Consolidated.docx");
            var consolidatedDocumentFi = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, consolidatedDocxName));

            WmlDocument         source1Wml      = new WmlDocument(originalCopiedToDestDocx.FullName);
            WmlComparerSettings settings        = new WmlComparerSettings();
            WmlDocument         consolidatedWml = WmlComparer.Consolidate(
                source1Wml,
                revisedDocumentsArray,
                settings);

            consolidatedWml.SaveAs(consolidatedDocumentFi.FullName);

            using (MemoryStream ms = new MemoryStream())
            {
                ms.Write(consolidatedWml.DocumentByteArray, 0, consolidatedWml.DocumentByteArray.Length);
                using (WordprocessingDocument wDoc = WordprocessingDocument.Open(ms, true))
                {
                    OpenXmlValidator validator = new OpenXmlValidator();
                    var errors = validator.Validate(wDoc).Where(e => !ExpectedErrors.Contains(e.Description));
                    if (errors.Count() > 0)
                    {
                        var ind = "  ";
                        var sb  = new StringBuilder();
                        foreach (var err in errors)
                        {
#if true
                            sb.Append("Error" + Environment.NewLine);
                            sb.Append(ind + "ErrorType: " + err.ErrorType.ToString() + Environment.NewLine);
                            sb.Append(ind + "Description: " + err.Description + Environment.NewLine);
                            sb.Append(ind + "Part: " + err.Part.Uri.ToString() + Environment.NewLine);
                            sb.Append(ind + "XPath: " + err.Path.XPath + Environment.NewLine);
#else
                            sb.Append("            \"" + err.Description + "\"," + Environment.NewLine);
#endif
                        }
                        var sbs = sb.ToString();
                        Assert.Equal("", sbs);
                    }
                }
            }

            /************************************************************************************************************************/

            if (s_OpenWord)
            {
                FileInfo wordExe = new FileInfo(@"C:\Program Files (x86)\Microsoft Office\root\Office16\WINWORD.EXE");
                WordRunner.RunWord(wordExe, consolidatedDocumentFi);
            }

            /************************************************************************************************************************/
        }