Changing the Hyperlink style when saving as Word

Hi,

our customer reported a need to change the Hyperlinks style in the Word documents. They have documents that we convert into PDF. They want to remove the underline and change the color globally, in the output PDF that we create using Aspose.Words for Java. Is there a global/general way to do it, similar to changing the color of revision inserted text?

doc.getLayoutOptions().getRevisionOptions().setInsertedTextColor(...)

Thanks in advance for any hint,
Mariusz

@Mariusz_Pala

To change the hyperlink style globally in a Word document using Aspose.Words for Java, you can iterate through all the hyperlinks in the document and modify their formatting properties. Unfortunately, there isn’t a direct global setting for hyperlink styles, but you can achieve the desired effect by updating each hyperlink’s font properties individually.

Here’s a code snippet that demonstrates how to remove the underline and change the color of all hyperlinks in a document:

import com.aspose.words.*;

public class ChangeHyperlinkStyle {
    public static void main(String[] args) throws Exception {
        Document doc = new Document("input.docx");

        for (Field field : doc.getRange().getFields()) {
            if (field.getType() == FieldType.FieldHyperlink) {
                FieldHyperlink hyperlink = (FieldHyperlink) field;
                // Change the hyperlink's display text color
                hyperlink.getFont().setColor(Color.RED); // Set your desired color
                hyperlink.getFont().setUnderline(Underline.None); // Remove underline
            }
        }

        doc.save("output.pdf");
    }
}

In this example, replace "input.docx" with the path to your source document and adjust the color as needed. This will ensure that all hyperlinks in the document are updated before saving it as a PDF.

Please note that there are no direct global settings for hyperlink styles, and each hyperlink must be modified individually.

@Mariusz_Pala Formatting applied to hyperlinks in the document usually defined in the appropriate Hyperlink style. So you can simply adjust this style before conversion. For example see the following code:

Document doc = new Document("C:\\Temp\\in.docx");
        
Style hyperlink = doc.getStyles().getByStyleIdentifier(StyleIdentifier.HYPERLINK);
hyperlink.getFont().setUnderline(Underline.NONE);
hyperlink.getFont().setColor(Color.GREEN);
        
doc.save("C:\\Temp\\out.pdf");

Thank you, that’s really helpful.

1 Like