Text in DOCX mit C# formatieren
Die Klasse TextStyle von IronWord ermöglicht .NET-Entwicklern, Word-Dokumente programmgesteuert professionell zu formatieren, unter anderem mit Schriftarten, Farben, Fettdruck, Kursivschrift und Unterstreichungen. Ob Sie Berichte generieren, Vorlagen erstellen oder die Dokumentenerstellung automatisieren: IronWord bietet umfassende Formatierungswerkzeuge, die die Formatierungsoptionen von Microsoft Word nachbilden.
-
1Installieren Sie IronWord mit NuGet Package Manager
-
2Kopieren Sie diesen Codeausschnitt und führen Sie ihn aus.
// Quick example using IronWord; using IronWord.Models; // Initialize a new Word document WordDocument doc = new WordDocument(); // Create a Run with styled text Run textRun = new Run(new TextContent("Styled text")); // Apply styling properties to the Run textRun.Style = new TextStyle() { IsBold = true, Color = Color.Red, FontSize = 16, TextFont = new Font() { FontFamily = "Arial" } }; // Create paragraph and add the styled Run Paragraph paragraph = new Paragraph(); paragraph.AddChild(textRun); // Add paragraph to document and save doc.AddParagraph(paragraph); doc.SaveAs("styled.docx");Imports IronWord Imports IronWord.Models ' Initialize a new Word document Dim doc As New WordDocument() ' Create a Run with styled text Dim textRun As New Run(New TextContent("Styled text")) ' Apply styling properties to the Run textRun.Style = New TextStyle() With { .IsBold = True, .Color = Color.Red, .FontSize = 16, .TextFont = New Font() With { .FontFamily = "Arial" } } ' Create paragraph and add the styled Run Dim paragraph As New Paragraph() paragraph.AddChild(textRun) ' Add paragraph to document and save doc.AddParagraph(paragraph) doc.SaveAs("styled.docx")Imports IronWord Imports IronWord.Models ' Initialize a new Word document Dim doc As New WordDocument() ' Create a Run with styled text Dim textRun As New Run(New TextContent("Styled text")) ' Apply styling properties to the Run textRun.Style = New TextStyle() With { .IsBold = True, .Color = Color.Red, .FontSize = 16, .TextFont = New Font() With { .FontFamily = "Arial" } } ' Create paragraph and add the styled Run Dim paragraph As New Paragraph() paragraph.AddChild(textRun) ' Add paragraph to document and save doc.AddParagraph(paragraph) doc.SaveAs("styled.docx") -
3Bereitstellen zum Testen in Ihrer Live-Umgebung
Beginnen Sie noch heute, IronWord in Ihrem Projekt mit einer kostenlosen Testversion zu verwenden
Minimaler Arbeitsablauf (5 Schritte)
- Installieren Sie die IronWord C#-Bibliothek
- Erstellen Sie ein
Run-Objekt, dasTextContententhält. - Weisen Sie dem
RuneinenTextStylemit Eigenschaften wieFontSize,IsBoldundColorzu. - Fügen Sie mit
AddChildeinen formatiertenRunzu einemParagraphhinzu. - Speichern Sie das Dokument mit angewendeten Stilen
Wie füge ich einen Textstil zu DOCX hinzu?
Um Textstile in IronWord anzuwenden, verwenden Sie das Run-Wrapper-Muster. Erstellen Sie ein WordDocument-Objekt und dann ein Run-Objekt, das TextContent mit Ihrem Text enthält. Wenden Sie einen TextStyle auf den Run (nicht auf den TextContent) an, indem Sie Eigenschaften wie IsBold, Color oder FontSize verwenden.
Fügen Sie nach der Formatierung den Run mit AddChild einem Paragraph hinzu, fügen Sie den Absatz in das Dokument ein und speichern Sie das Ergebnis. Dieser Ansatz bietet programmgesteuerte Kontrolle über die Textformatierung bei der automatisierten Dokumentenerstellung mit einheitlicher Formatierung.
Document → (optional DocumentSection) → Paragraph → Run → TextContent. Absätze können direkt zu WordDocument über AddParagraph ohne einen expliziten DocumentSection hinzugefügt werden, wie in den Beispielen auf dieser Seite gezeigt. Stile werden auf der Ebene Run angewendet, nicht direkt auf TextContent.using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;
// Load docx
WordDocument doc = new WordDocument("sample.docx");
// Configure text
Run textRun = new Run(new TextContent("Add text using IronWord"));
// Configure text style settings
textRun.Style = new TextStyle()
{
FontSize = 24, // Text Size is 24
TextFont = new Font()
{
FontFamily = "Calibri" // Text Font is "Calibri"
},
Color = Color.Red, // Set text color to red
IsBold = true, // Make text bold
IsItalic = true, // Make text italic
Underline = new Underline(), // Have an underline
Strike = StrikeValue.DoubleStrike, // Double strike-through
};
Paragraph paragraph = new Paragraph();
// Add text to paragraph
paragraph.AddChild(textRun);
// Add paragraph to document
doc.AddParagraph(paragraph);
// Save document
doc.SaveAs("add-text-style.docx");using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;
// Load docx
WordDocument doc = new WordDocument("sample.docx");
// Configure text
Run textRun = new Run(new TextContent("Add text using IronWord"));
// Configure text style settings
textRun.Style = new TextStyle()
{
FontSize = 24, // Text Size is 24
TextFont = new Font()
{
FontFamily = "Calibri" // Text Font is "Calibri"
},
Color = Color.Red, // Set text color to red
IsBold = true, // Make text bold
IsItalic = true, // Make text italic
Underline = new Underline(), // Have an underline
Strike = StrikeValue.DoubleStrike, // Double strike-through
};
Paragraph paragraph = new Paragraph();
// Add text to paragraph
paragraph.AddChild(textRun);
// Add paragraph to document
doc.AddParagraph(paragraph);
// Save document
doc.SaveAs("add-text-style.docx");Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums
' Load docx
Dim doc As New WordDocument("sample.docx")
' Configure text
Dim textRun As New Run(New TextContent("Add text using IronWord"))
' Configure text style settings
textRun.Style = New TextStyle() With {
.FontSize = 24, ' Text Size is 24
.TextFont = New Font() With {
.FontFamily = "Calibri" ' Text Font is "Calibri"
},
.Color = Color.Red, ' Set text color to red
.IsBold = True, ' Make text bold
.IsItalic = True, ' Make text italic
.Underline = New Underline(), ' Have an underline
.Strike = StrikeValue.DoubleStrike ' Double strike-through
}
Dim paragraph As New Paragraph()
' Add text to paragraph
paragraph.AddChild(textRun)
' Add paragraph to document
doc.AddParagraph(paragraph)
' Save document
doc.SaveAs("add-text-style.docx")Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums
' Load docx
Dim doc As New WordDocument("sample.docx")
' Configure text
Dim textRun As New Run(New TextContent("Add text using IronWord"))
' Configure text style settings
textRun.Style = New TextStyle() With {
.FontSize = 24, ' Text Size is 24
.TextFont = New Font() With {
.FontFamily = "Calibri" ' Text Font is "Calibri"
},
.Color = Color.Red, ' Set text color to red
.IsBold = True, ' Make text bold
.IsItalic = True, ' Make text italic
.Underline = New Underline(), ' Have an underline
.Strike = StrikeValue.DoubleStrike ' Double strike-through
}
Dim paragraph As New Paragraph()
' Add text to paragraph
paragraph.AddChild(textRun)
' Add paragraph to document
doc.AddParagraph(paragraph)
' Save document
doc.SaveAs("add-text-style.docx")Welche Ausgabe wird damit erzeugt?

