Gestionar párrafos de texto de PowerPoint en .NET

Descripción general

Aspose.Slides for .NET representa el texto como una jerarquía de marcos de texto, párrafos y porciones:

  • ITextFrame representa el contenedor de texto en una forma y proporciona acceso a su colección de párrafos.
  • IParagraph representa un párrafo en un marco de texto y proporciona acceso a sus porciones y al formato a nivel de párrafo.
  • IPortion representa una ejecución de texto dentro de un párrafo. Cada porción puede tener su propio texto y formato a nivel de carácter.

Por lo tanto, un párrafo puede contener texto con diferentes fuentes, colores, tamaños y otros formatos mediante el uso de varias porciones.

Crear y dar formato a los párrafos

Crear párrafos con varias porciones

Los siguientes pasos crean un marco de texto con tres párrafos, cada uno con tres porciones:

  1. Crear una instancia de la clase Presentation.
  2. Acceder a la referencia de la diapositiva pertinente mediante su índice.
  3. Añadir una IAutoShape rectangular a la diapositiva.
  4. Acceder al ITextFrame de la forma.
  5. Utilizar el párrafo predeterminado y añadir dos objetos IParagraph más al marco de texto.
  6. Añadir suficientes objetos IPortion para que cada párrafo contenga tres porciones. El párrafo predeterminado ya contiene una porción vacía.
  7. Establecer el texto de cada porción.
  8. Aplicar formato a nivel de carácter mediante IPortion.PortionFormat.
  9. Guardar la presentación modificada.

Este ejemplo en C# implementa los pasos:

using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 150, 300, 150);
var textFrame = shape.TextFrame;

var firstParagraph = textFrame.Paragraphs[0];
firstParagraph.Portions.Add(new Portion());
firstParagraph.Portions.Add(new Portion());

var secondParagraph = new Paragraph();
secondParagraph.Portions.Add(new Portion());
secondParagraph.Portions.Add(new Portion());
secondParagraph.Portions.Add(new Portion());
textFrame.Paragraphs.Add(secondParagraph);

var thirdParagraph = new Paragraph();
thirdParagraph.Portions.Add(new Portion());
thirdParagraph.Portions.Add(new Portion());
thirdParagraph.Portions.Add(new Portion());
textFrame.Paragraphs.Add(thirdParagraph);

var paragraphCount = textFrame.Paragraphs.Count;
for (var paragraphIndex = 0; paragraphIndex < paragraphCount; paragraphIndex++)
{
    var paragragaph = textFrame.Paragraphs[paragraphIndex];
    var portionCount = paragragaph.Portions.Count;
    for (var portionIndex = 0; portionIndex < portionCount; portionIndex++)
    {
        var portion = paragragaph.Portions[portionIndex];
        portion.Text = $"Portion {paragraphIndex + 1}.{portionIndex + 1}";

        if (portionIndex == 0)
        {
            portion.PortionFormat.FillFormat.FillType = FillType.Solid;
            portion.PortionFormat.FillFormat.SolidFillColor.Color = Color.Red;
            portion.PortionFormat.FontBold = NullableBool.True;
            portion.PortionFormat.FontHeight = 15;
        }
        else if (portionIndex == 1)
        {
            portion.PortionFormat.FillFormat.FillType = FillType.Solid;
            portion.PortionFormat.FillFormat.SolidFillColor.Color = Color.Blue;
            portion.PortionFormat.FontItalic = NullableBool.True;
            portion.PortionFormat.FontHeight = 18;
        }
    }
}

presentation.Save("paragraphs_with_portions.pptx", SaveFormat.Pptx);

Crear listas con viñetas y numeradas

Crear una lista con viñetas o numerada

Las viñetas y la numeración facilitan la lectura de elementos relacionados. En Aspose.Slides, la configuración de la lista se define mediante IBulletFormat.

  1. Crear una instancia de la clase Presentation.
  2. Acceder a la referencia de la diapositiva pertinente mediante su índice.
  3. Añadir una IAutoShape a la diapositiva seleccionada.
  4. Acceder al ITextFrame de la forma.
  5. Eliminar el párrafo predeterminado del marco de texto.
  6. Crear un Paragraph para una viñeta de símbolo.
  7. Establecer IBulletFormat.Type a BulletType.Symbol y especificar el carácter de la viñeta.
  8. Establecer el texto del párrafo, la sangría, el color de la viñeta y la altura de la viñeta.
  9. Añadir el párrafo al marco de texto.
  10. Crear un segundo párrafo y establecer IBulletFormat.Type a BulletType.Numbered.
  11. Configurar el estilo de la viñeta numerada y añadir el párrafo al marco de texto.
  12. Guardar la presentación.

