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.

Wed May 16, 2018 6:45 pm

I have template word files and i want fill the value for each one
how can i use the Spire.DOc to set values

if any help with VB.NET code

mando
 
Posts: 17
Joined: Wed May 16, 2018 6:05 pm

Wed May 16, 2018 7:06 pm

or sample for c#.net

mando
 
Posts: 17
Joined: Wed May 16, 2018 6:05 pm

Wed May 16, 2018 8:59 pm

Any Help

mando
 
Posts: 17
Joined: Wed May 16, 2018 6:05 pm

Thu May 17, 2018 2:59 am

Dear mando,

Sorry for late reply as different time zone.
Here is sample code for your kind reference:
Code: Select all
            using (Document document = new Document(@"F:\SDTsample.docx"))
            {
                StructureTags structureTags = GetAllTags(document);
                List<StructureDocumentTagInline> tagInlines = structureTags.tagInlines;

                for (int i = 0; i < tagInlines.Count; i++)
                {
                    string alias = tagInlines[i].SDTProperties.Alias;
                    decimal id = tagInlines[i].SDTProperties.Id;
                    string tag = tagInlines[i].SDTProperties.Tag;
                    string STDType = tagInlines[i].SDTProperties.SDTType.ToString();

                    //find the SDT which you want to fill accroding to it's type(e.g. SdtText, RichText), tag and so on.
                    if (tagInlines[i].SDTProperties.SDTType == SdtType.RichText && tag == "tag")
                    {
                        //set the text
                        tagInlines[i].OwnerParagraph.Text = "text";
                                                   
                    }
                }
                document.SaveToFile("13836.docx", FileFormat.Docx);
                System.Diagnostics.Process.Start("13836.docx");

            }
        }

        static StructureTags GetAllTags(Document document)
        {
            StructureTags structureTags = new StructureTags();
            foreach (Section section in document.Sections)
            {
                foreach (DocumentObject obj in section.Body.ChildObjects)
                {
                    if (obj.DocumentObjectType == DocumentObjectType.Paragraph)
                    {
                        foreach (DocumentObject pobj in (obj as Paragraph).ChildObjects)
                        {
                            if (pobj.DocumentObjectType == DocumentObjectType.StructureDocumentTagInline)
                            {
                                structureTags.tagInlines.Add(pobj as StructureDocumentTagInline);
                            }
                        }
                    }
                    else if (obj.DocumentObjectType == DocumentObjectType.Table)
                    {
                        foreach (TableRow row in (obj as Table).Rows)
                        {
                            foreach (TableCell cell in row.Cells)
                            {
                                foreach (DocumentObject cellChild in cell.ChildObjects)
                                {
                                    if (cellChild.DocumentObjectType == DocumentObjectType.StructureDocumentTag)
                                    {
                                        structureTags.tags.Add(cellChild as StructureDocumentTag);
                                    }
                                    else if (cellChild.DocumentObjectType == DocumentObjectType.Paragraph)
                                    {
                                        foreach (DocumentObject pobj in (cellChild as Paragraph).ChildObjects)
                                        {
                                            if (pobj.DocumentObjectType == DocumentObjectType.StructureDocumentTagInline)
                                            {
                                                structureTags.tagInlines.Add(pobj as StructureDocumentTagInline);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return structureTags;
        }
        public class StructureTags
        {
            List<StructureDocumentTagInline> m_tagInlines;
            public List<StructureDocumentTagInline> tagInlines
            {
                get
                {
                    if (m_tagInlines == null)
                        m_tagInlines = new List<StructureDocumentTagInline>();
                    return m_tagInlines;
                }
                set
                {
                    m_tagInlines = value;
                }
            }
            List<StructureDocumentTag> m_tags;
            public List<StructureDocumentTag> tags
            {
                get
                {
                    if (m_tags == null)
                        m_tags = new List<StructureDocumentTag>();
                    return m_tags;
                }
                set
                {
                    m_tags = value;
                }
            }
        }

Hope it helps. If you still have the issue, please provide us with your input document and the code you were using for further investigation.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Thu May 17, 2018 10:15 am

thank u there is example for vb.net ?

mando
 
Posts: 17
Joined: Wed May 16, 2018 6:05 pm

Thu May 17, 2018 11:41 am

thank u for ur help

when i applied ur code it's work fine but
i received null when i check the tagInlines[i].SDTProperties.SDTType

mando
 
Posts: 17
Joined: Wed May 16, 2018 6:05 pm

Thu May 17, 2018 11:46 am

also i want work on Header and footer
any Sample code

mando
 
Posts: 17
Joined: Wed May 16, 2018 6:05 pm

Thu May 17, 2018 12:54 pm

also when i run the code i am not see these Tag at the code
i put them in red box
other tag i see them

mando
 
Posts: 17
Joined: Wed May 16, 2018 6:05 pm

Fri May 18, 2018 7:02 am

Dear mando,

Thanks for your response.
Generally, if the type of the content control is not specified in xml file, it will be none, in this case, the SDT should be a rich text box. As for your situation, the title and tag are unique, you could use them to find the content control you want to edit.
I have changed my code, please try it with the latest Spire.Doc V6.5.2. If you still have the issue, please share the input Word document, then I will look into it and provide corresponding code for you.
Code: Select all
        public static StructureTags structureTags = new StructureTags();
        public static void FillSDT13836()
        {
            using (Document document = new Document(@"F:\testing\doc form\original document\SDTsample.docx"))
            {
                StructureTags structureTags = GetAllTags(document);
                List<StructureDocumentTag> tagInlines = structureTags.tags;

                for (int i = 0; i < tagInlines.Count; i++)
                {
                    string alias = tagInlines[i].SDTProperties.Alias;
                    string tag = tagInlines[i].SDTProperties.Tag;

                    //use alias and tag to find the content control that you want to edit
                    if (tag == "header tag" && alias == "header title")
                    {
                        //get the paragraph
                        Paragraph par = tagInlines[i].ChildObjects.FirstItem as Paragraph;
                        //set the text, you could set other settings you want.
                        par.Text = "text";

                    }
                }
                document.SaveToFile("13836.docx", FileFormat.Docx);
                System.Diagnostics.Process.Start("13836.docx");

            }
        }

        static StructureTags GetAllTags(Document document)
        {
            foreach (Section section in document.Sections)
            {
                HeaderFooter header = section.HeadersFooters.Header;
                foreach (DocumentObject obj in header.ChildObjects)
                {
                    GetSDTInDocumentObject(obj);
                }

                HeaderFooter footer = section.HeadersFooters.Footer;
                foreach (DocumentObject obj in footer.ChildObjects)
                {
                    GetSDTInDocumentObject(obj);
                }

                foreach (DocumentObject obj in section.Body.ChildObjects)
                {
                    GetSDTInDocumentObject(obj);
                }

            }
            return structureTags;
        }

        public static void GetSDTInDocumentObject(DocumentObject obj)
        {

            if (obj.DocumentObjectType == DocumentObjectType.Paragraph)
            {
                foreach (DocumentObject pobj in (obj as Paragraph).ChildObjects)
                {
                    if (pobj.DocumentObjectType == DocumentObjectType.StructureDocumentTagInline)
                    {
                        structureTags.tagInlines.Add(pobj as StructureDocumentTagInline);
                    }
                }
            }
            else if (obj is StructureDocumentTag)
            {
                structureTags.tags.Add(obj as StructureDocumentTag);
            }

            else if (obj.DocumentObjectType == DocumentObjectType.Table)
            {
                foreach (TableRow row in (obj as Table).Rows)
                {
                    foreach (TableCell cell in row.Cells)
                    {
                        foreach (DocumentObject cellChild in cell.ChildObjects)
                        {
                            if (cellChild.DocumentObjectType == DocumentObjectType.StructureDocumentTag)
                            {
                                structureTags.tags.Add(cellChild as StructureDocumentTag);
                            }
                            else if (cellChild.DocumentObjectType == DocumentObjectType.Paragraph)
                            {
                                foreach (DocumentObject pobj in (cellChild as Paragraph).ChildObjects)
                                {
                                    if (pobj.DocumentObjectType == DocumentObjectType.StructureDocumentTagInline)
                                    {
                                        structureTags.tagInlines.Add(pobj as StructureDocumentTagInline);
                                    }
                                }
                            }
                        }
                    }
                }

            }

        }
        public class StructureTags
        {
            List<StructureDocumentTagInline> m_tagInlines;
            public List<StructureDocumentTagInline> tagInlines
            {
                get
                {
                    if (m_tagInlines == null)
                        m_tagInlines = new List<StructureDocumentTagInline>();
                    return m_tagInlines;
                }
                set
                {
                    m_tagInlines = value;
                }
            }
            List<StructureDocumentTag> m_tags;
            public List<StructureDocumentTag> tags
            {
                get
                {
                    if (m_tags == null)
                        m_tags = new List<StructureDocumentTag>();
                    return m_tags;
                }
                set
                {
                    m_tags = value;
                }
            }
        }


Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Sat May 19, 2018 12:24 am

thank u Betsy.jiang

also there is way to make word form editor ? like this

mando
 
Posts: 17
Joined: Wed May 16, 2018 6:05 pm

Mon May 21, 2018 2:28 am

Dear mando,

Sorry for late reply as weekend.
According to your information, I guess you want to edit your document with real-time interaction. I am afraid our product cannot do that, it only supports editing the file in code.
If I misunderstand your meaning, please let me know.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Mon May 21, 2018 12:01 pm

thank u

is Spire.Doc support docx preview such what i attached in last image
i hope u reply ASAP

Reagrds

mando
 
Posts: 17
Joined: Wed May 16, 2018 6:05 pm

Tue May 22, 2018 2:01 am

Dear mando,

Thanks for your information.
Sorry that Spire.Doc doesn't support docx preview. I think our Spire.DocViewer which supports viewing docx document could meet your requirement. Note, if you want to use our two or more products in one project, you need to download Spire.Office and reference the corresponding dlls from it.
If there is any question, welcome to get it back to us.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Tue May 22, 2018 11:06 pm

i have worked on ur code but it's not appear the Header and footer
please could u provide me a new code also it must be VB.NET

Code: Select all
        public static StructureTags structureTags = new StructureTags();
        public static void FillSDT13836()
        {
            using (Document document = new Document(@"F:\testing\doc form\original document\SDTsample.docx"))
            {
                StructureTags structureTags = GetAllTags(document);
                List<StructureDocumentTag> tagInlines = structureTags.tags;

                for (int i = 0; i < tagInlines.Count; i++)
                {
                    string alias = tagInlines[i].SDTProperties.Alias;
                    string tag = tagInlines[i].SDTProperties.Tag;

                    //use alias and tag to find the content control that you want to edit
                    if (tag == "header tag" && alias == "header title")
                    {
                        //get the paragraph
                        Paragraph par = tagInlines[i].ChildObjects.FirstItem as Paragraph;
                        //set the text, you could set other settings you want.
                        par.Text = "text";

                    }
                }
                document.SaveToFile("13836.docx", FileFormat.Docx);
                System.Diagnostics.Process.Start("13836.docx");

            }
        }

        static StructureTags GetAllTags(Document document)
        {
            foreach (Section section in document.Sections)
            {
                HeaderFooter header = section.HeadersFooters.Header;
                foreach (DocumentObject obj in header.ChildObjects)
                {
                    GetSDTInDocumentObject(obj);
                }

                HeaderFooter footer = section.HeadersFooters.Footer;
                foreach (DocumentObject obj in footer.ChildObjects)
                {
                    GetSDTInDocumentObject(obj);
                }

                foreach (DocumentObject obj in section.Body.ChildObjects)
                {
                    GetSDTInDocumentObject(obj);
                }

            }
            return structureTags;
        }

        public static void GetSDTInDocumentObject(DocumentObject obj)
        {

            if (obj.DocumentObjectType == DocumentObjectType.Paragraph)
            {
                foreach (DocumentObject pobj in (obj as Paragraph).ChildObjects)
                {
                    if (pobj.DocumentObjectType == DocumentObjectType.StructureDocumentTagInline)
                    {
                        structureTags.tagInlines.Add(pobj as StructureDocumentTagInline);
                    }
                }
            }
            else if (obj is StructureDocumentTag)
            {
                structureTags.tags.Add(obj as StructureDocumentTag);
            }

            else if (obj.DocumentObjectType == DocumentObjectType.Table)
            {
                foreach (TableRow row in (obj as Table).Rows)
                {
                    foreach (TableCell cell in row.Cells)
                    {
                        foreach (DocumentObject cellChild in cell.ChildObjects)
                        {
                            if (cellChild.DocumentObjectType == DocumentObjectType.StructureDocumentTag)
                            {
                                structureTags.tags.Add(cellChild as StructureDocumentTag);
                            }
                            else if (cellChild.DocumentObjectType == DocumentObjectType.Paragraph)
                            {
                                foreach (DocumentObject pobj in (cellChild as Paragraph).ChildObjects)
                                {
                                    if (pobj.DocumentObjectType == DocumentObjectType.StructureDocumentTagInline)
                                    {
                                        structureTags.tagInlines.Add(pobj as StructureDocumentTagInline);
                                    }
                                }
                            }
                        }
                    }
                }

            }

        }
        public class StructureTags
        {
            List<StructureDocumentTagInline> m_tagInlines;
            public List<StructureDocumentTagInline> tagInlines
            {
                get
                {
                    if (m_tagInlines == null)
                        m_tagInlines = new List<StructureDocumentTagInline>();
                    return m_tagInlines;
                }
                set
                {
                    m_tagInlines = value;
                }
            }
            List<StructureDocumentTag> m_tags;
            public List<StructureDocumentTag> tags
            {
                get
                {
                    if (m_tags == null)
                        m_tags = new List<StructureDocumentTag>();
                    return m_tags;
                }
                set
                {
                    m_tags = value;
                }
            }
        }
 

mando
 
Posts: 17
Joined: Wed May 16, 2018 6:05 pm

Wed May 23, 2018 12:37 am

here is my template fields
i want fill it

mando
 
Posts: 17
Joined: Wed May 16, 2018 6:05 pm

Return to Spire.Doc