Die Klasse TextStyle bietet grundlegende Formatierungsoptionen, einschließlich Schriftarten-Eigenschaften, Textfarbe, Fett, Kursiv und Unterstreichen. Beachten Sie, dass FontSize auf der Ebene TextStyle konfiguriert wird (nicht innerhalb von Font), während FontFamily innerhalb der Eigenschaft TextFont festgelegt ist. Das Objekt Run umschließt TextContent und enthält den Stil, entsprechend dem Dokumentenhierarchie-Muster von IronWord. Das Beispiel zeigt, wie mehrere Stil-Eigenschaften kombiniert werden, um reichhaltig formatierten Text zu erstellen, wenn sie auf ein Run angewendet werden.
Welche spezifischen Stile kann ich hinzufügen?
Wie kann ich die Textfarbe ändern?
Die Eigenschaft Color in TextStyle legt die Textfarbe mithilfe vordefinierter Farben aus IronWord.Models.Color oder benutzerdefinierter Hex-Werte fest. Damit können Sie bestimmte Inhalte hervorheben oder die Textfarbe an Markenfarben anpassen. IronWord unterstützt eine breite Palette von Farben, darunter Rot, Blau, Grün, Olivgrün, Marineblau und Kastanienbraun.
using IronWord;
using IronWord.Models;
// Create document
WordDocument doc = new WordDocument();
// Add colored text
Run textRun = new Run(new TextContent("This text is olive-colored!"));
textRun.Style = new TextStyle()
{
Color = IronWord.Models.Color.Olive // defining text to be colored olive
};
Paragraph paragraph = new Paragraph();
paragraph.AddChild(textRun);
doc.AddParagraph(paragraph);
// Save document
doc.SaveAs("colored-text.docx");using IronWord;
using IronWord.Models;
// Create document
WordDocument doc = new WordDocument();
// Add colored text
Run textRun = new Run(new TextContent("This text is olive-colored!"));
textRun.Style = new TextStyle()
{
Color = IronWord.Models.Color.Olive // defining text to be colored olive
};
Paragraph paragraph = new Paragraph();
paragraph.AddChild(textRun);
doc.AddParagraph(paragraph);
// Save document
doc.SaveAs("colored-text.docx");Imports IronWord
Imports IronWord.Models
' Create document
Dim doc As New WordDocument()
' Add colored text
Dim textRun As New Run(New TextContent("This text is olive-colored!"))
textRun.Style = New TextStyle() With {
.Color = IronWord.Models.Color.Olive ' defining text to be colored olive
}
Dim paragraph As New Paragraph()
paragraph.AddChild(textRun)
doc.AddParagraph(paragraph)
' Save document
doc.SaveAs("colored-text.docx")Imports IronWord
Imports IronWord.Models
' Create document
Dim doc As New WordDocument()
' Add colored text
Dim textRun As New Run(New TextContent("This text is olive-colored!"))
textRun.Style = New TextStyle() With {
.Color = IronWord.Models.Color.Olive ' defining text to be colored olive
}
Dim paragraph As New Paragraph()
paragraph.AddChild(textRun)
doc.AddParagraph(paragraph)
' Save document
doc.SaveAs("colored-text.docx")Wie sieht farbiger Text aus?

