В этом разделе
Представляет текстовое содержимое документа Open XML.
Объектная модель
Синтаксис
'Declaration
Public Class OpenXmlTextContent
public class OpenXmlTextContent
public __gc class OpenXmlTextContent
public ref class OpenXmlTextContent
Пример
Вот C#/VB.NET код, который демонстрирует, как изменить свойства текста текстового содержимого (в примере используется шаблон документа).ChangeTextProperties_template.docx):
Public Shared Sub DocxChangeTextPropertiesExample()
Dim templateFilename As String = "ChangeTextProperties_template.docx"
Dim outFilename As String = "ChangeTextProperties.docx"
Dim outPdfFilename As String = "ChangeTextProperties.pdf"
' create DocxDocumentEditor that allows to edit file "ChangeTextProperties_template.docx"
Using editor As New Vintasoft.Imaging.Office.OpenXml.Editor.Docx.DocxDocumentEditor(templateFilename)
' get document body
Dim documentBody As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentElement = editor.Body
' set text color
documentBody.FindText("COLOR").Substring(0, 2).SetTextProperties(CreateColorTextProperties(System.Drawing.Color.Red))
documentBody.FindText("COLOR").Substring(2, 1).SetTextProperties(CreateColorTextProperties(System.Drawing.Color.Green))
documentBody.FindText("COLOR").Substring(3, 2).SetTextProperties(CreateColorTextProperties(System.Drawing.Color.Blue))
' highlight text
Dim highlightedTextProperties As New Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties()
highlightedTextProperties.Highlight = Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextHighlightType.Green
documentBody.FindText("highlighted text").SetTextProperties(highlightedTextProperties)
' set text "bold text" as bold text
documentBody.FindText("bold text").SetTextProperties(Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties.BoldText)
' set text "italic text" as italic text
documentBody.FindText("italic text").SetTextProperties(Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties.ItalicText)
' set text "underline text" as underline text
documentBody.FindText("underline text").SetTextProperties(Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties.UnderlineText)
' set text "strike text" as striked out text
documentBody.FindText("strike text").SetTextProperties(Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties.StrikeText)
' change font size
Dim setTextSize As New Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties()
setTextSize.FontSize = 16
documentBody.FindText("text with size 16pt").Substring(0, 4).SetTextProperties(setTextSize)
' change text style
Dim setTextStyle As New Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties()
setTextStyle.Style = editor.Styles.FindByName("RedStyle")
documentBody.FindText("RedStyle").SetTextProperties(setTextStyle)
' save changed document to a DOCX file
editor.Save(outFilename)
' export changed document to a PDF document
editor.Export(outPdfFilename)
End Using
End Sub
''' <summary>
''' Creates the color text properties.
''' </summary>
''' <param name="color">The color.</param>
Private Shared Function CreateColorTextProperties(color As System.Drawing.Color) As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties
Dim result As New Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties()
result.Color = color
Return result
End Function
public static void DocxChangeTextPropertiesExample()
{
string templateFilename = "ChangeTextProperties_template.docx";
string outFilename = "ChangeTextProperties.docx";
string outPdfFilename = "ChangeTextProperties.pdf";
// create DocxDocumentEditor that allows to edit file "ChangeTextProperties_template.docx"
using (Vintasoft.Imaging.Office.OpenXml.Editor.Docx.DocxDocumentEditor editor =
new Vintasoft.Imaging.Office.OpenXml.Editor.Docx.DocxDocumentEditor(templateFilename))
{
// get document body
Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentElement documentBody = editor.Body;
// set text color
documentBody.FindText("COLOR").Substring(0, 2).SetTextProperties(CreateColorTextProperties(System.Drawing.Color.Red));
documentBody.FindText("COLOR").Substring(2, 1).SetTextProperties(CreateColorTextProperties(System.Drawing.Color.Green));
documentBody.FindText("COLOR").Substring(3, 2).SetTextProperties(CreateColorTextProperties(System.Drawing.Color.Blue));
// highlight text
Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties highlightedTextProperties =
new Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties();
highlightedTextProperties.Highlight = Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextHighlightType.Green;
documentBody.FindText("highlighted text").SetTextProperties(highlightedTextProperties);
// set text "bold text" as bold text
documentBody.FindText("bold text").SetTextProperties(Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties.BoldText);
// set text "italic text" as italic text
documentBody.FindText("italic text").SetTextProperties(Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties.ItalicText);
// set text "underline text" as underline text
documentBody.FindText("underline text").SetTextProperties(Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties.UnderlineText);
// set text "strike text" as striked out text
documentBody.FindText("strike text").SetTextProperties(Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties.StrikeText);
// change font size
Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties setTextSize =
new Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties();
setTextSize.FontSize = 16;
documentBody.FindText("text with size 16pt").Substring(0, 4).SetTextProperties(setTextSize);
// change text style
Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties setTextStyle =
new Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties();
setTextStyle.Style = editor.Styles.FindByName("RedStyle");
documentBody.FindText("RedStyle").SetTextProperties(setTextStyle);
// save changed document to a DOCX file
editor.Save(outFilename);
// export changed document to a PDF document
editor.Export(outPdfFilename);
}
}
/// <summary>
/// Creates the color text properties.
/// </summary>
/// <param name="color">The color.</param>
private static Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties CreateColorTextProperties(System.Drawing.Color color)
{
Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties result =
new Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties();
result.Color = color;
return result;
}
Иерархия наследования
System.Object
 Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextContent
Требования
Целевые платформы: .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5
Смотрите также