Hi,
We are trying to insert some HTML fragment which contains hyperlinks into a Word document using Aspose.Words for Java 14.12.0. However, we encountered some issue with the inserted hyperlinks.
The following is the sample code we used and I am also attaching the test source DOCX file and the output DOCX file. The key point here is we want to ensure the inserted HTML fragment using the same format as other content in the document, so we used DocumentBuilder.insertHtml(htmlString, true) as highlighted below. As seen in the output document, the inserted hyperlink is NOT shown as a hyperlink (not in blue color or underlined).
Document doc = new Document("TestHTMLInsertion.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToSection(0);
builder.moveToParagraph(1, 0);
builder.insertHtml("<a href=\"http://www.aspose.com\">http://www.aspose.com</a>", true);
doc.save("TestHTMLInsertionUseBuilerFormat.docx");
We tried to use DocumentBuilder.insertHtml(htmlString, false) as below. The inserted hyperlink is shown as a hyperlink (in blue color and underlined), however the format (e.g., font family) is not what we want. I am attaching this alternative output file as well.
Document doc = new Document("TestHTMLInsertion.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToSection(0);
builder.moveToParagraph(1, 0);
builder.insertHtml("<a href=\"http://www.aspose.com\">http://www.aspose.com</a>", false);
doc.save("TestHTMLInsertionNotUseBuilerFormat.docx");
We believe this is a defect when hyperlinks are inserted to a document with the second parameter of insertHtml() is set to true.
Could you help try this and let us know when this could be fixed or if there is a workaround?
Appreciate your help!
Hi Laurie,
Thanks for your inquiry. You are facing the expected behavior of Aspose.Words in both cases.
When useBuilderFormatting is false, DocumentBuilder formating is ignored and formatting of inserted text is based on default HTML formatting. As a result, the text looks as it is rendered in browsers.
When useBuilderFormatting is true, formatting of inserted text is based on DocumentBuilder formatting, and the text looks as if it were inserted with write method.
Please use the following code example get the required output for first case.
Document doc = new Document(MyDir + "TestHTMLInsertion.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToSection(0);
builder.moveToParagraph(1, 0);
// Specify font formatting for the hyperlink.
builder.getFont().setColor(Color.BLUE);
builder.getFont().setUnderline(Underline.SINGLE);
builder.insertHtml("<a href=\"http://www.aspose.com\">http://www.aspose.com</a>", true);
// Revert to default formatting.
builder.getFont().clearFormatting();
doc.save(MyDir + "Out.docx");
Thanks for your reply!
However, you may be simplifying our scenario. In our case, the insert HTML fragment could contain hyperlinks, but not just hyperlinks. This means hyperlinks are included with other HTML content. In this case, how could we just make the hyperlinks in blue and underlined?
Hi Laurie,
Thanks for your inquiry. After inserting the html into document using insertHtml method, please use the following code snippet to change the color of hyperlink. Hope this helps you. Please let us know if you have any more queries.
for (com.aspose.words.Field field : doc.getRange().getFields())
{
if (field.getType() == FieldType.FIELD_HYPERLINK)
{
Node currentNode = field.getStart();
field.getStart().getFont().setColor(Color.BLUE);
field.getStart().getFont().setUnderline(Underline.SINGLE);
while (currentNode != field.getEnd())
{
if (currentNode.getNodeType() == NodeType.RUN)
{
((Run)currentNode).getFont().setColor(Color.BLUE);
((Run)currentNode).getFont().setUnderline(Underline.SINGLE);
}
currentNode = currentNode.nextPreOrder(doc);
}
}
}
doc.save(MyDir + "Out.docx");
Hi Tahir,
Thanks for your reply!
This works. However we have some further inquiry: is it possible to only run through the code you provided above to the inserted HTML fragment since 1) we may not want to touch any hyperlinks before the HTML fragment is inserted; 2) better performance if the document is big?
Thank you!
Hi Laurie,
Thanks
for your inquiry. In your case, I suggest you please load the html into separate Document and insert into your main document using the code example shared in following documentation link.
https://docs.aspose.com/words/java/insert-and-append-documents/
Following code example shows how to load the html document into Aspose.Words DOM and insert it into the end of main document. Hope this helps you. Please let us know if you have any more queries.
LoadOptions options = new LoadOptions();
options.setLoadFormat(LoadFormat.HTML);
Document htmldoc = new Document(MyDir + "in.html", options);
for (com.aspose.words.Field field : htmldoc.getRange().getFields())
{
if (field.getType() == FieldType.FIELD_HYPERLINK)
{
Node currentNode = field.getStart();
field.getStart().getFont().setColor(Color.BLUE);
field.getStart().getFont().setUnderline(Underline.SINGLE);
while (currentNode != field.getEnd())
{
if (currentNode.getNodeType() == NodeType.RUN)
{
((Run)currentNode).getFont().setColor(Color.BLUE);
((Run)currentNode).getFont().setUnderline(Underline.SINGLE);
}
currentNode = currentNode.nextPreOrder(htmldoc);
}
}
}
Document doc = new Document(MyDir + "in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToDocumentEnd();
insertDocument(doc.getLastSection().getBody().getLastParagraph(), htmldoc);
doc.save(MyDir + "Out.docx");
Thanks for your reply!
We tried your suggestion, however, the inserted html does not seem following the format of the target document, e.g., the font family, etc. Is there a way to achieve that just as using DocumentBuilder.insertHtml(String, true)?
Hi Laurie,
Thanks for your inquiry. Please check the detail of DocumentBuilder.insertHtml method from here.
It would be great if you please share following detail for investigation purposes.
-
Please attach your input Word document.
-
Please create a standalone/runnable simple Java application that demonstrates the code (Aspose.Words code) you used to generate your output document
-
Please attach the output Word file that shows the undesired behavior.
-
Please attach your target Word document showing the desired behavior. You can
use Microsoft Word to create your target Word document. I will investigate as to how you are expecting your final document be generated like.
Unfortunately, it is difficult to say what the problem is without the Document(s) and
simplified application. We need your Document(s) and simple project to reproduce the problem. As soon as you get these pieces of information to us we’ll start our investigation into your issue.
You can use the documents I attached in my original post. All we want to do is to insert a hyperlink into the target document. And the inserted hyperlink should follow the format of the target document and highlighted in blue and underlined. For example, in the sample file I attached, the inserted hyperlink should be in font Calibri, size 11, blue, and underlined.
Hi Laurie,
Thanks for your inquiry. Please use the following code example to achieve your requirements. I have attached the sample input html and output document with this post for your kind reference. Hope this helps you.
Document doc = new Document(MyDir + "TestHTMLInsertion.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToDocumentEnd();
builder.startBookmark("BookmarkHtml");
builder.insertHtml(ReadAllText(MyDir + "in.html"), true);
builder.endBookmark("BookmarkHtml");
ArrayList fields = new ArrayList();
Bookmark bm = doc.getRange().getBookmarks().get("BookmarkHtml");
Node currentNode = bm.getBookmarkStart();
while (currentNode != bm.getBookmarkEnd())
{
if (currentNode.getNodeType() == NodeType.FIELD_START && ((FieldStart)currentNode).getField().getType() == FieldType.FIELD_HYPERLINK)
fields.add(((FieldStart)currentNode).getField());
currentNode = currentNode.nextPreOrder(doc);
}
for (com.aspose.words.Field field : (Iterable)fields)
{
currentNode = field.getStart();
field.getStart().getFont().setColor(Color.BLUE);
field.getStart().getFont().setUnderline(Underline.SINGLE);
while (currentNode != field.getEnd())
{
if (currentNode.getNodeType() == NodeType.RUN)
{
((Run)currentNode).getFont().setColor(Color.BLUE);
((Run)currentNode).getFont().setUnderline(Underline.SINGLE);
}
currentNode = currentNode.nextPreOrder(doc);
}
}
doc.save(MyDir + "Out.docx");
Thank you for the solution you provided! This works for us.
Hi Laurie,
Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.