Adding URL fragment to AttachmentResourceReference

Hi.

I am creating a link to an attachment in my Java macro.
Is it possible to add an URL fragment (like #page=3) to an attachment link?

I would like to do something like this:

XWikiAttachment attachment = sourceDocument.getAttachment("sample.pdf);
String ref = entityReferenceSerializer.serialize(attachment.getReference());
AttachmentResourceReference resource = new AttachmentResourceReference(ref);

resource.setParameter("anchor","page=3");

return List.of(new LinkBlock(List.of(new WordBlock("Show")), resource, false));

However, this approach did not work.
I tried adding the anchor parameter to an attachment link in wiki syntax like this:

//Does work
[[MyLink>>doc:MySpace.MyPage.WebHome||queryString="param1=val1" anchor="test"]]

//Does not work
[[attach:sample.pdf||target="_blank" anchor="test"]]

I am assuming this means an attachment link does not support the parameters queryString and anchor, while other types of links (like a DocumentResourceReference) do support these parameters.

Is there another possibility to add an URL fragment to an attachment link?

From what I can see, AttachmentResourceReference does expose get/setAnchor and get/setQueryString APIs, just like DocumentResourceReference.

Howether, the code suggests that the anchor is not really taken into account when generating the URL, but the query string is.

The issue for that seems to be Loading... .

I guess a possible alternative would be directly pass the final href value through the path reference type (which main purpose is to allow directly passing the final href), as in [[sample.pdf>>path:/xwiki/bin/download/MySpace/MyPage/WebHome/sample.pdf#anchor]].

It does, see xwiki-rendering/xwiki-rendering-api/src/main/java/org/xwiki/rendering/listener/reference/AttachmentResourceReference.java at 26bc8eb7f28a47775fe092edd46547efb384498c · xwiki/xwiki-rendering · GitHub

You need to call setAnchor().

I’ve debugged it and it’s a current limitation that we should support and implementation.

Details:

Then it would work.

The issue is Loading...

Thx