超链接
Contents
[
Hide
]
本文演示了如何在形状上使用 Aspose.Slides for Android via Java 添加、访问、删除和更新超链接。
添加超链接
创建一个矩形形状,并为其添加指向外部网站的超链接。
static void addHyperlink() {
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 150, 50);
shape.getTextFrame().setText("Aspose");
IParagraph paragraph = shape.getTextFrame().getParagraphs().get_Item(0);
IPortion textPortion = paragraph.getPortions().get_Item(0);
textPortion.getPortionFormat().setHyperlinkClick(new Hyperlink("https://www.aspose.com"));
} finally {
presentation.dispose();
}
}
访问超链接
读取形状文本部分的超链接信息。
static void accessHyperlink() {
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 150, 50);
shape.getTextFrame().setText("Aspose");
IParagraph paragraph = shape.getTextFrame().getParagraphs().get_Item(0);
IPortion textPortion = paragraph.getPortions().get_Item(0);
textPortion.getPortionFormat().setHyperlinkClick(new Hyperlink("https://www.aspose.com"));
IHyperlink hyperlink = textPortion.getPortionFormat().getHyperlinkClick();
} finally {
presentation.dispose();
}
}
删除超链接
清除形状文本中的超链接。
static void removeHyperlink() {
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 150, 50);
shape.getTextFrame().setText("Aspose");
IParagraph paragraph = shape.getTextFrame().getParagraphs().get_Item(0);
IPortion textPortion = paragraph.getPortions().get_Item(0);
textPortion.getPortionFormat().setHyperlinkClick(new Hyperlink("https://www.aspose.com"));
textPortion.getPortionFormat().setHyperlinkClick(null);
} finally {
presentation.dispose();
}
}
更新超链接
更改现有超链接的目标。使用 HyperlinkManager 修改已包含超链接的文本,这模拟了 PowerPoint 安全更新超链接的方式。
static void updateHyperlink() {
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 150, 50);
shape.getTextFrame().setText("Aspose");
IParagraph paragraph = shape.getTextFrame().getParagraphs().get_Item(0);
IPortion textPortion = paragraph.getPortions().get_Item(0);
textPortion.getPortionFormat().setHyperlinkClick(new Hyperlink("https://old.example.com"));
// 在已存在的文本中更改超链接应通过
// HyperlinkManager 而不是直接设置属性。
// 这模拟了 PowerPoint 安全更新超链接的方式。
textPortion.getPortionFormat().getHyperlinkManager().setExternalHyperlinkClick("https://new.example.com");
} finally {
presentation.dispose();
}
}