Este ejemplo en C# crea una viñeta de símbolo y una viñeta numerada:

using System;
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
var textFrame = shape.TextFrame;
textFrame.Paragraphs.Clear();

var symbolParagraph = new Paragraph { Text = "Welcome to Aspose.Slides" };
symbolParagraph.ParagraphFormat.Bullet.Type = BulletType.Symbol;
symbolParagraph.ParagraphFormat.Bullet.Char = Convert.ToChar(0x2022);
symbolParagraph.ParagraphFormat.Indent = 25;
symbolParagraph.ParagraphFormat.Bullet.Color.ColorType = ColorType.RGB;
symbolParagraph.ParagraphFormat.Bullet.Color.Color = Color.Black;
symbolParagraph.ParagraphFormat.Bullet.IsBulletHardColor = NullableBool.True;
symbolParagraph.ParagraphFormat.Bullet.Height = 100;
textFrame.Paragraphs.Add(symbolParagraph);

var numberedParagraph = new Paragraph { Text = "This is a numbered item" };
numberedParagraph.ParagraphFormat.Bullet.Type = BulletType.Numbered;
numberedParagraph.ParagraphFormat.Bullet.NumberedBulletStyle = NumberedBulletStyle.BulletCircleNumWDBlackPlain;
numberedParagraph.ParagraphFormat.Indent = 25;
numberedParagraph.ParagraphFormat.Bullet.Color.ColorType = ColorType.RGB;
numberedParagraph.ParagraphFormat.Bullet.Color.Color = Color.Black;
numberedParagraph.ParagraphFormat.Bullet.IsBulletHardColor = NullableBool.True;
numberedParagraph.ParagraphFormat.Bullet.Height = 100;
textFrame.Paragraphs.Add(numberedParagraph);

presentation.Save("bulleted_and_numbered_list.pptx", SaveFormat.Pptx);

Utilizar viñetas con imagen

Las viñetas con imagen permiten usar una imagen personalizada en lugar de un símbolo o número.

  1. Crear una instancia de la clase Presentation.
  2. Acceder a la referencia de la diapositiva pertinente mediante su índice.
  3. Añadir una IAutoShape y acceder a su ITextFrame.
  4. Eliminar el párrafo predeterminado del marco de texto.
  5. Cargar la imagen de la viñeta y añadirla a la colección de imágenes de la presentación como un IPPImage.
  6. Crear un Paragraph y establecer su texto.
  7. Establecer IBulletFormat.Type a BulletType.Picture.
  8. Asignar la imagen mediante IBulletFormat.Picture y establecer la altura de la viñeta.
  9. Añadir el párrafo al marco de texto.
  10. Guardar la presentación modificada.

Este ejemplo en C# crea una viñeta de imagen:

using Aspose.Slides;
using Aspose.Slides.Export;

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

using var bulletImage = Images.FromFile("bullets.png");
var presentationImage = presentation.Images.AddImage(bulletImage);

var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
var textFrame = shape.TextFrame;
textFrame.Paragraphs.Clear();

var paragraph = new Paragraph { Text = "Welcome to Aspose.Slides" };
paragraph.ParagraphFormat.Bullet.Type = BulletType.Picture;
paragraph.ParagraphFormat.Bullet.Picture.Image = presentationImage;
paragraph.ParagraphFormat.Bullet.Height = 100;
textFrame.Paragraphs.Add(paragraph);

presentation.Save("picture_bullet.pptx", SaveFormat.Pptx);
presentation.Save("picture_bullet.ppt", SaveFormat.Ppt);

Crear una lista multinivel

