Hantera PowerPoint-textstycken i Java
Översikt
Aspose.Slides for Java representerar text som en hierarki av textrutor, stycken och portioner:
- ITextFrame representerar textbehållaren i en form och ger åtkomst till dess styckesamling.
- IParagraph representerar ett stycke i en textruta och ger åtkomst till dess portioner och formatering på styckesnivå.
- IPortion representerar ett textkörning inom ett stycke. Varje portion kan ha sin egen text och tecken‑nivå formatering.
Ett stycke kan därför innehålla text med olika typsnitt, färger, storlekar och annan formatering genom att använda flera portioner.
Skapa och formatera stycken
Skapa stycken i en forms textruta och använd portioner för att formatera enskilda textrunor. Den här sektionen visar hur man kombinerar flera portioner i varje stycke och tillämpar olika färger, teckenstorlekar och stilar.
Skapa stycken med flera portioner
Följande Java‑exempel skapar tre stycken, var och en innehållande tre textrunor i samlingen som returneras av IParagraph.getPortions collection. Det återanvänder standardstycket och dess tomma portion, och lägger sedan till de återstående styckena och portionerna. Formatering appliceras via IPortion.getPortionFormat: den första portionen i varje stycke är röd, fet och 15 punkter; den andra är blå, kursiv och 18 punkter. Den tredje portionen behåller sin standardformatering:
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();
}
Skapa punktlistor och numrerade listor
Organisera styckestext i listor med symboler, siffror eller anpassade bilder. Exemplen i den här sektionen täcker punktformatering, flernivålistor och anpassade startvärden.
Skapa en punktlista eller numrerad lista
Punkter och numrering gör relaterade objekt enklare att skanna. I Aspose.Slides definieras listinställningar via IBulletFormat.
Detta Java‑exempel lägger till en symbolpunkt och ett numrerat objekt i samma textruta. IBulletFormat.setType väljer punkttypen, medan IBulletFormat.setChar anger symbolen och IBulletFormat.setNumberedBulletStyle väljer numreringsstilen. Båda objekten använder svarta punkter med en höjd på 100 procent och ett styckeindrag på 25 punkter:
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();
}
Använd bildpunkter
Bildpunkter låter dig använda en anpassad bild istället för en symbol eller siffra.
Detta Java‑exempel lägger till en bild i presentationens bildsamling som en IPPImage och använder den som en styckepunkt. Det anropar IBulletFormat.setType för att använda en bildpunkt och tilldelar bilden via IBulletFormat.getPicture. IBulletFormat.setHeight sätts till 100 procent för att storleksanpassa punkten relativt styckestexten:
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();
}
Skapa en flernivålista
Ställ in IParagraphFormat.setDepth för att placera stycken på olika nivåer i en lista. Högsta nivån har ett djup på 0.
Detta Java‑exempel tilldelar IParagraphFormat.setDepth värden från 0 till 3 till fyra stycken, vilket skapar en hierarki inom en textruta. Varje stycke använder en symbolpunkt, där IBulletFormat.setChar växlar mellan en rund punkt och ett bindestreck för att särskilja intilliggande nivåer:
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();
}
Starta numrerade listobjekt på egna värden
Använd IBulletFormat.setNumberedBulletStartWith för att ange det första numret som visas för ett numrerat stycke.
Detta Java‑exempel sätter IBulletFormat.setType för att använda numrering för tre stycken och tilldelar IBulletFormat.setNumberedBulletStartWith värdena 2, 3 och 7. Startvärdet konfigureras separat för varje stycke, vilket gör att de visade siffrorna kan hoppa över värden:
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();
}
Styr stycke layout och slutegenskaper
Justera stycke‑marginaler och indrag för att kontrollera positionen för den första raden och de radbrytade raderna. Du kan även formatera paragrafens slutmärke separat från de synliga textportionerna.
Ställ in indrag för första raden
Använd metoden IParagraphFormat.setIndent för att kontrollera indraget för den första raden i ett stycke. Denna inställning flyttar endast den första raden relativt styckets vänstra marginal. Ett positivt värde flyttar den första raden åt höger, medan resterande rader förblir justerade till styckets kropp.
Använd IParagraphFormat.setMarginLeft när du behöver flytta hela stycket. Använd IParagraphFormat.setIndent när du bara vill flytta den första raden.
Följande exempel ger tre stycken samma IParagraphFormat.setMarginLeft på 20 punkter och IParagraphFormat.setIndent värden på 0, 20 och 40 punkter. De första raderna börjar vid successivt större offset, medan radbrytade rader behåller samma vänstra marginal:
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();
}
Resultatet:

