Gestionar cuadros de texto en presentaciones en .NET

Los textos en diapositivas normalmente existen en cuadros de texto o formas. Por lo tanto, para agregar texto a una diapositiva, primero debe agregar un cuadro de texto y luego colocar algo de texto dentro del cuadro de texto.

Para permitirle agregar una forma que pueda contener texto, Aspose.Slides for .NET proporciona la interfaz IAutoShape.

Crear un cuadro de texto en una diapositiva

  1. Cree una instancia de la clase Presentation.
  2. Obtenga la referencia de la primera diapositiva a través de su índice.
  3. Agregue un objeto IAutoShape con ShapeType establecido como Rectangle en una posición especificada en la diapositiva y obtenga la referencia del objeto IAutoShape recién añadido.
  4. Agregue una propiedad TextFrame al objeto IAutoShape que contendrá un texto. En el ejemplo a continuación, añadimos este texto: Aspose TextBox
  5. Finalmente, guarde el archivo PPTX mediante el objeto Presentation.

Este código C#—una implementación de los pasos anteriores—le muestra cómo agregar texto a una diapositiva:

// Instancia PresentationEx
using (Presentation pres = new Presentation())
{
    // Obtiene la primera diapositiva de la presentación
    ISlide sld = pres.Slides[0];

    // Agrega un AutoShape con el tipo establecido como Rectangle
    IAutoShape ashp = sld.Shapes.AddAutoShape(ShapeType.Rectangle, 150, 75, 150, 50);

    // Agrega TextFrame al rectángulo
    ashp.AddTextFrame(" ");

    // Accede al marco de texto
    ITextFrame txtFrame = ashp.TextFrame;

    // Crea el objeto Paragraph para el marco de texto
    IParagraph para = txtFrame.Paragraphs[0];

    // Crea un objeto Portion para el párrafo
    IPortion portion = para.Portions[0];

    // Establece el texto
    portion.Text = "Aspose TextBox";

    // Guarda la presentación en el disco
    pres.Save("TextBox_out.pptx", Aspose.Slides.Export.SaveFormat.Pptx);
}

Comprobar una forma de cuadro de texto

Aspose.Slides proporciona la propiedad IsTextBox de la interfaz IAutoShape, lo que le permite examinar formas e identificar cuadros de texto.

Text box and shape

Este código C# le muestra cómo comprobar si una forma fue creada como un cuadro de texto:

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");
        }
    });
}

Tenga en cuenta que si simplemente agrega una autoshape usando el método AddAutoShape de la interfaz IShapeCollection, la propiedad IsTextBox de la autoshape devolverá false. Sin embargo, después de agregar texto a la autoshape mediante el método AddTextFrame o la propiedad Text, la propiedad IsTextBox devolverá true.

using (Presentation presentation = new Presentation())
{
    ISlide slide = presentation.Slides[0];

    IAutoShape shape1 = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 10, 10, 100, 40);
    // shape1.IsTextBox es falso
    shape1.AddTextFrame("shape 1");
    // shape1.IsTextBox es verdadero

    IAutoShape shape2 = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 10, 110, 100, 40);
    // shape2.IsTextBox es falso
    shape2.TextFrame.Text = "shape 2";
    // shape2.IsTextBox es verdadero

    IAutoShape shape3 = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 10, 210, 100, 40);
    // shape3.IsTextBox es falso
    shape3.AddTextFrame("");
    // shape3.IsTextBox es falso

    IAutoShape shape4 = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 10, 310, 100, 40);
    // shape4.IsTextBox es falso
    shape4.TextFrame.Text = "";
    // shape4.IsTextBox es falso
}

Agregar columnas a un cuadro de texto

Aspose.Slides proporciona las propiedades ColumnCount y ColumnSpacing (de la interfaz ITextFrameFormat y la clase TextFrameFormat) para permitirle agregar columnas a los cuadros de texto. Puede especificar el número de columnas en un cuadro de texto y luego especificar el espaciado en puntos entre columnas.

Este código en C# demuestra la operación descrita:

using (Presentation presentation = new Presentation())
{
	// Obtiene la primera diapositiva de la presentación
	ISlide slide = presentation.Slides[0];

	// Agrega un AutoShape con el tipo establecido como Rectangle
	IAutoShape aShape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 100, 100, 300, 300);

	// Agrega TextFrame al rectángulo
	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!");

	// Obtiene el formato de texto del TextFrame
	ITextFrameFormat format = aShape.TextFrame.TextFrameFormat;

	// Especifica el número de columnas en el TextFrame
	format.ColumnCount = 3;

	// Especifica el espaciado entre columnas
	format.ColumnSpacing = 10;

	// Guarda la presentación
	presentation.Save("ColumnCount.pptx", SaveFormat.Pptx);
}

