Gestire le caselle di testo nelle presentazioni in .NET
Introduzione
Il testo nelle diapositive è tipicamente presente in caselle di testo o forme. Pertanto, per aggiungere testo a una diapositiva, è necessario aggiungere prima una casella di testo e poi inserire del testo all’interno della casella.
Per consentirti di aggiungere una forma che possa contenere testo, Aspose.Slides per .NET fornisce l’interfaccia IAutoShape.
Note
Aspose.Slides fornisce anche l’interfaccia IShape per consentirti di aggiungere forme alle diapositive. Tuttavia, non tutte le forme aggiunte tramite l’interfaccia IShape possono contenere testo. Le forme aggiunte tramite l’interfaccia IAutoShape tipicamente contengono testo.
Pertanto, quando si lavora con una forma esistente a cui si desidera aggiungere testo, potrebbe essere opportuno verificare e confermare che sia stata convertita tramite l’interfaccia IAutoShape. Solo allora sarà possibile utilizzare TextFrame, che è una proprietà di IAutoShape. Vedi la sezione Update Text in questa pagina.
Crea una casella di testo su una diapositiva
- Crea un’istanza della classe Presentation.
- Ottieni il riferimento della prima diapositiva tramite il suo indice.
- Aggiungi un oggetto IAutoShape con ShapeType impostato su
Rectanglein una posizione specificata sulla diapositiva e ottieni il riferimento per il nuovo oggettoIAutoShapeaggiunto. - Aggiungi la proprietà
TextFrameall’oggettoIAutoShapeche conterrà del testo. Nell’esempio seguente, abbiamo aggiunto questo testo: Aspose TextBox - Infine, scrivi il file PPTX tramite l’oggetto
Presentation.
Questo codice C# — un’implementazione dei passaggi sopra — mostra come aggiungere testo a una diapositiva:
// Istanzia PresentationEx
using (Presentation pres = new Presentation())
{
// Ottiene la prima diapositiva nella presentazione
ISlide sld = pres.Slides[0];
// Aggiunge un AutoShape con tipo impostato a Rectangle
IAutoShape ashp = sld.Shapes.AddAutoShape(ShapeType.Rectangle, 150, 75, 150, 50);
// Aggiunge TextFrame al rettangolo
ashp.AddTextFrame(" ");
// Accede al riquadro di testo
ITextFrame txtFrame = ashp.TextFrame;
// Crea l'oggetto Paragraph per il riquadro di testo
IParagraph para = txtFrame.Paragraphs[0];
// Crea un oggetto Portion per il paragrafo
IPortion portion = para.Portions[0];
// Imposta il testo
portion.Text = "Aspose TextBox";
// Salva la presentazione su disco
pres.Save("TextBox_out.pptx", Aspose.Slides.Export.SaveFormat.Pptx);
}
Verifica la presenza di una forma casella di testo
Aspose.Slides fornisce la proprietà IsTextBox dall’interfaccia IAutoShape, permettendoti di esaminare le forme e identificare le caselle di testo.