Wie stelle ich Schriftfamilie und -größe ein?
Passen Sie das Erscheinungsbild des Textes mit der Eigenschaft TextFont an. Setzen Sie FontFamily auf den Namen einer installierten Schriftart (z.B. "Arial", "Times New Roman") und FontSize in Punktgröße. Dadurch wird eine visuelle Hierarchie geschaffen und die Lesbarkeit auf verschiedenen Geräten und Plattformen gewährleistet.
using IronWord;
using IronWord.Models;
// Create document
WordDocument doc = new WordDocument();
// Add text with custom font family and size
Run textRun = new Run(new TextContent("This text uses Arial at 24pt!"));
textRun.Style = new TextStyle()
{
FontSize = 24, // Set font size in points
TextFont = new IronWord.Models.Font()
{
FontFamily = "Arial" // Set font family
}
};
Paragraph paragraph = new Paragraph();
paragraph.AddChild(textRun);
doc.AddParagraph(paragraph);
// Save document
doc.SaveAs("font-styled-text.docx");using IronWord;
using IronWord.Models;
// Create document
WordDocument doc = new WordDocument();
// Add text with custom font family and size
Run textRun = new Run(new TextContent("This text uses Arial at 24pt!"));
textRun.Style = new TextStyle()
{
FontSize = 24, // Set font size in points
TextFont = new IronWord.Models.Font()
{
FontFamily = "Arial" // Set font family
}
};
Paragraph paragraph = new Paragraph();
paragraph.AddChild(textRun);
doc.AddParagraph(paragraph);
// Save document
doc.SaveAs("font-styled-text.docx");Imports IronWord
Imports IronWord.Models
' Create document
Dim doc As New WordDocument()
' Add text with custom font family and size
Dim textRun As New Run(New TextContent("This text uses Arial at 24pt!"))
textRun.Style = New TextStyle() With {
.FontSize = 24, ' Set font size in points
.TextFont = New IronWord.Models.Font() With {
.FontFamily = "Arial" ' Set font family
}
}
Dim paragraph As New Paragraph()
paragraph.AddChild(textRun)
doc.AddParagraph(paragraph)
' Save document
doc.SaveAs("font-styled-text.docx")Imports IronWord
Imports IronWord.Models
' Create document
Dim doc As New WordDocument()
' Add text with custom font family and size
Dim textRun As New Run(New TextContent("This text uses Arial at 24pt!"))
textRun.Style = New TextStyle() With {
.FontSize = 24, ' Set font size in points
.TextFont = New IronWord.Models.Font() With {
.FontFamily = "Arial" ' Set font family
}
}
Dim paragraph As New Paragraph()
paragraph.AddChild(textRun)
doc.AddParagraph(paragraph)
' Save document
doc.SaveAs("font-styled-text.docx")Wie sieht eine benutzerdefinierte Schriftart aus?

