Annotations en filigrane utilisant Java

Ajouter une annotation en filigrane

  1. Ouvrez le PDF d’entrée et définissez le rectangle où l’annotation en filigrane sera placée.

  2. Créez l’objet WatermarkAnnotation, ajoutez-le à la page et configurez l’état et l’opacité du texte du filigrane.

  3. Appliquez les lignes de texte en filigrane et enregistrez le PDF modifié.

public static void watermarkAdd(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        WatermarkAnnotation watermarkAnnotation = new WatermarkAnnotation(
                document.getPages().get_Item(1), new Rectangle(100, 0, 400, 100, true));

        document.getPages().get_Item(1).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());
    }
}