Správa odstavců textu PowerPointu na Androidu

Přehled

Aspose.Slides pro Android via Java představuje text jako hierarchii textových rámců, odstavců a částí:

  • ITextFrame představuje kontejner textu ve tvaru a poskytuje přístup k jeho kolekci odstavců.
  • IParagraph představuje jeden odstavec v textovém rámci a poskytuje přístup k jeho částem a formátování na úrovni odstavce.
  • IPortion představuje běh textu v odstavci. Každá část může mít vlastní text a formátování na úrovni znaků.

Odstavec tak může obsahovat text s různými písmy, barvami, velikostmi a dalším formátováním pomocí více částí.

Vytváření a formátování odstavců

Vytvoření odstavců s více částmi

Následující kroky vytvoří textový rámec se třemi odstavci, z nichž každý obsahuje tři části:

  1. Vytvořte instanci třídy Presentation.
  2. Přistupte k odpovídajícímu snímku pomocí jeho indexu.
  3. Přidejte obdélníkový IAutoShape na snímek.
  4. Získejte ITextFrame tvaru.
  5. Použijte výchozí odstavec a přidejte další dva objekty IParagraph do textového rámce.
  6. Přidejte dostatek objektů IPortion pro každý odstavec tak, aby obsahoval tři části. Výchozí odstavec již obsahuje jednu prázdnou část.
  7. Nastavte text pro každou část.
  8. Použijte formátování na úrovni znaků pomocí IPortion.getPortionFormat.
  9. Uložte upravenou prezentaci.

Tento příklad Android via Java implementuje tyto kroky:

