-2

LINK Extract Embedded Image Object in RTFI have the code below same as the link i provided it works well in 97-2003 document and same code is not working now in office 2007/2010. We usually check the RTFs with OLE object in it and Reject our conversion as per company rules but our code rejects even if there is a text written as "object" in office-2007/2010 created RTFs. Is there any solution to identify in Office-2007 created RTFs ?

        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; }
    }
rajesh
  • 115
  • 1
  • 2
  • 8
  • what namespace/package does `RtfReader` come from? – Keith Hall May 22 '17 at 11:38
  • Its a TextReader – rajesh May 22 '17 at 13:28
  • 1
    There is no built-in class called `RtfReader`. This looks like a class form your own project. It's not possible to help with code that you haven't posted, except guess at obvious problems. For example, why are you checking the *contents* of the element instead of its type? Doesn't `RtfReader` expose the element type? Right now, you are rejecting any document with the word `object` in its text. What if the users started using a template with an `Object` heading ? – Panagiotis Kanavos May 22 '17 at 13:40
  • NO RtfReader Doesn't expose the element type and Yes its class from my own project . I want to know if there are any OLE objects in an RTF . I have searched a lot in google but couldnt find the Soultion except this link below . please have a look and suggest me how to find an Object if exists in RTF . http://stackoverflow.com/questions/14779647/extract-embedded-image-object-in-rtf . – rajesh May 22 '17 at 15:41

1 Answers1

0

I have added one more if condition to check for an OLE object if (RtfReader.MoveToNextControlWord(enumerator, "objclass"))

May be this helps someone

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;
            }
        }
rajesh
  • 115
  • 1
  • 2
  • 8