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.

Fri Jan 30, 2015 10:13 am

Rather than having 4 separate lists of StructureDocumentTag, StructureDocumentTagInline, StructureDocumentTagCell and StructureDocumentTagRow, which all have the same methods and settings, is there a generic object I can cast them all to in order to have 1 list?

Hampo
 
Posts: 28
Joined: Thu Jan 22, 2015 2:29 pm

Fri Jan 30, 2015 11:00 am

I have created a class which does this job, code is below:
Code: Select all
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Collections
Imports Spire.Doc.Fields

Public Class ContentControl
    Dim tagObj As DocumentObject
    Dim type As DocumentObjectType
    Public Sub New(ByVal tag As DocumentObject)
        tagObj = tag
        Select Case tag.DocumentObjectType
            Case DocumentObjectType.StructureDocumentTag
                type = tag.DocumentObjectType
            Case DocumentObjectType.StructureDocumentTagCell
                type = tag.DocumentObjectType
            Case DocumentObjectType.StructureDocumentTagRow
                type = tag.DocumentObjectType
            Case DocumentObjectType.StructureDocumentTagInline
                type = tag.DocumentObjectType
            Case Else
                type = Nothing
        End Select
    End Sub

    Public Property ControlAlias As String
        Get
            If IsNothing(type) Then Return Nothing
            Select Case type
                Case DocumentObjectType.StructureDocumentTag
                    Dim sdtTag As StructureDocumentTag = TryCast(tagObj, StructureDocumentTag)
                    Return sdtTag.SDTProperties.Alias
                Case DocumentObjectType.StructureDocumentTagCell
                    Dim sdtTag As StructureDocumentTagCell = TryCast(tagObj, StructureDocumentTagCell)
                    Return sdtTag.SDTProperties.Alias
                Case DocumentObjectType.StructureDocumentTagRow
                    Dim sdtTag As StructureDocumentTagRow = TryCast(tagObj, StructureDocumentTagRow)
                    Return sdtTag.SDTProperties.Alias
                Case DocumentObjectType.StructureDocumentTagInline
                    Dim sdtTag As StructureDocumentTagInline = TryCast(tagObj, StructureDocumentTagInline)
                    Return sdtTag.SDTProperties.Alias
                Case Else
                    Return Nothing
            End Select
        End Get
        Set(value As String)
            If IsNothing(type) Then Return
            Select Case type
                Case DocumentObjectType.StructureDocumentTag
                    Dim sdtTag As StructureDocumentTag = TryCast(tagObj, StructureDocumentTag)
                    sdtTag.SDTProperties.Alias = value
                Case DocumentObjectType.StructureDocumentTagCell
                    Dim sdtTag As StructureDocumentTagCell = TryCast(tagObj, StructureDocumentTagCell)
                    sdtTag.SDTProperties.Alias = value
                Case DocumentObjectType.StructureDocumentTagRow
                    Dim sdtTag As StructureDocumentTagRow = TryCast(tagObj, StructureDocumentTagRow)
                    sdtTag.SDTProperties.Alias = value
                Case DocumentObjectType.StructureDocumentTagInline
                    Dim sdtTag As StructureDocumentTagInline = TryCast(tagObj, StructureDocumentTagInline)
                    sdtTag.SDTProperties.Alias = value
            End Select
        End Set
    End Property

    Public Property ControlTag As String
        Get
            If IsNothing(type) Then Return Nothing
            Select Case type
                Case DocumentObjectType.StructureDocumentTag
                    Dim sdtTag As StructureDocumentTag = TryCast(tagObj, StructureDocumentTag)
                    Return sdtTag.SDTProperties.Tag
                Case DocumentObjectType.StructureDocumentTagCell
                    Dim sdtTag As StructureDocumentTagCell = TryCast(tagObj, StructureDocumentTagCell)
                    Return sdtTag.SDTProperties.Tag
                Case DocumentObjectType.StructureDocumentTagRow
                    Dim sdtTag As StructureDocumentTagRow = TryCast(tagObj, StructureDocumentTagRow)
                    Return sdtTag.SDTProperties.Tag
                Case DocumentObjectType.StructureDocumentTagInline
                    Dim sdtTag As StructureDocumentTagInline = TryCast(tagObj, StructureDocumentTagInline)
                    Return sdtTag.SDTProperties.Tag
                Case Else
                    Return Nothing
            End Select
        End Get
        Set(value As String)
            If IsNothing(type) Then Return
            Select Case type
                Case DocumentObjectType.StructureDocumentTag
                    Dim sdtTag As StructureDocumentTag = TryCast(tagObj, StructureDocumentTag)
                    sdtTag.SDTProperties.Tag = value
                Case DocumentObjectType.StructureDocumentTagCell
                    Dim sdtTag As StructureDocumentTagCell = TryCast(tagObj, StructureDocumentTagCell)
                    sdtTag.SDTProperties.Tag = value
                Case DocumentObjectType.StructureDocumentTagRow
                    Dim sdtTag As StructureDocumentTagRow = TryCast(tagObj, StructureDocumentTagRow)
                    sdtTag.SDTProperties.Tag = value
                Case DocumentObjectType.StructureDocumentTagInline
                    Dim sdtTag As StructureDocumentTagInline = TryCast(tagObj, StructureDocumentTagInline)
                    sdtTag.SDTProperties.Tag = value
            End Select
        End Set
    End Property

    Public ReadOnly Property ControlContent As List(Of String)
        Get
            If IsNothing(type) Then Return Nothing
            Dim content As New List(Of String)
            For Each obj As DocumentObject In ControlChildObjects
                Select Case obj.DocumentObjectType
                    Case DocumentObjectType.Paragraph
                        Dim p As Paragraph = TryCast(obj, Paragraph)
                        content.Add(p.Text)
                    Case DocumentObjectType.TextRange
                        Dim tr As TextRange = TryCast(obj, TextRange)
                        content.Add(tr.Text)
                End Select
            Next
            Return content
        End Get
    End Property

    Public Property ControlId As Integer
        Get
            If IsNothing(type) Then Return Nothing
            Select Case type
                Case DocumentObjectType.StructureDocumentTag
                    Dim sdtTag As StructureDocumentTag = TryCast(tagObj, StructureDocumentTag)
                    Return sdtTag.SDTProperties.Id
                Case DocumentObjectType.StructureDocumentTagCell
                    Dim sdtTag As StructureDocumentTagCell = TryCast(tagObj, StructureDocumentTagCell)
                    Return sdtTag.SDTProperties.Id
                Case DocumentObjectType.StructureDocumentTagRow
                    Dim sdtTag As StructureDocumentTagRow = TryCast(tagObj, StructureDocumentTagRow)
                    Return sdtTag.SDTProperties.Id
                Case DocumentObjectType.StructureDocumentTagInline
                    Dim sdtTag As StructureDocumentTagInline = TryCast(tagObj, StructureDocumentTagInline)
                    Return sdtTag.SDTProperties.Id
                Case Else
                    Return Nothing
            End Select
        End Get
        Set(value As Integer)
            If IsNothing(type) Then Return
            Select Case type
                Case DocumentObjectType.StructureDocumentTag
                    Dim sdtTag As StructureDocumentTag = TryCast(tagObj, StructureDocumentTag)
                    sdtTag.SDTProperties.Id = value
                Case DocumentObjectType.StructureDocumentTagCell
                    Dim sdtTag As StructureDocumentTagCell = TryCast(tagObj, StructureDocumentTagCell)
                    sdtTag.SDTProperties.Id = value
                Case DocumentObjectType.StructureDocumentTagRow
                    Dim sdtTag As StructureDocumentTagRow = TryCast(tagObj, StructureDocumentTagRow)
                    sdtTag.SDTProperties.Id = value
                Case DocumentObjectType.StructureDocumentTagInline
                    Dim sdtTag As StructureDocumentTagInline = TryCast(tagObj, StructureDocumentTagInline)
                    sdtTag.SDTProperties.Id = value
            End Select
        End Set
    End Property

    Public ReadOnly Property ControlChildObjects As DocumentObjectCollection
        Get
            If IsNothing(type) Then Return Nothing
            Select Case type
                Case DocumentObjectType.StructureDocumentTag
                    Dim sdtTag As StructureDocumentTag = TryCast(tagObj, StructureDocumentTag)
                    Return sdtTag.ChildObjects
                Case DocumentObjectType.StructureDocumentTagCell
                    Dim sdtTag As StructureDocumentTagCell = TryCast(tagObj, StructureDocumentTagCell)
                    Return sdtTag.ChildObjects
                Case DocumentObjectType.StructureDocumentTagRow
                    Dim sdtTag As StructureDocumentTagRow = TryCast(tagObj, StructureDocumentTagRow)
                    Return sdtTag.ChildObjects
                Case DocumentObjectType.StructureDocumentTagInline
                    Dim sdtTag As StructureDocumentTagInline = TryCast(tagObj, StructureDocumentTagInline)
                    Return sdtTag.ChildObjects
                Case Else
                    Return Nothing
            End Select
        End Get
    End Property