Establecer IParagraphFormat.Depth para colocar los párrafos en diferentes niveles de una lista. El nivel superior tiene una profundidad de 0.

  1. Crear una Presentation y acceder a una diapositiva.
  2. Añadir una IAutoShape y eliminar el párrafo predeterminado de su marco de texto.
  3. Crear cuatro párrafos y configurar sus símbolos de viñeta.
  4. Establecer sus valores de IParagraphFormat.Depth a 0, 1, 2 y 3.
  5. Añadir los párrafos al marco de texto y guardar la presentación.

Este ejemplo en C# crea una lista con viñetas de cuatro niveles:

using System;
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
var textFrame = shape.TextFrame;
textFrame.Paragraphs.Clear();

var firstParagraph = new Paragraph { Text = "Content" };
firstParagraph.ParagraphFormat.Bullet.Type = BulletType.Symbol;
firstParagraph.ParagraphFormat.Bullet.Char = Convert.ToChar(0x2022);
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
firstParagraph.ParagraphFormat.Depth = 0;

var secondParagraph = new Paragraph { Text = "Second level" };
secondParagraph.ParagraphFormat.Bullet.Type = BulletType.Symbol;
secondParagraph.ParagraphFormat.Bullet.Char = '-';
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
secondParagraph.ParagraphFormat.Depth = 1;

var thirdParagraph = new Paragraph { Text = "Third level" };
thirdParagraph.ParagraphFormat.Bullet.Type = BulletType.Symbol;
thirdParagraph.ParagraphFormat.Bullet.Char = Convert.ToChar(0x2022);
thirdParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
thirdParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
thirdParagraph.ParagraphFormat.Depth = 2;

var fourthParagraph = new Paragraph { Text = "Fourth level" };
fourthParagraph.ParagraphFormat.Bullet.Type = BulletType.Symbol;
fourthParagraph.ParagraphFormat.Bullet.Char = '-';
fourthParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
fourthParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
fourthParagraph.ParagraphFormat.Depth = 3;

textFrame.Paragraphs.Add(firstParagraph);
textFrame.Paragraphs.Add(secondParagraph);
textFrame.Paragraphs.Add(thirdParagraph);
textFrame.Paragraphs.Add(fourthParagraph);

presentation.Save("multilevel_list.pptx", SaveFormat.Pptx);

Iniciar los elementos de la lista numerada con valores personalizados

Utilizar IBulletFormat.NumberedBulletStartWith para establecer el número inicial que se muestra en un párrafo numerado.

  1. Crear una Presentation y añadir una IAutoShape a una diapositiva.
  2. Eliminar el párrafo predeterminado del marco de texto de la forma.
  3. Crear tres párrafos numerados.
  4. Establecer IBulletFormat.NumberedBulletStartWith a 2, 3 y 7 para los párrafos respectivos.
  5. Añadir los párrafos al marco de texto y guardar la presentación.

Este ejemplo en C# asigna un número de inicio personalizado a cada párrafo:

using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
var textFrame = shape.TextFrame;
textFrame.Paragraphs.Clear();

var firstParagraph = new Paragraph { Text = "Start at 2" };
firstParagraph.ParagraphFormat.Bullet.Type = BulletType.Numbered;
firstParagraph.ParagraphFormat.Bullet.NumberedBulletStartWith = 2;
textFrame.Paragraphs.Add(firstParagraph);

var secondParagraph = new Paragraph { Text = "Start at 3" };
secondParagraph.ParagraphFormat.Bullet.Type = BulletType.Numbered;
secondParagraph.ParagraphFormat.Bullet.NumberedBulletStartWith = 3;
textFrame.Paragraphs.Add(secondParagraph);

var thirdParagraph = new Paragraph { Text = "Start at 7" };
thirdParagraph.ParagraphFormat.Bullet.Type = BulletType.Numbered;
thirdParagraph.ParagraphFormat.Bullet.NumberedBulletStartWith = 7;
textFrame.Paragraphs.Add(thirdParagraph);

presentation.Save("custom_numbered_list.pptx", SaveFormat.Pptx);

Controlar el diseño y las propiedades finales del párrafo

Establecer una sangría de primera línea

Utilizar la propiedad IParagraphFormat.Indent para controlar la sangría de la primera línea de un párrafo. Esta propiedad solo desplaza la primera línea respecto al margen izquierdo del párrafo. Un valor positivo desplaza la primera línea a la derecha, mientras que el resto de líneas permanece alineado con el cuerpo del párrafo.

