Gestisci i collegamenti ipertestuali della presentazione in C++
Introduzione
Un collegamento ipertestuale è un riferimento a un oggetto, dati o a una posizione in qualcosa. Questi sono collegamenti ipertestuali comuni nelle presentazioni PowerPoint:
- Collegamenti a siti web all’interno di testi, forme o media
- Collegamenti a diapositive
Aspose.Slides per C++ consente di eseguire molte operazioni relative ai collegamenti ipertestuali nelle presentazioni.
Aggiungi Collegamenti URL
Aggiungi Collegamenti URL al Testo
Questo codice C++ mostra come aggiungere un collegamento a un sito web a un testo:
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);
Aggiungi Collegamenti URL a Forme o Riquadri
Questo esempio di codice in C++ mostra come aggiungere un collegamento a un sito web a una forma:
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);
Aggiungi Collegamenti URL a Media
Aspose.Slides consente di aggiungere collegamenti a immagini, file audio e video.
Questo esempio di codice mostra come aggiungere un collegamento a un'immagine:
auto pres = System::MakeObject<Presentation>();
auto shapes = pres->get_Slides()->idx_get(0)->get_Shapes();
// Aggiunge immagine alla presentazione
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);
Questo esempio di codice mostra come aggiungere un collegamento a un file audio:
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);
Questo esempio di codice mostra come aggiungere un collegamento a un video:
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);
Consiglio
Potresti voler vedere Gestisci OLE.Usa i Collegamenti per Creare un Indice
Poiché i collegamenti consentono di aggiungere riferimenti a oggetti o posizioni, è possibile usarli per creare un indice.
Questo esempio di codice mostra come creare un indice con collegamenti ipertestuali:
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);
Formatta i Collegamenti
Colore
Con i metodi set_ColorSource() e get_ColorSource() nell’interfaccia IHyperlink è possibile impostare il colore dei collegamenti e recuperare le informazioni sul colore. La funzionalità è stata introdotta per la prima volta in PowerPoint 2019, quindi le modifiche relative alla proprietà non si applicano alle versioni precedenti di PowerPoint.
Questo esempio di codice dimostra un’operazione in cui collegamenti con colori diversi vengono aggiunti alla stessa diapositiva:
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);
Rimuovi i Collegamenti dalle Presentazioni
Rimuovi i Collegamenti dal Testo
Questo codice C++ mostra come rimuovere il collegamento ipertestuale da un testo in una diapositiva della presentazione:
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);
Rimuovi i Collegamenti da Forme o Riquadri
Questo codice C++ mostra come rimuovere il collegamento ipertestuale da una forma in una diapositiva della presentazione:
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);
Collegamento Ipertestuale Mutabile
La classe Hyperlink è mutabile. Con questa classe, è possibile modificare i valori per questi metodi:
- IHyperlink::set_TargetFrame()
- IHyperlink::set_Tooltip()
- IHyperlink.set_History()
- IHyperlink.set_HighlightClick()
- IHyperlink.set_StopSoundOnClick()
Il frammento di codice mostra come aggiungere un collegamento a una diapositiva e modificare successivamente il tooltip:
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);
Metodi Supportati in IHyperlinkQueries
È possibile accedere a IHyperlinkQueries da una presentazione, diapositiva o testo per cui è definito il collegamento.
- IPresentation::get_HyperlinkQueries()
- IBaseSlide::get_HyperlinkQueries()
- ITextFrame::get_HyperlinkQueries()
La classe IHyperlinkQueries supporta questi metodi:
- IHyperlinkQueries::GetHyperlinkClicks()
- IHyperlinkQueries::GetHyperlinkMouseOvers()
- IHyperlinkQueries::GetAnyHyperlinks()
- IHyperlinkQueries::RemoveAllHyperlinks()
FAQ
Come posso creare una navigazione interna non solo a una diapositiva, ma a una “sezione” o alla prima diapositiva di una sezione?
Le sezioni in PowerPoint raggruppano le diapositive; la navigazione tecnicamente punta a una diapositiva specifica. Per “navigare a una sezione”, di solito si collega alla sua prima diapositiva.
Posso allegare un collegamento ipertestuale agli elementi della diapositiva master in modo che funzioni su tutte le diapositive?
Sì. Gli elementi del master e dei layout supportano i collegamenti ipertestuali. Tali collegamenti appaiono nelle diapositive figlie e sono cliccabili durante la presentazione.
I collegamenti ipertestuali saranno conservati durante l’esportazione in PDF, HTML, immagini o video?
In PDF e HTML, sì — i collegamenti sono generalmente mantenuti. Quando si esporta in immagini e video, la cliccabilità non viene trasferita a causa della natura di quei formati (frame raster/video non supportano collegamenti).