End Class


Please post any improvements that could be made.

Hampo
 
Posts: 28
Joined: Thu Jan 22, 2015 2:29 pm

Mon Feb 02, 2015 9:30 am

Hello Hampo,

Thanks for your inquiry.

You code works well on my machine, just some parameters name are inappropriate, so I changed it, you can refer to the whole code below:
Code: Select all

Public Class ContentControl
   Private tagObj As DocumentObject
   Private type As DocumentObjectType

   Public Sub New(tag As DocumentObject)
      tagObj = tag
      Select Case tag.DocumentObjectType
         Case DocumentObjectType.StructureDocumentTag
            type = tag.DocumentObjectType
            Exit Select
         Case DocumentObjectType.StructureDocumentTagCell
            type = tag.DocumentObjectType
            Exit Select
         Case DocumentObjectType.StructureDocumentTagRow
            type = tag.DocumentObjectType
            Exit Select
         Case DocumentObjectType.StructureDocumentTagInline
            type = tag.DocumentObjectType
            Exit Select
         Case Else
            type = tag.DocumentObjectType
            Exit Select
      End Select
   End Sub

   Public Property ControlAlias() As String
      Get
         If (type Is Nothing) Then
            Return Nothing
         End If
         Select Case type
            Case DocumentObjectType.StructureDocumentTag
               Dim sdt As StructureDocumentTag = TryCast(tagObj, StructureDocumentTag)
               Return sdt.SDTProperties.[Alias]
            Case DocumentObjectType.StructureDocumentTagCell
               Dim sdtc As StructureDocumentTagCell = TryCast(tagObj, StructureDocumentTagCell)
               Return sdtc.SDTProperties.[Alias]
            Case DocumentObjectType.StructureDocumentTagRow
               Dim sdtr As StructureDocumentTagRow = TryCast(tagObj, StructureDocumentTagRow)
               Return sdtr.SDTProperties.[Alias]
            Case DocumentObjectType.StructureDocumentTagInline
               Dim sdti As StructureDocumentTagInline = TryCast(tagObj, StructureDocumentTagInline)
               Return sdti.SDTProperties.[Alias]
            Case Else
               Return Nothing
         End Select
      End Get
      Set
         If (type Is Nothing) Then
            Return
         End If
         Select Case type
            Case DocumentObjectType.StructureDocumentTag
               Dim sdt As StructureDocumentTag = TryCast(tagObj, StructureDocumentTag)
               sdt.SDTProperties.[Alias] = value
               Exit Select
            Case DocumentObjectType.StructureDocumentTagCell
               Dim sdtc As StructureDocumentTagCell = TryCast(tagObj, StructureDocumentTagCell)
               sdtc.SDTProperties.[Alias] = value
               Exit Select
            Case DocumentObjectType.StructureDocumentTagRow
               Dim sdtr As StructureDocumentTagRow = TryCast(tagObj, StructureDocumentTagRow)
               sdtr.SDTProperties.[Alias] = value
               Exit Select
            Case DocumentObjectType.StructureDocumentTagInline
               Dim sdti As StructureDocumentTagInline = TryCast(tagObj, StructureDocumentTagInline)
               sdti.SDTProperties.[Alias] = value
               Exit Select
         End Select
      End Set
   End Property

   Public Property ControlTag() As String
      Get
         If (type Is Nothing) Then
            Return Nothing
         End If
         Select Case type
            Case DocumentObjectType.StructureDocumentTag
               Dim sdt As StructureDocumentTag = TryCast(tagObj, StructureDocumentTag)
               Return sdt.SDTProperties.Tag
            Case DocumentObjectType.StructureDocumentTagCell
               Dim sdtc As StructureDocumentTagCell = TryCast(tagObj, StructureDocumentTagCell)
               Return sdtc.SDTProperties.Tag
            Case DocumentObjectType.StructureDocumentTagRow
               Dim sdtr As StructureDocumentTagRow = TryCast(tagObj, StructureDocumentTagRow)
               Return sdtr.SDTProperties.Tag
            Case DocumentObjectType.StructureDocumentTagInline
               Dim sdti As StructureDocumentTagInline = TryCast(tagObj, StructureDocumentTagInline)
               Return sdti.SDTProperties.Tag
            Case Else
               Return Nothing
         End Select
      End Get
      Set
         If (type Is Nothing) Then
            Return
         End If
         Select Case type
            Case DocumentObjectType.StructureDocumentTag
               Dim sdt As StructureDocumentTag = TryCast(tagObj, StructureDocumentTag)
               sdt.SDTProperties.Tag = value
               Exit Select
            Case DocumentObjectType.StructureDocumentTagCell
               Dim sdtc As StructureDocumentTagCell = TryCast(tagObj, StructureDocumentTagCell)
               sdtc.SDTProperties.Tag = value
               Exit Select
            Case DocumentObjectType.StructureDocumentTagRow
               Dim sdtr As StructureDocumentTagRow = TryCast(tagObj, StructureDocumentTagRow)
               sdtr.SDTProperties.Tag = value
               Exit Select
            Case DocumentObjectType.StructureDocumentTagInline
               Dim sdti As StructureDocumentTagInline = TryCast(tagObj, StructureDocumentTagInline)
               sdti.SDTProperties.Tag = value
               Exit Select
         End Select
      End Set
   End Property

   Public ReadOnly Property ControlContent() As List(Of String)
      Get
         If (type Is Nothing) Then
            Return Nothing
         End If
         Dim content As New List(Of String)()
         For Each obj As DocumentObject In ControlChildObjects
            Select Case obj.DocumentObjectType
               Case DocumentObjectType.Paragraph
                  Dim p As Paragraph = TryCast(obj, Paragraph)
                  content.Add(p.Text)
                  Exit Select
               Case DocumentObjectType.TextRange
                  Dim tr As TextRange = TryCast(obj, TextRange)
                  content.Add(tr.Text)
                  Exit Select
            End Select
         Next
         Return content
      End Get
   End Property

   Public Property ControlId() As Decimal
      Get
         If (type Is Nothing) Then
            Return 0
         End If
         Select Case type
            Case DocumentObjectType.StructureDocumentTag
               Dim sdtTag As StructureDocumentTag = TryCast(tagObj, StructureDocumentTag)
               Return sdtTag.SDTProperties.Id
            Case DocumentObjectType.StructureDocumentTagCell
               Dim sdtc As StructureDocumentTagCell = TryCast(tagObj, StructureDocumentTagCell)
               Return sdtc.SDTProperties.Id
            Case DocumentObjectType.StructureDocumentTagRow
               Dim sdtr As StructureDocumentTagRow = TryCast(tagObj, StructureDocumentTagRow)
               Return sdtr.SDTProperties.Id
            Case DocumentObjectType.StructureDocumentTagInline
               Dim sdti As StructureDocumentTagInline = TryCast(tagObj, StructureDocumentTagInline)
               Return sdti.SDTProperties.Id
            Case Else
               Return 0
         End Select
      End Get
      Set
         If (type Is Nothing) Then
            Return
         End If
         Select Case type
            Case DocumentObjectType.StructureDocumentTag
               Dim sdt As StructureDocumentTag = TryCast(tagObj, StructureDocumentTag)
               sdt.SDTProperties.Id = value
               Exit Select
            Case DocumentObjectType.StructureDocumentTagCell
               Dim sdtc As StructureDocumentTagCell = TryCast(tagObj, StructureDocumentTagCell)
               sdtc.SDTProperties.Id = value
               Exit Select
            Case DocumentObjectType.StructureDocumentTagRow
               Dim sdtr As StructureDocumentTagRow = TryCast(tagObj, StructureDocumentTagRow)
               sdtr.SDTProperties.Id = value
               Exit Select
            Case DocumentObjectType.StructureDocumentTagInline
               Dim sdti As StructureDocumentTagInline = TryCast(tagObj, StructureDocumentTagInline)
               sdti.SDTProperties.Id = value
               Exit Select
         End Select
      End Set
   End Property

   Public ReadOnly Property ControlChildObjects() As DocumentObjectCollection
      Get
         If (type Is Nothing) Then
            Return Nothing
         End If
         Select Case type
            Case DocumentObjectType.StructureDocumentTag
               Dim sdt As StructureDocumentTag = TryCast(tagObj, StructureDocumentTag)
               Return sdt.ChildObjects
            Case DocumentObjectType.StructureDocumentTagCell
               Dim sdtc As StructureDocumentTagCell = TryCast(tagObj, StructureDocumentTagCell)
               Return sdtc.ChildObjects
            Case DocumentObjectType.StructureDocumentTagRow
               Dim sdtr As StructureDocumentTagRow = TryCast(tagObj, StructureDocumentTagRow)
               Return sdtr.ChildObjects
            Case DocumentObjectType.StructureDocumentTagInline
               Dim sdti As StructureDocumentTagInline = TryCast(tagObj, StructureDocumentTagInline)
               Return sdti.ChildObjects
            Case Else
               Return Nothing
         End Select
      End Get
   End Property
End Class
Best Regards,
Burning
E-iceblue Support Team
User avatar

burning.liu
 
Posts: 406
Joined: Mon Aug 04, 2014 6:47 am

Return to Spire.Doc