VintaSoft Imaging .NET SDK 15.1: Документация для .NET разработчика
Vintasoft.Imaging.Office.OpenXml.Editor.Docx Namespace / DocxDocumentEditor Class / Body Property
Синтаксис Example Требования Смотрите также
В этом разделе
Body Свойство (DocxDocumentEditor)
В этом разделе
Возвращает тело (корневой элемент) документа Open XML.
Синтаксис
Пример

Вот C#/VB.NET код, который демонстрирует, как получить дерево документа DOCX в виде дерева XML:


''' <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


/// <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 static string GetDocxDocumentTreeAsXml(string filePath)
{
    // open DOCX document
    using (Vintasoft.Imaging.Office.OpenXml.Editor.Docx.DocxDocumentEditor editor =
        new Vintasoft.Imaging.Office.OpenXml.Editor.Docx.DocxDocumentEditor(filePath))
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();

        // create XML writer settings
        System.Xml.XmlWriterSettings settings = new System.Xml.XmlWriterSettings();
        settings.Indent = true;
        settings.CheckCharacters = false;
        settings.OmitXmlDeclaration = true;

        // create XML writer
        using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(sb, settings))
        {
            // add DOCX focument body to the XML writer
            AddElement(writer, editor.Body);
        }

        // return a string that contains XML tree
        return sb.ToString();
    }
}

/// <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 static void AddElement(
    System.Xml.XmlWriter writer,
    Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentElement element)
{
    // if element is root
    if (element is Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentRootElement)
    {
        writer.WriteStartElement("Document");

        foreach (Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentElement child in element)
            AddElement(writer, child);

        writer.WriteEndElement();
    }
    // if element is paragraph
    else if (element is Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentParagraph)
    {
        Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentParagraph paragraph =
            (Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentParagraph)element;
        writer.WriteStartElement("Paragraph");

        // write paragraph properties
        Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlParagraphProperties paragraphProperties = paragraph.GetParagraphProperties();
        AddParagraphProperties(writer, paragraphProperties);

        // write paragraph text properties
        Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties textProperties = paragraph.GetParagraphTextProperties();
        AddTextProperties(writer, textProperties);

        // write paragraph content
        foreach (Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentElement child in paragraph)
            AddElement(writer, child);

        writer.WriteEndElement();
    }
    // if element is image
    else if (element is Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentImage)
    {
        Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentImage image =
            (Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentImage)element;
        writer.WriteStartElement("Image");

        // write image properties

        if (!string.IsNullOrEmpty(image.ContentType))
            writer.WriteAttributeString("ContentType", image.ContentType);
        AddGraphicsProperties(writer, image);

        writer.WriteEndElement();
    }
    // if element is chart
    else if (element is Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentChart)
    {
        Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentChart chart =
            (Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentChart)element;
        writer.WriteStartElement("Chart");

        // write chart properties

        writer.WriteAttributeString("Title", chart.Title);
        AddGraphicsProperties(writer, chart);

        writer.WriteEndElement();
    }
    // if element is graphics
    else if (element is Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentGraphics)
    {
        Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentGraphics graphics =
            (Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentGraphics)element;
        writer.WriteStartElement("Graphics");

        // write graphics properties

        AddGraphicsProperties(writer, graphics);

        writer.WriteEndElement();
    }
    // if element is table
    else if (element is Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTable)
    {
        Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTable table =
            (Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTable)element;
        writer.WriteStartElement("Table");

        foreach (Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentElement child in table)
            AddElement(writer, child);

        writer.WriteEndElement();
    }
    // if element is table row
    else if (element is Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTableRow)
    {
        Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTableRow row =
            (Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTableRow)element;
        writer.WriteStartElement("Row");

        foreach (Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentElement child in row)
            AddElement(writer, child);

        writer.WriteEndElement();
    }
    // if element is table cell
    else if (element is Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTableCell)
    {
        Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTableCell cell =
            (Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTableCell)element;
        writer.WriteStartElement("Cell");

        foreach (Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentElement child in cell)
            AddElement(writer, child);

        writer.WriteEndElement();
    }
    // if element is text content
    else if (element is Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTextContent)
    {
        Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTextContent textContent =
            (Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTextContent)element;
        writer.WriteStartElement("TextContent");

        Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties textProperties = textContent.GetTextProperties();
        AddTextProperties(writer, textProperties);

        if (!string.IsNullOrEmpty(textContent.Text))
            writer.WriteElementString("Content", textContent.Text);

        writer.WriteEndElement();
    }
    else
    {
        writer.WriteStartElement("Element");

        foreach (Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentElement child in element)
            AddElement(writer, child);

        writer.WriteEndElement();
    }
}

/// <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 static void AddGraphicsProperties(
    System.Xml.XmlWriter writer,
    Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentGraphics graphics)
{
    writer.WriteAttributeString("Id", graphics.Id.ToString());

    if (!string.IsNullOrEmpty(graphics.Name))
        writer.WriteAttributeString("Name", graphics.Name);

    if (!string.IsNullOrEmpty(graphics.Description))
        writer.WriteAttributeString("Description", graphics.Description);

    writer.WriteAttributeString("Rotation", graphics.Rotation.ToString());

    // if graphics has hyperlink
    if (graphics.Hyperlink != null)
    {
        writer.WriteStartElement("Hyperlink");

        writer.WriteAttributeString("Name", graphics.Hyperlink.Name);
        writer.WriteAttributeString("Url", graphics.Hyperlink.Url);

        writer.WriteEndElement();
    }
}

/// <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 static void AddParagraphProperties(
    System.Xml.XmlWriter writer,
    Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlParagraphProperties properties)
{
    if (properties == null || properties.IsEmpty)
        return;

    writer.WriteStartElement("ParagraphProperties");

    if (properties.FillColor.HasValue)
        writer.WriteElementString("FillColor", properties.FillColor.ToString());

    if (properties.FirstLineIndentation.HasValue)
        writer.WriteElementString("FirstLineIndentation", properties.FirstLineIndentation.ToString());

    if (properties.Justification.HasValue)
        writer.WriteElementString("Justification", properties.Justification.ToString());

    if (properties.KeepLines.HasValue)
        writer.WriteElementString("KeepLines", properties.KeepLines.ToString());

    if (properties.KeepNext.HasValue)
        writer.WriteElementString("KeepNext", properties.KeepNext.ToString());

    if (properties.LeftIndentation.HasValue)
        writer.WriteElementString("LeftIndentation", properties.LeftIndentation.ToString());

    if (properties.LineHeightExact.HasValue)
        writer.WriteElementString("LineHeightExact", properties.LineHeightExact.ToString());

    if (properties.LineHeightFactor.HasValue)
        writer.WriteElementString("LineHeightFactor", properties.LineHeightFactor.ToString());

    if (properties.LineHeightMinimum.HasValue)
        writer.WriteElementString("LineHeightMinimum", properties.LineHeightMinimum.ToString());

    // if paragraph has numeration
    if (properties.Numeration != null)
    {
        Vintasoft.Imaging.Office.OpenXml.Editor.Docx.DocxDocumentNumbering docxNumeration = (Vintasoft.Imaging.Office.OpenXml.Editor.Docx.DocxDocumentNumbering)properties.Numeration;

        writer.WriteStartElement("Numeration");
        writer.WriteAttributeString("Id", docxNumeration.Id.ToString());
        writer.WriteAttributeString("Name", docxNumeration.Name);
        writer.WriteEndElement();
    }
    if (properties.NumerationLevel.HasValue)
        writer.WriteElementString("NumerationLevel", properties.NumerationLevel.ToString());

    if (properties.PageBreakBefore.HasValue)
        writer.WriteElementString("PageBreakBefore", properties.PageBreakBefore.ToString());

    if (properties.RightIndentation.HasValue)
        writer.WriteElementString("RightIndentation", properties.RightIndentation.ToString());

    if (properties.SpacingAfterParagraph.HasValue)
        writer.WriteElementString("SpacingAfterParagraph", properties.SpacingAfterParagraph.ToString());

    if (properties.SpacingBeforeParagraph.HasValue)
        writer.WriteElementString("SpacingBeforeParagraph", properties.SpacingBeforeParagraph.ToString());

    if (properties.WidowControl.HasValue)
        writer.WriteElementString("WidowControl", properties.WidowControl.ToString());

    writer.WriteEndElement();
}

/// <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 static void AddTextProperties(
    System.Xml.XmlWriter writer,
    Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties properties)
{
    if (properties == null || properties.IsEmpty)
        return;

    writer.WriteStartElement("TextProperties");

    if (properties.CharacterHorizontalScaling.HasValue)
        writer.WriteElementString("CharacterHorizontalScaling", properties.CharacterHorizontalScaling.ToString());

    if (properties.CharacterSpacing.HasValue)
        writer.WriteElementString("CharacterSpacing", properties.CharacterSpacing.ToString());

    if (properties.Color.HasValue)
        writer.WriteElementString("Color", properties.Color.ToString());

    if (!string.IsNullOrEmpty(properties.FontName))
        writer.WriteElementString("FontName", properties.FontName);

    if (properties.FontSize.HasValue)
        writer.WriteElementString("FontSize", properties.FontSize.ToString());

    if (properties.Highlight.HasValue)
        writer.WriteElementString("Highlight", properties.Highlight.ToString());

    if (properties.IsBold.HasValue)
        writer.WriteElementString("IsBold", properties.IsBold.ToString());

    if (properties.IsDoubleStrike.HasValue)
        writer.WriteElementString("IsDoubleStrike", properties.IsDoubleStrike.ToString());

    if (properties.IsItalic.HasValue)
        writer.WriteElementString("IsItalic", properties.IsItalic.ToString());

    if (properties.IsStrike.HasValue)
        writer.WriteElementString("IsStrike", properties.IsStrike.ToString());

    // if text has style
    if (properties.Style != null)
    {
        Vintasoft.Imaging.Office.OpenXml.Editor.Docx.DocxDocumentStyle style = (Vintasoft.Imaging.Office.OpenXml.Editor.Docx.DocxDocumentStyle)properties.Style;

        writer.WriteStartElement("Style");

        writer.WriteElementString("Name", style.Name);
        writer.WriteElementString("StyleId", style.StyleId.ToString());

        writer.WriteEndElement();
    }

    if (properties.Underline.HasValue)
        writer.WriteElementString("Underline", properties.Underline.ToString());

    if (properties.UnderlineColor.HasValue)
        writer.WriteElementString("UnderlineColor", properties.UnderlineColor.ToString());

    if (properties.VerticalAlignment.HasValue)
        writer.WriteElementString("VerticalAlignment", properties.VerticalAlignment.ToString());

    writer.WriteEndElement();
}

Требования

Целевые платформы: .NET 10; .NET 9; .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5

Смотрите также