Správa textových odstavců PowerPointu v Javě
Přehled
Aspose.Slides for Java reprezentuje text jako hierarchii textových rámců, odstavců a úseků:
- ITextFrame představuje kontejner textu v tvaru a poskytuje přístup ke kolekci odstavců.
- IParagraph představuje jeden odstavec v textovém rámci a poskytuje přístup k úsekům a formátování na úrovni odstavce.
- IPortion představuje úsek textu v odstavci. Každý úsek 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 úseků.
Vytvoření a formátování odstavců
Vytvoření odstavců s více úseky
Následující kroky vytvoří textový rámec se třemi odstavci, z nichž každý obsahuje tři úseky:
- Vytvořte instanci třídy Presentation.
- Přistupte k požadovanému snímku pomocí jeho indexu.
- Přidejte obdélníkový IAutoShape na snímek.
- Získejte ITextFrame tvého tvaru.
- Použijte výchozí odstavec a přidejte dva další objekty IParagraph do textového rámce.
- Přidejte dostatek objektů IPortion tak, aby každý odstavec obsahoval tři úseky. Výchozí odstavec již obsahuje jeden prázdný úsek.
- Nastavte text každého úseku.
- Použijte formátování na úrovni znaků pomocí IPortion.getPortionFormat.
- Uložte upravenou prezentaci.
Tento Java příklad implementuje kroky:
import com.aspose.slides.*;
import java.awt.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();
}
Vytvoření odrážkových a číslovaných seznamů
Vytvoření odrážkového nebo číslovaného seznamu
Odrážky a číslování usnadňují přehlednost souvisejících položek. V Aspose.Slides jsou nastavení seznamu definována pomocí IBulletFormat.
- Vytvořte instanci třídy Presentation.
- Přistupte k požadovanému snímku pomocí jeho indexu.
- Přidejte IAutoShape na vybraný snímek.
- Získejte ITextFrame tvaru.
- Odstraňte výchozí odstavec z textového rámce.
- Vytvořte Paragraph pro symbolickou odrážku.
- Nastavte IBulletFormat.setType na BulletType.Symbol a uveďte znak odrážky.
- Nastavte text odstavce, odsazení, barvu odrážky a výšku odrážky.
- Přidejte odstavec do textového rámce.
- Vytvořte druhý odstavec a nastavte IBulletFormat.setType na BulletType.Numbered.
- Nakonfigurujte styl číslované odrážky a přidejte odstavec do textového rámce.
- Uložte prezentaci.
Tento Java příklad vytváří symbolickou a číslovanou odrážku:
import com.aspose.slides.*;
import java.awt.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.
- Vytvořte instanci třídy Presentation.
- Přistupte k požadovanému snímku pomocí jeho indexu.
- Přidejte IAutoShape a získejte jeho ITextFrame.
- Odstraňte výchozí odstavec z textového rámce.
- Načtěte obrázek odrážky a přidejte jej do kolekce obrázků prezentace jako IPPImage.
- Vytvořte Paragraph a nastavte jeho text.
- Nastavte IBulletFormat.setType na BulletType.Picture.
- Přiřaďte obrázek pomocí IBulletFormat.getPicture a nastavte výšku odrážky.
- Přidejte odstavec do textového rámce.
- Uložte upravenou prezentaci.
Tento Java příklad vytváří 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ícestupňového seznamu
Nastavte IParagraphFormat.setDepth pro umístění odstavců na různé úrovně seznamu. Nejvyšší úroveň má hloubku 0.
- Vytvořte Presentation a přistupte k snímku.
- Přidejte IAutoShape a vymažte výchozí odstavec z jeho textového rámce.
- Vytvořte čtyři odstavce a nakonfigurujte jejich symboly odrážek.
- Nastavte jejich hodnoty IParagraphFormat.setDepth na
0,1,2a3. - Přidejte odstavce do textového rámce a uložte prezentaci.
Tento Java příklad vytváří čtyřúrovňový odrážkový seznam:
import com.aspose.slides.*;
import java.awt.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ími hodnotami
Použijte IBulletFormat.setNumberedBulletStartWith pro nastavení počátečního čísla zobrazeného u číslovaného odstavce.
- Vytvořte Presentation a přidejte IAutoShape na snímek.
- Vymažte výchozí odstavec z textového rámce tvaru.
- Vytvořte tři číslované odstavce.
- Nastavte IBulletFormat.setNumberedBulletStartWith na
2,3a7pro příslušné odstavce. - Přidejte odstavce do textového rámce a uložte prezentaci.
Tento Java příklad přiřazuje vlastní počáteční číslo každému odstavci:
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í rozložení odstavce a koncových vlastností
Nastavení odsazení první řádky
Použijte IParagraphFormat.setIndent pro řízení odsazení první řádky odstavce. Tato metoda posouvá jen první řádek relativně k levému okraji odstavce. Kladná hodnota posune první řádek doprava, zatímco ostatní řádky zůstávají zarovnané ke tělu odstavce.
Použijte IParagraphFormat.setMarginLeft pokud potřebujete posunout celý odstavec. Použijte IParagraphFormat.setIndent pokud chcete posunout jen první řádek.
Následující příklad vytváří několik odstavců a používá různé hodnoty IParagraphFormat.setIndent k ukázce, jak odsazení první řádky ovlivňuje rozložení odstavce.
- Vytvořte instanci třídy Presentation.
- Přistupte k cílovému snímku.
- Přidejte obdélníkový IAutoShape na snímek.
- Získejte ITextFrame tvaru a odstraňte výchozí odstavec.
- Vytvořte několik odstavců a nastavte pro ně různé hodnoty IParagraphFormat.setIndent.
- Přidejte odstavce do textového rámce.
- Uložte upravenou prezentaci.
Tento kód ukazuje, jak nastavit odsazení odstavce:
import com.aspose.slides.*;
import java.awt.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:

