Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Fri Aug 26, 2016 10:53 am

Hi,

I need to take a MergeField(not FormField) by a dotx file.

This is my code to read document:

Code: Select all
Spire.Doc.Document document = new Spire.Doc.Document();
            document.LoadFromFile(sourceFile);


How can I do?

Riccardo

riccardo94
 
Posts: 2
Joined: Fri Aug 26, 2016 8:32 am

Mon Aug 29, 2016 7:06 am

Dear Riccardo,

Thanks for your inquiry.
Sorry that there is no method to get the mergeField directly, but you can traverse whole document and then find it. Here is sample code for your reference:
Code: Select all
            Document doc = new Document();
            doc.LoadFromFile(FilePath + "8424name.dotx");
            List<MergeField> mergeFields = new List<MergeField>();
            //traverse section           
            foreach (Section sec in doc.Sections)
            {
                //traverse section's body and then find the MergeField
                foreach(DocumentObject obj in sec.Body.ChildObjects)
                {
                    if (obj is Paragraph)
                    {
                        Paragraph par = obj as Paragraph;
                        foreach (DocumentObject o in par.ChildObjects)
                        {
                            if (o.DocumentObjectType == DocumentObjectType.MergeField)
                            {
                                MergeField field = o as MergeField;
                                mergeFields.Add(field);
                            }
                        }
                    }
                }
            }

Hope this can help you. If this doesn't meet your requirement, please provide us sample document and then we can provide you corresponding code.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Tue Aug 30, 2016 7:39 am

Dear Riccardo,

Did you test the code I provide ? Has your issue been resolved ?

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Return to Spire.Doc