Wie kann ich Text fett machen?
Setzen Sie die Eigenschaft IsBold auf true, um den Text Bold zu machen. Bold Text wird häufig für Überschriften, Hervorhebungen oder um wichtige Informationen hervorzuheben, verwendet. In Kombination mit anderen Stil-Eigenschaften schafft Bold Text eine visuelle Hierarchie und verbessert die Lesbarkeit.
using IronWord;
using IronWord.Models;
// Create document
WordDocument doc = new WordDocument();
// Add bold text
Run textRun = new Run(new TextContent("this is bold!"));
textRun.Style = new TextStyle()
{
IsBold = true // Make text bold
};
Paragraph paragraph = new Paragraph();
paragraph.AddChild(textRun);
doc.AddParagraph(paragraph);
// Save document
doc.SaveAs("bold-text.docx");using IronWord;
using IronWord.Models;
// Create document
WordDocument doc = new WordDocument();
// Add bold text
Run textRun = new Run(new TextContent("this is bold!"));
textRun.Style = new TextStyle()
{
IsBold = true // Make text bold
};
Paragraph paragraph = new Paragraph();
paragraph.AddChild(textRun);
doc.AddParagraph(paragraph);
// Save document
doc.SaveAs("bold-text.docx");Imports IronWord
Imports IronWord.Models
' Create document
Dim doc As New WordDocument()
' Add bold text
Dim textRun As New Run(New TextContent("this is bold!"))
textRun.Style = New TextStyle() With {
.IsBold = True ' Make text bold
}
Dim paragraph As New Paragraph()
paragraph.AddChild(textRun)
doc.AddParagraph(paragraph)
' Save document
doc.SaveAs("bold-text.docx")Imports IronWord
Imports IronWord.Models
' Create document
Dim doc As New WordDocument()
' Add bold text
Dim textRun As New Run(New TextContent("this is bold!"))
textRun.Style = New TextStyle() With {
.IsBold = True ' Make text bold
}
Dim paragraph As New Paragraph()
paragraph.AddChild(textRun)
doc.AddParagraph(paragraph)
' Save document
doc.SaveAs("bold-text.docx")Wie sieht fetter Text aus?

