在 Java 中管理簡報超連結

簡介

超連結將簡報內容連結至網站或簡報內的某個位置。在 PowerPoint 中,超連結通常具有兩個目的:

  • 從文字、形狀或媒體框架開啟網站。
  • 導覽至其他投影片,例如從目錄。

Aspose.Slides for Java 允許您新增這些連結、控制其外觀與音效、更新其屬性,並將其移除。以下範例示範如何在個別元素上操作超連結,以及如何在簡報、投影片或文字框層級存取超連結。

新增 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將第一張投影片上的「Page 2」文字連結至第二張投影片。

import com.aspose.slides.*;
import java.awt.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();
}

格式化超連結

您可以變更已連結文字的顏色、在讀者啟動連結時播放或停止音效,並設定滑鼠懸停時顯示的提示文字。

顏色

setColorSource 方法屬於IHyperlink,用以決定超連結是使用簡報的超連結顏色還是文字部分的格式。若要套用自訂文字顏色,請選取HyperlinkColorSource.PortionFormat並設定該部分的填滿顏色。此功能於 PowerPoint 2019 中推出;較舊版本不會套用此設定。

在此範例中,setColorSource 讓第一個連結使用文字部分的紅色填滿。第二個連結則保留簡報的超連結顏色。

import com.aspose.slides.*;
import java.awt.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在讀者點擊前進時播放 sampleaudio.wav 音訊。第二張投影片上的另一個按鈕則使用IHyperlink.setStopSoundOnClick停止播放,並讓讀者停留在該投影片上。

import com.aspose.slides.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

Presentation presentation = new Presentation();
try {
    byte[] audioData = Files.readAllBytes(Paths.get("sampleaudio.wav"));
    IAudio hyperlinkSound = presentation.getAudios().addAudio(audioData);

    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 可一次呼叫移除選取範圍內的兩種啟動方式。若需選擇性清理並涵蓋母片、版面配置與備註,請參閱Report, Sanitize, and Verify Hyperlinks。

在取代 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 取得。每個範圍支援相同的查詢:

此範例將點擊與滑鼠懸停動作指派給一個形狀及其文字,並將巨集動作指派給另一個按鈕。它比較 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();
}

對於此範例,簡報與投影片的查詢各報告三個點擊容器、兩個滑鼠懸停容器,以及三個含任一動作的容器。文字框的查詢在每個類別各報告一個容器。

分類動作與目的地

使用IHyperlink.getActionType 以在解讀目的地前先判斷動作。HyperlinkActionType 的值涵蓋了超過網頁導覽的情況:

值 於稽核時的意義
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.nio.file.Files;
import java.nio.file.Paths;
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" + String.join(",\n", fields) + "\n  }");
        }
        return "[\n" + String.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);
    Files.write(Paths.get("hyperlink-audit.json"), 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 匯出可能會保留超連結;點陣圖像與影片則無法保留。請參閱Report, Sanitize, and Verify Hyperlinks 中的匯出考量。