Чтобы добавить текстовую аннотацию, мы будем использовать API Aspose.PDF for Java, который является многофункциональным, мощным и простым в использовании API преобразования для платформы Java. Вы можете загрузить его последнюю версию непосредственно из Maven и установить ее в свой проект на базе Maven, добавив следующие конфигурации в pom.xml.
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java AP</name>
<url>https://releases.aspose.com/java/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>version of aspose-pdf API</version>
</dependency>
Создание аннотаций в документе PDF с помощью Java
Вам понадобится Aspose.PDF for Java, чтобы попробовать код в своей среде.
- Загрузите PDF в экземпляр класса Document.
- Создайте аннотацию, которую хотите добавить в PDF.
- Добавьте аннотацию в коллекцию Annotations объекта Page.
- Сохраните PDF-файл.
Текстовая аннотация PDF — Java
Document document = new Document(_dataDir + "sample.pdf");
Page page = document.getPages().get_Item(1);
Rectangle rect = new Rectangle(200, 750, 400, 790);
TextAnnotation textAnnotation = new TextAnnotation(page, rect);
textAnnotation.setTitle("Aspose User");
textAnnotation.setSubject("Sample Subject");
textAnnotation.setState (AnnotationState.Accepted);
textAnnotation.setContents("Sample contents for the annotation");
textAnnotation.setOpen(true);
textAnnotation.setIcon(TextIcon.Circle);
Border border = new Border(textAnnotation);
border.setWidth(5);
border.setDash(new Dash(1, 1));
textAnnotation.setBorder(border);
textAnnotation.setRect(rect);
page.getAnnotations().add(textAnnotation);
document.save(_dataDir + "sample_textannot.pdf");