Aspose.Slides for .NET - PowerPoint Manipulation Skill
Overview
This skill enables Claude Code to effectively manipulate Microsoft PowerPoint presentations using the Aspose.Slides.NET library. It provides comprehensive guidance for working with presentations programmatically without requiring Microsoft PowerPoint installation.
Core Capabilities
Presentation Structure
- Creating presentations: New presentations from scratch or from templates
- Loading presentations: PPT, PPTX, ODP, and other formats
- Saving presentations: Multiple format support (PPTX, PDF, HTML, images)
- Slide management: Add, remove, clone, reorder slides
- Master slides and layouts: Work with slide masters and apply layouts
Content Manipulation
- Text handling: TextFrames, Paragraphs, Portions with formatting
- Shapes: AutoShapes, custom shapes, grouping, positioning
- Tables: Create, format, populate table data
- Charts: Create and customize various chart types
- Images: Add, replace, extract images and SVGs
- Media: Embed and configure audio/video
Formatting and Styling
- Text formatting: Fonts, colors, alignment, spacing
- Shape formatting: Fill, line, effects, 3D properties
- Themes and color schemes: Apply and customize themes
- Backgrounds: Solid colors, gradients, patterns, images
Advanced Features
- Animations: Timeline, effects, triggers
- Transitions: Slide transitions and timing
- Comments: Add and manage presentation comments
- Properties: Document properties (built-in and custom)
- SmartArt: Work with SmartArt graphics
- VBA macros: Access and manipulate VBA code
Object Model Understanding
Core Hierarchy
Presentation (IPresentation)
├── Slides (ISlideCollection)
│ ├── Slide (ISlide)
│ │ ├── Shapes (IShapeCollection)
│ │ │ ├── AutoShape (IAutoShape)
│ │ │ ├── Table (ITable)
│ │ │ ├── Chart (IChart)
│ │ │ ├── PictureFrame (IPictureFrame)
│ │ │ └── GroupShape (IGroupShape)
│ │ ├── Background (IBackground)
│ │ └── SlideShowTransition
│ └── NotesSlide (INotesSlide)
├── Masters (IMasterSlideCollection)
│ └── MasterSlide (IMasterSlide)
├── Layouts (ILayoutSlideCollection)
│ └── LayoutSlide (ILayoutSlide)
└── DocumentProperties (IDocumentProperties)
Text Hierarchy
TextFrame (ITextFrame)
├── Paragraphs (IParagraphCollection)
│ └── Paragraph (IParagraph)
│ ├── Portions (IPortionCollection)
│ │ └── Portion (IPortion)
│ │ └── PortionFormat (IPortionFormat)
│ └── ParagraphFormat (IParagraphFormat)
└── TextFrameFormat (ITextFrameFormat)
Modern C# Patterns
Resource Management
Always use using statements for proper disposal:
using Aspose.Slides;
// Single presentation
using var presentation = new Presentation("input.pptx");
// Work with presentation
presentation.Save("output.pptx", SaveFormat.Pptx);
// Multiple resources
using var sourcePresentation = new Presentation("source.pptx");
using var targetPresentation = new Presentation();
// Combine presentations
Functional Collection Processing
Leverage LINQ and functional patterns:
// Find shapes by type
var textShapes = slide.Shapes
.OfType<IAutoShape>()
.Where(s => s.TextFrame != null)
.ToList();
// Process all text portions
var allText = slide.Shapes
.OfType<IAutoShape>()
.Where(s => s.TextFrame != null)
.SelectMany(s => s.TextFrame.Paragraphs)
.SelectMany(p => p.Portions)
.Select(p => p.Text);
// Update text declaratively
slide.Shapes
.OfType<IAutoShape>()
.Where(s => s.Name == "Title")
.Select(s => s.TextFrame)
.Where(tf => tf != null)
.ToList()
.ForEach(tf => tf.Text = "New Title");
Pattern Matching and Switch Expressions
Use modern C# features for shape handling:
foreach (var shape in slide.Shapes)
{
var result = shape switch
{
IAutoShape autoShape when autoShape.TextFrame != null
=> ProcessTextShape(autoShape),
ITable table
=> ProcessTable(table),
IChart chart
=> ProcessChart(chart),
IPictureFrame picture
=> ProcessImage(picture),
_ => null
};
}
Immutability and Builder Patterns
Create helper methods for declarative configuration:
IAutoShape AddConfiguredShape(
ISlide slide,
ShapeType shapeType,
float x, float y, float width, float height,
Action<IAutoShape> configure)
{
var shape = slide.Shapes.AddAutoShape(shapeType, x, y, width, height);
configure(shape);
return shape;
}
// Usage
var titleShape = AddConfiguredShape(
slide,
ShapeType.Rectangle,
50, 50, 600, 100,
shape =>
{
shape.TextFrame.Text = "Title";
shape.FillFormat.FillType = FillType.Solid;
shape.FillFormat.SolidFillColor.Color = Color.Blue;
});
Common Task Patterns
Creating a Presentation from Template
using var presentation = new Presentation("template.pptx");
// Populate placeholder text
foreach (var slide in presentation.Slides)
{
foreach (var shape in slide.Shapes.OfType<IAutoShape>())
{
if (shape.Placeholder != null)
{
var placeholderType = shape.Placeholder.Type;
shape.TextFrame.Text = placeholderType switch
{
PlaceholderType.Title => "Dynamic Title",
PlaceholderType.Body => "Dynamic Content",
_ => shape.TextFrame.Text
};
}
}
}
presentation.Save("output.pptx", SaveFormat.Pptx);
Adding a Table with Data
// Define table dimensions
var columnWidths = new[] { 100.0, 150.0, 200.0 };
var rowHeights = new[] { 50.0, 40.0, 40.0, 40.0 };
var table = slide.Shapes.AddTable(
x: 50,
y: 50,
columnWidths,
rowHeights);
// Populate headers
var headers = new[] { "Name", "Value", "Description" };
for (int col = 0; col < headers.Length; col++)
{
table[col, 0].TextFrame.Text = headers[col];
table[col, 0].CellFormat.FillFormat.FillType = FillType.Solid;
table[col, 0].CellFormat.FillFormat.SolidFillColor.Color =
Color.FromArgb(68, 114, 196);
table[col, 0].TextFrame.Paragraphs[0].Portions[0].PortionFormat
.FillFormat.SolidFillColor.Color = Color.White;
}
// Populate data rows
var data = new[]
{
new[] { "Item 1", "100", "First item" },
new[] { "Item 2", "200", "Second item" },
new[] { "Item 3", "300", "Third item" }
};
for (int row = 0; row < data.Length; row++)
{
for (int col = 0; col < data[row].Length; col++)
{
table[col, row + 1].TextFrame.Text = data[row][col];
}
}
Creating a Chart
// Add chart to slide
var chart = slide.Shapes.AddChart(
ChartType.ClusteredColumn,
x: 50,
y: 50,
width: 500,
height: 400);
// Clear default data
chart.ChartData.Series.Clear();
chart.ChartData.Categories.Clear();
// Set categories
var categories = new[] { "Q1", "Q2", "Q3", "Q4" };
foreach (var category in categories)
{
chart.ChartData.Categories.Add(
chart.ChartData.ChartDataWorkbook.GetCell(0, 0, 0, category));
}
// Add data series
var series1 = chart.ChartData.Series.Add(
chart.ChartData.ChartDataWorkbook.GetCell(0, 0, 1, "Sales"),
chart.Type);
var salesData = new[] { 120, 150, 180, 160 };
for (int i = 0; i < salesData.Length; i++)
{
series1.DataPoints.AddDataPointForBarSeries(
chart.ChartData.ChartDataWorkbook.GetCell(0, i + 1, 1, salesData[i]));
}
// Style the chart
series1.Format.Fill.FillType = FillType.Solid;
series1.Format.Fill.SolidFillColor.Color = Color.FromArgb(68, 114, 196);
chart.HasTitle = true;
chart.ChartTitle.AddTextFrameForOverriding("Quarterly Sales");
Text Replacement with Formatting Preservation
void ReplaceTextPreservingFormat(
IPresentation presentation,
string searchText