Java의 Word에서 하이퍼링크 찾기 및 추출
가져오기 종속성
방법 1: free library (Free Spire.Doc for Java)을 다운로드하고 압축을 풉니다. 그런 다음 Spire.Doc.jar 파일을 Java 애플리케이션에 종속성으로 추가합니다.
방법 2: 다음 구성을 pom.xml에 추가하여 maven 프로젝트에 jar 종속성을 직접 추가합니다.
<repositories>
   <repository>
      <id>com.e-iceblue</id>
      <name>e-iceblue</name>
      <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
   </repository>
</repositories>
<dependencies>
   <dependency>
      <groupId>e-iceblue</groupId>
      <artifactId>spire.doc.free</artifactId>
      <version>5.2.0</version>
   </dependency>
</dependencies>
샘플 코드
무료 Spire.Doc for Java는 Field.get().getFieldText() 메서드를 제공하여 하이퍼링크의 텍스트를 가져온 다음 Field.get().getValue() 메서드를 사용하여 링크를 가져올 수 있습니다. 전체 샘플 코드는 아래와 같습니다.
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.Field;
import java.io.*;
import java.util.ArrayList;
public class findHyperlinks {
    public static void main(String[] args) throws IOException {
        //Create a Document instance and load a Word document from file
        Document doc = new Document();
        doc.loadFromFile("E:\\Files\\sample0.docx");
        //Create an object of ArrayList
        ArrayList hyperlinks = new ArrayList();
        String hyperlinkText = "";
        String hyperlinkAddress = "";
        //Iterate through the items in the sections to find all hyperlinks
        for (Section section : (Iterable<Section>) doc.getSections()) {
            for (DocumentObject object : (Iterable<DocumentObject>) section.getBody().getChildObjects()) {
                if (object.getDocumentObjectType().equals(DocumentObjectType.Paragraph)) {
                    Paragraph paragraph = (Paragraph) object;
                    for (DocumentObject cObject : (Iterable<DocumentObject>) paragraph.getChildObjects()) {
                        if (cObject.getDocumentObjectType().equals(DocumentObjectType.Field)) {
                            Field field = (Field) cObject;
                            if (field.getType().equals(FieldType.Field_Hyperlink)) {
                                hyperlinks.add(field);
                                //Get the texts and links of all hyperlinks
                                hyperlinkText += field.getFieldText() + "\r\n";
                                hyperlinkAddress += field.getValue() + "\r\n";
                            }
                        }
                    }
                }
            }
        }
        //Save the texts and the links of the hyperlinks to a TXT file
        writeStringToText("Text:\r\n " + hyperlinkText + "\r\n" + "Link:\r\n" + hyperlinkAddress + "\r\n", "HyperlinksTextsAndLinks.txt");
    }
    //Create a method to write the text and link of hyperlinks to a TXT file
    public static void writeStringToText(String content, String textFileName) throws IOException {
        File file = new File(textFileName);
        if (file.exists())
        {
            file.delete();
        }
        FileWriter fWriter = new FileWriter(textFileName, true);
        try {
            fWriter.write(content);
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                fWriter.flush();
                fWriter.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}
 
                Reference
이 문제에 관하여(Java의 Word에서 하이퍼링크 찾기 및 추출), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/carlwils/find-and-extract-hyperlinks-in-word-in-java-97a텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)