通过字符串PowerPoint互操作

时间:2017-02-09 15:22:08

标签: c# interop office-addins

我有两个PowerPoints一个原始和一个编辑。我每个PowerPoint我都有一个包含许多单词的文本框。我们的想法是在编辑中找到原始PowerPoint中添加或删除的任何单词并将其放入列表中。然后,我想通过这些已添加或删除的单词进行删除但我无法访问我需要的任何属性,因为我有一个字符串而不是TextFrame。我有什么想法可以突破这些话吗?例如,原文“This Text”编辑了“This New Text”结果“This New Text”。

我的代码是休闲

        List<int> originalShapesListID = new List<int>();
        List<int> editedShapesListID = new List<int>();
        List<int> originalListID = new List<int>();
        List<int> editedListID = new List<int>();
        List<Microsoft.Office.Interop.PowerPoint.Shape> originalList = new List<Microsoft.Office.Interop.PowerPoint.Shape>();
        List<Microsoft.Office.Interop.PowerPoint.Shape> editList = new List<Microsoft.Office.Interop.PowerPoint.Shape>();
        Microsoft.Office.Interop.PowerPoint.Shape editedShpID;
        List<Microsoft.Office.Interop.PowerPoint.Shape> originalListWords = new List<Microsoft.Office.Interop.PowerPoint.Shape>();
        List<char> editListWords = new List<char>();
        editedShpID = null;
        Microsoft.Office.Interop.PowerPoint.Shape originalShpID;
        long editedShapeID;
        long originalShapeID;
        editedShapeID = 0;
        originalShapeID = 0;
        originalShpID = null;
        originalShp = null;
        editShp = null;
        Microsoft.Office.Interop.PowerPoint.TextFrame txtFrame;
        char delimiter = Convert.ToChar(" ");
        List<string> one = new List<string>();
        List<string> two = new List<string>();
        int indexOfWord = 0;

获取所有文字

    String pps = "";

    foreach (Microsoft.Office.Interop.PowerPoint.Slide slide in Originalslides)
    {
        foreach (Microsoft.Office.Interop.PowerPoint.Shape originalShape in slide.Shapes)
        {
            originalShp = originalShape;
            if (originalShape.HasTextFrame == Microsoft.Office.Core.MsoTriState.msoTrue)
            {
                var textFrame = originalShape.TextFrame;
                if (textFrame.HasText == Microsoft.Office.Core.MsoTriState.msoTrue)
                {
                    var textRange = textFrame.TextRange;
                    pps += originalShape.TextFrame.TextRange.Text;
                    foreach (char word in pps)
                    {

                        l.Add(word);
                        Debug.WriteLine(word);
                    }


                }
            }
            originalShapesListID.Add(originalShape.Id);
            originalShapeID = originalShape.Id;
            originalList.Add(originalShape);
        }

        originalListID.Add(slide.SlideID);
    }

    foreach (Microsoft.Office.Interop.PowerPoint.Slide slide in EditedSlides)
    {
        foreach (Microsoft.Office.Interop.PowerPoint.Shape editShape in slide.Shapes)
        {
            editShp = editShape;
            if (editShape.HasTextFrame == Microsoft.Office.Core.MsoTriState.msoTrue)
            {

                var textFrame = editShape.TextFrame;
                if (textFrame.HasText == Microsoft.Office.Core.MsoTriState.msoTrue)
                {
                    var textRange = textFrame.TextRange;
                    pps += editShape.TextFrame.TextRange.Text;
                    foreach (char word in pps)
                    {

                        l.Add(word);
                        Debug.WriteLine(word);
                    }


                }
            }

            editedShapesListID.Add(editShape.Id);
            editedShapeID = editShape.Id;
            editList.Add(editShape);




        }

        editedListID.Add(slide.SlideID);


    }

检查是否有添加或删除的单词

    var q = from original in originalList
            join editedTmp in editList on original.Id equals editedTmp.Id into g
            from edited in g.DefaultIfEmpty()
            select new
            {
                original,
                edited
            };



    //foreach  through both lists
    foreach (var item in q)
    {


        List<string> diff;

        var originalString = item.original.TextFrame.TextRange.Text;
    var editString = item.edited.TextFrame.TextRange.Text;

    var firstStringList = originalString.Split(delimiter).ToList();

    var secondStringList = editString.Split(delimiter).ToList();

        if (secondStringList.Count() > firstStringList.Count())
        {
            diff = secondStringList.Except(firstStringList).ToList();
            //change to blue for added word

        }
        else
        {

            diff = firstStringList.Except(secondStringList).ToList();


            // change to red for deleted word
        }

}

0 个答案:

没有答案