Utilice IParagraphFormat.MarginLeft cuando necesite mover todo el párrafo. Utilice IParagraphFormat.Indent cuando necesite mover solo la primera línea.

El ejemplo a continuación crea varios párrafos y aplica diferentes valores de IParagraphFormat.Indent para demostrar cómo la sangría de primera línea afecta el diseño del párrafo.

  1. Crear una instancia de la clase Presentation .
  2. Acceder a la diapositiva objetivo.
  3. Añadir una IAutoShape rectangular a la diapositiva.
  4. Acceder al ITextFrame de la forma y eliminar el párrafo predeterminado.
  5. Crear varios párrafos y establecer diferentes valores de Indent para ellos.
  6. Añadir los párrafos al marco de texto.
  7. Guardar la presentación modificada.

Este código muestra cómo establecer una sangría de párrafo:

using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 420, 220);
shape.FillFormat.FillType = FillType.NoFill;
shape.LineFormat.FillFormat.FillType = FillType.Solid;
shape.LineFormat.FillFormat.SolidFillColor.Color = Color.Gray;

var textFrame = shape.TextFrame;
textFrame.TextFrameFormat.AutofitType = TextAutofitType.Shape;
textFrame.Paragraphs.Clear();

var firstParagraph = new Paragraph { Text = "No first-line indent. Wrapped lines start at the same position as the first line." };
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
firstParagraph.ParagraphFormat.MarginLeft = 20;
firstParagraph.ParagraphFormat.Indent = 0;

var secondParagraph = new Paragraph { Text = "First-line indent of 20 points. The first line moves to the right, while wrapped lines remain aligned to the paragraph body." };
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
secondParagraph.ParagraphFormat.MarginLeft = 20;
secondParagraph.ParagraphFormat.Indent = 20;

var thirdParagraph = new Paragraph { Text = "First-line indent of 40 points. This paragraph shows a larger first-line offset to make the effect easier to see." };
thirdParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
thirdParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
thirdParagraph.ParagraphFormat.MarginLeft = 20;
thirdParagraph.ParagraphFormat.Indent = 40;

textFrame.Paragraphs.Add(firstParagraph);
textFrame.Paragraphs.Add(secondParagraph);
textFrame.Paragraphs.Add(thirdParagraph);

presentation.Save("paragraph_indent.pptx", SaveFormat.Pptx);

El resultado:

La sangría de primera línea de los párrafos

Establecer una sangría colgante

Una sangría colgante es un diseño de párrafo en el que la primera línea comienza a la izquierda del resto de líneas. En Aspose.Slides, crea este efecto con la propiedad IParagraphFormat.Indent. Establezca Indent a un valor negativo para mover la primera línea a la izquierda respecto al cuerpo del párrafo.

En la práctica, IParagraphFormat.MarginLeft define la posición izquierda del cuerpo del párrafo, y IParagraphFormat.Indent define la posición de la primera línea respecto a ese margen. Para crear una sangría colgante, establezca un valor positivo de MarginLeft y un valor negativo de Indent.

Este formato es útil para bibliografías, referencias, entradas de glosario y otros párrafos donde las líneas envueltas deben alinearse bajo el cuerpo del párrafo y no bajo el primer carácter de la primera línea.

  1. Crear una instancia de la clase Presentation .
  2. Acceder a la diapositiva objetivo.
  3. Añadir una IAutoShape rectangular a la diapositiva.
  4. Acceder al ITextFrame de la forma y eliminar el párrafo predeterminado.
  5. Crear párrafos y establecer un valor positivo de MarginLeft para cada párrafo.
  6. Establecer un valor negativo de Indent para crear el efecto de sangría colgante.
  7. Añadir los párrafos al marco de texto.
  8. Guardar la presentación modificada.

Este código muestra cómo establecer una sangría colgante para un párrafo:

using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 420, 220);
shape.FillFormat.FillType = FillType.NoFill;
shape.LineFormat.FillFormat.FillType = FillType.Solid;
shape.LineFormat.FillFormat.SolidFillColor.Color = Color.Gray;

var textFrame = shape.TextFrame;
textFrame.TextFrameFormat.AutofitType = TextAutofitType.Shape;
textFrame.Paragraphs.Clear();