Ställ in hängande indrag
Ett hängande indrag är en styckelayout där den första raden börjar åt vänster om de återstående raderna. I Aspose.Slides skapar du den här effekten med egenskapen IParagraphFormat.setIndent. Sätt Indent till ett negativt värde för att flytta den första raden åt vänster relativt styckets kropp.
I praktiken definierar IParagraphFormat.setMarginLeft den vänstra positionen för styckets kropp, och IParagraphFormat.setIndent anger positionen för den första raden relativt den marginalen. För att skapa ett hängande indrag, sätt ett positivt MarginLeft‑värde och ett negativt Indent‑värde.
Denna formatering är användbar för bibliografier, referenser, förteckningsposter och andra stycken där radbrytade rader måste justeras under styckets kropp snarare än under första tecknet i den första raden.
Följande Java‑exempel parar ihop IParagraphFormat.setMarginLeft värden på 40 och 60 punkter med IParagraphFormat.setIndent värden på –20 och –30 punkter. I varje stycke börjar den första raden åt vänster om de radbrytade raderna; det andra stycket har ett djupare hängande indrag:
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();
}
Resultatet:

Ställ in egenskaper för paragrafens slutsegment
Metoden IParagraph.setEndParagraphPortionFormat styr formateringen av paragrafens slutmärke. Följande exempel skapar ett PortionFormat med IBasePortionFormat.setFontHeight satt till 48 punkter och IBasePortionFormat.setLatinFont satt till Times New Roman. Det tilldelar detta format till det andra styckets slutmärke, utan att ändra de synliga textportionerna:
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();
}
Räkna renderade rader
För styckeregler som påverkar automatisk radbrytning och interpunktion vid radslut, se Styr radbrytning och Styr hängande interpunktion.
Använd IParagraph.getLinesCount för att räkna antalet rader som ett stycke upptar efter textlayout, inklusive automatisk radbrytning. Detta är användbart när du kontrollerar textlängd och layout i presentationsmallar.
Ett stycke är ett objekt i ITextFrame.getParagraphs, och det kan uppta flera renderade rader. Ett explicit radbrytningstecken i ett stycke tvingar en ny rad utan att skapa ett nytt stycke. Automatisk radbrytning skapar rader baserat på tillgänglig bredd utan att infoga explicita radbrytningstecken i texten. Att räkna stycken eller radbrytningstecken ger därför inte den renderade radräkningen.
Följande exempel skapar en textruta, räknar dess rader, gör rutan smalare och ersätter sedan texten med en kortare sträng. Exemplet aktiverar radbrytning med ITextFrameFormat.setWrapText och inaktiverar autofit med ITextFrameFormat.setAutofitType så att rutarbredden styr radbrytning utan att automatiskt krympa texten eller storleksändra rutan. Varje mätning använder IParagraph.getLinesCount. Rutdimensioner anges i punkter. Slutligen lägger exemplet till ett ytterligare stycke och summerar radantalet över textrutan.
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 400, 200);
ITextFrame textFrame = shape.getTextFrame();
textFrame.getTextFrameFormat().setWrapText(NullableBool.True);
textFrame.getTextFrameFormat().setAutofitType(TextAutofitType.None);
IParagraph paragraph = textFrame.getParagraphs().get_Item(0);
paragraph.getParagraphFormat().getDefaultPortionFormat().setFontHeight(20);
paragraph.setText("This text demonstrates how automatic wrapping changes the number of rendered lines.");
System.out.println("Original width: " + paragraph.getLinesCount());
shape.setWidth(150);
System.out.println("Narrower shape: " + paragraph.getLinesCount());
paragraph.setText("Short text.");
System.out.println("Shorter text: " + paragraph.getLinesCount());
Paragraph secondParagraph = new Paragraph();
secondParagraph.setText("Another paragraph.");
secondParagraph.getParagraphFormat().getDefaultPortionFormat().setFontHeight(20);
textFrame.getParagraphs().add(secondParagraph);
int totalLineCount = 0;
for (IParagraph currentParagraph : textFrame.getParagraphs()) {
totalLineCount += currentParagraph.getLinesCount();
}
System.out.println("Total lines in the text frame: " + totalLineCount);
} finally {
presentation.dispose();
}
Med denna text och dessa dimensioner ökar radantalet när rutan görs smalare, medan ersättningen med den korta strängen minskar det. Exakta räkningar kan variera med tillgängliga typsnitt och ersättningar, teckenstorlek, marginaler, indrag, radbrytning och autofit‑inställningar. Använd de typsnitt och layoutinställningar som är avsedda för målmiljön när du kontrollerar en mall.
Radantalet i sig avgör inte om texten överskrider sin behållare. Tillgänglig höjd, radhöjder, stycke‑ och radavstånd samt autofit‑beteende spelar också roll; även en enda rad kan överskrida tillgänglig bredd när radbrytning är inaktiverad.
Importera och exportera paragrafinnehåll
Använd HTML för att föra in text i en presentation eller exportera befintliga stycken för användning utanför den. Denna sektion visar även hur man renderar enskilda stycken som bilder, inklusive stycken i tabellceller.
Importera HTML‑text i stycken
Använd ParagraphCollection.addFromHtml för att konvertera HTML‑markup till stycken och portioner i en textruta.
Detta Java‑exempel läser HTML från en fil och skickar markupen till ParagraphCollection.addFromHtml. Det rensar textrutans standardstycke så att rutan endast innehåller det importerade innehållet. Den rektangulära formen lämnar ett 10‑punkts marginal på varje sida av bilden:
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();
}
Exportera paragraftext till HTML
Använd ParagraphCollection.exportToHtml för att exportera ett markerat intervall av stycken som HTML. När du återanvänder presentations‑text på en webbplats kan du vilja bevara lokal betoning utan att ta med presentationens typsnitt, storlek eller textrutsegenskaper. Ställ in TextToHtmlConversionOptions.setTextInheritanceLimit för att kontrollera hur långt exportören följer formaterings‑arv.
Ladda ner exempelpresentationen och placera den i applikationens arbetskatalog. Dess första bild innehåller en textruta med texten “Inherited text; explicit red 18pt”. Indatan använder medvetet flera formateringsnivåer:
- Presentationens standardtextstil levererar Calibri och en storlek på 28 pt.
- Textrutans första‑nivåstil levererar blå text och kursiv.
- Stycket sätter explicit fet stil.
- Den andra portionen, “explicit red 18pt”, sätter explicit röd text och en storlek på 18 pt.
Följande kompletta exempel exporterar samma stycken tre gånger. Det skickar TextToHtmlConversionOptions till exportmetoden och skriver varje resultat som en separat UTF‑8 HTML‑fil:
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("text-inheritance.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();
int[] modes = { TextInheritanceLimit.All, TextInheritanceLimit.TextBox, TextInheritanceLimit.ParagraphOnly };
String[] fileNames = { "All.html", "TextBox.html", "ParagraphOnly.html" };
for (int i = 0; i < modes.length; i++) {
TextToHtmlConversionOptions options = new TextToHtmlConversionOptions();
options.setTextInheritanceLimit(modes[i]);
String html = paragraphs.exportToHtml(0, paragraphs.getCount(), options);
byte[] htmlBytes = html.getBytes(StandardCharsets.UTF_8);
try {
Files.write(Paths.get(fileNames[i]), htmlBytes);
} 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();
}
Den genererade HTML‑koden innehåller följande CSS för den första portionen, “Inherited text; “. Tabellen listar relevanta deklarationer; andra genererade egenskaper har utelämnats för tydlighet.
| Utdata | Relevant CSS för den första portionen | Utseende |
|---|---|---|
All.html |
font-family:'Calibri'; font-size:28pt; color:#0000ff; font-style:italic; font-weight:bold |
Blå, fet, kursiv text i 28 pt i Calibri, om det typsnittet är tillgängligt. |
TextBox.html |
color:#0000ff; font-style:italic; font-weight:bold |
Blå, fet, kursiv text; den omgivande sidan eller webbläsaren tillhandahåller typsnittsfamilj och storlek. |
ParagraphOnly.html |
font-weight:bold |
Fet text; den omgivande sidan eller webbläsaren tillhandahåller typsnittsfamilj, storlek, färg och kursiv stil. |
Den andra portionen behåller color:#ff0000, font-size:18pt och font-weight:bold i alla tre utdata. Den behåller också kursiv med All och TextBox, men inte med ParagraphOnly. Att begränsa arv tar därför inte bort formatering som uttryckligen tilldelats ett stycke eller en portion.
Öppna filerna i samma webbläsare för att jämföra dem. I de två begränsade lägena löses utelämnade CSS‑egenskaper av destinationssidans stilar eller webbläsarstandarder, så det slutgiltiga utseendet beror på var HTML‑koden används.
Generera ett HTML‑urklippsfragment
Använd TextToHtmlConversionOptions.setAddClipboardFragmentHeader för att välja utdata från IParagraphCollection.exportToHtml. Med false returnerar metoden vanlig HTML. Med true börjar den returnerade strängen med ett Windows‑urklippshuvud. Dess StartHTML och EndHTML‑fält ger UTF‑8‑byteoffsetar för HTML, medan StartFragment och EndFragment identifierar innehållet som ska klistras in.
Detta exempel exporterar två formaterade stycken som vanlig HTML (html) och som ett HTML‑urklippsfragment (clipboardHtml). Applikationen kan sedan skriva fragmentet till system‑urklippet. Om urklipps‑API:t förväntar sig råa byte, kodas fragmentet som UTF‑8 utan byte‑order‑markering (BOM).
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 0, 0, 400, 100);
IParagraphCollection paragraphs = shape.getTextFrame().getParagraphs();
paragraphs.clear();
paragraphs.addFromHtml("<p>Status: <b>Ready</b></p><p><i>Next steps</i></p>");
TextToHtmlConversionOptions options = new TextToHtmlConversionOptions();
options.setAddClipboardFragmentHeader(false);
String html = paragraphs.exportToHtml(0, paragraphs.getCount(), options);
options.setAddClipboardFragmentHeader(true);
String clipboardHtml = paragraphs.exportToHtml(0, paragraphs.getCount(), options);
} finally {
presentation.dispose();
}
Rendera ett stycke som en bild
IParagraph.getImage renderar ett enskilt stycke direkt och returnerar en IImage. Spara resultatet till en fil eller ström med IImage.save. Du behöver inte rendera den omgivande formen eller beskära en bitmap manuellt.
IParagraph.getImage kan returnera null om stycket inte kan hittas i sin föräldrasamling, saknar giltiga renderingsgränser eller inte kan renderas. Kontrollera resultatet innan du sparar det och disponera den returnerade bilden efter användning.
Rendera ett stycke i standardskala
Exemplet använder en presentation med en bild vars första form är en textruta som innehåller tre stycken.