Questo codice C# mostra come verificare se una forma è stata creata come casella di testo:
using (Presentation presentation = new Presentation("sample.pptx"))
{
Aspose.Slides.LowCode.ForEach.Shape(presentation, (shape, slide, index) =>
{
if (shape is IAutoShape autoShape)
{
Console.WriteLine(autoShape.IsTextBox ? "shape is a text box" : "shape is not a text box");
}
});
}
Nota che se aggiungi semplicemente un autoshape usando il metodo AddAutoShape dall’interfaccia IShapeCollection, la proprietà IsTextBox dell’autoshape restituirà false. Tuttavia, dopo aver aggiunto testo all’autoshape usando il metodo AddTextFrame o la proprietà Text, la proprietà IsTextBox restituisce true.
using (Presentation presentation = new Presentation())
{
ISlide slide = presentation.Slides[0];
IAutoShape shape1 = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 10, 10, 100, 40);
// shape1.IsTextBox è false
shape1.AddTextFrame("shape 1");
// shape1.IsTextBox è true
IAutoShape shape2 = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 10, 110, 100, 40);
// shape2.IsTextBox è false
shape2.TextFrame.Text = "shape 2";
// shape2.IsTextBox è true
IAutoShape shape3 = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 10, 210, 100, 40);
// shape3.IsTextBox è false
shape3.AddTextFrame("");
// shape3.IsTextBox è false
IAutoShape shape4 = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 10, 310, 100, 40);
// shape4.IsTextBox è false
shape4.TextFrame.Text = "";
// shape4.IsTextBox è false
}
Aggiungi colonne a una casella di testo
Aspose.Slides fornisce le proprietà ColumnCount e ColumnSpacing (dall’interfaccia ITextFrameFormat e dalla classe TextFrameFormat) per consentirti di aggiungere colonne alle caselle di testo. Puoi specificare il numero di colonne in una casella di testo e poi impostare la spaziatura, in punti, tra le colonne.
Questo codice C# dimostra l’operazione descritta:
using (Presentation presentation = new Presentation())
{
// Ottiene la prima diapositiva nella presentazione
ISlide slide = presentation.Slides[0];
// Aggiunge un AutoShape con tipo impostato a Rectangle
IAutoShape aShape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 100, 100, 300, 300);
// Aggiunge TextFrame al rettangolo
aShape.AddTextFrame("All these columns are limited to be within a single text container -- " +
"you can add or delete text and the new or remaining text automatically adjusts " +
"itself to flow within the container. You cannot have text flow from one container " +
"to other though -- we told you PowerPoint's column options for text are limited!");
// Ottiene il formato di testo del TextFrame
ITextFrameFormat format = aShape.TextFrame.TextFrameFormat;
// Specifica il numero di colonne nel TextFrame
format.ColumnCount = 3;
// Specifica la spaziatura tra le colonne
format.ColumnSpacing = 10;
// Salva la presentazione
presentation.Save("ColumnCount.pptx", SaveFormat.Pptx);
}
Aggiungi colonne a un riquadro di testo
Aspose.Slides per .NET fornisce la proprietà ColumnCount (dall’interfaccia ITextFrameFormat) che consente di aggiungere colonne nei riquadri di testo. Attraverso questa proprietà, è possibile specificare il numero di colonne desiderato in un riquadro di testo.
Questo codice C# mostra come aggiungere una colonna all’interno di un riquadro di testo:
string outPptxFileName = "ColumnsTest.pptx";
using (Presentation pres = new Presentation())
{
IAutoShape shape1 = pres.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, 100, 100, 300, 300);
TextFrameFormat format = (TextFrameFormat)shape1.TextFrame.TextFrameFormat;
format.ColumnCount = 2;
shape1.TextFrame.Text = "All these columns are forced to stay within a single text container -- " +
"you can add or delete text - and the new or remaining text automatically adjusts " +
"itself to stay within the container. You cannot have text spill over from one container " +
"to other, though -- because PowerPoint's column options for text are limited!";
pres.Save(outPptxFileName, SaveFormat.Pptx);
using (Presentation test = new Presentation(outPptxFileName))
{
Debug.Assert(2 == ((AutoShape)test.Slides[0].Shapes[0]).TextFrame.TextFrameFormat.ColumnCount);
Debug.Assert(double.NaN == ((AutoShape)test.Slides[0].Shapes[0]).TextFrame.TextFrameFormat.ColumnSpacing);
}
format.ColumnSpacing = 20;
pres.Save(outPptxFileName, SaveFormat.Pptx);
using (Presentation test = new Presentation(outPptxFileName))
{
Debug.Assert(2 == ((AutoShape)test.Slides[0].Shapes[0]).TextFrame.TextFrameFormat.ColumnCount);
Debug.Assert(20 == ((AutoShape)test.Slides[0].Shapes[0]).TextFrame.TextFrameFormat.ColumnSpacing);
}
format.ColumnCount = 3;
format.ColumnSpacing = 15;
pres.Save(outPptxFileName, SaveFormat.Pptx);
using (Presentation test = new Presentation(outPptxFileName))
{
Debug.Assert(3 == ((AutoShape)test.Slides[0].Shapes[0]).TextFrame.TextFrameFormat.ColumnCount);
Debug.Assert(15 == ((AutoShape)test.Slides[0].Shapes[0]).TextFrame.TextFrameFormat.ColumnSpacing);
}
}
Aggiorna testo
Aspose.Slides consente di modificare o aggiornare il testo contenuto in una casella di testo o tutti i testi contenuti in una presentazione.
Questo codice C# dimostra un’operazione in cui tutti i testi di una presentazione vengono aggiornati o modificati:
using(Presentation pres = new Presentation("text.pptx"))
{
foreach (ISlide slide in pres.Slides)
{
foreach (IShape shape in slide.Shapes)
{
if (shape is IAutoShape autoShape) //Verifica se la forma supporta il riquadro di testo (IAutoShape).
{
foreach (IParagraph paragraph in autoShape.TextFrame.Paragraphs) //Itera attraverso i paragrafi nel riquadro di testo
{
foreach (IPortion portion in paragraph.Portions) //Itera attraverso ogni porzione nel paragrafo
{
portion.Text = portion.Text.Replace("years", "months"); //Cambia il testo
portion.PortionFormat.FontBold = NullableBool.True; //Cambia la formattazione
}
}
}
}
}
//Salva la presentazione modificata
pres.Save("text-changed.pptx", SaveFormat.Pptx);
}
Aggiungi una casella di testo con un collegamento ipertestuale
Puoi inserire un link all’interno di una casella di testo. Quando la casella di testo viene cliccata, gli utenti vengono indirizzati ad aprire il collegamento.
- Crea un’istanza della classe
Presentation. - Ottieni il riferimento della prima diapositiva tramite il suo indice.
- Aggiungi un oggetto
AutoShapeconShapeTypeimpostato suRectanglein una posizione specificata sulla diapositiva e ottieni un riferimento al nuovo oggetto AutoShape aggiunto. - Aggiungi un
TextFrameall’oggettoAutoShapeche contiene Aspose TextBox come testo predefinito. - Istanzia la classe
IHyperlinkManager. - Assegna l’oggetto
IHyperlinkManageralla proprietà HyperlinkClick associata alla porzione desiderata delTextFrame. - Infine, scrivi il file PPTX tramite l’oggetto
Presentation.
Questo codice C# — un’implementazione dei passaggi sopra — mostra come aggiungere una casella di testo con un collegamento ipertestuale a una diapositiva:
// Istanzia una classe Presentation che rappresenta un PPTX
Presentation pptxPresentation = new Presentation();
// Ottiene la prima diapositiva nella presentazione
ISlide slide = pptxPresentation.Slides[0];
// Aggiunge un oggetto AutoShape con tipo impostato a Rectangle
IShape pptxShape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 150, 150, 150, 50);
// Converte la forma in AutoShape
IAutoShape pptxAutoShape = (IAutoShape)pptxShape;
// Accede alla proprietà ITextFrame associata all'AutoShape
pptxAutoShape.AddTextFrame("");
ITextFrame ITextFrame = pptxAutoShape.TextFrame;
// Aggiunge del testo al riquadro
ITextFrame.Paragraphs[0].Portions[0].Text = "Aspose.Slides";
// Imposta il collegamento ipertestuale per il testo della porzione
IHyperlinkManager HypMan = ITextFrame.Paragraphs[0].Portions[0].PortionFormat.HyperlinkManager;
HypMan.SetExternalHyperlinkClick("http://www.aspose.com");
// Salva la presentazione PPTX
pptxPresentation.Save("hLinkPPTX_out.pptx", Aspose.Slides.Export.SaveFormat.Pptx);
FAQ
Qual è la differenza tra una casella di testo e un segnaposto di testo quando si lavora con le diapositive master?
Un placeholder eredita stile/posizione dal master e può essere sovrascritto nei layout, mentre una casella di testo regolare è un oggetto indipendente su una diapositiva specifica e non cambia quando si cambiano i layout.
Come posso eseguire una sostituzione di testo in blocco su tutta la presentazione senza modificare il testo all’interno di grafici, tabelle e SmartArt?
Limita la tua iterazione alle auto-forme che possiedono riquadri di testo ed escludi gli oggetti incorporati (grafici, tabelle, SmartArt) attraversando le loro collezioni separatamente o saltando quei tipi di oggetto.