Текстовые аннотации с использованием Java

Добавление текстовой аннотации

  1. Откройте входной PDF и выберите страницу, на которой должна быть размещена текстовая аннотация.
  2. Создайте TextAnnotation, определите его прямоугольник и задайте его заголовок, тему, флаги и цвет.
  3. Добавьте аннотацию на страницу и сохраните обновлённый документ.
public static void textAnnotationAdd(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        TextAnnotation textAnnotation = new TextAnnotation(
                document.getPages().get_Item(1), new Rectangle(299.988, 613.664, 428.708, 680.769, true));
        textAnnotation.setTitle("Aspose User");
        textAnnotation.setSubject("Inserted text 1");
        textAnnotation.setFlags(AnnotationFlags.Print);
        textAnnotation.setColor(Color.getBlue());

        document.getPages().get_Item(1).getAnnotations().add(textAnnotation, false);
        document.save(outputFile.toString());
    }
}

Добавление свободной текстовой аннотации

  1. Загрузите исходный PDF и выберите целевую страницу и прямоугольник для свободной текстовой заметки.
  2. Создайте FreeTextAnnotation, инициализируйте её стандартный внешний вид и установите заголовок и цвет.
  3. Добавьте аннотацию на страницу и сохраните результат.
public static void freeTextAnnotationAdd(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        FreeTextAnnotation freeTextAnnotation = new FreeTextAnnotation(
                document.getPages().get_Item(1),
                new Rectangle(299, 713, 308, 720, true),
                new DefaultAppearance());
        freeTextAnnotation.setTitle("Aspose User");
        freeTextAnnotation.setColor(Color.getLightGreen());

        document.getPages().get_Item(1).getAnnotations().add(freeTextAnnotation);
        document.save(outputFile.toString());
    }
}