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.
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)
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());
}