在 C++ 中管理簡報超連結
簡介
超連結是對某個物件、資料或位置的參考。以下是在 PowerPoint 簡報中常見的超連結:
- 文字、圖形或媒體內的網站連結
- 投影片連結
Aspose.Slides for C++ 允許您在簡報中執行許多與超連結相關的任務。
加入 URL 超連結
將 URL 超連結加入文字
此 C++ 程式碼示範如何將網站超連結加入文字:
auto presentation = System::MakeObject<Presentation>();
auto shapes = presentation->get_Slides()->idx_get(0)->get_Shapes();
auto shape = shapes->AddAutoShape(ShapeType::Rectangle, 100.0f, 100.0f, 600.0f, 50.0f, false);
shape->AddTextFrame(u"Aspose: File Format APIs");
auto portionFormat = shape->get_TextFrame()->get_Paragraphs()->idx_get(0)->get_Portions()->idx_get(0)->get_PortionFormat();
portionFormat->set_HyperlinkClick(MakeObject<Hyperlink>(u"https://www.aspose.com/"));
portionFormat->get_HyperlinkClick()->set_Tooltip(u"More than 70% Fortune 100 companies trust Aspose APIs");
portionFormat->set_FontHeight(32.0f);
presentation->Save(u"presentation-out.pptx", SaveFormat::Pptx);
將 URL 超連結加入圖形或框架
此 C++ 範例程式碼示範如何將網站超連結加入圖形:
auto pres = System::MakeObject<Presentation>();
auto shapes = pres->get_Slides()->idx_get(0)->get_Shapes();
auto shape = shapes->AddAutoShape(ShapeType::Rectangle, 100.0f, 100.0f, 600.0f, 50.0f);
shape->set_HyperlinkClick(System::MakeObject<Hyperlink>(u"https://www.aspose.com/"));
shape->get_HyperlinkClick()->set_Tooltip(u"More than 70% Fortune 100 companies trust Aspose APIs");
pres->Save(u"pres-out.pptx", SaveFormat::Pptx);
將 URL 超連結加入媒體
Aspose.Slides 允許您為圖片、音訊與影片檔案加入超連結。
此範例程式碼示範如何為 圖片 加入超連結:
auto pres = System::MakeObject<Presentation>();
auto shapes = pres->get_Slides()->idx_get(0)->get_Shapes();
// 將影像加入簡報
auto image = pres->get_Images()->AddImage(File::ReadAllBytes(u"image.png"));
// Creates picture frame on slide 1 based on previously added image
auto pictureFrame = shapes->AddPictureFrame(ShapeType::Rectangle, 10.0f, 10.0f, 100.0f, 100.0f, image);
pictureFrame->set_HyperlinkClick(MakeObject<Hyperlink>(u"https://www.aspose.com/"));
pictureFrame->get_HyperlinkClick()->set_Tooltip(u"More than 70% Fortune 100 companies trust Aspose APIs");
pres->Save(u"pres-out.pptx", SaveFormat::Pptx);
此範例程式碼示範如何為 音訊檔案 加入超連結:
auto pres = System::MakeObject<Presentation>();
auto shapes = pres->get_Slides()->idx_get(0)->get_Shapes();
auto audio = pres->get_Audios()->AddAudio(File::ReadAllBytes(u"audio.mp3"));
auto audioFrame = shapes->AddAudioFrameEmbedded(10.0f, 10.0f, 100.0f, 100.0f, audio);
audioFrame->set_HyperlinkClick(MakeObject<Hyperlink>(u"https://www.aspose.com/"));
audioFrame->get_HyperlinkClick()->set_Tooltip(u"More than 70% Fortune 100 companies trust Aspose APIs");
pres->Save(u"pres-out.pptx", SaveFormat::Pptx);
此範例程式碼示範如何為 影片 加入超連結:
auto pres = System::MakeObject<Presentation>();
auto shapes = pres->get_Slides()->idx_get(0)->get_Shapes();
auto video = pres->get_Videos()->AddVideo(File::ReadAllBytes(u"video.avi"));
auto videoFrame = shapes->AddVideoFrame(10.0f, 10.0f, 100.0f, 100.0f, video);
videoFrame->set_HyperlinkClick(MakeObject<Hyperlink>(u"https://www.aspose.com/"));
videoFrame->get_HyperlinkClick()->set_Tooltip(u"More than 70% Fortune 100 companies trust Aspose APIs");
pres->Save(u"pres-out.pptx", SaveFormat::Pptx);
Tip
您可能想查看 管理 OLE。使用超連結建立目錄
由於超連結允許您加入對物件或位置的參照,您可以利用它們建立目錄。
此範例程式碼示範如何使用超連結建立目錄:
auto presentation = System::MakeObject<Presentation>();
auto firstSlide = presentation->get_Slides()->idx_get(0);
auto secondSlide = presentation->get_Slides()->AddEmptySlide(firstSlide->get_LayoutSlide());
auto contentTable = firstSlide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 40.0f, 40.0f, 300.0f, 100.0f);
contentTable->get_FillFormat()->set_FillType(FillType::NoFill);
contentTable->get_LineFormat()->get_FillFormat()->set_FillType(FillType::NoFill);
contentTable->get_TextFrame()->get_Paragraphs()->Clear();
auto paragraph = System::MakeObject<Paragraph>();
auto paragraphFillFormat = paragraph->get_ParagraphFormat()->get_DefaultPortionFormat()->get_FillFormat();
paragraphFillFormat->set_FillType(FillType::Solid);
paragraphFillFormat->get_SolidFillColor()->set_Color(Color::get_Black());
paragraph->set_Text(u"Title of slide 2 .......... ");
auto linkPortion = System::MakeObject<Portion>();
linkPortion->set_Text(u"Page 2");
linkPortion->get_PortionFormat()->get_HyperlinkManager()->SetInternalHyperlinkClick(secondSlide);
paragraph->get_Portions()->Add(linkPortion);
contentTable->get_TextFrame()->get_Paragraphs()->Add(paragraph);
格式化超連結
顏色
使用 IHyperlink 介面的 set_ColorSource() 與 get_ColorSource() 方法,您可以設定超連結的顏色,亦可取得超連結的顏色資訊。此功能首次於 PowerPoint 2019 引入,故此屬性的變更不適用於較舊的 PowerPoint 版本。
此範例程式碼示範在同一投影片上加入不同顏色的超連結的操作:
auto presentation = System::MakeObject<Presentation>();
auto shapes = presentation->get_Slides()->idx_get(0)->get_Shapes();
auto shape1 = shapes->AddAutoShape(ShapeType::Rectangle, 100.0f, 100.0f, 450.0f, 50.0f, false);
shape1->AddTextFrame(u"This is a sample of colored hyperlink.");
auto shape1PortionFormat = shape1->get_TextFrame()->get_Paragraphs()->idx_get(0)->get_Portions()->idx_get(0)->get_PortionFormat();
shape1PortionFormat->set_HyperlinkClick(MakeObject<Hyperlink>(u"https://www.aspose.com/"));
shape1PortionFormat->get_HyperlinkClick()->set_ColorSource(HyperlinkColorSource::PortionFormat);
shape1PortionFormat->get_FillFormat()->set_FillType(FillType::Solid);
shape1PortionFormat->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Red());
auto shape2 = shapes->AddAutoShape(ShapeType::Rectangle, 100.0f, 200.0f, 450.0f, 50.0f, false);
shape2->AddTextFrame(u"This is a sample of usual hyperlink.");
auto shape2PortionFormat = shape2->get_TextFrame()->get_Paragraphs()->idx_get(0)->get_Portions()->idx_get(0)->get_PortionFormat();
shape2PortionFormat->set_HyperlinkClick(MakeObject<Hyperlink>(u"https://www.aspose.com/"));
presentation->Save(u"presentation-out-hyperlink.pptx", SaveFormat::Pptx);
從簡報中移除超連結
從文字中移除超連結
此 C++ 程式碼示範如何從簡報投影片的文字中移除超連結:
auto pres = System::MakeObject<Presentation>(u"pres.pptx");
auto slide = pres->get_Slides()->idx_get(0);
for (const auto& shape : slide->get_Shapes())
{
auto autoShape = System::AsCast<IAutoShape>(shape);
if (autoShape != nullptr)
{
for (const auto& paragraph : autoShape->get_TextFrame()->get_Paragraphs())
{
for (const auto& portion : paragraph->get_Portions())
{
auto hyperlinkManager = portion->get_PortionFormat()->get_HyperlinkManager();
hyperlinkManager->RemoveHyperlinkClick();
}
}
}
}
pres->Save(u"pres-removed-hyperlinks.pptx", SaveFormat::Pptx);
從圖形或框架中移除超連結
此 C++ 程式碼示範如何從簡報投影片的圖形中移除超連結:
auto pres = System::MakeObject<Presentation>(u"demo.pptx");
auto slide = pres->get_Slides()->idx_get(0);
for (const auto& shape : slide->get_Shapes())
{
shape->get_HyperlinkManager()->RemoveHyperlinkClick();
}
pres->Save(u"pres-removed-hyperlinks.pptx", SaveFormat::Pptx);
Mutable Hyperlink
Hyperlink 類別是可變的。使用此類別,您可以變更以下方法的值:
- IHyperlink::set_TargetFrame()
- IHyperlink::set_Tooltip()
- IHyperlink.set_History()
- IHyperlink.set_HighlightClick()
- IHyperlink.set_StopSoundOnClick()
此程式碼片段示範如何在投影片中加入超連結,並於之後編輯其工具提示:
auto presentation = System::MakeObject<Presentation>();
auto shapes = presentation->get_Slides()->idx_get(0)->get_Shapes();
auto shape = shapes->AddAutoShape(ShapeType::Rectangle, 100.0f, 100.0f, 600.0f, 50.0f, false);
shape->AddTextFrame(u"Aspose: File Format APIs");
auto shapePortionFormat = shape->get_TextFrame()->get_Paragraphs()->idx_get(0)->get_Portions()->idx_get(0)->get_PortionFormat();
shapePortionFormat->set_HyperlinkClick(MakeObject<Hyperlink>(u"https://www.aspose.com/"));
shapePortionFormat->get_HyperlinkClick()->set_Tooltip(u"More than 70% Fortune 100 companies trust Aspose APIs");
shapePortionFormat->set_FontHeight(32.0f);
presentation->Save(u"presentation-out.pptx", SaveFormat::Pptx);
IHyperlinkQueries 中支援的方法
您可以從定義了超連結的簡報、投影片或文字取得 IHyperlinkQueries。
- IPresentation::get_HyperlinkQueries()
- IBaseSlide::get_HyperlinkQueries()
- ITextFrame::get_HyperlinkQueries()
IHyperlinkQueries 類別支援以下方法:
- IHyperlinkQueries::GetHyperlinkClicks()
- IHyperlinkQueries::GetHyperlinkMouseOvers()
- IHyperlinkQueries::GetAnyHyperlinks()
- IHyperlinkQueries::RemoveAllHyperlinks()
FAQ
如何在內部導覽時不僅跳至投影片,而是跳至「區段」或區段的第一張投影片?
PowerPoint 中的區段是投影片的分組;導覽實際上是針對特定投影片。若要「導覽至區段」,通常會連結到該區段的第一張投影片。
我可以將超連結附加到母片元素,使其在所有投影片上都有效嗎?
可以。母片與版面配置元素支援超連結。這類連結會出現在子投影片上,且在投影片放映時可被點擊。
匯出為 PDF、HTML、圖片或影片時,超連結會被保留嗎?
在 PDF 與 HTML 中,會保留連結——通常會保留。匯出為 圖片 與 影片 時,因為這些格式本身不支援超連結(光柵影格/影片不具備可點擊性),連結將不會保留。