Java에서 PDF 링크 만들기
Contents
[
Hide
]
Aspose.PDF for Java는 LinkAnnotation을 작업 개체와 함께 사용하여 링크 동작을 정의합니다.
실행-작업 링크 생성
링크 주석이 외부 파일이나 대상을 시작해야 하는 경우 이 예를 사용하세요.
- 원본 PDF
Document를 열고 대상 페이지를 선택하세요. LinkAnnotation을 생성하고 테두리와 색상을 구성하세요.LaunchAction을 할당하고 문서를 저장하세요.
public static void createLinkAnnotationLaunchAction(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
Page page = document.getPages().get_Item(1);
LinkAnnotation link = new LinkAnnotation(page, new Rectangle(10, 580, 120, 600, true));
Border border = new Border(link);
border.setWidth(5);
border.setDash(new Dash(1, 1));
link.setBorder(border);
link.setColor(Color.getGreen());
link.setAction(new LaunchAction(document, inputFile.toString()));
page.getAnnotations().add(link);
document.save(outputFile.toString());
}
}
원격 이동 링크 만들기
링크가 다른 PDF 문서의 페이지를 열어야 하는 경우 이 예를 사용하세요.
- 원본 PDF
Document를 여세요. - 대상 페이지에
LinkAnnotation을 생성하세요. GoToRemoteAction을 할당하고 출력 파일을 저장하세요.
public static void createLinkAnnotationGoToRemoteAction(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
Page page = document.getPages().get_Item(1);
LinkAnnotation link = new LinkAnnotation(page, new Rectangle(10, 580, 120, 600, true));
link.setColor(Color.getGreen());
link.setAction(new GoToRemoteAction(inputFile.toString(), 1));
page.getAnnotations().add(link);
document.save(outputFile.toString());
}
}
내부 이동 링크 만들기
링크가 동일한 PDF 문서 내의 다른 페이지로 이동해야 하는 경우 이 예를 사용하세요.
- 원본 PDF
Document를 여세요. LinkAnnotation을 생성하고 모양을 구성하세요.- 대상 페이지에
GoToAction을 할당하고 문서를 저장하세요.
public static void createLinkAnnotationGoToAction(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
Page page = document.getPages().get_Item(1);
LinkAnnotation link = new LinkAnnotation(page, new Rectangle(10, 580, 120, 600, true));
Border border = new Border(link);
border.setWidth(5);
border.setDash(new Dash(1, 1));
link.setBorder(border);
link.setColor(Color.getGreen());
if (document.getPages().size() >= 4) {
link.setAction(new GoToAction(document.getPages().get_Item(4)));
} else {
link.setAction(new GoToAction(document.getPages().get_Item(document.getPages().size())));
}
page.getAnnotations().add(link);
document.save(outputFile.toString());
}
}
URI 링크 만들기
링크가 URI 작업을 통해 웹 리소스를 열어야 하는 경우 이 예를 사용합니다.
- 원본 PDF
Document를 여세요. - 페이지에
LinkAnnotation을 생성하세요. GoToURIAction을 할당하고 출력 파일을 저장하세요.
public static void createLinkAnnotationGoToUriAction(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
Page page = document.getPages().get_Item(1);
LinkAnnotation link = new LinkAnnotation(page, new Rectangle(10, 580, 120, 600, true));
link.setColor(Color.getGreen());
link.setAction(new GoToURIAction("https://docs.aspose.com/pdf/python"));
page.getAnnotations().add(link);
document.save(outputFile.toString());
}
}