var firstParagraph = new Paragraph { Text = "A hanging indent is created by combining a positive left margin with a negative indent. The first line starts to the left, while wrapped lines align with the paragraph body." };
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
firstParagraph.ParagraphFormat.MarginLeft = 40;
firstParagraph.ParagraphFormat.Indent = -20;

var secondParagraph = new Paragraph { Text = "This second example uses a deeper hanging indent so the difference between the first line and the wrapped lines is easier to compare." };
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
secondParagraph.ParagraphFormat.MarginLeft = 60;
secondParagraph.ParagraphFormat.Indent = -30;

textFrame.Paragraphs.Add(firstParagraph);
textFrame.Paragraphs.Add(secondParagraph);

presentation.Save("hanging_indent.pptx", SaveFormat.Pptx);

El resultado:

La sangría colgante de los párrafos

Establecer propiedades de ejecución al final del párrafo

La propiedad IParagraph.EndParagraphPortionFormat controla el formato del marcador de fin de párrafo. El siguiente ejemplo asigna un tamaño de fuente y una fuente latina al marcador de fin del segundo párrafo:

  1. Cargar una Presentation y acceder a una diapositiva.
  2. Añadir una IAutoShape y borrar su párrafo predeterminado.
  3. Crear dos párrafos y añadirles porciones de texto.
  4. Crear un PortionFormat para el marcador de fin del segundo párrafo.
  5. Establecer IBasePortionFormat.FontHeight y IBasePortionFormat.LatinFont .
  6. Asignar el formato a IParagraph.EndParagraphPortionFormat y guardar la presentación.
using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation("Test.pptx");
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 10, 10, 200, 250);
var textFrame = shape.TextFrame;
textFrame.Paragraphs.Clear();

var firstParagraph = new Paragraph();
firstParagraph.Portions.Add(new Portion("Sample text"));

var secondParagraph = new Paragraph();
secondParagraph.Portions.Add(new Portion("Sample text 2"));

var endParagraphFormat = new PortionFormat();
endParagraphFormat.FontHeight = 48;
endParagraphFormat.LatinFont = new FontData("Times New Roman");
secondParagraph.EndParagraphPortionFormat = endParagraphFormat;

textFrame.Paragraphs.Add(firstParagraph);
textFrame.Paragraphs.Add(secondParagraph);

presentation.Save("end_paragraph_format.pptx", SaveFormat.Pptx);

Contar líneas renderizadas

Para reglas de párrafo que afectan el ajuste automático y la puntuación al final de las líneas, consulte Control Line Breaking y Control Hanging Punctuation.

Utilice IParagraph.GetLinesCount para contar las líneas que ocupa un párrafo después del diseño del texto, incluido el ajuste automático. Esto es útil al comprobar la longitud del texto y el diseño en plantillas de presentaciones.

Un párrafo es un elemento de ITextFrame.Paragraphs, y puede ocupar varias líneas renderizadas. Un salto de línea explícito dentro de un párrafo fuerza una nueva línea sin crear otro párrafo. El ajuste automático crea líneas basándose en el ancho disponible sin insertar saltos de línea explícitos en el texto. Por lo tanto, contar párrafos o caracteres de salto de línea no proporciona el recuento de líneas renderizadas.

El siguiente ejemplo crea una forma de texto, cuenta sus líneas, estrecha la forma y luego sustituye el texto por una cadena más corta. El ajuste está habilitado y el autofit está desactivado, de modo que el ancho de la forma controla el ajuste sin reducir automáticamente el texto ni redimensionar la forma. Las dimensiones de la forma se expresan en puntos. Finalmente, el ejemplo añade otro párrafo y suma los recuentos de líneas en todo el marco de texto.

using System;
using Aspose.Slides;

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

var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 400, 200);
var textFrame = shape.TextFrame;
textFrame.TextFrameFormat.WrapText = NullableBool.True;
textFrame.TextFrameFormat.AutofitType = TextAutofitType.None;

var paragraph = textFrame.Paragraphs[0];
paragraph.ParagraphFormat.DefaultPortionFormat.FontHeight = 20;
paragraph.Text = "This text demonstrates how automatic wrapping changes the number of rendered lines.";
Console.WriteLine($"Original width: {paragraph.GetLinesCount()}");

