Anotações baseadas em texto usando Java

Anotações baseadas em texto permitem que revisores e desenvolvedores adicionem notas interativas, realces e marcações a documentos PDF sem alterar o conteúdo principal. Esta seção aborda cinco tipos práticos de anotações usados em fluxos de trabalho de revisão de documentos, cenários de conformidade e ciclos de feedback colaborativo.

Guia rápido: tipos de anotação

Este artigo cobre os seguintes tipos de anotação baseados em texto:

  • Free Text: Caixas de texto editáveis para adicionar notas e comentários
  • Realce: Ênfase visual em passagens de texto importantes
  • Strikeout: Marcar texto para exclusão ou revisão durante a revisão
  • Squiggly: Sublinhado ondulado para indicar erros ou preocupações
  • Sublinhado: Ênfase tradicional de sublinhado com precisão opcional de quatro pontos

Adicionar, obter e excluir anotações de texto livre

Anotações de texto livre funcionam como caixas de texto flutuantes que podem ser editadas sem afetar a estrutura do documento. Use estes exemplos para adicionar caixas de comentário, inspecionar suas propriedades ou removê-las.

Adicionar anotações de texto livre

  1. Abra o PDF de origem em um objeto Document.
  2. Crie um FreeTextAnnotation com um retângulo e configurações de aparência.
  3. Adicione a anotação à página e salve o documento.
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());
    }
}

Obter anotações de texto livre

  1. Abra o PDF de origem em um objeto Document.
  2. Itere através das anotações na página e filtre por AnnotationType.FreeText.
  3. Recupere as propriedades ou limites da anotação.
public static void freeTextAnnotationGet(Path inputFile) {
    try (Document document = new Document(inputFile.toString())) {
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.FreeText) {
                System.out.println(annotation.getRect());
            }
        }
    }
}

Excluir anotações de texto livre

  1. Abra o PDF de origem em um objeto Document.
  2. Encontre anotações de texto livre iterando pelas anotações da página e filtrando por tipo.
  3. Adicione anotações correspondentes a uma lista de exclusão e removê-las da página.
  4. Salve o documento atualizado.
public static void freeTextAnnotationDelete(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        List<Annotation> toDelete = new ArrayList<>();
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.FreeText) {
                toDelete.add(annotation);
            }
        }
        for (Annotation annotation : toDelete) {
            document.getPages().get_Item(1).getAnnotations().delete(annotation);
        }
        document.save(outputFile.toString());
    }
}

Adicionar, obter e excluir anotações de destaque

Anotações de destaque marcam trechos importantes com uma sobreposição semitransparente. Use estes exemplos para criar destaques para revisão de documentos, localizar destaques existentes e limpar a marcação.

Adicionar anotações de destaque

  1. Abra o PDF de origem em um objeto Document.
  2. Crie um HighlightAnnotation com um retângulo definindo a área de destaque.
  3. Adicione a anotação à página e salve o documento.
public static void textHighlightAnnotationAdd(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        HighlightAnnotation highlightAnnotation = new HighlightAnnotation(
                document.getPages().get_Item(1),
                new Rectangle(300, 750, 320, 770, true));

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

Obter anotações de realce

  1. Abra o PDF de origem em um objeto Document.
  2. Itere pelas anotações e filtre por AnnotationType.Highlight.
  3. Leia as propriedades da anotação, como limites ou cor.
public static void textHighlightAnnotationGet(Path inputFile) {
    try (Document document = new Document(inputFile.toString())) {
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Highlight) {
                System.out.println(annotation.getRect());
            }
        }
    }
}

Excluir anotações de destaque

  1. Abra o PDF de origem em um objeto Document.
  2. Colete anotações de destaque filtrando anotações por tipo.
  3. Remova cada anotação da página.
  4. Salve o documento atualizado.
public static void textHighlightAnnotationDelete(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        List<Annotation> toDelete = new ArrayList<>();
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Highlight) {
                toDelete.add(annotation);
            }
        }
        for (Annotation annotation : toDelete) {
            document.getPages().get_Item(1).getAnnotations().delete(annotation);
        }
        document.save(outputFile.toString());
    }
}

Adicionar, obter e excluir anotações de tachado

Anotações de tachado riscam o texto para indicar exclusão, rejeição ou revisão. Use estes exemplos para aplicar marcação de tachado durante a revisão de documentos, encontrar texto marcado e remover anotações de tachado.

Adicionar anotações de tachado

  1. Abra o PDF de origem em um objeto Document.
  2. Crie um StrikeOutAnnotation com um retângulo, título e cor.
  3. Adicione a anotação à página e salve o documento.
