''' <summary>
''' Returns the DOCX document tree as XML tree.
''' </summary>
''' <param name="filePath">The DOCX file path.</param>
''' <returns>
''' A string that contains XML tree.
''' </returns>
Public Shared Function GetDocxDocumentTreeAsXml(filePath As String) As String
' open DOCX document
Using editor As New Vintasoft.Imaging.Office.OpenXml.Editor.Docx.DocxDocumentEditor(filePath)
Dim sb As New System.Text.StringBuilder()
' create XML writer settings
Dim settings As New System.Xml.XmlWriterSettings()
settings.Indent = True
settings.CheckCharacters = False
settings.OmitXmlDeclaration = True
' create XML writer
Using writer As System.Xml.XmlWriter = System.Xml.XmlWriter.Create(sb, settings)
' add DOCX focument body to the XML writer
AddElement(writer, editor.Body)
End Using
' return a string that contains XML tree
Return sb.ToString()
End Using
End Function
''' <summary>
''' Adds the element information to the XML writer.
''' </summary>
''' <param name="writer">The XML writer.</param>
''' <param name="element">The element that should be processed.</param>
Private Shared Sub AddElement(writer As System.Xml.XmlWriter, element As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentElement)
' if element is root
If TypeOf element Is Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentRootElement Then
writer.WriteStartElement("Document")
For Each child As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentElement In element
AddElement(writer, child)
Next
writer.WriteEndElement()
' if element is paragraph
ElseIf TypeOf element Is Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentParagraph Then
Dim paragraph As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentParagraph = DirectCast(element, Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentParagraph)
writer.WriteStartElement("Paragraph")
' write paragraph properties
Dim paragraphProperties As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlParagraphProperties = paragraph.GetParagraphProperties()
AddParagraphProperties(writer, paragraphProperties)
' write paragraph text properties
Dim textProperties As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties = paragraph.GetParagraphTextProperties()
AddTextProperties(writer, textProperties)
' write paragraph content
For Each child As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentElement In paragraph
AddElement(writer, child)
Next
writer.WriteEndElement()
' if element is image
ElseIf TypeOf element Is Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentImage Then
Dim image As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentImage = DirectCast(element, Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentImage)
writer.WriteStartElement("Image")
' write image properties
If Not String.IsNullOrEmpty(image.ContentType) Then
writer.WriteAttributeString("ContentType", image.ContentType)
End If
AddGraphicsProperties(writer, image)
writer.WriteEndElement()
' if element is chart
ElseIf TypeOf element Is Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentChart Then
Dim chart As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentChart = DirectCast(element, Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentChart)
writer.WriteStartElement("Chart")
' write chart properties
writer.WriteAttributeString("Title", chart.Title)
AddGraphicsProperties(writer, chart)
writer.WriteEndElement()
' if element is graphics
ElseIf TypeOf element Is Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentGraphics Then
Dim graphics As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentGraphics = DirectCast(element, Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentGraphics)
writer.WriteStartElement("Graphics")
' write graphics properties
AddGraphicsProperties(writer, graphics)
writer.WriteEndElement()
' if element is table
ElseIf TypeOf element Is Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTable Then
Dim table As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTable = DirectCast(element, Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTable)
writer.WriteStartElement("Table")
For Each child As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentElement In table
AddElement(writer, child)
Next
writer.WriteEndElement()
' if element is table row
ElseIf TypeOf element Is Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTableRow Then
Dim row As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTableRow = DirectCast(element, Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTableRow)
writer.WriteStartElement("Row")
For Each child As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentElement In row
AddElement(writer, child)
Next
writer.WriteEndElement()
' if element is table cell
ElseIf TypeOf element Is Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTableCell Then
Dim cell As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTableCell = DirectCast(element, Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTableCell)
writer.WriteStartElement("Cell")
For Each child As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentElement In cell
AddElement(writer, child)
Next
writer.WriteEndElement()
' if element is text content
ElseIf TypeOf element Is Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTextContent Then
Dim textContent As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTextContent = DirectCast(element, Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTextContent)
writer.WriteStartElement("TextContent")
Dim textProperties As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties = textContent.GetTextProperties()
AddTextProperties(writer, textProperties)
If Not String.IsNullOrEmpty(textContent.Text) Then
writer.WriteElementString("Content", textContent.Text)
End If
writer.WriteEndElement()
Else
writer.WriteStartElement("Element")
For Each child As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentElement In element
AddElement(writer, child)
Next
writer.WriteEndElement()
End If
End Sub
''' <summary>
''' Adds the graphics properties to the XML tree.
''' </summary>
''' <param name="writer">The XML writer.</param>
''' <param name="graphics">The Open XML document graphics.</param>
Private Shared Sub AddGraphicsProperties(writer As System.Xml.XmlWriter, graphics As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentGraphics)
writer.WriteAttributeString("Id", graphics.Id.ToString())
If Not String.IsNullOrEmpty(graphics.Name) Then
writer.WriteAttributeString("Name", graphics.Name)
End If
If Not String.IsNullOrEmpty(graphics.Description) Then
writer.WriteAttributeString("Description", graphics.Description)
End If
writer.WriteAttributeString("Rotation", graphics.Rotation.ToString())
' if graphics has hyperlink
If graphics.Hyperlink IsNot Nothing Then
writer.WriteStartElement("Hyperlink")
writer.WriteAttributeString("Name", graphics.Hyperlink.Name)
writer.WriteAttributeString("Url", graphics.Hyperlink.Url)
writer.WriteEndElement()
End If
End Sub
''' <summary>
''' Adds the paragraph properties to the XML tree.
''' </summary>
''' <param name="writer">The XML writer.</param>
''' <param name="properties">The paragraph properties.</param>
Private Shared Sub AddParagraphProperties(writer As System.Xml.XmlWriter, properties As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlParagraphProperties)
If properties Is Nothing OrElse properties.IsEmpty Then
Return
End If
writer.WriteStartElement("ParagraphProperties")
If properties.FillColor.HasValue Then
writer.WriteElementString("FillColor", properties.FillColor.ToString())
End If
If properties.FirstLineIndentation.HasValue Then
writer.WriteElementString("FirstLineIndentation", properties.FirstLineIndentation.ToString())
End If
If properties.Justification.HasValue Then
writer.WriteElementString("Justification", properties.Justification.ToString())
End If
If properties.KeepLines.HasValue Then
writer.WriteElementString("KeepLines", properties.KeepLines.ToString())
End If
If properties.KeepNext.HasValue Then
writer.WriteElementString("KeepNext", properties.KeepNext.ToString())
End If
If properties.LeftIndentation.HasValue Then
writer.WriteElementString("LeftIndentation", properties.LeftIndentation.ToString())
End If
If properties.LineHeightExact.HasValue Then
writer.WriteElementString("LineHeightExact", properties.LineHeightExact.ToString())
End If
If properties.LineHeightFactor.HasValue Then
writer.WriteElementString("LineHeightFactor", properties.LineHeightFactor.ToString())
End If
If properties.LineHeightMinimum.HasValue Then
writer.WriteElementString("LineHeightMinimum", properties.LineHeightMinimum.ToString())
End If
' if paragraph has numeration
If properties.Numeration IsNot Nothing Then
Dim docxNumeration As Vintasoft.Imaging.Office.OpenXml.Editor.Docx.DocxDocumentNumbering = DirectCast(properties.Numeration, Vintasoft.Imaging.Office.OpenXml.Editor.Docx.DocxDocumentNumbering)
writer.WriteStartElement("Numeration")
writer.WriteAttributeString("Id", docxNumeration.Id.ToString())
writer.WriteAttributeString("Name", docxNumeration.Name)
writer.WriteEndElement()
End If
If properties.NumerationLevel.HasValue Then
writer.WriteElementString("NumerationLevel", properties.NumerationLevel.ToString())
End If
If properties.PageBreakBefore.HasValue Then
writer.WriteElementString("PageBreakBefore", properties.PageBreakBefore.ToString())
End If
If properties.RightIndentation.HasValue Then
writer.WriteElementString("RightIndentation", properties.RightIndentation.ToString())
End If
If properties.SpacingAfterParagraph.HasValue Then
writer.WriteElementString("SpacingAfterParagraph", properties.SpacingAfterParagraph.ToString())
End If
If properties.SpacingBeforeParagraph.HasValue Then
writer.WriteElementString("SpacingBeforeParagraph", properties.SpacingBeforeParagraph.ToString())
End If
If properties.WidowControl.HasValue Then
writer.WriteElementString("WidowControl", properties.WidowControl.ToString())
End If
writer.WriteEndElement()
End Sub
''' <summary>
''' Adds the text properties to the XML tree.
''' </summary>
''' <param name="writer">The XML writer.</param>
''' <param name="properties">The text properties.</param>
Private Shared Sub AddTextProperties(writer As System.Xml.XmlWriter, properties As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties)
If properties Is Nothing OrElse properties.IsEmpty Then
Return
End If
writer.WriteStartElement("TextProperties")
If properties.CharacterHorizontalScaling.HasValue Then
writer.WriteElementString("CharacterHorizontalScaling", properties.CharacterHorizontalScaling.ToString())
End If
If properties.CharacterSpacing.HasValue Then
writer.WriteElementString("CharacterSpacing", properties.CharacterSpacing.ToString())
End If
If properties.Color.HasValue Then
writer.WriteElementString("Color", properties.Color.ToString())
End If
If Not String.IsNullOrEmpty(properties.FontName) Then
writer.WriteElementString("FontName", properties.FontName)
End If
If properties.FontSize.HasValue Then
writer.WriteElementString("FontSize", properties.FontSize.ToString())
End If
If properties.Highlight.HasValue Then
writer.WriteElementString("Highlight", properties.Highlight.ToString())
End If
If properties.IsBold.HasValue Then
writer.WriteElementString("IsBold", properties.IsBold.ToString())
End If
If properties.IsDoubleStrike.HasValue Then
writer.WriteElementString("IsDoubleStrike", properties.IsDoubleStrike.ToString())
End If
If properties.IsItalic.HasValue Then
writer.WriteElementString("IsItalic", properties.IsItalic.ToString())
End If
If properties.IsStrike.HasValue Then
writer.WriteElementString("IsStrike", properties.IsStrike.ToString())
End If
' if text has style
If properties.Style IsNot Nothing Then
Dim style As Vintasoft.Imaging.Office.OpenXml.Editor.Docx.DocxDocumentStyle = DirectCast(properties.Style, Vintasoft.Imaging.Office.OpenXml.Editor.Docx.DocxDocumentStyle)
writer.WriteStartElement("Style")
writer.WriteElementString("Name", style.Name)
writer.WriteElementString("StyleId", style.StyleId.ToString())
writer.WriteEndElement()
End If
If properties.Underline.HasValue Then
writer.WriteElementString("Underline", properties.Underline.ToString())
End If
If properties.UnderlineColor.HasValue Then
writer.WriteElementString("UnderlineColor", properties.UnderlineColor.ToString())
End If
If properties.VerticalAlignment.HasValue Then
writer.WriteElementString("VerticalAlignment", properties.VerticalAlignment.ToString())
End If
writer.WriteEndElement()
End Sub