shape.Width = 150;
Console.WriteLine($"Narrower shape: {paragraph.GetLinesCount()}");

paragraph.Text = "Short text.";
Console.WriteLine($"Shorter text: {paragraph.GetLinesCount()}");

var secondParagraph = new Paragraph { Text = "Another paragraph." };
secondParagraph.ParagraphFormat.DefaultPortionFormat.FontHeight = 20;
textFrame.Paragraphs.Add(secondParagraph);

var totalLineCount = 0;
foreach (var currentParagraph in textFrame.Paragraphs)
{
    totalLineCount += currentParagraph.GetLinesCount();
}
Console.WriteLine($"Total lines in the text frame: {totalLineCount}");

Con este texto y estas dimensiones, estrechar la forma aumenta el recuento de líneas, mientras que sustituir el texto por la cadena corta lo reduce. Los recuentos exactos pueden variar según la disponibilidad y sustitución de fuentes, el tamaño de la fuente, los márgenes, la sangría, el ajuste y la configuración de autofit. Utilice las fuentes y la configuración de diseño previstas para el entorno objetivo al comprobar una plantilla.

El recuento de líneas por sí solo no determina si el texto desborda su contenedor. La altura disponible, la altura de las líneas, el interlineado del párrafo y el comportamiento de autofit también son importantes; incluso una sola línea puede exceder el ancho disponible cuando el ajuste está desactivado.

Importar y exportar contenido de párrafos

Importar texto HTML en párrafos

Utilice ParagraphCollection.AddFromHtml para convertir marcado HTML en párrafos y porciones dentro de un marco de texto.

  1. Crear una instancia de la clase Presentation .
  2. Acceder a una diapositiva y añadir una IAutoShape .
  3. Acceder al ITextFrame de la forma y limpiar su párrafo predeterminado.
  4. Leer el archivo HTML fuente.
  5. Pasar la cadena HTML a ParagraphCollection.AddFromHtml .
  6. Guardar la presentación modificada.

Este ejemplo en C# importa HTML a un marco de texto:

using System.IO;
using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shapeWidth = presentation.SlideSize.Size.Width - 20;
var shapeHeight = presentation.SlideSize.Size.Height - 20;
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 10, 10, shapeWidth, shapeHeight);
shape.FillFormat.FillType = FillType.NoFill;
shape.TextFrame.Paragraphs.Clear();

using var reader = new StreamReader("file.html");
var html = reader.ReadToEnd();
shape.TextFrame.Paragraphs.AddFromHtml(html);

presentation.Save("html_text.pptx", SaveFormat.Pptx);

Exportar texto de párrafo a HTML

Utilice ParagraphCollection.ExportToHtml para exportar un rango seleccionado de párrafos como HTML.

  1. Crear una instancia de la clase Presentation y cargar la presentación deseada.
  2. Acceder a la diapositiva y localizar la IAutoShape que contiene el texto.
  3. Acceder al ITextFrame de la forma.
  4. Llamar a ParagraphCollection.ExportToHtml con el índice del párrafo inicial y el número de párrafos a exportar.
  5. Escribir la cadena HTML devuelta en un archivo.

Este ejemplo en C# exporta todos los párrafos del primer objeto de texto:

using System;
using System.IO;
using System.Text;
using Aspose.Slides;

using var presentation = new Presentation("ExportingHTMLText.pptx");
var shape = presentation.Slides[0].Shapes[0];

if (shape is IAutoShape textShape && textShape.TextFrame != null)
{
    var paragraphs = textShape.TextFrame.Paragraphs;
    var html = paragraphs.ExportToHtml(0, paragraphs.Count, null);
    using var writer = new StreamWriter("paragraphs.html", false, Encoding.UTF8);
    writer.Write(html);
}
else
{
    Console.WriteLine("The first shape is not a text shape.");
}

Renderizar un párrafo como imagen

IParagraph.GetImage renderiza directamente un párrafo individual y devuelve un IImage. Guarde el resultado en un archivo o flujo con IImage.Save. No es necesario renderizar la forma contenedora ni recortar manualmente un mapa de bits.

IParagraph.GetImage puede devolver null si el párrafo no se encuentra en su colección principal, no tiene límites de renderizado válidos o no puede ser renderizado. Verifique el resultado antes de guardarlo y libere la imagen devuelta después de su uso.

