Knowledgebase (2300)
By splitting PDF pages into separate files, you get smaller PDF documents that have one or some pages extracted from the original. A split file contains less information and is naturally smaller in size and easier to share over the internet. In this article, you will learn how to split PDF into single-page PDFs and how to split PDF by page ranges in Java using Spire.PDF for Java.
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>11.11.11</version>
</dependency>
</dependencies>
Split a PDF File into Multiple Single-Page PDFs in Java
Spire.PDF for Java offers the split() method to divide a multipage PDF document into multiple single-page files. The following are the detailed steps.
- Create a PdfDcoument object.
- Load a PDF document using PdfDocument.loadFromFile() method.
- Split the document into one-page PDFs using PdfDocument.split(string destFilePattern, int startNumber) method.
- Java
import com.spire.pdf.PdfDocument;
public class SplitPdfByEachPage {
public static void main(String[] args) {
//Specify the input file path
String inputFile = "C:\\Users\\Administrator\\Desktop\\Terms of Service.pdf";
//Specify the output directory
String outputDirectory = "C:\\Users\\Administrator\\Desktop\\Output\\";
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();
//Load a PDF file
doc.loadFromFile(inputFile);
//Split the PDF to one-page PDFs
doc.split(outputDirectory + "output-{0}.pdf", 1);
}
}