Wie kann ich Text kursiv machen?
Setzen Sie die Eigenschaft IsItalic auf true für Italic Styling. Italic Text wird typischerweise für Hervorhebungen, Titel, fremde Wörter oder technische Begriffe verwendet. Diese subtile Formatierung unterscheidet Textelemente ohne das visuelle Gewicht von Bold Formatierungen.
using IronWord;
using IronWord.Models;
// Create document
WordDocument doc = new WordDocument();
// Add italic text
Run textRun = new Run(new TextContent("this is italic."));
textRun.Style = new TextStyle()
{
IsItalic = true // Make text italic
};
Paragraph paragraph = new Paragraph();
paragraph.AddChild(textRun);
doc.AddParagraph(paragraph);
// Save document
doc.SaveAs("italic-text.docx");using IronWord;
using IronWord.Models;
// Create document
WordDocument doc = new WordDocument();
// Add italic text
Run textRun = new Run(new TextContent("this is italic."));
textRun.Style = new TextStyle()
{
IsItalic = true // Make text italic
};
Paragraph paragraph = new Paragraph();
paragraph.AddChild(textRun);
doc.AddParagraph(paragraph);
// Save document
doc.SaveAs("italic-text.docx");Imports IronWord
Imports IronWord.Models
' Create document
Dim doc As New WordDocument()
' Add italic text
Dim textRun As New Run(New TextContent("this is italic."))
textRun.Style = New TextStyle() With {
.IsItalic = True ' Make text italic
}
Dim paragraph As New Paragraph()
paragraph.AddChild(textRun)
doc.AddParagraph(paragraph)
' Save document
doc.SaveAs("italic-text.docx")Imports IronWord
Imports IronWord.Models
' Create document
Dim doc As New WordDocument()
' Add italic text
Dim textRun As New Run(New TextContent("this is italic."))
textRun.Style = New TextStyle() With {
.IsItalic = True ' Make text italic
}
Dim paragraph As New Paragraph()
paragraph.AddChild(textRun)
doc.AddParagraph(paragraph)
' Save document
doc.SaveAs("italic-text.docx")Wie sieht kursiver Text aus?