Följande exempel väljer det andra stycket med index 1 och anropar IParagraph.getImage utan skalparameter. Det kontrollerar att formen innehåller det nödvändiga stycket och att renderingen returnerar en bild innan IImage.save anropas för att spara den i PNG‑format. finally‑blocket disponerar bilden efter användning.
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();
}
Resultatet:

Rendera ett stycke i en tabellcell med skalning
Använd överlagringen för IParagraph.getImage som accepterar float scaleX och float scaleY för att ange horisontella och vertikala skalningsfaktorer. Följande exempel skapar en tabell, renderar stycket i dess första cell med dubbelt så stor bredd och höjd som standard, och sparar resultatet som en PNG‑bild.
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();
}
En skalningsfaktor på 1 behåller den axeln på sin standardpixelstorlek. Till exempel ger 2 för båda faktorerna en bild vars bredd och höjd är ungefär dubbelt så stora som standardmåtten, vilket resulterar i fyra gånger så många pixlar. Större faktorer ger i regel skarpare text för zoomning eller högupplöst utskrift, men de ökar också minnesbruk och filstorlek. Faktorer under 1 ger mindre bilder med mindre detalj. Använd lika faktorer för att behålla styckets bildförhållande; olika horisontella och vertikala faktorer sträcker ut resultatet oberoende av varandra.
Att rendera en hel form med IShape.getImage är fortfarande användbart när utsprånget måste innehålla formens fyllning, kant eller annan visuell kontext. För enbart bild av ett stycke, använd IParagraph.getImage.
Vanliga frågor
Kan jag helt inaktivera radbrytning i en textruta?
Ja. Ställ in ITextFrameFormat.setWrapText för att inaktivera radbrytning så att rader inte bryts vid textrutans kanter.
Hur kan jag få de exakta gränserna på bilden för ett specifikt stycke?
Använd IParagraph.getRect för att hämta styckets omgivande rektangel. IPortion.getRect ger gränserna för en enskild portion.
Var styrs styckejustering (vänster, höger, centrerad eller marginaljustering)?
IParagraphFormat.setAlignment är en styckesnivåinställning och tillämpas på hela stycket oavsett enskild portionsformatering.
För att vertikalt justera portioner med olika teckenstorlekar inom varje rad, se Justera typsnitt inom en rad.
Kan jag ange korrekturläsningsspråk för en del av ett stycke?
Ja. Ställ in IBasePortionFormat.setLanguageId för enskilda portioner, så att ett stycke kan innehålla text på flera språk.