Nastavení zavěšeného odsazení
Zavěšené odsazení je rozložení odstavce, kde první řádek začíná vlevo od ostatních řádků. V Aspose.Slides vytvoříte tento efekt pomocí IParagraphFormat.setIndent. Předáním záporné hodnoty posunete první řádek doleva vůči tělu odstavce.
V praxi IParagraphFormat.setMarginLeft určuje levý okraj těla odstavce a IParagraphFormat.setIndent určuje pozici první řádky relativně k tomuto okraji. Pro vytvoření zavěšeného odsazení dejte kladnou hodnotu metodě setMarginLeft a zápornou hodnotu metodě setIndent.
Toto formátování je užitečné pro bibliografie, odkazy, glosáře a další odstavce, kde mají řádky zabalit pod tělo odstavce místo pod první znak první řádky.
- Vytvořte instanci třídy Presentation.
- Přistupte k cílovému snímku.
- Přidejte obdélníkový IAutoShape na snímek.
- Získejte ITextFrame tvaru a odstraňte výchozí odstavec.
- Vytvořte odstavce a dejte kladnou hodnotu IParagraphFormat.setMarginLeft pro každý odstavec.
- Předáním záporné hodnoty IParagraphFormat.setIndent vytvoříte efekt zavěšeného odsazení.
- Přidejte odstavce do textového rámce.
- Uložte upravenou prezentaci.
Tento kód ukazuje, jak nastavit zavěšené odsazení odstavce:
import com.aspose.slides.*;
import java.awt.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:

Nastavení koncových vlastností běhu odstavce
IParagraph.setEndParagraphPortionFormat řídí formátování koncového znaku odstavce. Následující příklad přiřadí velikost písma a latinské písmo ke koncovému znaku druhého odstavce:
- Načtěte Presentation a přistupte k snímku.
- Přidejte IAutoShape a vymažte jeho výchozí odstavec.
- Vytvořte dva odstavce a přidejte k nim textové úseky.
- Vytvořte PortionFormat pro koncový znak druhého odstavce.
- Nastavte IBasePortionFormat.setFontHeight a IBasePortionFormat.setLatinFont.
- 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 odstavce
Import HTML textu do odstavců
Použijte ParagraphCollection.addFromHtml pro převod HTML značkování na odstavce a úseky v textovém rámci.
- Vytvořte instanci třídy Presentation.
- Přistupte k snímku a přidejte IAutoShape.
- Získejte ITextFrame tvaru a vymažte jeho výchozí odstavec.
- Načtěte zdrojový HTML soubor.
- Předávejte HTML řetězec metodě ParagraphCollection.addFromHtml.
- Uložte upravenou prezentaci.
Tento Java příklad 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.
- Vytvořte instanci třídy Presentation a načtěte požadovanou prezentaci.
- Přistupte k snímku a najděte IAutoShape, který obsahuje text.
- Získejte ITextFrame tvaru.
- Zavolejte ParagraphCollection.exportToHtml s indexem počátečního odstavce a počtem odstavců k exportu.
- Zapište vrácený HTML řetězec do souboru.
Tento Java příklad 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í jednotlivý 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 najít v nadřazené kolekci, nemá platné vykreslovací hranice nebo jej nelze vykreslit. Zkontrolujte výsledek před uložením a po použití uvolněte vrácený obrázek.
Vykreslení odstavce ve výchozím měřítku
Předpokládejme, že máme soubor prezentace nazvaný sample.pptx s jedním snímkem, kde je první tvar textovým polem obsahujícím tři odstavce.

Následující příklad vykreslí druhý odstavec v běžném textovém tvaru ve výchozím měřítku a uloží vrácený obrázek ve formátu PNG. Blok finally zajistí, že obrázek bude řádně uvolněn.
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:

Vykreslení odstavce v buňce tabulky se škálováním
Použijte přetíženou metodu IParagraph.getImage přijímající parametry float scaleX a float scaleY pro nastavení horizontálního a vertikálního škálovacího faktoru. Následující příklad vytvoří tabulku, vykreslí odstavec v její první buňce dvakrát šířky a výšky výchozího měřítka 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();
}
Škálovací faktor 1 zachová výchozí velikost pixelu podél dané osy. Například 2 pro oba faktory vytvoří obrázek, jehož šířka i výška jsou přibližně dvojnásobné oproti výchozím rozměrům, což vede ke čtyřnásobnému počtu pixelů. Větší faktory obecně poskytují ostřejší text pro zvětšování nebo výstup ve vysokém rozlišení, ale také zvyšují spotřebu paměti a velikost souboru. Faktory pod 1 produkují menší obrázky s nižší úrovní detailu. Použijte stejné faktory pro zachování poměru stran odstavce; různé horizontální a vertikální faktory roztačí výstup nezávisle.
Vykreslování celého tvaru pomocí IShape.getImage je užitečné, když výstup musí zahrnovat výplň, okraj nebo jiný vizuální kontext tvaru. Pro obrázek pouze s odstavcem použijte IParagraph.getImage.
Často kladené otázky
Mohu zcela zakázat zalamování řádků uvnitř textového rámce?
Ano. Nastavením ITextFrameFormat.setWrapText zakážete zalamování, takže řádky se nebudou lámat na okrajích textového rámce.
Jak mohu získat přesné ohraničení odstavce na snímku?
Použijte IParagraph.getRect pro získání ohraničujícího obdélníku odstavce. IPortion.getRect poskytuje ohraničení jednotlivého úseku.
Kde se řídí zarovnání odstavce (vlevo, vpravo, na střed nebo do bloku)?
IParagraphFormat.setAlignment je nastavení na úrovni odstavce a vztahuje se na celý odstavec bez ohledu na formátování jednotlivých úseků.
Mohu nastavit jazyk kontroly pravopisu pro část odstavce?
Ano. Nastavte IBasePortionFormat.setLanguageId pro jednotlivé úseky, takže jeden odstavec může obsahovat text v několika jazycích.