Was sind die verfügbaren Styling-Eigenschaften?
IronWord bietet umfassende Formatierungseigenschaften, die die Formatierungsoptionen von Microsoft Word widerspiegeln. Diese Eigenschaften lassen sich kombinieren, um komplexe Textformatierungen zu erstellen, die professionellen Dokumentenstandards entsprechen.
| Styling-Methode | Beschreibung | Beispiel |
|---|---|---|
TextFont | Passt die Textdarstellung mithilfe eines Font-Objekts an und legt die Schriftfamilie fest. Hinweis: FontSize wird auf der Ebene von TextStyle und nicht innerhalb von Font festgelegt. | textRun.Style = new TextStyle() { FontSize = 24, TextFont = new Font() { FontFamily = "Calibri" } }; |
Color | Legt die Textfarbe mit vordefinierten Farben aus IronWord.Models.Color oder benutzerdefinierten Hex-Werten fest. | textRun.Style.Color = IronWord.Models.Color.Red; |
IsBold | Macht Text Bold, wenn auf true gesetzt, und wird häufig für Überschriften oder Hervorhebungen verwendet. | textRun.Style.IsBold = true; |
IsItalic | Wendet Italic-Stil auf Text an, wenn auf true gesetzt, typischerweise für Hervorhebungen oder Titel. | textRun.Style.IsItalic = true; |
Underline | Fügt dem Text eine Underline mit einem Underline-Objekt mit verschiedenen Unterstreichungsstilen hinzu. | textRun.Style.Underline = new Underline(); |
Strike | Wendet einen Durchstreichungseffekt auf Text mit dem StrikeValue-Enum (Strike oder DoubleStrike) an. | textRun.Style.Strike = StrikeValue.Strike; |
Caps | Wendet Großschreibeffekte auf den Text an und konvertiert alle Zeichen in Großbuchstaben. | textRun.Style.Caps = true; |
CharacterScale | Passt die proportionale Breite von Zeichen als Prozentsatz ihrer normalen Größe an. | textRun.Style.CharacterScale = 150; |
Emboss | Wendet einen Prägeeffekt auf Text an, der ein erhabenes Aussehen erzeugt. | textRun.Style.Emboss = true; |
Emphasis | Fügt formatiertem Text Betonungszeichen mithilfe von Werten der EmphasisMarkValues-Enumeration hinzu. | textRun.Style.Emphasis = EmphasisMarkValues.Dot; |
LineSpacing | Steuert den Abstand zwischen den Textzeilen für eine verbesserte Lesbarkeit mittels eines LineSpacing-Objekts, wobei Value ein Zeilenhöhen-Multiplikator ist. | textRun.Style.LineSpacing = new LineSpacing() { Value = 1.5 }; |
Outline | Gibt den Text mit einem Umriss-Effekt wieder, wobei nur die Zeichenränder angezeigt werden. | textRun.Style.Outline = true; |
Shading | Wendet Hintergrundfarbe oder Schattierung auf Text mit einem Shading-Objekt an. | textRun.Style.Shading = new Shading() { Color = Color.Yellow }; |
SmallCaps | Wandelt Kleinbuchstaben in Kapitälchen um, wobei die Unterscheidung zwischen Groß- und Kleinbuchstaben erhalten bleibt. | textRun.Style.SmallCaps = true; |
VerticalPosition | Passt die vertikale Platzierung von Text relativ zu seiner Grundlinie an, gemessen in Punkten. | textRun.Style.VerticalPosition = 5.0; |
VerticalTextAlignment | Positioniert Text vertikal innerhalb seines Containers mithilfe der VerticalPositionValues-Enumeration. | textRun.Style.VerticalTextAlignment = VerticalPositionValues.Superscript; |
Kombinieren mehrerer Stile
Die Stärke von IronWord bei der Textformatierung liegt in der Kombination mehrerer Eigenschaften für komplexe Formatierungen. Das folgende Beispiel zeigt, wie sich verschiedene Formatierungseigenschaften zu professionell formatiertem Text kombinieren lassen:
using IronWord;
using IronWord.Models;
// Create a new document
WordDocument doc = new WordDocument();
// Create richly formatted header text using Run
Run headerRun = new Run(new TextContent("Professional Document Header"));
headerRun.Style = new TextStyle()
{
FontSize = 28,
TextFont = new Font()
{
FontFamily = "Georgia"
},
Color = Color.DarkBlue,
IsBold = true,
SmallCaps = true,
Underline = new Underline(),
CharacterScale = 110, // Slightly expand character width
Shading = new Shading()
{
Color = Color.LightGray // Light background
}
};
// Add header to document using AddChild for styled Run
Paragraph headerParagraph = new Paragraph();
headerParagraph.AddChild(headerRun);
doc.AddParagraph(headerParagraph);
// Create body text with different styling
Run bodyRun = new Run(new TextContent("This is professionally formatted body text with custom styling."));
bodyRun.Style = new TextStyle()
{
FontSize = 11,
TextFont = new Font()
{
FontFamily = "Calibri"
},
Color = Color.Black
};
Paragraph bodyParagraph = new Paragraph();
bodyParagraph.AddChild(bodyRun);
doc.AddParagraph(bodyParagraph);
// Save the document
doc.SaveAs("professional-document.docx");using IronWord;
using IronWord.Models;
// Create a new document
WordDocument doc = new WordDocument();
// Create richly formatted header text using Run
Run headerRun = new Run(new TextContent("Professional Document Header"));
headerRun.Style = new TextStyle()
{
FontSize = 28,
TextFont = new Font()
{
FontFamily = "Georgia"
},
Color = Color.DarkBlue,
IsBold = true,
SmallCaps = true,
Underline = new Underline(),
CharacterScale = 110, // Slightly expand character width
Shading = new Shading()
{
Color = Color.LightGray // Light background
}
};
// Add header to document using AddChild for styled Run
Paragraph headerParagraph = new Paragraph();
headerParagraph.AddChild(headerRun);
doc.AddParagraph(headerParagraph);
// Create body text with different styling
Run bodyRun = new Run(new TextContent("This is professionally formatted body text with custom styling."));
bodyRun.Style = new TextStyle()
{
FontSize = 11,
TextFont = new Font()
{
FontFamily = "Calibri"
},
Color = Color.Black
};
Paragraph bodyParagraph = new Paragraph();
bodyParagraph.AddChild(bodyRun);
doc.AddParagraph(bodyParagraph);
// Save the document
doc.SaveAs("professional-document.docx");Imports IronWord
Imports IronWord.Models
' Create a new document
Dim doc As New WordDocument()
' Create richly formatted header text using Run
Dim headerRun As New Run(New TextContent("Professional Document Header"))
headerRun.Style = New TextStyle() With {
.FontSize = 28,
.TextFont = New Font() With {
.FontFamily = "Georgia"
},
.Color = Color.DarkBlue,
.IsBold = True,
.SmallCaps = True,
.Underline = New Underline(),
.CharacterScale = 110, ' Slightly expand character width
.Shading = New Shading() With {
.Color = Color.LightGray ' Light background
}
}
' Add header to document using AddChild for styled Run
Dim headerParagraph As New Paragraph()
headerParagraph.AddChild(headerRun)
doc.AddParagraph(headerParagraph)
' Create body text with different styling
Dim bodyRun As New Run(New TextContent("This is professionally formatted body text with custom styling."))
bodyRun.Style = New TextStyle() With {
.FontSize = 11,
.TextFont = New Font() With {
.FontFamily = "Calibri"
},
.Color = Color.Black
}
Dim bodyParagraph As New Paragraph()
bodyParagraph.AddChild(bodyRun)
doc.AddParagraph(bodyParagraph)
' Save the document
doc.SaveAs("professional-document.docx")Imports IronWord
Imports IronWord.Models
' Create a new document
Dim doc As New WordDocument()
' Create richly formatted header text using Run
Dim headerRun As New Run(New TextContent("Professional Document Header"))
headerRun.Style = New TextStyle() With {
.FontSize = 28,
.TextFont = New Font() With {
.FontFamily = "Georgia"
},
.Color = Color.DarkBlue,
.IsBold = True,
.SmallCaps = True,
.Underline = New Underline(),
.CharacterScale = 110, ' Slightly expand character width
.Shading = New Shading() With {
.Color = Color.LightGray ' Light background
}
}
' Add header to document using AddChild for styled Run
Dim headerParagraph As New Paragraph()
headerParagraph.AddChild(headerRun)
doc.AddParagraph(headerParagraph)
' Create body text with different styling
Dim bodyRun As New Run(New TextContent("This is professionally formatted body text with custom styling."))
bodyRun.Style = New TextStyle() With {
.FontSize = 11,
.TextFont = New Font() With {
.FontFamily = "Calibri"
},
.Color = Color.Black
}
Dim bodyParagraph As New Paragraph()
bodyParagraph.AddChild(bodyRun)
doc.AddParagraph(bodyParagraph)
' Save the document
doc.SaveAs("professional-document.docx")Mit diesem umfassenden Styling-Ansatz werden Dokumente erstellt, die ein konsistentes Branding und ein professionelles Erscheinungsbild während des gesamten Prozesses der Dokumentenerstellung Ihrer Anwendung aufweisen.
Häufig gestellte Fragen
Wie wende ich in C# programmgesteuert Textformatierungen auf Word-Dokumente an?
Mit der TextStyle-Klasse von IronWord können Sie professionelle Textformatierungen wie Schriftarten, Farben, fett, kursiv und unterstrichen anwenden. Erstellen Sie einfach ein TextContent-Objekt mit Ihrem Text, wenden Sie einen TextStyle mit den gewünschten Eigenschaften an, fügen Sie ihn zu einem Absatz hinzu und speichern Sie das Dokument.
Was sind die grundlegenden Schritte zur Formatierung von Text in DOCX-Dateien?
So gestalten Sie Text mit IronWord: 1) Installieren Sie IronWord über NuGet, 2) Erstellen Sie ein WordDocument-Objekt, 3) Erstellen Sie TextContent mit Ihrem Text, 4) Wenden Sie TextStyle-Eigenschaften wie Schriftart, Farbe oder Fettdruck an, 5) Fügen Sie den Text zu einem Absatz hinzu und speichern Sie.
Welche Textformatierungsoptionen sind verfügbar?
Die TextStyle-Klasse von IronWord bietet wichtige Formatierungsoptionen, darunter Schrifteigenschaften (FontFamily und FontSize), Textfarbe, fett, kursiv, unterstrichen und durchgestrichen. Diese Optionen können kombiniert werden, um reichhaltig formatierten Text zu erstellen.
Wie ändere ich die Schriftfamilie und die Größe des Textes?
Verwenden Sie die Eigenschaft TextFont in TextStyle, um Schriftfamilie und -größe festzulegen. Setzen Sie die FontFamily auf Schriftarten wie "Arial" oder "Times New Roman" und FontSize auf die gewünschte Punktgröße, z. B. 16 für größeren Text.
Kann ich mehrere Textstile gleichzeitig anwenden?
Ja, IronWord ermöglicht es Ihnen, mehrere Stileigenschaften in einem einzigen TextStyle-Objekt zu kombinieren. Sie können Fett-, Kursiv-, Farb- und Schriftänderungen auf einmal anwenden, um komplexe Textformatierungen zu erstellen.
Wie kann ich die Textfarbe in Word-Dokumenten mit C# ändern?
Mit der Eigenschaft Color in IronWord's TextStyle können Sie die Textfarbe mithilfe von vordefinierten Farben aus IronWord.Models.Color oder benutzerdefinierten Hex-Werten festlegen. Mit dieser Funktion können Sie bestimmte Inhalte hervorheben oder Markenfarben in Ihren Dokumenten anpassen.
What property is used to italicize text in IronWord?
The IsItalic property is used within the TextStyle class to apply italic formatting to text in IronWord documents.
How do I apply an underline to text using IronWord?
To underline text in an IronWord document, create a new Underline object and assign it to the Underline property in the TextStyle class for the Run object containing your text.
Can I apply complex text styling using multiple properties in IronWord?
Yes, IronWord enables complex text styling by combining multiple TextStyle properties, such as FontSize, Color, IsBold, and Underline, to create richly formatted text in Word documents.
What is the benefit of using IronWord for text styling in DOCX documents?
IronWord offers programmatic control over text styling in DOCX documents, allowing developers to automate document creation while ensuring consistent and professional formatting similar to Microsoft Word.
Ahmad ist ein Full-Stack-Entwickler mit einer soliden Grundlage in C#, Python und Webtechnologien. Er hat ein großes Interesse am Aufbau skalierbarer Softwarelösungen und genießt es, zu erkunden, wie Design und Funktionalität in realen Anwendungen aufeinandertreffen.