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
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;
}
}
}
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;
}
}
}
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;
}
}
}