public static void textStrikeoutAnnotationAdd(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        StrikeOutAnnotation strikeoutAnnotation = new StrikeOutAnnotation(
                document.getPages().get_Item(1),
                new Rectangle(299.988, 713.664, 308.708, 720.769, true));
        strikeoutAnnotation.setTitle("Aspose User");
        strikeoutAnnotation.setSubject("Inserted text 1");
        strikeoutAnnotation.setFlags(AnnotationFlags.Print);
        strikeoutAnnotation.setColor(Color.getBlue());

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

Obter anotações de tachado

  1. Abra o PDF de origem em um objeto Document.
  2. Itere pelas anotações e filtre por AnnotationType.StrikeOut.
  3. Leia metadados da anotação ou limites.
public static void textStrikeoutAnnotationGet(Path inputFile) {
    try (Document document = new Document(inputFile.toString())) {
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.StrikeOut) {
                System.out.println(annotation.getRect());
            }
        }
    }
}

Excluir anotações de tachado

  1. Abra o PDF de origem em um objeto Document.
  2. Colete anotações de tachado filtrando por tipo.
  3. Remova cada anotação da página.
  4. Salve o documento atualizado.
public static void textStrikeoutAnnotationDelete(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        List<Annotation> toDelete = new ArrayList<>();
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.StrikeOut) {
                toDelete.add(annotation);
            }
        }
        for (Annotation annotation : toDelete) {
            document.getPages().get_Item(1).getAnnotations().delete(annotation);
        }
        document.save(outputFile.toString());
    }
}

Adicionar, obter e excluir anotações onduladas

Anotações onduladas (sublinhados ondulados) destacam erros potenciais, preocupações ou itens que requerem atenção. Use estes exemplos para marcar texto problemático, inspecionar anotações onduladas e removê-las dos documentos.

Adicionar anotações onduladas

  1. Abra o PDF de origem em um objeto Document.
  2. Crie um SquigglyAnnotation com um retângulo e título.
  3. Adicione a anotação à página e salve o documento.
public static void textSquigglyAnnotationAdd(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        Page page = document.getPages().get_Item(1);
        SquigglyAnnotation squigglyAnnotation = new SquigglyAnnotation(
                page,
                new Rectangle(67, 317, 261, 459, true));
        squigglyAnnotation.setTitle("John Smith");
        squigglyAnnotation.setColor(Color.getBlue());

        page.getAnnotations().add(squigglyAnnotation);
        document.save(outputFile.toString());
    }
}

Obter anotações onduladas

  1. Abra o PDF de origem em um objeto Document.
  2. Itere pelas anotações e filtre por AnnotationType.Squiggly.
  3. Leia limites da anotação ou metadados.
public static void textSquigglyAnnotationGet(Path inputFile) {
    try (Document document = new Document(inputFile.toString())) {
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Squiggly) {
                System.out.println(annotation.getRect());
            }
        }
    }
}

Excluir anotações onduladas

  1. Abra o PDF de origem em um objeto Document.
  2. Colete anotações onduladas filtrando por tipo.
  3. Remova cada anotação da página.
  4. Salve o documento atualizado.
public static void textSquigglyAnnotationDelete(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        List<Annotation> toDelete = new ArrayList<>();
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Squiggly) {
                toDelete.add(annotation);
            }
        }
        for (Annotation annotation : toDelete) {
            document.getPages().get_Item(1).getAnnotations().delete(annotation);
        }
        document.save(outputFile.toString());
    }
}

Adicionar, obter e excluir anotações de sublinhado

Anotações de sublinhado enfatizam trechos importantes com um sublinhado tradicional. Use estes exemplos para criar sublinhados, ler o conteúdo de texto marcado e excluir anotações de sublinhado das páginas.

Adicionar anotações de sublinhado

  1. Abra o PDF de origem em um objeto Document.
  2. Crie um UnderlineAnnotation com um retângulo e cor.
  3. Adicione a anotação à página e salve o documento.
public static void textUnderlineAnnotationAdd(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        UnderlineAnnotation underlineAnnotation = new UnderlineAnnotation(
                document.getPages().get_Item(1),
                new Rectangle(299.988, 713.664, 308.708, 720.769, true));
        underlineAnnotation.setTitle("Aspose User");
        underlineAnnotation.setSubject("Inserted Underline 1");
        underlineAnnotation.setFlags(AnnotationFlags.Print);
        underlineAnnotation.setColor(Color.getBlue());

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

Obter anotações sublinhadas

  1. Abra o PDF de origem em um objeto Document.
  2. Itere pelas anotações e filtre por AnnotationType.Underline.
  3. Leia propriedades da anotação ou limites.
public static void textUnderlineAnnotationGet(Path inputFile) {
    try (Document document = new Document(inputFile.toString())) {
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Underline) {
                System.out.println(annotation.getRect());
            }
        }
    }
}

Excluir anotações de sublinhado

  1. Abra o PDF de origem em um objeto Document.
  2. Colete anotações de sublinhado filtrando por tipo.
  3. Remova cada anotação da página.
  4. Salve o documento atualizado.
