• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
1

How to Get and Set Hyperlink Alt Text Programmatically in InDesign SDK

Community Beginner ,
Nov 25, 2024 Nov 25, 2024

Copy link to clipboard

Copied

Hello everyone,

I'm working on a project using the InDesign SDK, and I need to programmatically get and set the alt text for hyperlinks. I found some interfaces like IHyperlinkCmdData and IHyperlinkTable, but I'm having trouble figuring out how to retrieve or modify the alt text for a specific hyperlink.

Does anyone have an example snippet or guidance on how to achieve this?

Here's what I've tried so far

 

int32 hyperlinkCountint = hyperlinkTable->GetHyperlinkCount();

UID hyperlinkUID = hyperlinkTable->GetNthHyperlinkSource(n); // n = 1 

TI_CHECKED_NEW_CMD(myNewCmd, kNewHyperlinkCmdBoss); 
// custom macro defintion below
#define TI_CHECKED_NEW_CMD(theVar, theCmdBoss) \
    InterfacePtr<ICommand> theVar(CmdUtils::CreateCommand(theCmdBoss));\
    if (theVar == nil) {\
        break;\
    }

TI_CHECKED_INTERFACE(IHyperlinkCmdData, myNewCmdData, myNewCmd, IID_IHYPERLINKCMDDATA); 
// custom macro defintion below
#define TI_CHECKED_INTERFACE(theTypeSymbol, theVar, theBase, theInterface) \
    InterfacePtr<theTypeSymbol> theVar(theBase, theInterface);\
    if (theVar == nil) {\
        break;\
    }

InterfacePtr<IHyperlinkDestination> myDestination(db, hyperlinkUID,UseDefaultIID());

UIDRef myResult;
myResult = UIDRef(db, hyperlinkUID);

myNewCmdData->SetHyperlinkDestination(myResult);

PMString hyperlinkaltText = myNewCmdData->GetHyperlinkAltText();

 

 

My questions:

  1. How can I retrieve the alt text for a specific existing hyperlink?
  2. How can I set or update the alt text for that hyperlink?

 

If there's a better approach or additional steps I need to take, please let me know. A code snippet or reference would be greatly appreciated!

 

Thanks in advance for your help! 😊

TOPICS
SDK

Views

44

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 26, 2024 Nov 26, 2024

Copy link to clipboard

Copied

LATEST

The following code can be used to set the alt text. But I am not sure how to read the existing alt text.

IActiveContext* acn = GetExecutionContextSession()->GetActiveContext();
IDocument* doc = acn->GetContextDocument();
IDataBase* myDB = ::GetDataBase(doc);


InterfacePtr<IHyperlinkTable> hyperlinkTable(myDB, myDB->GetRootUID(), IID_IHYPERLINKTABLE);

int32 numHyperlinks = hyperlinkTable->GetHyperlinkCount();
for (int32 i = 0; i < numHyperlinks; ++i) {
const UID myHyperlinkId(hyperlinkTable->GetNthHyperlink(i));
UIDRef itemRef = UIDRef(myDB, myHyperlinkId);
UIDList itemList(itemRef);
InterfacePtr<IHyperlink> myHyperlink(myDB, myHyperlinkId, UseDefaultIID());
if (!myHyperlink) continue;

InterfacePtr<ICommand> myNewCmd(CmdUtils::CreateCommand(kChangeHyperlinkCmdBoss));
if (myNewCmd == nil) {
break;
}
myNewCmd->SetItemList(itemList);
InterfacePtr<IHyperlinkCmdData> myNewCmdData(myNewCmd, IID_IHYPERLINKCMDDATA);
if (myNewCmdData == nil) {
break;
}
ErrorCode err = kSuccess;

PMString text = "abc";
myNewCmdData->SetHyperlinkAltText(text);
err = CmdUtils::ProcessCommand(myNewCmd);

}



Does anybody have an idea how to read the existing alt text?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines