Tutoriel sur les éléments de diapositive en C# - IronPPT
IronPPT est une bibliothèque PowerPoint fiable conçue pour aider les développeurs .NET C# à intégrer la capacité de créer, lire et modifier des présentations PowerPoint dans leurs applications. Dans le cadre d'une présentation PowerPoint, les diapositives sont les éléments fondamentaux qui structurent et organisent le contenu.
Démarrage rapide : Inserer du texte dans une diapositive nouvelle ou existanteCet exemple montre avec quelle facilité vous pouvez ajouter du texte à une diapositive avec IronPPT. En seulement quelques lignes, insérez dans la première diapositive si elle existe ou créez-en une, puis enregistrez - configuration rapide, sans tracas.
-
1Installez IronPPT avec NuGet Package Manager
-
2Copiez et exécutez cet extrait de code.
var doc = new IronPPT.PresentationDocument(); var text = doc.Slides.Count > 0 ? doc.Slides[0].AddText("Quick Option") : doc.AddSlide(new IronPPT.Models.Slide()).AddText("Quick Option"); doc.Save("quick.pptx");Dim doc = New IronPPT.PresentationDocument() Dim text = If(doc.Slides.Count > 0, doc.Slides(0).AddText("Quick Option"), doc.AddSlide(New IronPPT.Models.Slide()).AddText("Quick Option")) doc.Save("quick.pptx")Dim doc = New IronPPT.PresentationDocument() Dim text = If(doc.Slides.Count > 0, doc.Slides(0).AddText("Quick Option"), doc.AddSlide(New IronPPT.Models.Slide()).AddText("Quick Option")) doc.Save("quick.pptx") -
3Déployez pour tester sur votre environnement de production.
Commencez à utiliser IronPPT dans votre projet dès aujourd'hui avec un essai gratuit
Table des matières
- Ajouter du texte
- Contenu textuel (Ajouter, Append & Supprimer)
- Définir le style (Famille de polices & Taille, Couleur, Gras & Italique, Barré, Souligné)
- Ajouter Image
- Charger une image (Fichier & FileStream)
- Définir les dimensions et l'angle (Largeur & Hauteur)
- Définir la position
- Ajouter une forme
Ajouter du texte
Contenu textuel
Que vous créiez une nouvelle présentation ou que vous en modifiiez une existante, les outils de gestion de texte vous offrent un contrôle total sur le placement et la mise en forme du texte, vous permettant ainsi de concevoir des diapositives qui transmettent votre message clairement et professionnellement.
using IronPPT;
using IronPPT.Models;
// Create a new PowerPoint presentation
var document = new PresentationDocument();
// Ensure there is at least one slide to work with
if (document.Slides.Count == 0)
{
document.Slides.Add(new Slide());
}
// Add text to the first slide
var text = document.Slides[0].AddText("Hello");
// Append text to the existing text on the slide
text.Text += " There!";
// Check if there is any text element to remove from the first slide
if (document.Slides[0].Texts.Count > 0)
{
document.Slides[0].Texts[0].Remove();
}
// Export the PowerPoint presentation with the specified file name
document.Save("addText.pptx");using IronPPT;
using IronPPT.Models;
// Create a new PowerPoint presentation
var document = new PresentationDocument();
// Ensure there is at least one slide to work with
if (document.Slides.Count == 0)
{
document.Slides.Add(new Slide());
}
// Add text to the first slide
var text = document.Slides[0].AddText("Hello");
// Append text to the existing text on the slide
text.Text += " There!";
// Check if there is any text element to remove from the first slide
if (document.Slides[0].Texts.Count > 0)
{
document.Slides[0].Texts[0].Remove();
}
// Export the PowerPoint presentation with the specified file name
document.Save("addText.pptx");Imports IronPPT
Imports IronPPT.Models
' Create a new PowerPoint presentation
Dim document = New PresentationDocument()
' Ensure there is at least one slide to work with
If document.Slides.Count = 0 Then
document.Slides.Add(New Slide())
End If
' Add text to the first slide
Dim text = document.Slides(0).AddText("Hello")
' Append text to the existing text on the slide
text.Text &= " There!"
' Check if there is any text element to remove from the first slide
If document.Slides(0).Texts.Count > 0 Then
document.Slides(0).Texts(0).Remove()
End If
' Export the PowerPoint presentation with the specified file name
document.Save("addText.pptx")Imports IronPPT
Imports IronPPT.Models
' Create a new PowerPoint presentation
Dim document = New PresentationDocument()
' Ensure there is at least one slide to work with
If document.Slides.Count = 0 Then
document.Slides.Add(New Slide())
End If
' Add text to the first slide
Dim text = document.Slides(0).AddText("Hello")
' Append text to the existing text on the slide
text.Text &= " There!"
' Check if there is any text element to remove from the first slide
If document.Slides(0).Texts.Count > 0 Then
document.Slides(0).Texts(0).Remove()
End If
' Export the PowerPoint presentation with the specified file name
document.Save("addText.pptx")Mise en scène
La mise en forme du texte permet de personnaliser son apparence visuelle en définissant des attributs tels que la taille, la couleur, le style, le barré et le souligné. L'application de ces styles améliore la présentation du texte et l'aspect général du document.
using IronPPT;
using IronPPT.Models; // Ensure the library is available
using IronPPT.Enums;
// Create a new presentation document
var document = new PresentationDocument();
// Define and customize the text style
var textStyle = new TextStyle
{
IsBold = true, // Text is bold
IsItalic = true, // Text is italic
Color = Color.Blue, // Text color is blue
Strike = StrikValue.SingleStrike, // Text is single struck-off
Outline = true, // Text has an outline
NoProof = true, // Disables proofing for the text
Spacing = 10.0, // Text spacing is set to 10
Underline = new Underline
{
LineValue = UnderlineValues.Single, // Single underline
Color = Color.Red // Underline color is red
},
Languages = "en-US", // Text language is set to U.S. English
SpecVanish = false, // Text does not vanish when special formatting is applied
};
// Create text content and apply the defined style
var text = new Text("Hello World"); // Instantiate text with a string
text.TextStyle = textStyle; // Apply the defined style to the text
// Add a new slide if none exist
if (document.Slides.Count == 0)
{
document.Slides.Add(new Slide()); // Add a new slide to the document
}
// Add the styled text to the first slide
document.Slides[0].AddText(text); // Add the newly created text object to the first slide
// Save the presentation document to a file
document.Save("textStyle.pptx"); // Save the document with the filename "textStyle.pptx"using IronPPT;
using IronPPT.Models; // Ensure the library is available
using IronPPT.Enums;
// Create a new presentation document
var document = new PresentationDocument();
// Define and customize the text style
var textStyle = new TextStyle
{
IsBold = true, // Text is bold
IsItalic = true, // Text is italic
Color = Color.Blue, // Text color is blue
Strike = StrikValue.SingleStrike, // Text is single struck-off
Outline = true, // Text has an outline
NoProof = true, // Disables proofing for the text
Spacing = 10.0, // Text spacing is set to 10
Underline = new Underline
{
LineValue = UnderlineValues.Single, // Single underline
Color = Color.Red // Underline color is red
},
Languages = "en-US", // Text language is set to U.S. English
SpecVanish = false, // Text does not vanish when special formatting is applied
};
// Create text content and apply the defined style
var text = new Text("Hello World"); // Instantiate text with a string
text.TextStyle = textStyle; // Apply the defined style to the text
// Add a new slide if none exist
if (document.Slides.Count == 0)
{
document.Slides.Add(new Slide()); // Add a new slide to the document
}
// Add the styled text to the first slide
document.Slides[0].AddText(text); // Add the newly created text object to the first slide
// Save the presentation document to a file
document.Save("textStyle.pptx"); // Save the document with the filename "textStyle.pptx"Imports IronPPT
Imports IronPPT.Models ' Ensure the library is available
Imports IronPPT.Enums
' Create a new presentation document
Dim document As New PresentationDocument()
' Define and customize the text style
Dim textStyle As New TextStyle With {
.IsBold = True, ' Text is bold
.IsItalic = True, ' Text is italic
.Color = Color.Blue, ' Text color is blue
.Strike = StrikValue.SingleStrike, ' Text is single struck-off
.Outline = True, ' Text has an outline
.NoProof = True, ' Disables proofing for the text
.Spacing = 10.0, ' Text spacing is set to 10
.Underline = New Underline With {
.LineValue = UnderlineValues.Single, ' Single underline
.Color = Color.Red ' Underline color is red
},
.Languages = "en-US", ' Text language is set to U.S. English
.SpecVanish = False ' Text does not vanish when special formatting is applied
}
' Create text content and apply the defined style
Dim text As New Text("Hello World") ' Instantiate text with a string
text.TextStyle = textStyle ' Apply the defined style to the text
' Add a new slide if none exist
If document.Slides.Count = 0 Then
document.Slides.Add(New Slide()) ' Add a new slide to the document
End If
' Add the styled text to the first slide
document.Slides(0).AddText(text) ' Add the newly created text object to the first slide
' Save the presentation document to a file
document.Save("textStyle.pptx") ' Save the document with the filename "textStyle.pptx"Imports IronPPT
Imports IronPPT.Models ' Ensure the library is available
Imports IronPPT.Enums
' Create a new presentation document
Dim document As New PresentationDocument()
' Define and customize the text style
Dim textStyle As New TextStyle With {
.IsBold = True, ' Text is bold
.IsItalic = True, ' Text is italic
.Color = Color.Blue, ' Text color is blue
.Strike = StrikValue.SingleStrike, ' Text is single struck-off
.Outline = True, ' Text has an outline
.NoProof = True, ' Disables proofing for the text
.Spacing = 10.0, ' Text spacing is set to 10
.Underline = New Underline With {
.LineValue = UnderlineValues.Single, ' Single underline
.Color = Color.Red ' Underline color is red
},
.Languages = "en-US", ' Text language is set to U.S. English
.SpecVanish = False ' Text does not vanish when special formatting is applied
}
' Create text content and apply the defined style
Dim text As New Text("Hello World") ' Instantiate text with a string
text.TextStyle = textStyle ' Apply the defined style to the text
' Add a new slide if none exist
If document.Slides.Count = 0 Then
document.Slides.Add(New Slide()) ' Add a new slide to the document
End If
' Add the styled text to the first slide
document.Slides(0).AddText(text) ' Add the newly created text object to the first slide
' Save the presentation document to a file
document.Save("textStyle.pptx") ' Save the document with the filename "textStyle.pptx"Ajouter des images
Ajustez les paramètres d'image pour un affichage optimal. Une configuration adéquate garantit que les images sont visuellement attrayantes et adaptées à leur contexte.
using IronPPT;
using IronPPT.Models;
// This script demonstrates the creation of a PowerPoint presentation using the IronPPT library.
// An image is added to the presentation, its properties are modified, and then the presentation is saved.
// Create a new PowerPoint presentation
var document = new PresentationDocument();
// Create a new Image object and load an image file.
var image = new Image();
image.LoadFromFile("sample.png");
// Add the image to the first slide (index 0) of the presentation.
var newImage = document.AddImage(image, 0);
// Set the properties of the added image.
// Position property is set using an ElementPosition, which holds X and Y coordinates.
newImage.Position = (200, 200); // Set image position on the slide
newImage.Angle = 45; // Set the rotation angle of the image
newImage.Name = "new image"; // Assign a descriptive name to the image
newImage.Width = 150; // Set the width of the image in pixels
newImage.Height = 150; // Set the height of the image in pixels
// Export the PowerPoint presentation to a file named "addImage.pptx"
document.Save("addImage.pptx");using IronPPT;
using IronPPT.Models;
// This script demonstrates the creation of a PowerPoint presentation using the IronPPT library.
// An image is added to the presentation, its properties are modified, and then the presentation is saved.
// Create a new PowerPoint presentation
var document = new PresentationDocument();
// Create a new Image object and load an image file.
var image = new Image();
image.LoadFromFile("sample.png");
// Add the image to the first slide (index 0) of the presentation.
var newImage = document.AddImage(image, 0);
// Set the properties of the added image.
// Position property is set using an ElementPosition, which holds X and Y coordinates.
newImage.Position = (200, 200); // Set image position on the slide
newImage.Angle = 45; // Set the rotation angle of the image
newImage.Name = "new image"; // Assign a descriptive name to the image
newImage.Width = 150; // Set the width of the image in pixels
newImage.Height = 150; // Set the height of the image in pixels
// Export the PowerPoint presentation to a file named "addImage.pptx"
document.Save("addImage.pptx");Imports IronPPT
Imports IronPPT.Models
' This script demonstrates the creation of a PowerPoint presentation using the IronPPT library.
' An image is added to the presentation, its properties are modified, and then the presentation is saved.
' Create a new PowerPoint presentation
Dim document = New PresentationDocument()
' Create a new Image object and load an image file.
Dim image = New Image()
image.LoadFromFile("sample.png")
' Add the image to the first slide (index 0) of the presentation.
Dim newImage = document.AddImage(image, 0)
' Set the properties of the added image.
' Position property is set using an ElementPosition, which holds X and Y coordinates.
newImage.Position = (200, 200) ' Set image position on the slide
newImage.Angle = 45 ' Set the rotation angle of the image
newImage.Name = "new image" ' Assign a descriptive name to the image
newImage.Width = 150 ' Set the width of the image in pixels
newImage.Height = 150 ' Set the height of the image in pixels
' Export the PowerPoint presentation to a file named "addImage.pptx"
document.Save("addImage.pptx")Imports IronPPT
Imports IronPPT.Models
' This script demonstrates the creation of a PowerPoint presentation using the IronPPT library.
' An image is added to the presentation, its properties are modified, and then the presentation is saved.
' Create a new PowerPoint presentation
Dim document = New PresentationDocument()
' Create a new Image object and load an image file.
Dim image = New Image()
image.LoadFromFile("sample.png")
' Add the image to the first slide (index 0) of the presentation.
Dim newImage = document.AddImage(image, 0)
' Set the properties of the added image.
' Position property is set using an ElementPosition, which holds X and Y coordinates.
newImage.Position = (200, 200) ' Set image position on the slide
newImage.Angle = 45 ' Set the rotation angle of the image
newImage.Name = "new image" ' Assign a descriptive name to the image
newImage.Width = 150 ' Set the width of the image in pixels
newImage.Height = 150 ' Set the height of the image in pixels
' Export the PowerPoint presentation to a file named "addImage.pptx"
document.Save("addImage.pptx")Ajouter des formes
Ajoutez et personnalisez facilement des formes dans votre présentation en définissant leur type, leurs dimensions (largeur et hauteur), leurs couleurs de remplissage et de contour, ainsi que leur position sur la diapositive.
using IronPPT;
using IronPPT.Models;
using IronPPT.Enums;
// Load a PowerPoint presentation.
// The PresentationDocument is assumed to represent an entire PPTX file loaded from disk.
var document = new PresentationDocument("output.pptx");
// Configure a new shape.
// Shape is assumed to be a model object representing drawable elements on a slide.
Shape shape = new Shape
{
Name = "triangle",
Type = ShapeType.Triangle,
Width = 100,
Height = 100,
FillColor = new Color("#444444"),
OutlineColor = Color.Black,
// Position is set via the Position property, which accepts an (x, y) tuple.
// It's important that these coordinates are valid for display on the slide.
Position = (200, 200)
};
// Add the shape to the first slide in the presentation.
// Slides[0] refers to the first slide in the collection. Ensure a slide exists at this index.
document.Slides[0].AddShape(shape);
// Export the modified PowerPoint presentation.
// Saves the changes to a new file, ensuring the original presentation is not overwritten.
document.Save("addShape.pptx");using IronPPT;
using IronPPT.Models;
using IronPPT.Enums;
// Load a PowerPoint presentation.
// The PresentationDocument is assumed to represent an entire PPTX file loaded from disk.
var document = new PresentationDocument("output.pptx");
// Configure a new shape.
// Shape is assumed to be a model object representing drawable elements on a slide.
Shape shape = new Shape
{
Name = "triangle",
Type = ShapeType.Triangle,
Width = 100,
Height = 100,
FillColor = new Color("#444444"),
OutlineColor = Color.Black,
// Position is set via the Position property, which accepts an (x, y) tuple.
// It's important that these coordinates are valid for display on the slide.
Position = (200, 200)
};
// Add the shape to the first slide in the presentation.
// Slides[0] refers to the first slide in the collection. Ensure a slide exists at this index.
document.Slides[0].AddShape(shape);
// Export the modified PowerPoint presentation.
// Saves the changes to a new file, ensuring the original presentation is not overwritten.
document.Save("addShape.pptx");Imports IronPPT
Imports IronPPT.Models
Imports IronPPT.Enums
' Load a PowerPoint presentation.
' The PresentationDocument is assumed to represent an entire PPTX file loaded from disk.
Dim document = New PresentationDocument("output.pptx")
' Configure a new shape.
' Shape is assumed to be a model object representing drawable elements on a slide.
Dim shape As New Shape With {
.Name = "triangle",
.Type = ShapeType.Triangle,
.Width = 100,
.Height = 100,
.FillColor = New Color("#444444"),
.OutlineColor = Color.Black,
' Position is set via the Position property, which accepts an (x, y) tuple.
' It's important that these coordinates are valid for display on the slide.
.Position = (200, 200)
}
' Add the shape to the first slide in the presentation.
' Slides(0) refers to the first slide in the collection. Ensure a slide exists at this index.
document.Slides(0).AddShape(shape)
' Export the modified PowerPoint presentation.
' Saves the changes to a new file, ensuring the original presentation is not overwritten.
document.Save("addShape.pptx")Imports IronPPT
Imports IronPPT.Models
Imports IronPPT.Enums
' Load a PowerPoint presentation.
' The PresentationDocument is assumed to represent an entire PPTX file loaded from disk.
Dim document = New PresentationDocument("output.pptx")
' Configure a new shape.
' Shape is assumed to be a model object representing drawable elements on a slide.
Dim shape As New Shape With {
.Name = "triangle",
.Type = ShapeType.Triangle,
.Width = 100,
.Height = 100,
.FillColor = New Color("#444444"),
.OutlineColor = Color.Black,
' Position is set via the Position property, which accepts an (x, y) tuple.
' It's important that these coordinates are valid for display on the slide.
.Position = (200, 200)
}
' Add the shape to the first slide in the presentation.
' Slides(0) refers to the first slide in the collection. Ensure a slide exists at this index.
document.Slides(0).AddShape(shape)
' Export the modified PowerPoint presentation.
' Saves the changes to a new file, ensuring the original presentation is not overwritten.
document.Save("addShape.pptx")Questions fréquemment posées
À quoi sert IronPPT dans le développement C# ?
IronPPT est une bibliothèque puissante pour les développeurs C# qui permet une intégration simple pour créer, lire et modifier des présentations PowerPoint dans les applications .NET.
Comment puis-je ajouter du texte à une diapositive PowerPoint en utilisant IronPPT ?
Vous pouvez ajouter du texte à une diapositive PowerPoint avec IronPPT en utilisant la méthode 'AddText' sur un objet diapositive, soit en l'ajoutant à une diapositive existante, soit en créant une nouvelle diapositive à l'aide des méthodes de la bibliothèque IronPPT.
Comment appliquer du style au texte dans une présentation PowerPoint en utilisant IronPPT ?
Le style peut être appliqué au texte dans IronPPT en définissant des attributs tels que la taille de la police, la couleur et le style via l'objet 'TextStyle', que vous pouvez ensuite appliquer à vos éléments texte dans une diapositive.
IronPPT peut-il être utilisé pour ajouter des images aux diapositives PowerPoint ?
Oui, IronPPT peut être utilisé pour ajouter des images aux diapositives PowerPoint. Il vous permet de charger des images à partir de fichiers, d'ajuster leur position, leurs dimensions, et même d'appliquer des rotations pour une présentation personnalisée.
Comment IronPPT gère-t-il l'insertion de formes dans les présentations ?
IronPPT facilite l'insertion de formes en permettant aux développeurs de définir les types de formes, leurs dimensions et leurs couleurs, ainsi que de les positionner précisément sur les diapositives, améliorant ainsi le contenu visuel de manière dynamique.
Quelles sont les étapes de base pour créer une nouvelle présentation avec IronPPT ?
Pour créer une nouvelle présentation, instanciez un 'PresentationDocument', ajoutez les diapositives selon les besoins, et utilisez les méthodes fournies pour insérer du texte, des images et des formes. Sauvegarder votre document générera un fichier .pptx.
Est-il possible de modifier des diapositives existantes en utilisant IronPPT ?
Oui, IronPPT permet la modification de diapositives existantes en les accédant via la collection de diapositives du document, où vous pouvez ajouter ou modifier du texte, des images et des formes.
IronPPT prend-il en charge la vérification du texte et les paramètres de langue ?
IronPPT prend en charge la désactivation de la vérification du texte et permet de spécifier la langue pour le formatage du texte, offrant ainsi aux développeurs plus de contrôle sur les propriétés de présentation du texte.
Comment IronPPT améliore-t-il la gestion du texte dans les présentations PowerPoint ?
IronPPT améliore la gestion du texte grâce à des fonctionnalités qui permettent un contrôle total sur le placement, le formatage, le style, et les attributs de style du texte, rendant les présentations claires et professionnelles.
Dans quels formats de fichier IronPPT exporte-t-il ?
IronPPT exporte des présentations au format de fichier .pptx, compatible avec Microsoft PowerPoint et d'autres logiciels de présentation prenant en charge ce format.
Curtis Chau détient un baccalauréat en informatique (Université de Carleton) et se spécialise dans le développement front-end avec expertise en Node.js, TypeScript, JavaScript et React. Passionné par la création d'interfaces utilisateur intuitives et esthétiquement plaisantes, Curtis aime travailler avec des frameworks modernes et créer des manuels bien structurés et visuellement attrayants.