在 Android 上管理簡報超連結
簡介
超連結將簡報內容連結至網站或簡報內的某個位置。在 PowerPoint 中,超連結通常有兩個用途:
- 從文字、圖形或多媒體框架開啟網站。
- 從目錄等位置導覽至另一張投影片。
Aspose.Slides for Android via Java 讓您可以新增這些連結、控制其外觀與音效、更新屬性,並移除它們。下列範例示範如何在個別元素上操作超連結,以及如何在簡報、投影片或文字框層級存取超連結。
Note
您也可以使用免費線上 Aspose PowerPoint 編輯器編輯簡報。新增 URL 超連結
您可以將網站 URL 指派給文字、圖形或多媒體框架。指派超連結的元素決定了可點擊區域:文字部份只會讓選取的文字變成可點擊,圖形或框架則會讓整個投影片物件變成可點擊。
若簡報中包含相對超連結,請參閱管理超連結基底以讀取、變更或清除文件層級的基底。
將 URL 超連結新增至文字
要將文字連結至網站,將 Hyperlink 傳遞給文字部份的 setHyperlinkClick 方法,如下所示。只有該文字部份會變成可點擊。
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
IAutoShape textShape = presentation.getSlides().get_Item(0).getShapes().addAutoShape(ShapeType.Rectangle, 100, 100, 600, 50, false);
textShape.addTextFrame("Aspose: File Format APIs");
IPortionFormat portionFormat = textShape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat();
portionFormat.setHyperlinkClick(new Hyperlink("https://www.aspose.com/"));
portionFormat.getHyperlinkClick().setTooltip("Explore Aspose file format APIs");
portionFormat.setFontHeight(32);
presentation.save("presentation-out.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
將 URL 超連結新增至圖形與多媒體框架
對矩形呼叫 setHyperlinkClick 即可使其整個區域可點擊。使用 setTooltip 在游標懸停矩形時顯示目的地提示。這兩個方法同樣適用於圖片、音訊與影片框架。
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
IAutoShape shape = presentation.getSlides().get_Item(0).getShapes().addAutoShape(ShapeType.Rectangle, 100, 100, 600, 50);
shape.setHyperlinkClick(new Hyperlink("https://www.aspose.com/"));
shape.getHyperlinkClick().setTooltip("Explore Aspose file format APIs");
presentation.save("presentation-out.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
使用超連結建立目錄
內部超連結讓讀者從目錄跳至特定投影片。以下範例使用 setInternalHyperlinkClick,將第一張投影片上「第 2 頁」文字連結至第二張投影片。
import com.aspose.slides.*;
import android.graphics.Color;
Presentation presentation = new Presentation();
try {
ISlide firstSlide = presentation.getSlides().get_Item(0);
ISlide secondSlide = presentation.getSlides().addEmptySlide(firstSlide.getLayoutSlide());
IAutoShape tableOfContents = firstSlide.getShapes().addAutoShape(ShapeType.Rectangle, 40, 40, 300, 100);
tableOfContents.getFillFormat().setFillType(FillType.NoFill);
tableOfContents.getLineFormat().getFillFormat().setFillType(FillType.NoFill);
tableOfContents.getTextFrame().getParagraphs().clear();
Paragraph paragraph = new Paragraph();
paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
paragraph.setText("Title of slide 2 .......... ");
Portion linkPortion = new Portion();
linkPortion.setText("Page 2");
linkPortion.getPortionFormat().getHyperlinkManager().setInternalHyperlinkClick(secondSlide);
paragraph.getPortions().add(linkPortion);
tableOfContents.getTextFrame().getParagraphs().add(paragraph);
presentation.save("link_to_slide.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
格式化超連結
您可以變更已連結文字的顏色、在讀者啟動連結時播放或停止音效,以及設定懸停時顯示的工具提示。
顏色
IHyperlink 的 setColorSource 方法決定超連結是使用簡報的超連結顏色,還是使用文字部份的格式。若要套用自訂文字顏色,請選取 HyperlinkColorSource.PortionFormat 並設定部份的填色。此功能在 PowerPoint 2019 中引入;較早版本不會套用此設定。
在此範例中,setColorSource 使第一個連結使用文字部份的紅色填色。第二個連結則保留簡報的超連結顏色。
import com.aspose.slides.*;
import android.graphics.Color;
Presentation presentation = new Presentation();
try {
IAutoShape coloredShape = presentation.getSlides().get_Item(0).getShapes().addAutoShape(ShapeType.Rectangle, 100, 100, 450, 50, false);
coloredShape.addTextFrame("This hyperlink uses a custom color.");
IPortionFormat coloredPortionFormat = coloredShape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat();
coloredPortionFormat.setHyperlinkClick(new Hyperlink("https://www.aspose.com/"));
coloredPortionFormat.getHyperlinkClick().setColorSource(HyperlinkColorSource.PortionFormat);
coloredPortionFormat.getFillFormat().setFillType(FillType.Solid);
coloredPortionFormat.getFillFormat().getSolidFillColor().setColor(Color.RED);
IAutoShape defaultShape = presentation.getSlides().get_Item(0).getShapes().addAutoShape(ShapeType.Rectangle, 100, 200, 450, 50, false);
defaultShape.addTextFrame("This hyperlink uses the default color.");
defaultShape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat().setHyperlinkClick(new Hyperlink("https://www.aspose.com/"));
presentation.save("presentation-out-hyperlink.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
音效
超連結可以在被啟動時播放音效,或停止已在播放的音效。使用下列方法來設定這些行為:
- IHyperlink.setSound 指定與超連結相關聯的音訊。
- IHyperlink.setStopSoundOnClick 決定啟動超連結時是否停止先前的音效。
新增超連結音效
第一張投影片上的按鈕使用 IHyperlink.setSound 在讀者點擊以前進時播放 sampleaudio.wav。第二張投影片上的另一個按鈕則使用 IHyperlink.setStopSoundOnClick 停止播放,並讓讀者停留在該投影片。
import com.aspose.slides.*;
import java.io.IOException;
import java.io.FileInputStream;
Presentation presentation = new Presentation();
try {
IAudio hyperlinkSound;
try (FileInputStream audioStream = new FileInputStream("sampleaudio.wav")) {
hyperlinkSound = presentation.getAudios().addAudio(audioStream);
}
ISlide firstSlide = presentation.getSlides().get_Item(0);
IAutoShape playButton = firstSlide.getShapes().addAutoShape(ShapeType.SoundButton, 100, 100, 100, 50);
playButton.setHyperlinkClick(Hyperlink.getNextSlide());
if (!playButton.getHyperlinkClick().getStopSoundOnClick() && playButton.getHyperlinkClick().getSound() == null)
{
playButton.getHyperlinkClick().setSound(hyperlinkSound);
}
ISlide secondSlide = presentation.getSlides().addEmptySlide(firstSlide.getLayoutSlide());
IAutoShape stopButton = secondSlide.getShapes().addAutoShape(ShapeType.Rectangle, 100, 100, 100, 50);
stopButton.setHyperlinkClick(Hyperlink.getNoAction());
stopButton.getHyperlinkClick().setStopSoundOnClick(true);
presentation.save("hyperlink-sound.pptx", SaveFormat.Pptx);
} catch (IOException exception) {
System.out.println("Unable to read the audio file: " + exception.getMessage());
} finally {
presentation.dispose();
}
擷取超連結音效
以下範例開啟前述建立的簡報,並透過 getSound 與 getBinaryData 讀取第一個圖形的超連結音訊至記憶體。
import com.aspose.slides.*;
Presentation presentation = new Presentation("hyperlink-sound.pptx");
try {
if (presentation.getSlides().size() > 0 && presentation.getSlides().get_Item(0).getShapes().size() > 0) {
IHyperlink hyperlink = presentation.getSlides().get_Item(0).getShapes().get_Item(0).getHyperlinkClick();
IAudio sound = hyperlink == null ? null : hyperlink.getSound();
if (sound != null) {
byte[] audioData = sound.getBinaryData();
System.out.println("Extracted " + audioData.length + " bytes of hyperlink audio.");
} else {
System.out.println("The first shape has no hyperlink sound.");
}
} else {
System.out.println("The presentation has no first slide or shape to inspect.");
}
} finally {
presentation.dispose();
}
工具提示與互動設定
在將超連結指派給文字或圖形後,您可以呼叫下列 IHyperlink 方法:
- setTooltip 設定觀眾在懸停時可顯示的提示文字。
- setTargetFrame (若適用)指定父 HTML frameset 中的目標框架。
- setHistory 控制啟動連結時是否將其目的地加入已檢視超連結清單。
- setHighlightClick 控制點擊時是否以高亮方式顯示超連結。
從簡報移除超連結
使用 getAnyHyperlinks 收集包括文字部份連結在內的所有超連結容器,再對其進行修改。下列範例同時移除第一張投影片上的兩種啟動方式。如僅需移除其中一種,請只呼叫 removeHyperlinkClick 或 removeHyperlinkMouseOver;移除點擊動作不會同時移除滑鼠懸停對應項。
import com.aspose.slides.*;
import java.util.ArrayList;
import java.util.List;
Presentation presentation = new Presentation("pres.pptx");
try {
if (presentation.getSlides().size() > 0) {
List<IHyperlinkContainer> containers = new ArrayList<>();
for (IHyperlinkContainer container : presentation.getSlides().get_Item(0).getHyperlinkQueries().getAnyHyperlinks()) {
containers.add(container);
}
for (IHyperlinkContainer container : containers) {
container.getHyperlinkManager().removeHyperlinkClick();
container.getHyperlinkManager().removeHyperlinkMouseOver();
}
presentation.save("pres-removed-hyperlinks.pptx", SaveFormat.Pptx);
} else {
System.out.println("The presentation has no slides to process.");
}
} finally {
presentation.dispose();
}
若要無條件移除,removeAllHyperlinks 會在選取範圍內一次呼叫即可移除兩種啟動方式。若需針對母片、版面配置與備註進行選擇性清理,請參閱報告、清理與驗證超連結。
在取代 SmartArt 文字前清除超連結
當您在既有 SmartArt 節點中取代段落時,先前的超連結仍可能存在。先透過 ISmartArtNode.getTextFrame 取得段落所在的文字框。清除文字部份前,先對顯示該段落的節點圖形之文字框呼叫 getHyperlinkQueries,再於回傳物件上呼叫 removeAllHyperlinks。如此即可移除該框架內的所有超連結,包括其他段落中的連結。
此範例假設 SmartArtHyperlink.pptx 首張投影片的第三個圖形為 SmartArt 圖形,並取代第四個節點的第一段落。該節點的第一個圖形顯示此段落。
在加入兩個部份後,範例使用 getEffective 讀取其實際格式。於該格式上呼叫 getHyperlinkClick,會得到第一個部份的新超連結,而第二個部份則返回 null。
import com.aspose.slides.*;
Presentation presentation = new Presentation("SmartArtHyperlink.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
ISmartArt smartArt = (ISmartArt) slide.getShapes().get_Item(2);
ISmartArtNode node = smartArt.getAllNodes().get_Item(3);
IParagraph paragraph = node.getTextFrame().getParagraphs().get_Item(0);
node.getShapes().get_Item(0).getTextFrame().getHyperlinkQueries().removeAllHyperlinks();
paragraph.getPortions().clear();
IPortion link = new Portion("New reference");
link.getPortionFormat().setHyperlinkClick(new Hyperlink("https://example.com/new"));
paragraph.getPortions().add(link);
IPortion text = new Portion(" (updated)");
paragraph.getPortions().add(text);
System.out.println(link.getPortionFormat().getEffective().getHyperlinkClick().getExternalUrl()); // https://example.com/new
System.out.println(text.getPortionFormat().getEffective().getHyperlinkClick()); // null
} finally {
presentation.dispose();
}
建立完整的超連結清單
在分發簡報前,請先盤點其互動動作與網路連結。getAnyHyperlinks 會回傳 IHyperlinkContainer 物件,而非單純的 URL 字串。請同時檢查每個容器的 getHyperlinkClick 與 getHyperlinkMouseOver。兩者是獨立的:同一容器可同時提供兩種動作,因此完整報告可能需要每個容器兩列。
僅檢視圖形層級的超連結會遺漏附加於文字部份的連結。請改為對適當的範圍進行查詢,並保留回傳的容器,以便稍後更新或移除其動作。
查詢簡報、投影片與文字框層級
IHyperlinkQueries 介面可透過 IPresentation.getHyperlinkQueries、IBaseSlide.getHyperlinkQueries 與 ITextFrame.getHyperlinkQueries 取得。每個範圍支援相同的查詢:
- getHyperlinkClicks 回傳具有點擊動作的容器。
- getHyperlinkMouseOvers 回傳具有滑鼠懸停動作的容器。
- getAnyHyperlinks 回傳同時包含任一或兩種動作的容器。
本範例將點擊與滑鼠懸停動作指派給同一圖形與其文字,並為另一個按鈕指派巨集動作。它比較 getAnyHyperlinks 回傳的容器數量與各個啟動類型的計數。文字框查詢僅計算文字中的連結。外部圖形的動作會出現在投影片與簡報查詢中。
import com.aspose.slides.*;
class QueryCounts {
void print(String scope, IHyperlinkQueries queries) {
int clickCount = queries.getHyperlinkClicks().size();
int mouseOverCount = queries.getHyperlinkMouseOvers().size();
int anyCount = queries.getAnyHyperlinks().size();
System.out.println(scope + ": click=" + clickCount + ", mouse-over=" + mouseOverCount + ", any=" + anyCount);
}
}
QueryCounts counts = new QueryCounts();
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
ISlide destination = presentation.getSlides().addEmptySlide(slide.getLayoutSlide());
IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 20, 20, 400, 60);
shape.getTextFrame().setText("Click the text to go to slide 2");
shape.getHyperlinkManager().setExternalHyperlinkClick("https://example.com/");
shape.getHyperlinkClick().setTooltip("Public website");
shape.getHyperlinkManager().setExternalHyperlinkMouseOver("file:///C:/private/report.xlsx");
IPortionFormat portionFormat = shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat();
portionFormat.getHyperlinkManager().setInternalHyperlinkClick(destination);
portionFormat.getHyperlinkManager().setExternalHyperlinkMouseOver("https://example.com/help");
IAutoShape macroButton = slide.getShapes().addAutoShape(ShapeType.Rectangle, 20, 120, 200, 60);
macroButton.getHyperlinkManager().setMacroHyperlinkClick("ReviewPresentation");
counts.print("Presentation", presentation.getHyperlinkQueries());
counts.print("Slide 1", slide.getHyperlinkQueries());
counts.print("Text frame", shape.getTextFrame().getHyperlinkQueries());
presentation.save("hyperlink-audit-input.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
在此範例中,簡報與投影片查詢各報告 3 個點擊容器、2 個滑鼠懸停容器與 3 個任一動作的容器。文字框查詢在每個類別各報告 1 個容器。
分類動作與目的地
在解讀目的地之前,先使用 IHyperlink.getActionType 取得動作類型。HyperlinkActionType 的值涵蓋超出網頁導覽的情況:
| Values | Meaning for an audit |
|---|---|
Hyperlink |
外部超連結;檢查 URL 與其協議。 |
JumpSpecificSlide |
內部導覽至特定投影片。 |
JumpFirstSlide, JumpPreviousSlide, JumpNextSlide, JumpLastSlide, JumpLastViewedSlide |
內建投影片秀導覽,於投影片秀環境中解析。 |
JumpEndShow, StartCustomSlideShow |
結束目前的秀或啟動自訂秀。 |
StartMacro |
執行巨集。 |
StartProgram |
啟動程式。 |
OpenFile, OpenPresentation |
開啟檔案或其他簡報;須與網頁 URL 分別審查。 |
StartStopMedia |
開始或停止媒體播放。 |
NoAction, Unknown |
無導覽動作,或未辨識的動作,需要進一步審查。 |
使用 getExternalUrl 讀取外部目的地,使用 getTargetSlide 讀取特定內部目的地。內部動作與內建指令可能沒有外部 URL;空的 URL 並不表示容器沒有動作。若 getExternalUrlOriginal 回傳值與正規化後的 URL 不同,請保留原始值;若有工具提示,亦請加入 getTooltip 回傳的文字。
報告、清理與驗證超連結
此範例開啟前述簡報,並為每個動作寫入一筆 JSON 記錄。它使用 getAnyHyperlinks 收集普通投影片、母片、版面配置與備註中的容器(包括備註母片與講義母片)。透過參考相等性計算每個容器只計一次,套用下列政策後重新開啟儲存的簡報,檢查點擊與滑鼠懸停動作。
報告會記錄從 1 開始的投影片索引與 getSlideId(若可用)。ISlideComponent.getSlide 提供支援容器的所屬投影片。母片、版面配置與備註沒有普通投影片索引,會以其範圍識別。圖形容器與文字部份格式容器分別標記;其他容器保留其執行時型別名稱。每個容器取得報告內部 ID,以便關聯其兩個動作。報告將動作類型儲存為 Java 列舉定義的整數常數。
此嚴格的應用政策僅允許絕對 HTTPS URL 與有效的內部投影片目標。它會拒絕巨集、程式、檔案動作、其他投影片秀動作、未知動作以及其他 URL 協議。這些拒絕屬於政策決策,而非 Aspose.Slides 安全性判斷。HTTPS 本身不等同於信任:請在您的應用程式中加入主機白名單與其他檢查。原始與正規化的外部 URL 皆會被檢查。此範例僅審核中繼資料,並不會跟隨連結或執行動作。
若需修正,容器的 getHyperlinkManager 支援 setExternalHyperlinkClick、removeHyperlinkClick 與 removeHyperlinkMouseOver。此處將不允許的外部點擊連結替換為固定的 HTTPS 登陸頁面;其他不允許的點擊與滑鼠懸停動作則分別移除。將 replaceExternalClicks 設為 false 可直接移除所有政策違規。請在部署前先決定應用程式擁有的替換頁面。
報告的匯出旗標使用保守的 PDF 審查政策:將滑鼠懸停動作以及除外部連結或特定投影片跳轉以外的任何動作標記為可能不受支援。這僅為審查提示,非功能測試或保證未標記的連結在匯出後仍可使用。支援的 PDF 與 HTML 匯出可能保留超連結,取決於動作、匯出選項與檢視器。點陣圖 圖片 與 影片 無法保留互動超連結;在對這些輸出進行審核時,請將每個動作都標記。
import com.aspose.slides.*;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.io.FileOutputStream;
import android.text.TextUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
class HyperlinkAudit {
Integer slideIndex(IPresentation presentation, IBaseSlide slide) {
for (int index = 0; index < presentation.getSlides().size(); index++) {
if (presentation.getSlides().get_Item(index) == slide) return index + 1;
}
return null;
}
boolean isHttps(String value) {
if (value == null || value.isEmpty()) return false;
try {
URI uri = new URI(value);
return uri.isAbsolute() && "https".equalsIgnoreCase(uri.getScheme()) && uri.getHost() != null;
} catch (URISyntaxException exception) {
return false;
}
}
String policyViolation(IHyperlink link) {
if (link == null) return null;
if (link.getActionType() == HyperlinkActionType.JumpSpecificSlide) {
return link.getTargetSlide() == null ? "Missing target slide" : null;
}
if (link.getActionType() != HyperlinkActionType.Hyperlink) return "Action is not allowed";
if (!isHttps(link.getExternalUrl())) return "Normalized URL is not absolute HTTPS";
String original = link.getExternalUrlOriginal();
if (original != null && !original.isEmpty() && !isHttps(original)) return "Original URL is not absolute HTTPS";
return null;
}
void addScope(List<IHyperlinkContainer> found, IBaseSlide slide) {
if (slide != null) {
for (IHyperlinkContainer container : slide.getHyperlinkQueries().getAnyHyperlinks()) {
found.add(container);
}
}
}
List<IHyperlinkContainer> collectContainers(IPresentation presentation) {
List<IHyperlinkContainer> found = new ArrayList<>();
for (IHyperlinkContainer container : presentation.getHyperlinkQueries().getAnyHyperlinks()) {
found.add(container);
}
for (IMasterSlide master : presentation.getMasters()) addScope(found, master);
for (ILayoutSlide layout : presentation.getLayoutSlides()) addScope(found, layout);
for (ISlide slide : presentation.getSlides()) addScope(found, slide.getNotesSlideManager().getNotesSlide());
addScope(found, presentation.getMasterNotesSlideManager().getMasterNotesSlide());
addScope(found, presentation.getMasterHandoutSlideManager().getMasterHandoutSlide());
Set<IHyperlinkContainer> seen = Collections.newSetFromMap(new IdentityHashMap<IHyperlinkContainer, Boolean>());
List<IHyperlinkContainer> unique = new ArrayList<>();
for (IHyperlinkContainer container : found) {
if (seen.add(container)) unique.add(container);
}
return unique;
}
void addRow(List<Map<String, Object>> rows, IPresentation presentation, IHyperlink link, String activation, IHyperlinkContainer container, int containerId) {
if (link == null) return;
IBaseSlide ownerSlide = container instanceof ISlideComponent ? ((ISlideComponent) container).getSlide() : null;
ISlide targetSlide = link.getTargetSlide();
String violation = policyViolation(link);
String ownerType = container instanceof IShape ? "Shape" : container instanceof IPortionFormat ? "Text portion" : container.getClass().getSimpleName();
boolean ordinaryAction = link.getActionType() == HyperlinkActionType.Hyperlink || link.getActionType() == HyperlinkActionType.JumpSpecificSlide;
Map<String, Object> row = new LinkedHashMap<>();
row.put("ContainerId", containerId);
row.put("SlideIndex", slideIndex(presentation, ownerSlide));
row.put("SlideId", ownerSlide == null ? null : ownerSlide.getSlideId());
row.put("Scope", ownerSlide == null ? null : ownerSlide.getClass().getSimpleName());
row.put("OwnerType", ownerType);
row.put("Activation", activation);
row.put("ActionType", link.getActionType());
row.put("ExternalUrl", link.getExternalUrl());
row.put("TargetSlideIndex", slideIndex(presentation, targetSlide));
row.put("TargetSlideId", targetSlide == null ? null : targetSlide.getSlideId());
row.put("Tooltip", link.getTooltip());
row.put("OriginalExternalUrl", Objects.equals(link.getExternalUrlOriginal(), link.getExternalUrl()) ? null : link.getExternalUrlOriginal());
row.put("PotentiallyUnsafe", violation != null);
row.put("PolicyViolation", violation);
row.put("TargetExport", "PDF");
row.put("PotentiallyUnsupportedByExport", "mouse-over".equals(activation) || !ordinaryAction);
rows.add(row);
}
// 在不依賴額外 JSON 程式庫的情況下,將此報告的平面列序列化。
String jsonValue(Object value) {
if (value == null) return "null";
if (value instanceof Number || value instanceof Boolean) return value.toString();
StringBuilder escaped = new StringBuilder("\"");
for (char character : value.toString().toCharArray()) {
if (character == '"' || character == '\\') {
escaped.append('\\').append(character);
} else if (character < 0x20 || Character.isSurrogate(character)) {
escaped.append(String.format("\\u%04x", (int) character));
} else {
escaped.append(character);
}
}
return escaped.append('"').toString();
}
String toJson(List<Map<String, Object>> rows) {
List<String> objects = new ArrayList<>();
for (Map<String, Object> row : rows) {
List<String> fields = new ArrayList<>();
for (Map.Entry<String, Object> field : row.entrySet()) {
fields.add(" " + jsonValue(field.getKey()) + ": " + jsonValue(field.getValue()));
}
objects.add(" {\n" + TextUtils.join(",\n", fields) + "\n }");
}
return "[\n" + TextUtils.join(",\n", objects) + "\n]\n";
}
}
boolean replaceExternalClicks = true;
String replacementUrl = "https://example.com/blocked-link";
HyperlinkAudit audit = new HyperlinkAudit();
Presentation presentation = new Presentation("hyperlink-audit-input.pptx");
try {
List<IHyperlinkContainer> containers = audit.collectContainers(presentation);
List<Map<String, Object>> rows = new ArrayList<>();
for (int index = 0; index < containers.size(); index++) {
IHyperlinkContainer container = containers.get(index);
audit.addRow(rows, presentation, container.getHyperlinkClick(), "click", container, index + 1);
audit.addRow(rows, presentation, container.getHyperlinkMouseOver(), "mouse-over", container, index + 1);
}
String json = audit.toJson(rows);
byte[] jsonData = json.getBytes(StandardCharsets.UTF_8);
try (FileOutputStream reportStream = new FileOutputStream("hyperlink-audit.json")) {
reportStream.write(jsonData);
}
for (IHyperlinkContainer container : containers) {
IHyperlink click = container.getHyperlinkClick();
if (audit.policyViolation(click) != null) {
if (replaceExternalClicks && click.getActionType() == HyperlinkActionType.Hyperlink) {
container.getHyperlinkManager().setExternalHyperlinkClick(replacementUrl);
} else {
container.getHyperlinkManager().removeHyperlinkClick();
}
}
if (audit.policyViolation(container.getHyperlinkMouseOver()) != null) {
container.getHyperlinkManager().removeHyperlinkMouseOver();
}
}
presentation.save("hyperlink-sanitized.pptx", SaveFormat.Pptx);
Presentation reopened = new Presentation("hyperlink-sanitized.pptx");
try {
List<IHyperlinkContainer> remainingContainers = audit.collectContainers(reopened);
int violations = 0;
for (IHyperlinkContainer container : remainingContainers) {
if (audit.policyViolation(container.getHyperlinkClick()) != null) violations++;
if (audit.policyViolation(container.getHyperlinkMouseOver()) != null) violations++;
}
System.out.println("Audit rows: " + rows.size() + "; prohibited actions after reopening: " + violations);
if (violations != 0) {
System.out.println("Verification failed: do not distribute the saved presentation.");
}
} finally {
reopened.dispose();
}
} catch (IOException exception) {
System.out.println("Unable to write the audit report: " + exception.getMessage());
} finally {
presentation.dispose();
}
使用上述產生的輸入,報告包含五筆動作列。檔案滑鼠懸停連結與巨集點擊被移除,而 HTTPS 連結與內部投影片導覽則保留。驗證階段列印零筆違規動作。若輸入包含不允許的外部點擊 URL,亦會觸發替換分支。帶有允許點擊且不允許滑鼠懸停的容器會保留其點擊動作。
此選擇性清理不同於 removeAllHyperlinks,後者會在選取範圍內不分政策全部移除兩種啟動方式。此處的驗證僅檢查超連結動作;它不會移除嵌入的 VBA 專案、OLE 物件或其他活性內容,也不會驗證匯出的 PDF 或 HTML 檔案。
常見問題
如何將連結指向某個節或其第一張投影片?
PowerPoint 中的節會將投影片分組,但內部超連結只能針對單一投影片。若要導覽至節,請將連結指向該節的第一張投影片。
我可以將超連結附加在母片元素上,讓所有投影片都生效嗎?
可以。母片與版面配置的元素支援超連結。這些元素上的連結會在使用相應母片或版面配置的投影片放映時可用。
匯出為 PDF、HTML、圖片或影片時,超連結會保留嗎?
支援的 PDF 與 HTML 匯出可能保留超連結;點陣圖與影片則不會。請參閱 報告、清理與驗證超連結 中的匯出考量。