Hyperlink

Hyperlink (3)

Thursday, 13 August 2020 07:49

Change Hyperlink Appearance in Word in Java

Written by support iceblue

This article demonstrates how to change the appearance of hyperlinks in a Word document using Spire.Doc for Java.

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.BreakType;
import com.spire.doc.documents.HyperlinkType;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.UnderlineStyle;
import com.spire.doc.fields.TextRange;

import java.awt.*;

public class ChangeHyperlinkAppearance {

    public static void main(String[] args) {

        //Create a Document object
        Document document = new Document();

        //Add a section
        Section section = document.addSection();

        //Add a paragraph
        Paragraph para= section.addParagraph();
        para.appendText("Regular Link: ");

        //Insert a hyperlink
        TextRange txtRange = para.appendHyperlink("www.e-iceblue.com", "www.e-iceblue.com", HyperlinkType.Web_Link);
        txtRange.getCharacterFormat().setFontName("Times New Roman");
        txtRange.getCharacterFormat().setFontSize(12f);
        para.appendBreak(BreakType.Line_Break);

        //Add a paragraph
        para = section.addParagraph();
        para.appendText("Change Color: ");

        //Insert a hyperlink and set its color to red
        txtRange = para.appendHyperlink("www.e-iceblue.com", "www.e-iceblue.com", HyperlinkType.Web_Link);
        txtRange.getCharacterFormat().setFontName("Times New Roman");
        txtRange.getCharacterFormat().setFontSize(12f);
        txtRange.getCharacterFormat().setTextColor(Color.RED);
        para.appendBreak(BreakType.Line_Break);

        //Add a paragraph
        para = section.addParagraph();
        para.appendText("Remove Underline: ");

        //Insert a hyperlink and remove its underline
        txtRange = para.appendHyperlink("www.e-iceblue.com", "www.e-iceblue.com", HyperlinkType.Web_Link);
        txtRange.getCharacterFormat().setFontName("Times New Roman");
        txtRange.getCharacterFormat().setFontSize(12f);
        txtRange.getCharacterFormat().setUnderlineStyle(UnderlineStyle.None);

        //Save the document
        document.saveToFile("output/ChangeHyperlinkAppearance.docx", FileFormat.Docx_2013);
    }
}

Change Hyperlink Appearance in Word in Java

Tuesday, 12 November 2019 05:56

Modify Hyperlinks in Word in Java

Written by support iceblue

This article demonstrates how to modify hyperlinks in Word including modifying hyperlink address and display text using Spire.Doc for Java.

Below is the sample Word document we used for demonstration:

Modify Hyperlinks in Word in Java

import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.Field;

import java.util.ArrayList;
import java.util.List;

public class ModifyHyperlink {
    public static void main(String[] args) {
        //Load Word document
        Document doc = new Document();
        doc.loadFromFile("Hyperlink.docx");

        List hyperlinks = new ArrayList();

        //Loop through the section in the document
        for (Section section : (Iterable<Section>) doc.getSections()
                ) {
            //Loop through the section in the document
            for (Paragraph para : (Iterable<Paragraph>) section.getParagraphs()
                    ) {
                for (DocumentObject obj:(Iterable<DocumentObject>) para.getChildObjects()
                     ) {
                    if (obj.getDocumentObjectType().equals(DocumentObjectType.Field)) {
                        Field field = (Field) obj;
                        if (field.getType().equals(FieldType.Field_Hyperlink)) {
                            hyperlinks.add(field);
                        }
                    }
                }
            }
        }

        hyperlinks.get(0).setCode("HYPERLINK \"http://www.google.com\"");
        hyperlinks.get(0).setFieldText("www.google.com");

        doc.saveToFile("EditHyperlink.docx", FileFormat.Docx_2013);
    }
}

Output:

Modify Hyperlinks in Word in Java

Monday, 15 April 2019 09:36

Insert Hyperlinks in Word in Java

Written by support iceblue

This article demonstrates how to add a text hyperlink, an image hyperlink, an email link and a file link to a Word document using Spire.Doc for Java.

Code Snippets

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.HyperlinkType;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.DocPicture;

public class InsertHyperlinks {

    public static void main(String[] args) {

        //create a Word document
        Document doc = new Document();
        Section section = doc.addSection();

        //insert web link
        Paragraph paragraph = section.addParagraph();
        paragraph.appendText("Web Link: ");
        paragraph.appendHyperlink("https://www.e-iceblue.com/","Home Page", HyperlinkType.Web_Link);

        //insert email link
        paragraph = section.addParagraph();
        paragraph.appendText("Email Link: ");
        paragraph.appendHyperlink("mailto:support@e-iceblue.com","support@e-iceblue.com", HyperlinkType.E_Mail_Link);

        //insert file link
        paragraph = section.addParagraph();
        paragraph.appendText("File Link: ");
        paragraph.appendHyperlink("C:\\Users\\Administrator\\Desktop\\Report.pdf","Click to open Report.pdf", HyperlinkType.File_Link);

        //insert image hyperlink
        paragraph = section.addParagraph();
        paragraph.appendText("Image Hyper Link: ");
        paragraph = section.addParagraph();
        DocPicture picture = paragraph.appendPicture("C:\\Users\\Administrator\\Desktop\\logo-2.png");
        paragraph.appendHyperlink("https://www.e-iceblue.com/",picture, HyperlinkType.Web_Link);

        for (int i = 0; i < section.getParagraphs().getCount(); i++) {

            //align paragraph to center
            section.getParagraphs().get(i).getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
            //add auto spacing after paragraph
            section.getParagraphs().get(i).getFormat().setAfterAutoSpacing(true);
        }

        //save to file
        doc.saveToFile("output/InsertHyperlinks.docx", FileFormat.Docx_2013);
    }
}

Output

Insert Hyperlinks in Word in Java