В документе Word гиперссылка относится к ссылке, вставленной в определенный текст или изображение, что позволяет переходить между различными текстовыми элементами в документе или между различными веб-сайтами. В этой статье будет показано, как добавить текстовую гиперссылку, гиперссылку на изображение, ссылку на электронную почту и ссылку на файл в документ Word с помощью бесплатного Spire. Документ для Java.
Установка Способ 1: Загрузите Бесплатный шпиль. Doc для Java и распакуйте его. Затем добавьте Spire.Doc.jar файл в ваше Java-приложение в качестве зависимости.
Метод 2: Вы также можете добавить зависимость jar в проект maven, добавив следующие конфигурации в pom.xml .
com.e-iceblue e-iceblue http://repo.e-iceblue.com/nexus/content/groups/public/ e-iceblue spire.doc.free 2.7.3
Фрагменты Кода
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.google.com/", "Home Page", HyperlinkType.Web_Link);
//insert email link
paragraph = section.addParagraph();
paragraph.appendText("Email Link: ");
paragraph.appendHyperlink("mailto:xxx@outlook.com", "xxx@outlook.com", HyperlinkType.E_Mail_Link);
//insert file link
paragraph = section.addParagraph();
paragraph.appendText("File Link: ");
String filePath = "C:\\Users\\Administrator\\Desktop\\sample.pptx";
paragraph.appendHyperlink(filePath, "Click to open report", HyperlinkType.File_Link);
//insert image hyperlink
paragraph = section.addParagraph();
paragraph.appendText("Image Hyper Link: ");
paragraph = section.addParagraph();
DocPicture picture = paragraph.appendPicture("C:\\Users\\Administrator\\IdeaProjects\\Spire.Doc\\logo (2).jpg");
paragraph.appendHyperlink("https://www.google.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("InsertHyperlinks.docx", FileFormat.Docx_2013);
}
}
}
Оригинал: “https://dev.to/jazzzzz/java-insert-hyperlinks-in-word-documents-277d”