import com.aspose.slides.*;
import android.graphics.Color;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 150, 300, 150);
    ITextFrame textFrame = shape.getTextFrame();

    IParagraph firstParagraph = textFrame.getParagraphs().get_Item(0);
    firstParagraph.getPortions().add(new Portion());
    firstParagraph.getPortions().add(new Portion());

    IParagraph secondParagraph = new Paragraph();
    secondParagraph.getPortions().add(new Portion());
    secondParagraph.getPortions().add(new Portion());
    secondParagraph.getPortions().add(new Portion());
    textFrame.getParagraphs().add(secondParagraph);

    IParagraph thirdParagraph = new Paragraph();
    thirdParagraph.getPortions().add(new Portion());
    thirdParagraph.getPortions().add(new Portion());
    thirdParagraph.getPortions().add(new Portion());
    textFrame.getParagraphs().add(thirdParagraph);

    int paragraphCount = textFrame.getParagraphs().getCount();
    for (int paragraphIndex = 0; paragraphIndex < paragraphCount; paragraphIndex++) {
        IParagraph paragraph = textFrame.getParagraphs().get_Item(paragraphIndex);
        int portionCount = paragraph.getPortions().getCount();
        for (int portionIndex = 0; portionIndex < portionCount; portionIndex++) {
            IPortion portion = paragraph.getPortions().get_Item(portionIndex);
            portion.setText("Portion " + (paragraphIndex + 1) + "." + (portionIndex + 1));

            if (portionIndex == 0) {
                portion.getPortionFormat().getFillFormat().setFillType(FillType.Solid);
                portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.RED);
                portion.getPortionFormat().setFontBold(NullableBool.True);
                portion.getPortionFormat().setFontHeight(15);
            } else if (portionIndex == 1) {
                portion.getPortionFormat().getFillFormat().setFillType(FillType.Solid);
                portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLUE);
                portion.getPortionFormat().setFontItalic(NullableBool.True);
                portion.getPortionFormat().setFontHeight(18);
            }
        }
    }

    presentation.save("paragraphs_with_portions.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

Vytváření odrážkových a číslovaných seznamů

Vytvoření odrážkového nebo číslovaného seznamu

Odrážky a číslování usnadňují skenování souvisejících položek. V Aspose.Slides jsou nastavení seznamu definována pomocí IBulletFormat.

  1. Vytvořte instanci třídy Presentation.
  2. Přistupte k odpovídajícímu snímku pomocí jeho indexu.
  3. Přidejte IAutoShape na vybraný snímek.
  4. Získejte ITextFrame tvaru.
  5. Odstraňte výchozí odstavec z textového rámce.
  6. Vytvořte Paragraph pro symbolickou odrážku.
  7. Nastavte IBulletFormat.setType na BulletType.Symbol a určete znak odrážky.
  8. Nastavte text odstavce, odsazení, barvu odrážky a výšku odrážky.
  9. Přidejte odstavec do textového rámce.
  10. Vytvořte druhý odstavec a nastavte IBulletFormat.setType na BulletType.Numbered.
  11. Nakonfigurujte styl číslované odrážky a přidejte odstavec do textového rámce.
  12. Uložte prezentaci.

Tento příklad Android via Java vytvoří symbolickou odrážku a číslovanou odrážku:

import com.aspose.slides.*;
import android.graphics.Color;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
    ITextFrame textFrame = shape.getTextFrame();
    textFrame.getParagraphs().clear();

    Paragraph symbolParagraph = new Paragraph();
    symbolParagraph.setText("Welcome to Aspose.Slides");
    symbolParagraph.getParagraphFormat().getBullet().setType(BulletType.Symbol);
    symbolParagraph.getParagraphFormat().getBullet().setChar((char) 0x2022);
    symbolParagraph.getParagraphFormat().setIndent(25);
    symbolParagraph.getParagraphFormat().getBullet().getColor().setColorType(ColorType.RGB);
    symbolParagraph.getParagraphFormat().getBullet().getColor().setColor(Color.BLACK);
    symbolParagraph.getParagraphFormat().getBullet().setBulletHardColor(NullableBool.True);
    symbolParagraph.getParagraphFormat().getBullet().setHeight(100);
    textFrame.getParagraphs().add(symbolParagraph);

    Paragraph numberedParagraph = new Paragraph();
    numberedParagraph.setText("This is a numbered item");
    numberedParagraph.getParagraphFormat().getBullet().setType(BulletType.Numbered);
    numberedParagraph.getParagraphFormat().getBullet().setNumberedBulletStyle(NumberedBulletStyle.BulletCircleNumWDBlackPlain);
    numberedParagraph.getParagraphFormat().setIndent(25);
    numberedParagraph.getParagraphFormat().getBullet().getColor().setColorType(ColorType.RGB);
    numberedParagraph.getParagraphFormat().getBullet().getColor().setColor(Color.BLACK);
    numberedParagraph.getParagraphFormat().getBullet().setBulletHardColor(NullableBool.True);
    numberedParagraph.getParagraphFormat().getBullet().setHeight(100);
    textFrame.getParagraphs().add(numberedParagraph);

    presentation.save("bulleted_and_numbered_list.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

Použití obrázkových odrážek

Obrázkové odrážky vám umožní použít vlastní obrázek místo symbolu nebo čísla.

  1. Vytvořte instanci třídy Presentation.
  2. Přistupte k odpovídajícímu snímku pomocí jeho indexu.
  3. Přidejte IAutoShape a získejte jeho ITextFrame.
  4. Odstraňte výchozí odstavec z textového rámce.
  5. Načtěte obrázek odrážky a přidejte jej do kolekce obrázků prezentace jako IPPImage.
  6. Vytvořte Paragraph a nastavte jeho text.
  7. Nastavte IBulletFormat.setType na BulletType.Picture.
  8. Přiřaďte obrázek pomocí IBulletFormat.getPicture a nastavte výšku odrážky.
  9. Přidejte odstavec do textového rámce.
  10. Uložte upravenou prezentaci.

Tento příklad Android via Java vytvoří obrázkovou odrážku:

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IImage bulletImage = Images.fromFile("bullets.png");
    IPPImage presentationImage;
    try {
        presentationImage = presentation.getImages().addImage(bulletImage);
    } finally {
        bulletImage.dispose();
    }

    IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
    ITextFrame textFrame = shape.getTextFrame();
    textFrame.getParagraphs().clear();

    Paragraph paragraph = new Paragraph();
    paragraph.setText("Welcome to Aspose.Slides");
    paragraph.getParagraphFormat().getBullet().setType(BulletType.Picture);
    paragraph.getParagraphFormat().getBullet().getPicture().setImage(presentationImage);
    paragraph.getParagraphFormat().getBullet().setHeight(100);
    textFrame.getParagraphs().add(paragraph);

    presentation.save("picture_bullet.pptx", SaveFormat.Pptx);
    presentation.save("picture_bullet.ppt", SaveFormat.Ppt);
} finally {
    presentation.dispose();
}

Vytvoření víceúrovňového seznamu

Nastavte IParagraphFormat.setDepth pro umístění odstavců na různé úrovně seznamu. Nejvyšší úroveň má hloubku 0.

  1. Vytvořte Presentation a přistupte k snímku.
  2. Přidejte IAutoShape a vymažte výchozí odstavec z jeho textového rámce.
  3. Vytvořte čtyři odstavce a nakonfigurujte jejich symboly odrážek.
  4. Nastavte jejich hodnoty IParagraphFormat.setDepth na 0, 1, 2 a 3.
  5. Přidejte odstavce do textového rámce a uložte prezentaci.

Tento příklad Android via Java vytvoří čtyřúrovňový odrážkový seznam:

import com.aspose.slides.*;
import android.graphics.Color;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
    ITextFrame textFrame = shape.getTextFrame();
    textFrame.getParagraphs().clear();

    IParagraph firstParagraph = new Paragraph();
    firstParagraph.setText("Content");
    firstParagraph.getParagraphFormat().getBullet().setType(BulletType.Symbol);
    firstParagraph.getParagraphFormat().getBullet().setChar((char) 0x2022);
    firstParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
    firstParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
    firstParagraph.getParagraphFormat().setDepth((short) 0);

    IParagraph secondParagraph = new Paragraph();
    secondParagraph.setText("Second level");
    secondParagraph.getParagraphFormat().getBullet().setType(BulletType.Symbol);
    secondParagraph.getParagraphFormat().getBullet().setChar('-');
    secondParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
    secondParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
    secondParagraph.getParagraphFormat().setDepth((short) 1);

    IParagraph thirdParagraph = new Paragraph();
    thirdParagraph.setText("Third level");
    thirdParagraph.getParagraphFormat().getBullet().setType(BulletType.Symbol);
    thirdParagraph.getParagraphFormat().getBullet().setChar((char) 0x2022);
    thirdParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
    thirdParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
    thirdParagraph.getParagraphFormat().setDepth((short) 2);

    IParagraph fourthParagraph = new Paragraph();
    fourthParagraph.setText("Fourth level");
    fourthParagraph.getParagraphFormat().getBullet().setType(BulletType.Symbol);
    fourthParagraph.getParagraphFormat().getBullet().setChar('-');
    fourthParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
    fourthParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
    fourthParagraph.getParagraphFormat().setDepth((short) 3);

    textFrame.getParagraphs().add(firstParagraph);
    textFrame.getParagraphs().add(secondParagraph);
    textFrame.getParagraphs().add(thirdParagraph);
    textFrame.getParagraphs().add(fourthParagraph);

    presentation.save("multilevel_list.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

Zahájení číslovaných položek seznamu vlastní hodnotou

Použijte IBulletFormat.setNumberedBulletStartWith pro nastavení počátečního čísla zobrazovaného pro číslovaný odstavec.

  1. Vytvořte Presentation a přidejte IAutoShape na snímek.
  2. Vymažte výchozí odstavec z textového rámce tvaru.
  3. Vytvořte tři číslované odstavce.
  4. Nastavte IBulletFormat.setNumberedBulletStartWith na 2, 3 a 7 pro příslušné odstavce.
  5. Přidejte odstavce do textového rámce a uložte prezentaci.

Tento příklad Android via Java přiřadí každému odstavci vlastní počáteční číslo:

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
    ITextFrame textFrame = shape.getTextFrame();
    textFrame.getParagraphs().clear();

    Paragraph firstParagraph = new Paragraph();
    firstParagraph.setText("Start at 2");
    firstParagraph.getParagraphFormat().getBullet().setType(BulletType.Numbered);
    firstParagraph.getParagraphFormat().getBullet().setNumberedBulletStartWith((short) 2);
    textFrame.getParagraphs().add(firstParagraph);

    Paragraph secondParagraph = new Paragraph();
    secondParagraph.setText("Start at 3");
    secondParagraph.getParagraphFormat().getBullet().setType(BulletType.Numbered);
    secondParagraph.getParagraphFormat().getBullet().setNumberedBulletStartWith((short) 3);
    textFrame.getParagraphs().add(secondParagraph);

    Paragraph thirdParagraph = new Paragraph();
    thirdParagraph.setText("Start at 7");
    thirdParagraph.getParagraphFormat().getBullet().setType(BulletType.Numbered);
    thirdParagraph.getParagraphFormat().getBullet().setNumberedBulletStartWith((short) 7);
    textFrame.getParagraphs().add(thirdParagraph);

    presentation.save("custom_numbered_list.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

Řízení rozvržení odstavců a koncových vlastností

Nastavení odsazení první řádky

Použijte IParagraphFormat.setIndent pro kontrolu odsazení první řádky odstavce. Tato metoda posouvá jen první řádek relativně k levému okraji odstavce. Kladná hodnota posune první řádek doprava, zatímco zbývající řádky zůstanou zarovnané k tělu odstavce.

Použijte IParagraphFormat.setMarginLeft když potřebujete posunout celý odstavec. Použijte IParagraphFormat.setIndent když potřebujete posunout jen první řádek.

Níže uvedený příklad vytvoří několik odstavců a aplikuje různé hodnoty IParagraphFormat.setIndent k demonstraci, jak odsazení první řádky ovlivňuje rozvržení odstavce.

  1. Vytvořte instanci třídy Presentation.
  2. Přistupte k cílovému snímku.
  3. Přidejte obdélníkový IAutoShape na snímek.
  4. Získejte ITextFrame tvaru a odstraňte výchozí odstavec.
  5. Vytvořte několik odstavců a nastavte pro ně různé hodnoty IParagraphFormat.setIndent.
  6. Přidejte odstavce do textového rámce.
  7. Uložte upravenou prezentaci.

Tento kód ukazuje, jak nastavit odsazení odstavce:

import com.aspose.slides.*;
import android.graphics.Color;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 420, 220);
    shape.getFillFormat().setFillType(FillType.NoFill);
    shape.getLineFormat().getFillFormat().setFillType(FillType.Solid);
    shape.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.GRAY);

    ITextFrame textFrame = shape.getTextFrame();
    textFrame.getTextFrameFormat().setAutofitType(TextAutofitType.Shape);
    textFrame.getParagraphs().clear();

    Paragraph firstParagraph = new Paragraph();
    firstParagraph.setText("No first-line indent. Wrapped lines start at the same position as the first line.");
    firstParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
    firstParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
    firstParagraph.getParagraphFormat().setMarginLeft(20f);
    firstParagraph.getParagraphFormat().setIndent(0f);

    Paragraph secondParagraph = new Paragraph();
    secondParagraph.setText("First-line indent of 20 points. The first line moves to the right, while wrapped lines remain aligned to the paragraph body.");
    secondParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
    secondParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
    secondParagraph.getParagraphFormat().setMarginLeft(20f);
    secondParagraph.getParagraphFormat().setIndent(20f);

    Paragraph thirdParagraph = new Paragraph();
    thirdParagraph.setText("First-line indent of 40 points. This paragraph shows a larger first-line offset to make the effect easier to see.");
    thirdParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
    thirdParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
    thirdParagraph.getParagraphFormat().setMarginLeft(20f);
    thirdParagraph.getParagraphFormat().setIndent(40f);

    textFrame.getParagraphs().add(firstParagraph);
    textFrame.getParagraphs().add(secondParagraph);
    textFrame.getParagraphs().add(thirdParagraph);

    presentation.save("paragraph_indent.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

Výsledek:

Odsazení první řádky odstavců

Nastavení závěsného odsazení

Závěsné odsazení je rozvržení odstavce, ve kterém první řádek začíná vlevo od zbývajících řádků. V Aspose.Slides tento efekt vytvoříte pomocí IParagraphFormat.setIndent. Zadejte zápornou hodnotu pro posun první řádky doleva relativně k tělu odstavce.

V praxi IParagraphFormat.setMarginLeft určuje levou pozici těla odstavce a IParagraphFormat.setIndent určuje pozici první řádky relativně k tomuto okraji. Pro vytvoření závěsného odsazení zadejte kladnou hodnotu do setMarginLeft a zápornou hodnotu do setIndent.

Toto formátování je užitečné pro bibliografie, odkazy, položky glosáře a další odstavce, kde obalované řádky musí být zarovnány pod tělo odstavce místo pod první znak první řádky.

  1. Vytvořte instanci třídy Presentation.
  2. Přistupte k cílovému snímku.
  3. Přidejte obdélníkový IAutoShape na snímek.
  4. Získejte ITextFrame tvaru a odstraňte výchozí odstavec.
  5. Vytvořte odstavce a zadejte kladnou hodnotu do IParagraphFormat.setMarginLeft pro každý odstavec.
  6. Zadejte zápornou hodnotu do IParagraphFormat.setIndent pro vytvoření efektu závěsného odsazení.
  7. Přidejte odstavce do textového rámce.
  8. Uložte upravenou prezentaci.

Tento kód ukazuje, jak nastavit závěsné odsazení odstavce:

import com.aspose.slides.*;
import android.graphics.Color;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 420, 220);
    shape.getFillFormat().setFillType(FillType.NoFill);
    shape.getLineFormat().getFillFormat().setFillType(FillType.Solid);
    shape.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.GRAY);

    ITextFrame textFrame = shape.getTextFrame();
    textFrame.getTextFrameFormat().setAutofitType(TextAutofitType.Shape);
    textFrame.getParagraphs().clear();

    Paragraph firstParagraph = new Paragraph();
    firstParagraph.setText("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.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
    firstParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
    firstParagraph.getParagraphFormat().setMarginLeft(40f);
    firstParagraph.getParagraphFormat().setIndent(-20f);

    Paragraph secondParagraph = new Paragraph();
    secondParagraph.setText("This second example uses a deeper hanging indent so the difference between the first line and the wrapped lines is easier to compare.");
    secondParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
    secondParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
    secondParagraph.getParagraphFormat().setMarginLeft(60f);
    secondParagraph.getParagraphFormat().setIndent(-30f);

    textFrame.getParagraphs().add(firstParagraph);
    textFrame.getParagraphs().add(secondParagraph);

    presentation.save("hanging_indent.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

Výsledek:

Závěsné odsazení odstavců

Nastavení koncových vlastností běhu odstavce

IParagraph.setEndParagraphPortionFormat řídí formátování koncového značky odstavce. Následující příklad přiřadí velikost písma a latinské písmo ke koncové značce druhého odstavce:

  1. Načtěte Presentation a přistupte k snímku.
  2. Přidejte IAutoShape a vymažte jeho výchozí odstavec.
  3. Vytvořte dva odstavce a přidejte k nim textové části.
  4. Vytvořte PortionFormat pro koncovou značku druhého odstavce.
  5. Nastavte IBasePortionFormat.setFontHeight a IBasePortionFormat.setLatinFont.
  6. Přiřaďte formát pomocí IParagraph.setEndParagraphPortionFormat a uložte prezentaci.
import com.aspose.slides.*;

Presentation presentation = new Presentation("Test.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 10, 10, 200, 250);
    ITextFrame textFrame = shape.getTextFrame();
    textFrame.getParagraphs().clear();

    Paragraph firstParagraph = new Paragraph();
    firstParagraph.getPortions().add(new Portion("Sample text"));

    Paragraph secondParagraph = new Paragraph();
    secondParagraph.getPortions().add(new Portion("Sample text 2"));

    PortionFormat endParagraphFormat = new PortionFormat();
    endParagraphFormat.setFontHeight(48);
    endParagraphFormat.setLatinFont(new FontData("Times New Roman"));
    secondParagraph.setEndParagraphPortionFormat(endParagraphFormat);

    textFrame.getParagraphs().add(firstParagraph);
    textFrame.getParagraphs().add(secondParagraph);

    presentation.save("end_paragraph_format.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

Import a export obsahu odstavců

Import HTML textu do odstavců

Použijte ParagraphCollection.addFromHtml pro konverzi HTML značek na odstavce a části v textovém rámci.

  1. Vytvořte instanci třídy Presentation.
  2. Přistupte k snímku a přidejte IAutoShape.
  3. Získejte ITextFrame tvaru a vymažte jeho výchozí odstavec.
  4. Přečtěte zdrojový HTML soubor.
  5. Předávejte HTML řetězec metodě ParagraphCollection.addFromHtml.
  6. Uložte upravenou prezentaci.

Tento příklad Android via Java importuje HTML do textového rámce:

import com.aspose.slides.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    float shapeWidth = (float) presentation.getSlideSize().getSize().getWidth() - 20;
    float shapeHeight = (float) presentation.getSlideSize().getSize().getHeight() - 20;
    IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 10, 10, shapeWidth, shapeHeight);
    shape.getFillFormat().setFillType(FillType.NoFill);
    shape.getTextFrame().getParagraphs().clear();

    try {
        byte[] htmlBytes = Files.readAllBytes(Paths.get("file.html"));
        String html = new String(htmlBytes, StandardCharsets.UTF_8);
        shape.getTextFrame().getParagraphs().addFromHtml(html);
        presentation.save("html_text.pptx", SaveFormat.Pptx);
    } catch (IOException exception) {
        System.out.println("The HTML file could not be read: " + exception.getMessage());
    }
} finally {
    presentation.dispose();
}

Export textu odstavce do HTML

Použijte ParagraphCollection.exportToHtml pro export vybraného rozsahu odstavců jako HTML.

  1. Vytvořte instanci třídy Presentation a načtěte požadovanou prezentaci.
  2. Přistupte k snímku a najděte IAutoShape, který obsahuje text.
  3. Získejte ITextFrame tvaru.
  4. Zavolejte ParagraphCollection.exportToHtml s počátečním indexem odstavce a počtem odstavců k exportu.
  5. Zapište vrácený HTML řetězec do souboru.

Tento příklad Android via Java exportuje všechny odstavce z prvního textového tvaru:

import com.aspose.slides.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

Presentation presentation = new Presentation("ExportingHTMLText.pptx");
try {
    IShape shape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);

    if (shape instanceof IAutoShape) {
        IAutoShape textShape = (IAutoShape) shape;
        ITextFrame textFrame = textShape.getTextFrame();
        if (textFrame != null) {
            IParagraphCollection paragraphs = textFrame.getParagraphs();
            String html = paragraphs.exportToHtml(0, paragraphs.getCount(), null);
            try {
                Files.write(Paths.get("paragraphs.html"), html.getBytes(StandardCharsets.UTF_8));
            } catch (IOException exception) {
                System.out.println("The HTML file could not be written: " + exception.getMessage());
            }
        } else {
            System.out.println("The first shape does not contain a text frame.");
        }
    } else {
        System.out.println("The first shape is not a text shape.");
    }
} finally {
    presentation.dispose();
}

Vykreslení odstavce jako obrázku

IParagraph.getImage vykreslí samostatný odstavec přímo a vrátí IImage. Výsledek uložte do souboru nebo proudu pomocí IImage.save. Není nutné vykreslovat celý tvar nebo ručně ořezávat bitmapu.

IParagraph.getImage může vrátit null, pokud odstavec nelze nalézt v rodičovské kolekci, nemá platné vykreslovací rozměry nebo jej nelze vykreslit. Zkontrolujte výsledek před uložením a po použití uvolněte vrácený obrázek.

Vykreslení odstavce v základním měřítku

Předpokládejme, že máme soubor prezentace s názvem sample.pptx s jedním snímkem, kde je první tvar textové pole obsahující tři odstavce.

Textové pole se třemi odstavci

Následující příklad vykreslí druhý odstavec v běžném textovém tvaru v základním měřítku a uloží vrácený obrázek ve formátu PNG. Blok finally zajistí správné uvolnění obrázku.

import com.aspose.slides.*;

Presentation presentation = new Presentation("sample.pptx");
try {
    IShape shape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);

    if (shape instanceof IAutoShape) {
        IAutoShape textShape = (IAutoShape) shape;
        ITextFrame textFrame = textShape.getTextFrame();
        if (textFrame != null && textFrame.getParagraphs().getCount() > 1) {
            IParagraph paragraph = textFrame.getParagraphs().get_Item(1);
            IImage paragraphImage = paragraph.getImage();

            if (paragraphImage != null) {
                try {
                    paragraphImage.save("paragraph.png", ImageFormat.Png);
                } finally {
                    paragraphImage.dispose();
                }
            } else {
                System.out.println("The paragraph could not be rendered.");
            }
        } else {
            System.out.println("The expected paragraph was not found.");
        }
    } else {
        System.out.println("The first shape is not a text shape.");
    }
} finally {
    presentation.dispose();
}

