Comment mettre en forme du texte dans un document DOCX avec C#
La classe TextStyle d’IronWord permet aux développeurs .NET de mettre en forme le texte de documents Word par programmation, notamment en choisissant les polices et les couleurs et en appliquant le gras, l’italique ou le soulignement. Que vous génériez des rapports, créiez des modèles ou automatisiez la création de documents, IronWord offre des outils qui reproduisent les options de mise en forme de Microsoft Word.
-
1Installez IronWord avec NuGet Package Manager
-
2Copiez et exécutez cet extrait de code.
// 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") -
3Déployez pour tester sur votre environnement de production.
Commencez à utiliser IronWord dans votre projet dès aujourd'hui avec un essai gratuit
Flux de travail minimal (5 étapes)
- Installez la bibliothèque C# IronWord
- Créez un objet
RuncontenantTextContent - Appliquez un
TextStyleauRunen définissant des propriétés telles queFontSize,IsBoldetColor - Ajoutez le
Runmis en forme à unParagraphà l’aide deAddChild - Enregistrez le document avec les styles appliqués
Comment mettre en forme du texte dans un document DOCX ?
Pour appliquer des styles de texte dans IronWord, utilisez un objet Run comme conteneur. Créez un objet WordDocument, puis un objet Run qui contient votre texte dans un TextContent. Appliquez un TextStyle au Run (pas au TextContent) en définissant des propriétés telles que IsBold, Color ou FontSize.
Après avoir appliqué le style, ajoutez le Run à un Paragraph à l’aide de AddChild, insérez le paragraphe dans le document et enregistrez le résultat. Vous pouvez ainsi maîtriser la mise en forme du texte lorsque vous générez automatiquement des documents qui doivent respecter un style uniforme.
Document → (optionnel DocumentSection) → Paragraph → Run → TextContent. Les paragraphes peuvent être ajoutés directement à WordDocument via AddParagraph sans un DocumentSection explicite, comme montré dans les exemples sur cette page. Les styles sont appliqués au niveau Run, et non directement sur 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")Quel est le résultat de cette opération ?

La classe TextStyle propose les principales options de mise en forme, notamment les propriétés de police, la couleur du texte, le gras, l’italique et le soulignement. Définissez FontSize dans TextStyle (en dehors de Font) et FontFamily dans la propriété TextFont. L’objet Run contient le TextContent et porte le style, conformément à la hiérarchie des documents d’IronWord. L’exemple montre comment combiner plusieurs propriétés de mise en forme dans un même Run.
Quels styles spécifiques puis-je ajouter ?
Comment changer la couleur du texte?
La propriété Color de TextStyle définit la couleur du texte à partir des couleurs prédéfinies de IronWord.Models.Color ou de valeurs hexadécimales personnalisées. Vous pouvez ainsi mettre en valeur un passage ou reprendre les couleurs de votre marque. IronWord prend en charge de nombreuses couleurs, dont le rouge, le bleu, le vert, le vert olive, le bleu marine et le bordeaux.
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")À quoi ressemble un texte coloré?

Comment définir la famille et la taille des polices de caractères ?
Personnalisez l’apparence du texte avec la propriété TextFont. Indiquez dans FontFamily le nom d’une police installée (par exemple « Arial » ou « Times New Roman ») et définissez FontSize en points. Vous pouvez ainsi établir une hiérarchie visuelle et assurer la lisibilité sur différents appareils et plateformes.
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")À quoi ressemble un style de police personnalisé?

Comment mettre un texte en gras?
Définissez la propriété IsBold sur true pour appliquer le gras (Bold). Le style Bold sert notamment à distinguer les titres ou à mettre en valeur des informations importantes. Combiné à d’autres propriétés de style, Bold aide à structurer le texte et à améliorer sa lisibilité.
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")À quoi ressemble un texte en gras?

Comment mettre un texte en italique?
Définissez la propriété IsItalic sur true pour appliquer l’italique (Italic). Le style Italic sert généralement à mettre en valeur un passage ou à signaler des titres, des mots étrangers ou des termes techniques. Cette mise en forme distingue ces éléments plus discrètement que le style Bold.
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")À quoi ressemble un texte en italique?

