We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Word to Pdf Conversion - Change the hyperlink text to blue color

Hi,

I am trying to convert a word document to pdf. The additional requirement is to make all the hyperlinks in the converted pdf to be in blue color. When I try to manually change the color using the below code:

public static void formatWordHyperlinks(com.aspose.words.Document doc) {
    try {
        NodeList fieldStarts = doc.selectNodes("//FieldStart");
        for (FieldStart fieldStart : (Iterable) fieldStarts) {
            if (fieldStart.getFieldType() == FieldType.FIELD_HYPERLINK) {
                // Run fieldResult = (Run)fieldStart.getField().getSeparator().getNextSibling();
                // fieldResult.getFont().setColor(Color.BLUE);
                markNodeTextAsBlue(fieldStart);
            } else if (fieldStart.getFieldType() == FieldType.FIELD_REF
                    || fieldStart.getFieldType() == FieldType.FIELD_PAGE_REF
                    || fieldStart.getFieldType() == FieldType.FIELD_FOOTNOTE_REF) {
                // Run fieldResult = (Run)fieldStart.getField().getSeparator().getNextSibling();
                // fieldResult.getFont().setColor(Color.BLUE);
                markNodeTextAsBlue(fieldStart);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void markNodeTextAsBlue(Node fieldStart) throws Exception {
    Node startNode = findNextSibling(fieldStart, NodeType.FIELD_SEPARATOR);
    Node endNode = findNextSibling(startNode, NodeType.FIELD_END);
    if (startNode == null) {
        throw new IllegalStateException("Cannot find field separator.");
    }
    if ((endNode != null) && (startNode.getParentNode() != endNode.getParentNode())) {
        throw new IllegalArgumentException("Start and end nodes are expected to have the same parent.");
    }
    for (Node child = startNode; !child.equals(endNode); child = child.getNextSibling()) {
        if(child instanceof Run) {
            ((Run)child).getFont().setColor(Color.BLUE);
        }
    }
}

private static Node findNextSibling(Node startNode, int nodeType) throws Exception {
    for (Node node = startNode; node != null; node = node.getNextSibling()) {
        if (node.getNodeType() == nodeType) {
            return node;
        }
    }
    return null;
}

Version used: aspose-words-15.6.0

The color of the first reference (say to a table) changes, but the color of second reference to the same table is not changed.

Please confirm/evaluate if the code is proper or anything is missing in the code?
(This code was taken from the articles/posts available in the forum and modified for the requirement)

Thanks in advance.

Hi Manohar,

Thanks
for your inquiry. Please use following code example to change the color of hyperlink in output Pdf. Hope this helps you. If you still face problem, please share your input word
document here for testing. I will investigate the issue on my side and
provide you
more information.

Document doc = new Document(MyDir + "in.docx");
Style hypelinkStyle = doc.getStyles().getByStyleIdentifier(StyleIdentifier.HYPERLINK);
// Change color of hyperlink 
hypelinkStyle.getFont().setColor(Color.BLUE);
doc.save(MyDir + "Out.pdf");

Hi Tahir Manzoor,

Thanks a lot for the quick reply, and many thanks for suggesting a simple way to get it working.
Actually I want to set blue color for Hyperlinks, Field References, Page References, and Foot Note references in the document.
StyleIdentifier.HYPERLINK* will gets us Hyperlink style.
StyleIdentifier.FOOTNOTE_REFERENCE will get us FootNoteReference style.
But I was not able to find the constants for FieldRef and PageRef (equivalent to FieldType.FIELD_REF and FieldType.FIELD_PAGE_REF respectively)
It will be very helpful if you can suggest me to equivalent StyleIdentifier constants for these two field types.

Thanks and Regards,
Manohar

Hi Manohar,

Thanks
for your inquiry. Please use following code snippet to change the color of FieldRef and PageRef fields. Hope this helps you. If you still face problem, please share your input document here for investigation purposes.

Document doc = new Document(MyDir + "in.docx");
for (Field field : doc.getRange().getFields())
{
    if (field.getType() == FieldType.FIELD_PAGE_REF || field.getType() == FieldType.FIELD_REF)
    {
        Node currentNode = field.getStart();
        field.getStart().getFont().setColor(Color.BLUE);
        while (currentNode != field.getEnd())
        {
            if (currentNode.getNodeType() == NodeType.RUN)
            {
                ((Run)currentNode).getFont().setColor(Color.BLUE);
            }
            currentNode = currentNode.getNextSibling();
        }
    }
}

Hi Tahir Manzoor,

The color of reference field text is not set to blue. Please find the attached source document. The document consists of two references to Table 10.1.1.1 in Page 2 (in section 1.1.1 and 1.1.1.1)
But neither of the reference color is set to blue.
Following code is used to change the color, the System.out.println line prints the text required in blue color but still no change in the color of the text.

boolean isRef = false;
for (Field field : doc.getRange().getFields())
{
    if (field.getType() == FieldType.FIELD_PAGE_REF || field.getType() == FieldType.FIELD_REF)
    {
        Node currentNode = field.getStart();
        field.getStart().getFont().setColor(Color.BLUE);
        isRef = field.getType() == FieldType.FIELD_REF;
        while (currentNode != field.getEnd())
        {
            if (currentNode.getNodeType() == NodeType.RUN)
            {
                ((Run)currentNode).getFont().setColor(Color.BLUE);
                if (isRef)
                {
                    System.out.println(((Run)currentNode).getText());
                }
            }
            currentNode = currentNode.getNextSibling();
        }
    }
}

Regards,
Manohar

Hi Manohar,

Thanks
for your inquiry. I have not found the input document in this forum thread. Could you please attach your input Word document here for testing? I will investigate the issue on my side and provide you more information.

Hi Tahir Manzoor,

The
color of reference field text is not set to blue. Please find the
attached source document. The document consists of two references to
Table 10.1.1.1 in Page 2 (in section 1.1.1 and 1.1.1.1)
But neither of the reference color is set to blue.
Following code is used to change the color, the System.out.println line prints the text required in blue color but still no change in the color of the text.

boolean isRef = false;
for (Field field : doc.getRange().getFields())
{
    if (field.getType() == FieldType.FIELD_PAGE_REF || field.getType() == FieldType.FIELD_REF)
    {
        Node currentNode = field.getStart();
        field.getStart().getFont().setColor(Color.BLUE);
        isRef = field.getType() == FieldType.FIELD_REF;
        while (currentNode != field.getEnd())
        {
            if (currentNode.getNodeType() == NodeType.RUN)
            {
                ((Run)currentNode).getFont().setColor(Color.BLUE);
                if (isRef)
                {
                    System.out.println(((Run)currentNode).getText());
                }
            }
            currentNode = currentNode.getNextSibling();
        }
    }
}

Regards,
Manohar

Hi Manohar,

Thanks
for sharing the detail. Please note that Aspose.Words mimics the same behavior as MS Word does. If you change the color of Ref field using MS Word and convert the document to Pdf using MS Word, you will get the same output.

Aspose.Words and MS Word update the ref field before saving document to Pdf. If you want to change the color of Ref field to bookmark, please change the color of bookmark text e.g. T10_1_1_1. See the following code example for your kind reference.

Document doc = new Document(MyDir + "sample.docx");
Bookmark bookmark = doc.getRange().getBookmarks().get("T10_1_1_1");
Node currentnode = bookmark.getBookmarkStart();
while (!currentnode.equals(bookmark.getBookmarkEnd()))
{
    currentnode = currentnode.nextPreOrder(doc);
    if (currentnode.getNodeType() == NodeType.RUN)
        ((Run)currentnode).getFont().setColor(Color.BLUE);
}
doc.save(MyDir + "Out.pdf");

Hope this answers your query. Please let us know if you have any more queries.

Hi Tahir Manzoor,

Thanks for the reply. The sample snippet is changing the color of all the bookmark target text items.

How do I modify it to just change the color of hyperlink text which refers to the table but not the content/title of the table itself.

for (Bookmark bookmark : doc.getRange().getBookmarks())
{
    Node currentnode = bookmark.getBookmarkStart();
    while (!currentnode.equals(bookmark.getBookmarkEnd()))
    {
        currentnode = currentnode.nextPreOrder(doc);
        if (currentnode.getNodeType() == NodeType.RUN)
        {
            ((Run)currentnode).getFont().setColor(Color.BLUE);
        }
    }
}

By using the above code, the resulting document against the shared sample is as attached.
Please provide a way to change the text color of the hyperlink field and not its target item. In the result file attached, all the hyperlinks referenced titles/headings are converted to blue.

Regards,
Manohar G.N.

Hi Manohar,

Thanks
for your inquiry. There are some hidden bookmark in your document. The hidden bookmarks are started from underscore. See the attached image for detail. Please use following code example to get the required output.

The first highlighted code snippet avoid hidden bookmark to color. The second highlighted code snippet is for bookmark “T10_1_1_3”. Its contents are in multiple lines/paragraphs.

Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document(MyDir + "sample.docx");
for (Bookmark bookmark : doc.getRange().getBookmarks())
{
    if (bookmark.getName().startsWith("_"))
        continue;
    Node currentnode = bookmark.getBookmarkStart();
    while (!currentnode.equals(bookmark.getBookmarkEnd()))
    {
        currentnode = currentnode.nextPreOrder(doc);
        if (currentnode.getNodeType() == NodeType.RUN)
        {
            ((Run)currentnode).getFont().setColor(Color.BLUE);
            // for bookmark "T10_1_1_3"
            if (((Run)currentnode).getText().contains(ControlChar.LINE_BREAK))
                break;
        }
    }
}

Hi Tahir Manzoor,

Please find the attached zip file which contains the code used for hyperlink color conversion.
The same code worked in previous version of the aspose words(13.5.0.0) and its failing in the later version of the aspose words(15.11.0)
The zip file also contains the word document ued for conversion and the resulting pdf documents for different version of aspose.
The difference can be found in the resulting pdf in pages 3,4,5,8 which has references(hyperlink) to some table which is colored in blue whereas this color change fails in later versions.
The suggested code in previous posts in this thread is not working/sufficing the requirement.
Please suggest a way to get the desired output.

Regards,
Manohar G.N.

Hi Manohar,

Thanks for your inquiry. Please note that Aspose.Words mimics the same behavior as MS Word does. If you change the color of Ref fields e.g Table 10.1.1.1 and save it to Pdf using MS Word, the changed color will not reflect in output Pdf.

To change the color of Ref field to bookmark, please change the color of bookmark. Please let us know if you have any more queries.

Hi Tahir Manzoor,

Thanks for the clarification. The code changes suggested in the previous posts of this thread are not helping in achieving the desired output.
It will be helpful if you could suggest a way to achieve this.

Or is there a way to change the color of the hyperlink in the converted pdf instead of changing the color in word document? Any suggestions will be of great help.

Regards,
Manohar G.N.

Hi Manohar,

Thanks for your inquiry. Please manually change the colors of hyperlinks and reference fields in your input document using MS Word and share that document here for our reference. We will investigate how you want your final output be generated like. We will then provide you more information on this along with code.