Výsledek:

Obrázek odstavce

Vykreslení odstavce v buňce tabulky se škálováním

Použijte přetížení IParagraph.getImage přijímající parametry float scaleX a float scaleY k nastavení horizontálního a vertikálního měřítka. Následující příklad vytvoří tabulku, vykreslí odstavec v její první buňce dvojnásobně v šířce i výšce a uloží výsledek jako PNG obrázek.

import com.aspose.slides.*;

float scaleX = 2f;
float scaleY = 2f;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    ITable table = slide.getShapes().addTable(50, 50, new double[] { 300 }, new double[] { 80 });
    IParagraph paragraph = table.get_Item(0, 0).getTextFrame().getParagraphs().get_Item(0);
    paragraph.setText("Text in a table cell");

    IImage paragraphImage = paragraph.getImage(scaleX, scaleY);
    if (paragraphImage != null) {
        try {
            paragraphImage.save("table_paragraph.png", ImageFormat.Png);
        } finally {
            paragraphImage.dispose();
        }
    } else {
        System.out.println("The paragraph could not be rendered.");
    }
} finally {
    presentation.dispose();
}

Měřítko 1 zachovává danou osu v jejím výchozím pixelovém rozměru. Například 2 pro oba faktory vytvoří obrázek, jehož šířka a výška jsou přibližně dvojnásobné výchozích rozměrů, což vede k čtyřnásobku pixelů. Větší faktory obecně poskytují ostřejší text pro zvětšení nebo výstup ve vysokém rozlišení, ale také zvyšují využití paměti a velikost souboru. Faktory pod 1 vytvářejí menší obrázky s méně podrobným obsahem. Použijte stejné faktory pro zachování poměru stran odstavce; odlišné horizontální a vertikální faktory rozšiřují výstup nezávisle.

Vykreslování celého tvaru pomocí IShape.getImage zůstává užitečné, když výstup musí zahrnovat výplň, okraj nebo jiný vizuální kontext tvaru. Pro obrázek pouze odstavce použijte IParagraph.getImage.

Často kladené otázky

Mohu úplně zakázat zalamování řádků v textovém rámci?

Ano. Nastavte ITextFrameFormat.setWrapText pro vypnutí zalamování, takže řádky nebudou přerušovány na okrajích textového rámce.

Jak mohu získat přesné rozměry konkrétního odstavce na snímku?

Použijte IParagraph.getRect pro získání ohraničujícího obdélníku odstavce. IPortion.getRect poskytuje rozměry jednotlivé části.

Kde je řízena zarovnání odstavce (levé, pravé, středové nebo do bloku)?

IParagraphFormat.setAlignment je nastavení na úrovni odstavce a vztahuje se na celý odstavec bez ohledu na formátování jednotlivých částí.

Mohu nastavit jazyk korektury pro část odstavce?

Ano. Nastavte IBasePortionFormat.setLanguageId pro jednotlivé části, takže jeden odstavec může obsahovat text v několika jazycích.