From 053c9d9abebfb454a81894b67eb2d4b38a21b073 Mon Sep 17 00:00:00 2001 From: Oleksandr Oboznyi Date: Wed, 8 Nov 2023 11:57:59 +0200 Subject: [PATCH] Fix external hyperlinks, fix ordered lists --- .../xml2docx/generator/DocxGenerator.java | 37 ++++++++++++++----- src/main/xsl/html2docx/baseProcessing.xsl | 6 +-- src/main/xsl/html2docx/get-style-name.xsl | 9 ++++- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/wordinator/xml2docx/generator/DocxGenerator.java b/src/main/java/org/wordinator/xml2docx/generator/DocxGenerator.java index ba2e990..3c0560e 100644 --- a/src/main/java/org/wordinator/xml2docx/generator/DocxGenerator.java +++ b/src/main/java/org/wordinator/xml2docx/generator/DocxGenerator.java @@ -46,6 +46,7 @@ import org.apache.poi.xwpf.usermodel.XWPFNum; import org.apache.poi.xwpf.usermodel.XWPFNumbering; import org.apache.poi.xwpf.usermodel.XWPFParagraph; +import org.apache.poi.xwpf.usermodel.XWPFRelation; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.apache.poi.xwpf.usermodel.XWPFStyle; import org.apache.poi.xwpf.usermodel.XWPFStyles; @@ -2154,23 +2155,39 @@ private void makeHyperlink(XWPFParagraph para, XmlCursor cursor) throws DocxGene // Set the appropriate target: + XWPFHyperlinkRun hyperlinkRun; + if (href.startsWith("#")) { // Just a fragment ID, must be to a bookmark String bookmarkName = href.substring(1); hyperlink.setAnchor(bookmarkName); + cursor.push(); + hyperlinkRun = makeHyperlinkRun(hyperlink, cursor, para); + cursor.pop(); } else { - // Create a relationship that targets the href and use the - // relationship's ID on the hyperlink - // It's not yet clear from the POI API how to create a new relationship for - // use by an external hyperlink. - // throw new NotImplementedException("Links to external resources not yet implemented."); - } + // Add the link as External relationship + String id = para.getDocument().getPackagePart().addExternalRelationship(href, XWPFRelation.HYPERLINK.getRelation()).getId(); - cursor.push(); - XWPFHyperlinkRun hyperlinkRun = makeHyperlinkRun(hyperlink, cursor, para); - cursor.pop(); - para.addRun(hyperlinkRun); + // Append the link and bind it to the relationship + hyperlink.setId(id); + + // Create the linked text + String linkedText = cursor.getTextValue(); + CTText ctText = CTText.Factory.newInstance(); + ctText.setStringValue(linkedText); + CTR ctr = CTR.Factory.newInstance(); + ctr.setTArray(new CTText[]{ctText}); + + // Create the formatting + CTRPr rpr = ctr.addNewRPr(); + rpr.addNewRStyle().setVal("Hyperlink"); + // Insert the linked text into the link + hyperlink.setRArray(new CTR[]{ctr}); + + hyperlinkRun = new XWPFHyperlinkRun(hyperlink, CTR.Factory.newInstance(), para); + } + para.addRun(hyperlinkRun); } /** diff --git a/src/main/xsl/html2docx/baseProcessing.xsl b/src/main/xsl/html2docx/baseProcessing.xsl index 22de534..c1ba5e7 100644 --- a/src/main/xsl/html2docx/baseProcessing.xsl +++ b/src/main/xsl/html2docx/baseProcessing.xsl @@ -141,10 +141,8 @@ - - - - + + diff --git a/src/main/xsl/html2docx/get-style-name.xsl b/src/main/xsl/html2docx/get-style-name.xsl index 54be602..2fa7f1b 100644 --- a/src/main/xsl/html2docx/get-style-name.xsl +++ b/src/main/xsl/html2docx/get-style-name.xsl @@ -87,7 +87,14 @@ - + + + + + + + +