Renderizar un párrafo a la escala predeterminada

Supongamos que tenemos un archivo de presentación llamado sample.pptx con una sola diapositiva, donde la primera forma es un cuadro de texto que contiene tres párrafos.

El cuadro de texto con tres párrafos

El siguiente ejemplo renderiza el segundo párrafo en una forma de texto normal a la escala predeterminada y guarda la imagen resultante en formato PNG. La declaración using garantiza que la imagen se libere correctamente.

using System;
using Aspose.Slides;

using var presentation = new Presentation("sample.pptx");

var shape = presentation.Slides[0].Shapes[0];
if (shape is IAutoShape textShape && 
    textShape.TextFrame != null && 
    textShape.TextFrame.Paragraphs.Count > 1)
{
    var paragraph = textShape.TextFrame.Paragraphs[1];
    using var paragraphImage = paragraph.GetImage();

    if (paragraphImage != null)
    {
        paragraphImage.Save("paragraph.png", ImageFormat.Png);
    }
    else
    {
        Console.WriteLine("The paragraph could not be rendered.");
    }
}
else
{
    Console.WriteLine("The expected text shape or paragraph was not found.");
}

El resultado:

La imagen del párrafo

Renderizar un párrafo en una celda de tabla con escalado

Utilice la sobrecarga de IParagraph.GetImage que acepta los parámetros float scaleX y float scaleY para establecer los factores de escala horizontal y vertical. El siguiente ejemplo crea una tabla, renderiza el párrafo en su primera celda al doble de su ancho y alto predeterminados y guarda el resultado como una imagen PNG.

using System;
using Aspose.Slides;

var scaleX = 2f;
var scaleY = 2f;

using var presentation = new Presentation();
var slide = presentation.Slides[0];
var table = slide.Shapes.AddTable(50, 50, new[] { 300d }, new[] { 80d });
var paragraph = table[0, 0].TextFrame.Paragraphs[0];
paragraph.Text = "Text in a table cell";

using var paragraphImage = paragraph.GetImage(scaleX, scaleY);
if (paragraphImage != null)
{
    paragraphImage.Save("table_paragraph.png", ImageFormat.Png);
}
else
{
    Console.WriteLine("The paragraph could not be rendered.");
}

Un factor de escala de 1 mantiene ese eje en su tamaño de píxel predeterminado. Por ejemplo, 2 para ambos factores produce una imagen cuya anchura y altura son aproximadamente el doble de las dimensiones predeterminadas, resultando en cuatro veces más píxeles. Los factores mayores suelen producir texto más nítido para ampliaciones o salidas de alta resolución, pero también aumentan el uso de memoria y el tamaño del archivo. Los factores inferiores a 1 generan imágenes más pequeñas con menos detalle. Use factores iguales para conservar la proporción del párrafo; factores diferentes en horizontal y vertical estiran la salida independientemente.

Renderizar una forma completa con IShape.GetImage sigue siendo útil cuando la salida debe incluir el relleno, el contorno u otro contexto visual de la forma. Para una imagen sólo de párrafo, utilice IParagraph.GetImage .

Preguntas frecuentes

¿Puedo desactivar completamente el ajuste de línea dentro de un marco de texto?

Sí. Establezca ITextFrameFormat.WrapText para desactivar el ajuste, de modo que las líneas no se partan en los bordes del marco de texto.

¿Cómo puedo obtener los límites exactos en la diapositiva de un párrafo específico?

Utilice IParagraph.GetRect para obtener el rectángulo delimitador del párrafo. IPortion.GetRect proporciona los límites de una porción individual.

¿Dónde se controla la alineación de los párrafos (izquierda, derecha, centrado o justificado)?

IParagraphFormat.Alignment es una configuración a nivel de párrafo y se aplica a todo el párrafo sin importar el formato de las porciones individuales.

Para alinear verticalmente porciones con diferentes tamaños de fuente dentro de cada línea, consulte Align Fonts Within a Line.

¿Puedo establecer el idioma de revisión para una parte de un párrafo?

Sí. Establezca IBasePortionFormat.LanguageId para porciones individuales, de modo que un párrafo pueda contener texto en varios idiomas.