Agregar fondos de PDF en Java
Contents
[
Hide
]
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.
- Abra el PDF de origen
Documenty la secuencia de entrada de la imagen. - Cree un
BackgroundArtifacty asigne el flujo de imagen. - 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.
- Abra el PDF de origen
Documenty flujo de imagen. - Cree un
BackgroundArtifact, asigna la imagen, y establezca la opacidad. - 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.
- Abra el PDF de origen
Document. - Cree un
BackgroundArtifacty asigne el color de fondo. - 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.
- Abra el PDF de origen
Document. - Itere a través de la colección de artefactos de la página en orden inverso.
- 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());
}
}