Spire.Doc for .NET is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files from any .NET platform (C#, VB.NET, ASP.NET, .NET Core) with fast and high quality performance.

Wed Jun 20, 2012 5:40 am

Hi,
Is it possible to insert a form field, i.e. a checkbox, at a specific location in the Word document? Maybe with bookmarks? How should it be done?
Thanks.

catalin
 
Posts: 48
Joined: Wed Mar 14, 2012 12:56 pm

Wed Jun 20, 2012 9:48 am

Hi catalin,

About your issue, we can offer you a demo program. But we need some time to finish it. We will send the demo to your mailbox you registered in the forum once we finish it.
And you can also see the following program guide: http://www.e-iceblue.com/Knowledgebase/Spire.Doc/Program-Guide.html
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Nancy
e-iceblue support
Nancy
e-iceblue support team
contact: nancy.wang@e-iceblue.com
User avatar

nancy
 
Posts: 51
Joined: Wed May 30, 2012 2:02 am

Wed Jun 20, 2012 11:49 am

Actually i managed to to that alone. I post a code example, maybe it will be usefull for others also:

Code: Select all
                Dim document as new Document
                document.LoadFromFile("C:\template.doc", FileFormat.Doc)
                Dim mark As Bookmark = document.Bookmarks("Bookmark1")
                Dim p As Paragraph = mark.BookmarkEnd.OwnerParagraph
                Dim startIndex As Integer = p.Items.IndexOf(mark.BookmarkEnd)
                Dim c As New Spire.Doc.Fields.CheckBoxFormField(document)
                c.Checked = True
                c.Name = "Chk1"
                p.Items.Insert(p.Items.IndexOf(mark.BookmarkStart), c)

catalin
 
Posts: 48
Joined: Wed Mar 14, 2012 12:56 pm

Wed Jun 20, 2012 1:41 pm

This is work based on something I found on the internet. I ported it to C# from VB
Hope it helps

Code: Select all
public void PopulateMacroPlaceHolder()
        {
            Regex regexPattern = new Regex(@"(\<(/?[^\>]+)\>)");

            FileStream fs = File.OpenRead(@"D:\Conversions\Templates\All Default Letters\twitter.png");
            byte[] data = new byte[fs.Length];
            fs.Read(data, 0, data.Length);
 

            Paragraph paragraph = new Paragraph(spireDoc);
            paragraph.AppendPicture(data);
           
            Spire.Doc.Documents.TextSelection[] macros = spireDoc.FindAllPattern(regexPattern);
           
            TextSelection ts = spireDoc.FindString("<MACRO>", false, true);
            TextRangeLocation trl = new TextRangeLocation(ts.GetAsOneRange());
            Replace(trl, paragraph);


         

        }

        private void Replace(TextRangeLocation location, Paragraph par)
        {
            TextRange textRange = location.Text;
            int index = location.Index;
            Paragraph paragraph = location.Owner;
            Body sectionBody = paragraph.OwnerTextBody;

            int paragraphIndex = sectionBody.ChildObjects.IndexOf(paragraph);

            int replacementIndex = -1;

            if(index == 0)
            {
                paragraph.ChildObjects.RemoveAt(0);
                    replacementIndex = sectionBody.ChildObjects.IndexOf(paragraph);
            }
            else
            {
                if(index == paragraph.ChildObjects.Count - 1)
                {
                    paragraph.ChildObjects.RemoveAt(index);
                    replacementIndex = paragraphIndex + 1;
                }
                else
                {
                    Paragraph paragraph1 = paragraph.Clone() as Paragraph;
                    while(paragraph.ChildObjects.Count > index)
                        paragraph.ChildObjects.RemoveAt(index);
               
                    int i = 0;
                    int count = index + 1;
                    while( i < count)
                    {
                        paragraph1.ChildObjects.RemoveAt(0);
                        i += 1;
                    }
                    sectionBody.ChildObjects.Insert(paragraphIndex + 1, paragraph1);
   
                    replacementIndex = paragraphIndex + 1;
                }
            }
           
            sectionBody.ChildObjects.Insert(replacementIndex, par.Clone());
        }

itsupport
 
Posts: 10
Joined: Thu Jun 14, 2012 7:18 pm

Thu Jun 21, 2012 6:12 am

Hello,

Sorry for the late reply and thanks for your thoughtful consideration.

Have a great day.

Kind Regards.
Suvi
e-iceblue support
User avatar

Suvi.Wu
 
Posts: 154
Joined: Thu Oct 20, 2011 2:53 am

Return to Spire.Doc