Page 1 of 1

Problems with Bookmark/Hyperlink

PostPosted: Tue Oct 06, 2020 2:28 am
by Clicki
Hi!
I created a document that contains an overview table and then some descriptions. Each descriptions starts with a header. Now i want to link the text from some cells in the table with the headers. I already created with bookmarkRun() the bookmarks from the headers and the hyperlinks in the cell with the names from the headers (with MainDocumentPart.hyperlinkToBookmark(). When I click on the link in the document it does not navigate me to the header. Does anyone have an idea how can I fix this?
Thank you!

Re: Problems with Bookmark/Hyperlink

PostPosted: Tue Oct 06, 2020 7:42 am
by jason
Please attach sample docx and/or code sample.

Alternatively, you could create a docx which does what you want in Word, then generate corresponding code using the docx4j webapp, or Helper Word AddIn (we're waiting for a new code signing certificate right now, so to install that, you'd need to temporarily backdate your system clock).

Or you could unzip and compare the XML in this new docx (word/document.xml), with what you have right now.

Re: Problems with Bookmark/Hyperlink

PostPosted: Tue Oct 06, 2020 7:43 pm
by Clicki
Hi Jason,
thank you for your answer. I will try right now!
I start with an empty docx in which I just chaged the header styles.

So this is the part where I add in a loop the headers to my docx and where I try to set the bookmark with your method bookmarkRun():
Code: Select all
P p = addParagraph(body, styleIdHeader2, newHeader);
R r =(R)p.getContent().get(0);
String bookmark = newHeader;
bookmarkRun(p, r, bookmark, ++bookmarkId);

public static P addParagraph(Body body, String styleId, String text)
   {
      P p = new P();
      Text t = new Text();
      R r = new R();
      t.setValue(text);
      r.getContent().add(t);

      PPr pPr = factory.createPPr();
      p.setPPr(pPr);
      PPrBase.PStyle pStyle = factory.createPPrBasePStyle();
      pPr.setPStyle(pStyle);
      pStyle.setVal(styleId);
      p.getContent().add(r);
      body.getContent().add(p);
      return p;
   }

And here is the part where I create the Hyperlink in a table row:
Code: Select all
public static Tr addHyperlinkInRow(String input)
   {
      Tr tableRow = factory.createTr();
      Tc tableCell = factory.createTc();
      P p = factory.createP();
      R r = factory.createR();
         
      Hyperlink h = MainDocumentPart.hyperlinkToBookmark(input, input);
   
      r.getContent().add(h);
      p.getContent().add(r);
      tableCell.getContent().add(p);
      tableRow.getContent().add(tableCell);

      return tableRow;
   }

And then i use this method in the part where I create my table:
Code: Select all
tbl.getContent().add(addHyperlinkInRow(newHeader));


I can see the hyperlinks in my docx, it looks like it should, but it does not link me to the header.

Mayby you can find my mistake.

Re: Problems with Bookmark/Hyperlink

PostPosted: Tue Oct 06, 2020 8:07 pm
by Clicki
jason wrote:Please attach sample docx and/or code sample.

Alternatively, you could create a docx which does what you want in Word, then generate corresponding code using the docx4j webapp, or Helper Word AddIn (we're waiting for a new code signing certificate right now, so to install that, you'd need to temporarily backdate your system clock).

Or you could unzip and compare the XML in this new docx (word/document.xml), with what you have right now.


Hi Jason,
how do I create Java Code with docx4j webapp? I tried it with the demo but I can see only the XML files.
Thanks!

Re: Problems with Bookmark/Hyperlink

PostPosted: Wed Oct 07, 2020 3:02 am
by Clicki
Hello Jason,
i fixed it! My headers contains spaces and as I used the headers for the anchor value that one also contains them, but it seems like this is not allowed.
Anyway, thank you very much, it was very helpful to look at the XML.