Split a PDF File by Page Ranges in Java
No straightforward method is offered for splitting PDF documents by page ranges. To do so, we create two or more new PDF documents and import the selected page or page range from the source document into them. Here are the detailed steps.
- Load the source PDF file while initialing the PdfDocument object.
- Create two additional PdfDocument objects.
- Import the first page from the source file to the first document using PdfDocument.insertPage() method.
- Import the remaining pages from the source file to the second document using PdfDocument.insertPageRange() method.
- Save the two documents as separate PDF files using PdfDocument.saveToFile() method.
- Java
import com.spire.pdf.PdfDocument;
public class SplitPdfByPageRange {
public static void main(String[] args) {
//Specify the input file path
String inputFile = "C:\\Users\\Administrator\\Desktop\\Terms of Service.pdf";
//Specify the output directory
String outputDirectory = "C:\\Users\\Administrator\\Desktop\\Output\\";
//Load the source PDF file while initialing the PdfDocument object
PdfDocument sourceDoc = new PdfDocument(inputFile);
//Create two additional PdfDocument objects
PdfDocument newDoc_1 = new PdfDocument();
PdfDocument newDoc_2 = new PdfDocument();
//Insert the first page of source file to the first document
newDoc_1.insertPage(sourceDoc, 0);
//Insert the rest pages of source file to the second document
newDoc_2.insertPageRange(sourceDoc, 1, sourceDoc.getPages().getCount() - 1);
//Save the two documents as PDF files
newDoc_1.saveToFile(outputDirectory + "output-1.pdf");
newDoc_2.saveToFile(outputDirectory + "output-2.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.
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>11.11.11</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.
A PDF image watermark is a semi-transparent picture integrated into a PDF page as a fixed element. It is often used to protect the copyright and other rights of the document owner since it can't be removed easily. What's more, for famous PDF publishers, image watermarks are also used to increase the credibility of their documents. With an image watermark of their logo, the readers of documents can identify them at a glance. This article will show how to insert image watermarks into PDF documents using Spire.PDF for Java with simple code.
Install Spire.PDF for Java
First of all, 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>11.11.11</version>
</dependency>
</dependencies>
Insert a Single Picture Watermark into PDF
A single image watermark is usually placed at the center of a PDF page. The detailed steps for inserting a single image watermark are as follows.
- Create a PdfDocument class instance.
- Load a PDF document using PdfDocument.loadFromFile() method.
- Load a picture using PdfImage.fromFile() method.
- Get the width and height of the picture using PdfImage.getWidth() and PdfImage.getHeight() methods.
- Loop through the pages in the PDF document to insert watermarks
- Get a page using PdfDocument.getPages().get() method.
- Get the width and height of the page using PdfPageBase.getActualSize().getWidth() and PdfPageBase.getActualSize().getHeight() methods.
- Set the transparency of the watermark picture using PdfPageBase.getCanvas().setTransparency() method.
- Draw the watermark picture at the center of the page using PdfPageBase.getCanvas().drawImage() method.
- Save the document using PdfDocument.saveToFile() method.
- Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.PdfBrush;
import com.spire.pdf.graphics.PdfImage;
import java.awt.*;
import java.awt.image.BufferedImage;
public class insertSingleImageWatermark {
public static void main(String[] args) {
//Create a PdfDocument class instance
PdfDocument pdf = new PdfDocument();
//Load a PDF file
pdf.loadFromFile("Goodbye Pixel.pdf");
//Load a picture
PdfImage image = PdfImage.fromFile("Logo.png");
//Get the width and height of the image
int imageWidth = image.getWidth();
int imageHeight = image.getHeight();
//Loop through the pages to insert watermark
for (int i = 0; i < pdf.getPages().getCount(); i++) {
//Get a page
PdfPageBase page = pdf.getPages().get(i);
//Get the width and height of the page
float pageWidth = (float) (page.getActualSize().getWidth());
float pageHeight = (float) (page.getActualSize().getHeight());
//Set the transparency of the watermark
page.getCanvas().setTransparency(0.3f);
//Draw the watermark picture at the middle of the page
page.getCanvas().drawImage(image, pageWidth/2 - imageWidth/2, pageHeight/2 - imageHeight/2, imageWidth, imageHeight);
}
//Save the file
pdf.saveToFile("SingleImageWatermark.pdf");
}
}

Insert Tiled Picture Watermarks into PDF
Tiled image watermarks contain a picture that is displayed multiple times on one PDF page. When inserting a tiled image watermark, we can set the number of repetitions of its picture. The detailed steps for inserting a tiled image watermark are as follow.
- Create a PdfDocument class instance.
- Load a PDF document using PdfDocument.loadFromFile() method.
- Load a picture using PdfImage.fromFile() method.
- Loop through the pages to insert watermarks.
- Get a specific page using PdfDocument.getPages().get() method.
- Use the custom method insertImageWatermark() to insert a tiled image watermark.
- Save the document using PdfDocument.saveToFile() method.
- Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.PdfBrush;
import com.spire.pdf.graphics.PdfImage;
import com.spire.pdf.graphics.PdfTilingBrush;
import java.awt.*;
public class insertTiledImageWatermark {
public static void main(String[] args) {
//Create a PdfDocument class instance
PdfDocument pdf = new PdfDocument();
//Load a PDF file
pdf.loadFromFile("Goodbye Pixel.pdf");
//Load a picture
PdfImage image = PdfImage.fromFile("Logo.png");
//Loop through the pages in the PDF document to insert watermark
for (int i = 0; i < pdf.getPages().getCount(); i++ ) {
//Get a Page
PdfPageBase page = pdf.getPages().get(i);
//Use the custom method to insert a tiled image watermark
insertImageWatermark(page, image, 3, 3);
}
//Save the file
pdf.saveToFile("TiledImageWatermark.pdf");
}
static void insertImageWatermark(PdfPageBase page, PdfImage image, int row, int column) {
//Create a tile brush
PdfTilingBrush brush = new PdfTilingBrush(new Dimension((int) (page.getActualSize().getWidth()/column), (int) (page.getActualSize().getHeight()/row)));
brush.getGraphics().setTransparency(0.3f);
brush.getGraphics().save();
brush.getGraphics().translateTransform(brush.getSize().getWidth()/2 - image.getWidth()/2, brush.getSize().getHeight()/2 - image.getHeight()/2);
//Draw the watermark image at the center of the page
brush.getGraphics().drawImage(image, 0, 0);
brush.getGraphics().restore();
//Use the tile brush to draw the watermark picture
page.getCanvas().drawRectangle(brush, new Rectangle(new Point(0, 0), new Dimension((int) (page.getActualSize().getWidth()), (int) (page.getActualSize().getHeight()))));
}
}

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.