如何检查rtf文件中是否存在OLE对象

时间:2017-05-22 11:29:56

标签: .net c#-4.0 rtf

LINK Extract Embedded Image Object in RTF我在下面的代码与我提供的链接相同,它在97-2003文档中运行良好,同样的代码现在在Office 2007/2010中不起作用。我们通常检查其中包含OLE对象的RTF并根据公司规则拒绝我们的转换,但即使在office-2007/2010创建的RTF中有一个写为“对象”的文本,我们的代码也会拒绝。是否有任何解决方案可以在Office-2007创建的RTF中识别?

        using (StreamReader sr = new StreamReader(filePath))
                    {
                        RtfReader reader = new RtfReader(sr);
                        IEnumerator<RtfObject> enumerator = reader.Read().GetEnumerator();
                        while (enumerator.MoveNext())
                        {
                            if (enumerator.Current.Text == "object")
                            {
                                hasOLEObjects = true;
                                break;
                            }
                        }
                    }


    public RtfReader(TextReader reader)
            {
                if (reader == null)
                    throw new ArgumentNullException("reader");

                Reader = reader;
            }


public class RtfObject
    {
        public RtfObject(string text)
        {
            if (text == null)
                throw new ArgumentNullException("text");

            Text = text.Trim();
        }

        public string Text { get; private set; }
    }

1 个答案:

答案 0 :(得分:0)

我添加了一个if条件来检查OLE对象 if(RtfReader.MoveToNextControlWord(enumerator,&#34; objclass&#34;))

可能会帮助某人

public static bool ValidateOLEObjects(string filePath)
        {
            bool hasOLEObjects = false;
            using (StreamReader sr = new StreamReader(filePath))
            {
                RtfReader reader = new RtfReader(sr);
                IEnumerator<RtfObject> enumerator = reader.Read().GetEnumerator();
                while (enumerator.MoveNext())
                {
                    if (enumerator.Current.Text == "object")
                    {
                        if (RtfReader.MoveToNextControlWord(enumerator, "objclass"))
                        {
                            hasOLEObjects = true;
                            break;

                            }
                    }
                }

                return hasOLEObjects;
            }
        }