本教程涵盖如何使用 Java 在 PPTX 中删除文本。它涵盖了建立环境的所有细节、逐步过程和使用 Java **删除 PPTX 文本的工作示例代码。它公开了详细信息,包括生成示例演示文稿、创建自选图形、通过插入文本添加文本框以及删除文本。
使用 Java 在 PPTX 中删除文本的步骤
- 配置环境添加Aspose.Slides for Java删除PPTX中的文字
- 使用 Presentation 类的实例创建一个空演示文稿并访问第一张幻灯片
- 创建矩形类型的自选图形并在其中插入示例文本框
- 在文本框架内添加一部分文本,并使用 TextStrikethroughType 枚举器设置双线删除线
- 在文本框架内添加第二部分文本,并使用 TextStrikethroughType 枚举器设置单行删除线
- 在 PPTX 上保存带删除线文本的演示文稿
上面提到的步骤涵盖了如何使用 Java 使用几个 API 调用在演示文稿中删除文本。所有相关的类和方法都以明确定义的方式指定和使用以获得所需的输出;像 Presentation 类实例用于创建一个空的或加载现有的 PPTX 文件,ShapeCollection 类实例用于在 PPTX 幻灯片中添加自选图形,TextStrikethroughType 枚举器用于为所选部分设置所需的文本删除线类型文本。
使用 Java 在 PPTX 中删除文本的代码
import com.aspose.slides.FillType; | |
import com.aspose.slides.IAutoShape; | |
import com.aspose.slides.IPortion; | |
import com.aspose.slides.IPortionFormat; | |
import com.aspose.slides.ISlide; | |
import com.aspose.slides.ITextFrame; | |
import com.aspose.slides.License; | |
import com.aspose.slides.Portion; | |
import com.aspose.slides.Presentation; | |
import com.aspose.slides.SaveFormat; | |
import com.aspose.slides.ShapeType; | |
import com.aspose.slides.TextStrikethroughType; | |
import java.awt.Color; | |
public class StrikethroughText { | |
public static void main(String[] presArgs) throws Exception{ | |
String filesPath = "/Users/mudassirkhan/Documents/KnowledgeBase/TestData/"; | |
License strikeTextlicense = new License(); | |
strikeTextlicense.setLicense(filesPath + "Conholdate.Total.Product.Family.lic"); | |
//Generate a default presentation to strikethrough the text | |
Presentation samplePresentation = new Presentation(); | |
//Load the first default slide from the presentation slides collection | |
ISlide slide = samplePresentation.getSlides().get_Item(0); | |
//Add an autoshape to add the text frame | |
IAutoShape autoShape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 120, 150, 300, 400); | |
//Fill the shape with no fill color | |
autoShape.getFillFormat().setFillType(FillType.NoFill); | |
//Add the text frame inside the autoshape | |
ITextFrame textFrame = autoShape.addTextFrame("This is sample strikethrough text"); | |
//Set the textual properties on the selected portion | |
IPortionFormat portionFormat = textFrame.getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat(); | |
portionFormat.getFillFormat().setFillType(FillType.Solid); | |
portionFormat.getFillFormat().getSolidFillColor().setColor(Color.RED); | |
//Strikethrough text with double line | |
portionFormat.setStrikethroughType(TextStrikethroughType.Double); | |
//Insert a second line of text | |
IPortion secondPortion = new Portion("Second text line "); | |
textFrame.getParagraphs().get_Item(0).getPortions().add(secondPortion); | |
portionFormat = secondPortion.getPortionFormat(); | |
portionFormat.getFillFormat().setFillType(FillType.Solid); | |
portionFormat.getFillFormat().getSolidFillColor().setColor(Color.BLUE); | |
//Strikethrough text with a single line | |
portionFormat.setStrikethroughType(TextStrikethroughType.Single); | |
// Save the presentation having strikethrough text on the disk | |
samplePresentation.save(filesPath + "StrikethroughText.pptx", SaveFormat.Pptx); | |
} | |
} |
此示例使用 Java* 演示 PPT 中的*删除线文本。它利用 Presentation 类对象生成默认的空演示文稿,并使用 Presentation.getSlides() 方法从演示文稿幻灯片集合中加载第一张幻灯片。创建一个自选图形,然后在其中插入文本。最后,使用 TextStrikethroughType 枚举器删除该部分内的选定文本,并将带有删除线文本的演示文稿保存在磁盘上。
在此示例中,我们学习了如何使用 Java* *删除演示文本。如果您有兴趣了解在演示文稿中合并幻灯片的过程,请参阅 如何在 Java 中合并幻灯片 上的文章。