Proper backgrounds can make different content elements of PDF documents better matched and improve the visual impression and reading experience of PDF documents. Besides, it's important to add different backgrounds to PDF documents for different usage scenarios to enhance the professionalism of the documents. This article will show how to use Spire.PDF for Java to set the background color and background image for PDF documents.
Install Spire.PDF for Java
First, you're required to add the Spire.Pdf.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.pdf</artifactId> <version>10.11.2</version> </dependency> </dependencies>
Add Background Color to PDF Documents in Java
As setting the background of a PDF document needs to be done page by page, we can loop through all the pages in the document and use the PdfPageBase.setBackgroundColor() method to set the background color for each page. The following are the detailed steps:
- Create an object of PdfDocument.
- Load a PDF document using PdfDocument.loadFromFile() method.
- Loop through the pages in the PDF document and add a background color to each page using PdfPageBase.setBackgroundColor() method. You can also use the PdfPageBase.setBackgroudOpacity() method to set the opacity of the background.
- Save the document using PdfDocument.saveToFile() method.
- Java
import com.spire.pdf.PdfDocument; import com.spire.pdf.PdfPageBase; import java.awt.*; public class SetPDFBackgroundColor { public static void main(String[] args) { //Create an object of PdfDocument PdfDocument pdf = new PdfDocument(); //Load a PDF file pdf.loadFromFile("Sample.pdf"); //Loop through the pages in the PDF file for (PdfPageBase page : (Iterable) pdf.getPages() ) { //Set the background color for each page page.setBackgroundColor(Color.PINK); //Set the opacity of the background page.setBackgroudOpacity(0.2f); } //Save the PDF file pdf.saveToFile("BackgroundColor.pdf"); } }
Add Background Picture to PDF Documents in Java
Spire.PDF for Java provides the PdfPageBase.setBackgroundImage() to set a picture as the PDF page background. The detailed steps for adding an image background to a PDF document are as follows:
- Create an object of PdfDocument.
- Load a PDF document using PdfDocument.loadFromFile() method.
- Loop through the pages in the PDF document and add a background picture to each page using PdfPageBase.setBackgroundImage() method. You can also use the PdfPageBase.setBackgroudOpacity() method to set the opacity of the background.
- Save the document using PdfDocument.saveToFile() method.
- Java
import com.spire.pdf.PdfDocument; import com.spire.pdf.PdfPageBase; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class SetPDFBackgroundImage { public static void main(String[] args) throws IOException { //Create an object of PdfDocument PdfDocument pdf = new PdfDocument(); //Load a PDF file pdf.loadFromFile("Sample.pdf"); //Load an image BufferedImage background = ImageIO.read(new File("background.jpg")); //Loop through the pages in the PDF file for (PdfPageBase page : (Iterable) pdf.getPages() ) { //Set the loaded image as the background image of a page page.setBackgroundImage(background); //Set the opacity of the background page.setBackgroudOpacity(0.2f); } //Save the PDF file pdf.saveToFile("BackgroundImage.pdf"); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.