Docx4j ImportXHTML:图像周围的超链接不起作用

发布于 2025-01-12 22:07:04 字数 1534 浏览 6 评论 0原文

我想使用 docx4j lib 在我的 Word 文档中添加图像。当单击该图像时,我想打开一个链接。但无法打开链接。此外,该链接在我的文档中可见(根据我的 HTML/CSS 应将其隐藏)。 我正在使用 xhtml 将 html 转换为 openxml。

我的图像 css 如下:

  

      <a href="https://www.google.com">
          <img id="custom_file_logo" style="width:40px;height:20px;" name="Google Logo" 
                     src="https://myxyzdomain.com/image8-2.jpg" 
                     />

我生成的文档如下所示: 输入图片此处描述

我想隐藏链接并想在单击图像后打开它。

Java代码如下:

String htmlString = "<a href=\"https://www.google.com\">\r\n"
                    + "  <img id=\"custom_file_logo\" style=\"width:40px;height:20px;\" name=\"Google Logo\" \r\n"
                    + "          src=\"https://myxyzdomain.com/image8-2.jpg\" \r\n"
                    + "             />";
            
            WordprocessingMLPackage wordPackage = WordprocessingMLPackage.createPackage();
            MainDocumentPart mainDocumentPart = wordPackage.getMainDocumentPart();
            
            XHTMLImporterImpl xHTMLImporter = new XHTMLImporterImpl(wordPackage);
            String html = convertToXhtml(htmlString);
            html = html.replace("&nbsp;", "\u00A0");
            wordPackage.getMainDocumentPart().getContent().addAll(xHTMLImporter.convert(html, null));
            File exportFile = new File("xyz.docx");
            wordPackage.save(exportFile)

I want to add a image in my Word doc using docx4j lib. When click on that image i want to open a link. But not able to open the link. Also the link is visible in my document (it should be hidden as per my HTML/CSS).
I am using xhtml to convert the html to openxml.

My css for the image is as follows :

  

      <a href="https://www.google.com">
          <img id="custom_file_logo" style="width:40px;height:20px;" name="Google Logo" 
                     src="https://myxyzdomain.com/image8-2.jpg" 
                     />

My generated doc is visible as follows :
enter image description here

I want to hide the link and want to open it after click on the image.

Java code is as follows :

String htmlString = "<a href=\"https://www.google.com\">\r\n"
                    + "  <img id=\"custom_file_logo\" style=\"width:40px;height:20px;\" name=\"Google Logo\" \r\n"
                    + "          src=\"https://myxyzdomain.com/image8-2.jpg\" \r\n"
                    + "             />";
            
            WordprocessingMLPackage wordPackage = WordprocessingMLPackage.createPackage();
            MainDocumentPart mainDocumentPart = wordPackage.getMainDocumentPart();
            
            XHTMLImporterImpl xHTMLImporter = new XHTMLImporterImpl(wordPackage);
            String html = convertToXhtml(htmlString);
            html = html.replace(" ", "\u00A0");
            wordPackage.getMainDocumentPart().getContent().addAll(xHTMLImporter.convert(html, null));
            File exportFile = new File("xyz.docx");
            wordPackage.save(exportFile)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荭秂 2025-01-19 22:07:04

我们需要添加对导入图像周围的超链接的支持。

现在跟踪 https://github.com/plutext/docx4j-ImportXHTML/issues/83

超链接图像包含 a:hlinkClick 元素,例如:

    <w:drawing>
      <wp:inline distT="0" distB="0" distL="0" distR="0" wp14:anchorId="4FD4236C" wp14:editId="010E5646">
        <wp:docPr id="1" name="Picture 1" descr="Diagram

Description automatically generated">
          <a:hlinkClick xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" r:id="rId4"/>
        </wp:docPr>

We need to add support for importing a hyperlink surrounding an image.

Now tracking at https://github.com/plutext/docx4j-ImportXHTML/issues/83

A hyperlinked image contains an a:hlinkClick element, for example:

    <w:drawing>
      <wp:inline distT="0" distB="0" distL="0" distR="0" wp14:anchorId="4FD4236C" wp14:editId="010E5646">
        <wp:docPr id="1" name="Picture 1" descr="Diagram

Description automatically generated">
          <a:hlinkClick xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" r:id="rId4"/>
        </wp:docPr>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文