Java를 사용한 워터마크 주석
Contents
[
Hide
]
워터마크 주석을 사용하면 재사용 가능한 오버레이 콘텐츠를 페이지에 배치하는 동시에 주석 컬렉션을 통해 관리할 수 있습니다.
워터마크 주석 추가
사용자 정의 글꼴 설정 및 불투명도가 포함된 텍스트 워터마크 주석이 필요한 경우 이 예를 사용하세요.
- 원본 PDF
Document를 여세요. WatermarkAnnotation을 생성하여 페이지에 추가하세요.TextState, 워터마크 텍스트, 불투명도를 설정한 후 문서를 저장하세요.
public static void watermarkAdd(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
Page page = document.getPages().get_Item(1);
WatermarkAnnotation watermarkAnnotation = new WatermarkAnnotation(
page,
new Rectangle(100, 100, 400, 200, true));
page.getAnnotations().add(watermarkAnnotation);
TextState textState = new TextState();
textState.setForegroundColor(Color.getBlue());
textState.setFontSize(25);
textState.setFont(FontRepository.findFont("Arial"));
watermarkAnnotation.setOpacity(0.5);
watermarkAnnotation.setTextAndState(new String[]{"HELLO", "Line 1", "Line 2"}, textState);
document.save(outputFile.toString());
}
}
워터마크 주석 받기
이 예에서는 주석 컬렉션을 스캔하고 각 워터마크 주석의 직사각형을 인쇄합니다.
- 원본 PDF
Document를 여세요. - 대상 페이지의 주석을 반복하세요.
AnnotationType.Watermark으로 주석을 필터링하고 해당 직사각형을 인쇄하세요.
public static void watermarkGet(Path inputFile) {
try (Document document = new Document(inputFile.toString())) {
for (Annotation a : document.getPages().get_Item(1).getAnnotations()) {
if (a.getAnnotationType() == AnnotationType.Watermark) {
System.out.println(a.getRect());
}
}
}
}
워터마크 주석 삭제
문서에서 기존 워터마크 주석을 제거해야 하는 경우 이 방법을 사용하세요.
- 원본 PDF
Document를 여세요. AnnotationType.Watermark유형의 주석을 수집하세요.- 수집된 주석을 삭제하고 출력 파일을 저장하세요.
public static void watermarkDelete(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
List<Annotation> toDelete = new ArrayList<>();
for (Annotation a : document.getPages().get_Item(1).getAnnotations()) {
if (a.getAnnotationType() == AnnotationType.Watermark) {
toDelete.add(a);
}
}
for (Annotation a : toDelete) {
document.getPages().get_Item(1).getAnnotations().delete(a);
}
document.save(outputFile.toString());
}
}