Java에서 PDF 링크 추출

페이지 주석을 반복하고 AnnotationType.Link을 필터링하여 PDF 링크를 검사할 수 있습니다.

링크 주석 추출

페이지의 링크 주석에 대한 위치 및 페이지 정보가 필요한 경우 이 예를 사용하세요.

  1. 원본 PDF Document를 여세요.
  2. 페이지 주석을 반복하고 링크 주석을 필터링하세요.
  3. 일치하는 각 링크에 대한 페이지 색인과 직사각형을 읽으세요.
public static void extractLinkAnnotation(Path inputFile) {
    try (Document document = new Document(inputFile.toString())) {
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Link && annotation instanceof LinkAnnotation) {
                LinkAnnotation linkAnnotation = (LinkAnnotation) annotation;
                System.out.println("Page: " + linkAnnotation.getPageIndex()
                        + ", location: " + linkAnnotation.getRect());
            }
        }
    }
}

하이퍼링크 대상 추출

웹 링크 주석에서 대상 URI를 읽어야 할 때 이 예를 사용하세요.

  1. 원본 PDF Document를 여세요.
  2. 작업이 GoToURIAction인 LinkAnnotation 개체를 찾으세요.
  3. 각 하이퍼링크에 대한 페이지 색인과 URI 대상을 인쇄하세요.
public static void extractHyperlinks(Path inputFile) {
    try (Document document = new Document(inputFile.toString())) {
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Link && annotation instanceof LinkAnnotation) {
                LinkAnnotation linkAnnotation = (LinkAnnotation) annotation;
                if (linkAnnotation.getAction() instanceof GoToURIAction) {
                    GoToURIAction action = (GoToURIAction) linkAnnotation.getAction();
                    System.out.println("Page " + linkAnnotation.getPageIndex() + ", URI:" + action.getURI());
                }
            }
        }
    }
}