public static void textUnderlineAnnotationDelete(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        List<Annotation> toDelete = new ArrayList<>();
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Underline) {
                toDelete.add(annotation);
            }
        }
        for (Annotation annotation : toDelete) {
            document.getPages().get_Item(1).getAnnotations().delete(annotation);
        }
        document.save(outputFile.toString());
    }
}

Adicionar uma anotação de sublinhado com quad points

Este exemplo define a área de sublinhado explicitamente através de pontos quad derivados de um retângulo.

  1. Abra o PDF de origem em um objeto Document.
  2. Crie um UnderlineAnnotation e calcule seus quad points.
  3. Adicione a anotação à página e salve o documento.
public static void textUnderlineWithQuadPointsAdd(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        Rectangle rect = new Rectangle(299.988, 713.664, 308.708, 720.769, true);
        UnderlineAnnotation underlineAnnotation = new UnderlineAnnotation(
                document.getPages().get_Item(1), rect);
        underlineAnnotation.setTitle("Aspose User");
        underlineAnnotation.setSubject("Inserted Underline with Quad Points");
        underlineAnnotation.setFlags(AnnotationFlags.Print);
        underlineAnnotation.setColor(Color.getBlue());
        underlineAnnotation.setQuadPoints(new com.aspose.pdf.Point[]{
                new com.aspose.pdf.Point(rect.getLLX(), rect.getLLY()),
                new com.aspose.pdf.Point(rect.getURX(), rect.getLLY()),
                new com.aspose.pdf.Point(rect.getURX(), rect.getURY()),
                new com.aspose.pdf.Point(rect.getLLX(), rect.getURY())
        });

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

Obter texto marcado a partir de anotações de sublinhado

Recupere o conteúdo de texto real coberto por anotações de sublinhado. Estes exemplos mostram duas abordagens: ler o texto marcado completo como uma única cadeia de caracteres, ou processar fragmentos de texto individualmente para uma análise detalhada.

  1. Abra o PDF de origem em um objeto Document.
  2. Itere através das anotações de sublinhado na página.
  3. Leia qualquer getMarkedText() ou getMarkedTextFragments() e imprima os resultados.
public static void textUnderlineMarkedTextGet(Path inputFile) {
    try (Document document = new Document(inputFile.toString())) {
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Underline) {
                UnderlineAnnotation ua = (UnderlineAnnotation) annotation;
                System.out.println("Marked text: " + ua.getMarkedText());
            }
        }
    }
}
public static void textUnderlineMarkedFragmentsGet(Path inputFile) {
    try (Document document = new Document(inputFile.toString())) {
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Underline) {
                UnderlineAnnotation ua = (UnderlineAnnotation) annotation;
                for (TextFragment fragment : ua.getMarkedTextFragments()) {
                    System.out.println("Fragment text: " + fragment.getText());
                }
            }
        }
    }
}

Excluir anotações de sublinhado pelo título

Remova anotações seletivamente filtrando por propriedades de metadados como título. Essa abordagem permite a limpeza direcionada de anotações por autor ou finalidade.

  1. Abra o PDF de origem em um objeto Document.
  2. Filtre anotações sublinhadas por título.
  3. Exclua as anotações correspondentes e salve o documento atualizado.
public static void textUnderlineByTitleDelete(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        List<UnderlineAnnotation> toDelete = new ArrayList<>();
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Underline) {
                UnderlineAnnotation ua = (UnderlineAnnotation) annotation;
                if ("Aspose User".equals(ua.getTitle())) {
                    toDelete.add(ua);
                }
            }
        }
        for (UnderlineAnnotation ua : toDelete) {
            document.getPages().get_Item(1).getAnnotations().delete(ua);
        }
        document.save(outputFile.toString());
    }
}

Adicionar e achatar uma anotação de sublinhado

Converta uma anotação interativa de sublinhado em conteúdo permanente da página, achatando-a. Isso impede edições posteriores enquanto preserva a aparência do sublinhado em qualquer visualizador de PDF.

  1. Abra o PDF de origem em um objeto Document.
  2. Adicione um UnderlineAnnotation para a página.
  3. Chame flatten() na anotação e salve o arquivo de saída.
public static void textUnderlineFlattenAdd(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        UnderlineAnnotation underlineAnnotation = new UnderlineAnnotation(
                document.getPages().get_Item(1),
                new Rectangle(299.988, 713.664, 308.708, 720.769, true));
        underlineAnnotation.setTitle("Aspose User");
        underlineAnnotation.setSubject("Inserted Underline to Flatten");
        underlineAnnotation.setFlags(AnnotationFlags.Print);
        underlineAnnotation.setColor(Color.getBlue());

        document.getPages().get_Item(1).getAnnotations().add(underlineAnnotation);
        underlineAnnotation.flatten();

        document.save(outputFile.toString());
    }
}

Tópicos relacionados de anotação