Working with Word fields Code

Technical support for Spire.Doc

Moderator: iceblue support

Working with Word fields Code

Postby ricags » Sun Feb 06, 2011 11:05 pm

Hi,

I'm trying the sprite.doc library to evaluate if the company should buy it. I have several word office doc file with fields code on it (history about 5 years), the objective is that I need to access to those fields code control them using the field code name and convert the field code to bookmark.

The main idea is during the utilization, the user when open the document the custom application switches the fields code to bookmark.

How I can get the access to the field code, get the value and convert the field to a bookmark with the value as a name? Note: I tested the library, and I see the field value under Non-Public members.

Regards,
RS
ricags
 
Posts: 2
Joined: Sat Feb 05, 2011 2:01 pm

Re: Working with Word fields Code

Postby harry.support » Tue Feb 08, 2011 9:22 am

Thanks for your inquiry.

Spire.Doc has no direct method to acquire code and value of a field.
But you could try:
Code: Select all
Document document = new Document(@"..\..\UserForm.doc");

//document elements, each of them has child elements
Queue<ICompositeObject> nodes = new Queue<ICompositeObject>();
nodes.Enqueue(document);

//traverse
while (nodes.Count > 0)
{
   ICompositeObject node = nodes.Dequeue();
   foreach (IDocumentObject child in node.ChildObjects)
   {
      if (child is ICompositeObject)
      {
         nodes.Enqueue(child as ICompositeObject);
      }
      else if (child.DocumentObjectType == DocumentObjectType.TextFormField)
      {
         TextFormField field = child as TextFormField;

         //five \u2002 are nonbreaking spaces
         String value = field.Text == "\u2002\u2002\u2002\u2002\u2002" ? "" : field.Text;
         Console.WriteLine("Field type: {0}, name: {1}, value: {2}", field.Type, field.Name, value);
      }
      else if (child.DocumentObjectType == DocumentObjectType.CheckBox)
      {
         CheckBoxFormField field = child as CheckBoxFormField;
         Console.WriteLine("Field type: {0}, name: {1}, value: {2}", field.Type, field.Name, field.Checked);
      }
      else if (child.DocumentObjectType == DocumentObjectType.DropDownFormField)
      {
         DropDownFormField field = child as DropDownFormField;
         String value = field.DropDownItems[field.DropDownSelectedIndex].Text;
         Console.WriteLine("Field type: {0}, name: {1}, value: {2}", field.Type, field.Name, value);
      }
      else if (child.DocumentObjectType == DocumentObjectType.MergeField)
      {
         MergeField field = child as MergeField;
         Console.WriteLine("Field type: {0}, name: {1}, value: {2}", field.Type, field.FieldName, field.Text);
      }
      else if (child.DocumentObjectType == DocumentObjectType.Field)
      {
         Field field = child as Field;
         String value = String.Empty;
         if (field.NextSibling != null && field.NextSibling.DocumentObjectType == DocumentObjectType.FieldMark)
         {
            FieldMark fieldMark = field.NextSibling as FieldMark;
            if (fieldMark.Type == FieldMarkType.FieldSeparator
               && fieldMark.NextSibling != null
               && fieldMark.NextSibling.DocumentObjectType == DocumentObjectType.TextRange)
            {
               TextRange text = fieldMark.NextSibling as TextRange;
               value = text.Text;
            }
         }
         Console.WriteLine("Field type: {0}, value: {1}", field.Type, value);
      }
   }
}


We may provide some direct properties to represent code and value in the future version.
Harry
Technical Support / Developer,
e-iceblue Support Team
User avatar
harry.support
 
Posts: 180
Joined: Mon Nov 08, 2010 3:11 pm

Re: Working with Word fields Code

Postby ricags » Tue Feb 08, 2011 7:56 pm

Hi,

First of all, Thank for your information.

I've tried with the following code and almost works, inevitably I don't have access to field from Non-Public members. There is another way to get the information?

Code: Select all
            Spire.Doc.Document doc = new Spire.Doc.Document();

            doc.LoadFromFile("c:\\DMS\\teste_123.doc", FileFormat.Doc);

            //document elements, each of them has child elements
            Queue<ICompositeObject> nodes = new Queue<ICompositeObject>();
            nodes.Enqueue(doc);

            //embedded images list.
            IList<DocOleObject> objectos_ = new List<DocOleObject>();

            //traverse
            while (nodes.Count > 0)
            {
                ICompositeObject node = nodes.Dequeue();
                foreach (IDocumentObject child in node.ChildObjects)
                {
                    if (child is ICompositeObject)
                    {
                        nodes.Enqueue(child as ICompositeObject);
                    }
                    else if (child.DocumentObjectType == DocumentObjectType.OleObject)
                    {
                        Spire.Doc.Fields.DocOleObject oleObject = child as Spire.Doc.Fields.DocOleObject;

                        objectos_.Add(oleObject);
                    }

                }
            }


Please advise.

Regards,
RS
ricags
 
Posts: 2
Joined: Sat Feb 05, 2011 2:01 pm

Re: Working with Word fields Code

Postby harry.support » Wed Feb 09, 2011 1:51 am

Hi,
Sorry, I can not understand what information you want to get from Non-Public members. But if you want to extract OLE object, please check out viewtopic.php?f=6&t=2214
Harry
Technical Support / Developer,
e-iceblue Support Team
User avatar
harry.support
 
Posts: 180
Joined: Mon Nov 08, 2010 3:11 pm


Return to Spire.Doc

Who is online

Users browsing this forum: No registered users and 0 guests