Quelles sont les propriétés de style disponibles ?
IronWord propose un ensemble complet de propriétés de style qui reprennent les options de mise en forme de Microsoft Word. Vous pouvez les combiner pour appliquer une mise en forme élaborée conforme aux normes des documents professionnels.
| Méthode de mise en forme | Description | Exemple |
|---|---|---|
TextFont | Personnalise l’apparence du texte en définissant la famille de polices dans un objet Font. La propriété FontSize se définit au niveau de TextStyle, en dehors de Font. | textRun.Style = new TextStyle() { FontSize = 24, TextFont = new Font() { FontFamily = "Calibri" } }; |
Color | Définit la couleur du texte en utilisant les couleurs prédéfinies de IronWord.Models.Color ou des valeurs hexadécimales personnalisées. | textRun.Style.Color = IronWord.Models.Color.Red; |
IsBold | Applique le gras (Bold) lorsque la propriété vaut true, notamment pour les titres ou pour mettre en valeur un passage. | textRun.Style.IsBold = true; |
IsItalic | Applique l’italique (Italic) lorsque la propriété vaut true, notamment pour mettre en valeur un passage ou signaler un titre. | textRun.Style.IsItalic = true; |
Underline | Souligne le texte avec Underline à l’aide d’un objet Underline qui propose plusieurs styles de soulignement. | textRun.Style.Underline = new Underline(); |
Strike | Barre le texte à l’aide de l’énumération StrikeValue (Strike ou DoubleStrike). | textRun.Style.Strike = StrikeValue.Strike; |
Caps | Affiche tous les caractères du texte en majuscules. | textRun.Style.Caps = true; |
CharacterScale | Ajuste la largeur proportionnelle des caractères en pourcentage de leur taille normale. | textRun.Style.CharacterScale = 150; |
Emboss | Applique un effet de gaufrage au texte, créant un aspect en relief. | textRun.Style.Emboss = true; |
Emphasis | Ajoute des marques d’accentuation au texte mis en forme à partir des valeurs de l’énumération EmphasisMarkValues. | textRun.Style.Emphasis = EmphasisMarkValues.Dot; |
LineSpacing | Règle l’interligne pour améliorer la lisibilité à l’aide d’un objet LineSpacing. La propriété Value sert de multiplicateur de hauteur de ligne. | textRun.Style.LineSpacing = new LineSpacing() { Value = 1.5 }; |
Outline | Affiche uniquement le contour des caractères. | textRun.Style.Outline = true; |
Shading | Applique une couleur de fond ou une trame au texte à l’aide d’un objet Shading. | textRun.Style.Shading = new Shading() { Color = Color.Yellow }; |
SmallCaps | Convertit les minuscules en petites capitales tout en conservant la distinction entre les majuscules et les minuscules. | textRun.Style.SmallCaps = true; |
VerticalPosition | Règle la position verticale du texte par rapport à sa ligne de base, en points. | textRun.Style.VerticalPosition = 5.0; |
VerticalTextAlignment | Aligne verticalement le texte dans son conteneur à l’aide de l’énumération VerticalPositionValues. | textRun.Style.VerticalTextAlignment = VerticalPositionValues.Superscript; |
Combiner plusieurs styles
Dans IronWord, vous pouvez combiner plusieurs propriétés de style pour obtenir une mise en forme élaborée. Voici un exemple qui montre comment les associer pour donner au texte un aspect professionnel.
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")En combinant ces propriétés, votre application peut générer des documents à l’apparence professionnelle qui respectent tous la même identité visuelle.
Questions fréquemment posées
Comment styliser le texte dans un fichier DOCX en utilisant C# ?
Vous pouvez styliser le texte dans un fichier DOCX en utilisant la bibliothèque IronWord en créant un objet Run contenant TextContent et en appliquant un TextStyle à ce Run. Les propriétés TextStyle telles que FontSize, Color, et IsBold vous permettent de personnaliser l'apparence du texte de manière programmatique.
À quoi sert la classe TextStyle dans IronWord ?
La classe TextStyle dans IronWord est utilisée pour appliquer diverses options de formatage de texte, telles que les polices, les couleurs, le gras, l'italique, et le soulignement, au texte dans les documents Word.
Comment changer la couleur de la police du texte en utilisant IronWord ?
Pour changer la couleur de la police du texte en utilisant IronWord, définissez la propriété Color dans la classe TextStyle sur une couleur prédéfinie d'IronWord.Models.Color ou utilisez des valeurs hexadécimales personnalisées pour des besoins de couleur spécifiques.
Puis-je définir à la fois la famille et la taille de la police dans IronWord ?
Oui, IronWord vous permet de définir à la fois la famille de la police et la taille du texte. Vous pouvez utiliser la propriété TextFont pour spécifier la FontFamily et la propriété FontSize dans TextStyle pour définir la taille en points.
Quel est le but de l'objet Run dans IronWord ?
L'objet Run dans IronWord sert de conteneur pour TextContent, permettant l'application de styles de texte tels que les polices, les couleurs et les décorations à un niveau granulaire dans les paragraphes.
Comment mettre le texte en gras dans un document IronWord ?
Pour mettre le texte en gras dans un document IronWord, définissez la propriété IsBold de la classe TextStyle sur true, et appliquez-la à l'objet Run contenant votre texte.
Quelle propriété est utilisée pour mettre le texte en italique dans IronWord ?
La propriété IsItalic est utilisée dans la classe TextStyle pour appliquer le formatage italique au texte dans les documents IronWord.
Comment appliquer un soulignement au texte en utilisant IronWord ?
Pour souligner le texte dans un document IronWord, créez un nouvel objet Underline et assignez-le à la propriété Underline dans la classe TextStyle pour l'objet Run contenant votre texte.
Puis-je appliquer un style de texte complexe en utilisant plusieurs propriétés dans IronWord ?
Oui, IronWord permet un style de texte complexe en combinant plusieurs propriétés TextStyle, telles que FontSize, Color, IsBold et Underline, pour créer un texte richement formaté dans les documents Word.
Quel est l'avantage d'utiliser IronWord pour le style de texte dans les documents DOCX ?
IronWord offre un contrôle programmatique sur le style de texte dans les documents DOCX, permettant aux développeurs d'automatiser la création de documents tout en assurant un formatage cohérent et professionnel similaire à Microsoft Word.
Ahmad est un développeur full-stack avec une solide fondation en C#, Python et technologies web. Il a un profond intérêt pour la construction de solutions logicielles évolutives et aime explorer comment le design et la fonctionnalité se rencontrent dans des applications du monde réel.