Agregar columnas a un marco de texto

Aspose.Slides for .NET proporciona la propiedad ColumnCount (de la interfaz ITextFrameFormat) que le permite agregar columnas en marcos de texto. Mediante esta propiedad, puede especificar el número de columnas deseado en un marco de texto.

Este código C# le muestra cómo agregar una columna dentro de un marco de texto:

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);
    }
}

Actualizar texto

Aspose.Slides le permite cambiar o actualizar el texto contenido en un cuadro de texto o todos los textos contenidos en una presentación.

Este código C# demuestra una operación en la que todos los textos de una presentación se actualizan o cambian:

using(Presentation pres = new Presentation("text.pptx"))
{
   foreach (ISlide slide in pres.Slides)
   {
       foreach (IShape shape in slide.Shapes)
       {
           if (shape is IAutoShape autoShape) //Comprueba si la forma admite marco de texto (IAutoShape). 
           {
              foreach (IParagraph paragraph in autoShape.TextFrame.Paragraphs) //Recorre los párrafos del marco de texto
               {
                   foreach (IPortion portion in paragraph.Portions) //Recorre cada porción del párrafo
                   {
                       portion.Text = portion.Text.Replace("years", "months"); //Modifica el texto
                       portion.PortionFormat.FontBold = NullableBool.True; //Modifica el formato
                   }
               }
           }
       }
   }
  
   //Guarda la presentación modificada
   pres.Save("text-changed.pptx", SaveFormat.Pptx);
}

Agregar un cuadro de texto con hipervínculo

Puede insertar un enlace dentro de un cuadro de texto. Cuando se hace clic en el cuadro de texto, los usuarios son dirigidos a abrir el enlace.

  1. Cree una instancia de la clase Presentation.
  2. Obtenga la referencia de la primera diapositiva a través de su índice.
  3. Agregue un objeto AutoShape con ShapeType establecido como Rectangle en una posición especificada en la diapositiva y obtenga una referencia del objeto AutoShape recién añadido.
  4. Agregue un TextFrame al objeto AutoShape que contenga Aspose TextBox como su texto predeterminado.
  5. Instancie la clase IHyperlinkManager.
  6. Asigne el objeto IHyperlinkManager a la propiedad HyperlinkClick asociada con la parte deseada del TextFrame.
  7. Finalmente, guarde el archivo PPTX mediante el objeto Presentation.

Este código C#—una implementación de los pasos anteriores—le muestra cómo agregar un cuadro de texto con un hipervínculo a una diapositiva:

// Instancia una clase Presentation que representa un PPTX
Presentation pptxPresentation = new Presentation();

// Obtiene la primera diapositiva de la presentación
ISlide slide = pptxPresentation.Slides[0];

// Agrega un objeto AutoShape con el tipo establecido como Rectangle
IShape pptxShape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 150, 150, 150, 50);

// Convierte la forma a AutoShape
IAutoShape pptxAutoShape = (IAutoShape)pptxShape;

// Accede a la propiedad ITextFrame asociada con el AutoShape
pptxAutoShape.AddTextFrame("");

ITextFrame ITextFrame = pptxAutoShape.TextFrame;

// Agrega texto al marco
ITextFrame.Paragraphs[0].Portions[0].Text = "Aspose.Slides";

// Establece el hipervínculo para el texto de la porción
IHyperlinkManager HypMan = ITextFrame.Paragraphs[0].Portions[0].PortionFormat.HyperlinkManager;
HypMan.SetExternalHyperlinkClick("http://www.aspose.com");

// Guarda la presentación PPTX
pptxPresentation.Save("hLinkPPTX_out.pptx", Aspose.Slides.Export.SaveFormat.Pptx);

FAQ

¿Cuál es la diferencia entre un cuadro de texto y un marcador de posición de texto al trabajar con diapositivas maestras?

Un placeholder hereda el estilo/posición de la master y puede sobrescribirse en los layouts, mientras que un cuadro de texto normal es un objeto independiente en una diapositiva específica y no cambia al cambiar de layout.

¿Cómo puedo realizar un reemplazo masivo de texto en toda la presentación sin afectar el texto dentro de gráficos, tablas y SmartArt?

Limite su iteración a auto‑shapes que tengan frames de texto y excluya los objetos incrustados (charts, tables, SmartArt) recorriendo sus colecciones por separado o omitiendo esos tipos de objetos.