Docx4j ImportXHTML:图像周围的超链接不起作用
我想使用 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(" ", "\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 :
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们需要添加对导入图像周围的超链接的支持。
现在跟踪 https://github.com/plutext/docx4j-ImportXHTML/issues/83
超链接图像包含 a:hlinkClick 元素,例如:
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: