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.
Tue Jan 27, 2015 9:47 am
I have used the demo version to read legacy forms, but it would be better to support the latest forms (content controls), which it does not seem to do, or I can not find where the option is. Could anyone point me in the right direction please?
-
Hampo
-
- Posts: 28
- Joined: Thu Jan 22, 2015 2:29 pm
Wed Jan 28, 2015 3:47 am
Hello Hampo,
Thanks for your inquiry.
Spire.Doc supports the content controls, please refer to the code below:
- Code: Select all
Document doc = new Document("Sample.docx", FileFormat.Docx2010);
foreach (Section section in doc.Sections)
{
foreach (DocumentObject obj in section.Body.ChildObjects)
{
if (DocumentObjectType.StructureDocumentTag == obj.DocumentObjectType)
{
StructureDocumentTag sdt = obj as StructureDocumentTag;
Console.WriteLine("Title: " + sdt.SDTProperties.Alias);
Console.WriteLine("ID: " + sdt.SDTProperties.Id);
Console.WriteLine("Markup: " + sdt.SDTProperties.Tag);
foreach (DocumentObject sdtobj in sdt.ChildObjects)
{
if (sdtobj.DocumentObjectType == DocumentObjectType.Paragraph)
{
Paragraph content = sdtobj as Paragraph;
Console.WriteLine("Contents: " + content.Text);
}
}
}
}
}
Best Regards,
Burning
E-iceblue Support Team
-
burning.liu
-
- Posts: 406
- Joined: Mon Aug 04, 2014 6:47 am
Wed Jan 28, 2015 8:21 am
SDTProperties doesn't contain ID or Alias or any of the others
-
Hampo
-
- Posts: 28
- Joined: Thu Jan 22, 2015 2:29 pm
Wed Jan 28, 2015 9:02 am
Hello Hampo,
Sorry that the free version doesn't support this feature, please try Spire.Doc v5.3.38.
Best Regards,
Burning
E-iceblue Support Team
-
burning.liu
-
- Posts: 406
- Joined: Mon Aug 04, 2014 6:47 am
Wed Jan 28, 2015 9:29 am
I got the demo version of the pro version, and the code you gave gets 1 control that doesn't seem to even exist in the word doc, and the code from
this page gets the 1 your code gets, and 5 actual ones, but the document has a lot more than 5 controls. Is this a restriction of the demo?
-
Hampo
-
- Posts: 28
- Joined: Thu Jan 22, 2015 2:29 pm
Wed Jan 28, 2015 9:42 am
Hello,
Thanks for your response. Please attach your document here, we will investigate and provide you the corresponding solution. If it is inconvenient to attach here, you could send it to
support@e-iceblue.com via email.
Thanks,
Gary
E-iceblue support team
-
Gary.zhang
-
- Posts: 1380
- Joined: Thu Apr 04, 2013 1:30 am
Wed Jan 28, 2015 9:54 am
Gary.zhang wrote:Hello,
Thanks for your response. Please attach your document here, we will investigate and provide you the corresponding solution. If it is inconvenient to attach here, you could send it to
support@e-iceblue.com via email.
Thanks,
Gary
E-iceblue support team
The file is attached
The code used is below:
- Code: Select all
Public Class StructureTags
Dim m_tagInlines As New List(Of StructureDocumentTagInline)
Public Property tagInLines As List(Of StructureDocumentTagInline)
Get
If m_tagInlines Is Nothing Then m_tagInlines = New List(Of StructureDocumentTagInline)
Return m_tagInlines
End Get
Set(value As List(Of StructureDocumentTagInline))
m_tagInlines = value
End Set
End Property
Dim m_tags As New List(Of StructureDocumentTag)
Public Property tags As List(Of StructureDocumentTag)
Get
If m_tags Is Nothing Then m_tags = New List(Of StructureDocumentTag)
Return m_tags
End Get
Set(value As List(Of StructureDocumentTag))
m_tags = value
End Set
End Property
End Class
- Code: Select all
Function GetAllTags(doc As Document) As StructureTags
Dim structureTags As New StructureTags()
For Each section As Section In doc.Sections
For Each obj As DocumentObject In section.Body.ChildObjects
Select Case obj.DocumentObjectType
Case Documents.DocumentObjectType.Paragraph
For Each pobj As DocumentObject In TryCast(obj, Paragraph).ChildObjects
If pobj.DocumentObjectType = DocumentObjectType.StructureDocumentTagInline Then
structureTags.tagInLines.Add(TryCast(pobj, StructureDocumentTagInline))
End If
Next
Exit Select
Case Documents.DocumentObjectType.Table
For Each row As TableRow In TryCast(obj, Table).Rows
For Each cell As TableCell In row.Cells
For Each cellChild As DocumentObject In cell.ChildObjects
Select Case cellChild.DocumentObjectType
Case DocumentObjectType.StructureDocumentTag
structureTags.tags.Add(TryCast(cellChild, StructureDocumentTag))
Exit Select
Case DocumentObjectType.Paragraph
For Each pobj As DocumentObject In TryCast(cellChild, Paragraph).ChildObjects
If pobj.DocumentObjectType = DocumentObjectType.StructureDocumentTagInline Then
structureTags.tagInLines.Add(TryCast(pobj, StructureDocumentTagInline))
End If
Next
Exit Select
Case Else
Exit Select
End Select
Next
Next
Next
Exit Select
Case Else
Exit Select
End Select
Next
Next
Return structureTags
End Function
- Code: Select all
Public Sub getControls(ByVal doc As Document)
Dim structureTags As StructureTags = GetAllTags(doc)
Dim tagInlines As List(Of StructureDocumentTagInline) = structureTags.tagInLines
Dim tags As List(Of StructureDocumentTag) = structureTags.tags
MsgBox(tagInlines.Count.ToString + " + " + tags.Count.ToString)
For Each tagInline As StructureDocumentTagInline In tagInlines
Dim msg As String = ""
msg += "Title: " + tagInline.SDTProperties.Alias + vbCrLf
msg += "ID: " + tagInline.SDTProperties.Id.ToString + vbCrLf
msg += "Markup: " + tagInline.SDTProperties.Tag + vbCrLf
For Each sdtobj As DocumentObject In tagInline.ChildObjects
If sdtobj.DocumentObjectType = DocumentObjectType.Paragraph Then
Dim content As Paragraph = TryCast(sdtobj, Paragraph)
msg += "Contents: " + content.Text + vbCrLf
End If
Next
MsgBox(msg)
Next
For Each tagInline As StructureDocumentTag In tags
Dim msg As String = ""
msg += "Title: " + tagInline.SDTProperties.Alias + vbCrLf
msg += "ID: " + tagInline.SDTProperties.Id.ToString + vbCrLf
msg += "Markup: " + tagInline.SDTProperties.Tag + vbCrLf
For Each sdtobj As DocumentObject In tagInline.ChildObjects
If sdtobj.DocumentObjectType = DocumentObjectType.Paragraph Then
Dim content As Paragraph = TryCast(sdtobj, Paragraph)
msg += "Contents: " + content.Text + vbCrLf
End If
Next
MsgBox(msg)
Next
End Sub
Login to view the files attached to this post.
-
Hampo
-
- Posts: 28
- Joined: Thu Jan 22, 2015 2:29 pm
Thu Jan 29, 2015 2:14 am
Hello Hampo,
Thanks for sharing your sample document.
After some investigation, I found out that the content control's type in the table is
StructureDocumentTagCell, not
StructureDocumentTagInline, so please try following code to read content controls:
- Code: Select all
Dim doc As New Document("form.docx", FileFormat.Docx2010)
For Each section As Section In doc.Sections
For Each objBody As DocumentObject In section.Body.ChildObjects
If DocumentObjectType.Table = objBody.DocumentObjectType Then
For Each tr As TableRow In TryCast(objBody, Table).Rows
For Each objTR As DocumentObject In tr.ChildObjects
If DocumentObjectType.StructureDocumentTagCell = objTR.DocumentObjectType Then
Dim sdtCell As StructureDocumentTagCell = TryCast(objTR, StructureDocumentTagCell)
Console.WriteLine("Title: " + sdtCell.SDTProperties.[Alias])
Console.WriteLine("ID: " + sdtCell.SDTProperties.Id)
Console.WriteLine("Markup: " + sdtCell.SDTProperties.Tag)
For Each sdtobj As DocumentObject In sdtCell.ChildObjects
If sdtobj.DocumentObjectType = DocumentObjectType.Paragraph Then
Dim content As Paragraph = TryCast(sdtobj, Paragraph)
Console.WriteLine("Contents: " + content.Text)
End If
Next
Console.WriteLine(New String("-"C, 30))
End If
Next
Next
End If
Next
Next
Console.ReadKey()
Best Regards,
Burning
E-iceblue Support Team
-
burning.liu
-
- Posts: 406
- Joined: Mon Aug 04, 2014 6:47 am