Agregar fondos de PDF en Java

Los artefactos de fondo le permiten colocar elementos visuales no contenidos detrás del contenido principal de la página sin cambiar el texto lógico del documento.

Agregar una imagen de fondo a un PDF

Utilice este ejemplo cuando la página debe mostrar una imagen como artefacto de fondo.

  1. Abra el PDF de origen Document y la secuencia de entrada de la imagen.
  2. Cree un BackgroundArtifact y asigne el flujo de imagen.
  3. Añada el artefacto a la página de destino y guarde el PDF de salida.
public static void addBackgroundImageToPdf(Path inputFile, Path imageFile, Path outputFile) throws Exception {
    try (Document document = new Document(inputFile.toString());
         InputStream imageStream = Files.newInputStream(imageFile)) {
        BackgroundArtifact artifact = new BackgroundArtifact();
        artifact.setBackgroundImage(imageStream);
        document.getPages().get_Item(1).getArtifacts().add(artifact);
        document.save(outputFile.toString());
    }
}

Agregar una imagen de fondo con opacidad

Este ejemplo coloca una imagen de fondo semitransparente detrás del contenido de la página.

  1. Abra el PDF de origen Document y flujo de imagen.
  2. Cree un BackgroundArtifact, asigna la imagen, y establezca la opacidad.
  3. Añada el artefacto a la página y guarde el documento.
public static void addBackgroundImageWithOpacityToPdf(Path inputFile, Path imageFile, Path outputFile)
        throws Exception {
    try (Document document = new Document(inputFile.toString());
         InputStream imageStream = Files.newInputStream(imageFile)) {
        BackgroundArtifact artifact = new BackgroundArtifact();
        artifact.setBackgroundImage(imageStream);
        artifact.setOpacity(0.5);
        document.getPages().get_Item(1).getArtifacts().add(artifact);
        document.save(outputFile.toString());
    }
}

Agregar un color de fondo a un PDF

Utilice este ejemplo cuando la página deba usar un color de fondo sólido en lugar de una imagen.

  1. Abra el PDF de origen Document.
  2. Cree un BackgroundArtifact y asigne el color de fondo.
  3. Agregue el artefacto a la página y guarde el archivo de salida.
public static void addBackgroundColorToPdf(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        BackgroundArtifact artifact = new BackgroundArtifact();
        artifact.setBackgroundColor(Color.getDarkKhaki().toRgb());
        document.getPages().get_Item(1).getArtifacts().add(artifact);
        document.save(outputFile.toString());
    }
}

Eliminar artefactos de fondo

Utilice este enfoque cuando los artefactos de fondo existentes deban eliminarse de la página.

  1. Abra el PDF de origen Document.
  2. Itere a través de la colección de artefactos de la página en orden inverso.
  3. Elimine los artefactos cuyo tipo es paginación y subtipo es fondo, luego guarda el documento.
public static void removeBackground(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        for (int i = document.getPages().get_Item(1).getArtifacts().size(); i >= 1; i--) {
            Artifact artifact = document.getPages().get_Item(1).getArtifacts().get_Item(i);
            if (artifact.getType() == Artifact.ArtifactType.Pagination
                    && artifact.getSubtype() == Artifact.ArtifactSubtype.Background) {
                document.getPages().get_Item(1).getArtifacts().delete(artifact);
            }
        }

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