Spire.PDF for Java offers PdfDocument.saveAsImage() method to convert PDF document to image. From Version 4.11.1, Spire.PDF for Java supports to set the transparent value for the background of the resulted images during PDF to image conversion. This article will show you how to convert PDF to images with transparent background in Java applications.
Install Spire.PDF for Java
First of all, you need 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 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.10.7</version> </dependency> </dependencies>
Convert PDF to Images with Transparent Background
- Create an object of PdfDocument class.
- Load a sample PDF document using PdfDocument.loadFromFile() method.
- Specify the transparent value for the background of the resulted images using PdfDocument.getConvertOptions().setPdfToImageOptions() method.
- Save the document to images using PdfDocument.saveAsImage() method.
- Java
import com.spire.pdf.*; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; public class PdftoImage { public static void main(String[] args) throws Exception { //Create an object of PdfDocument class. PdfDocument pdf = new PdfDocument(); //Load the sample PDF document pdf.loadFromFile("Sample.pdf"); //Specify the background transparent value as 0 during PDF to image conversion. pdf.getConvertOptions().setPdfToImageOptions(0); //Save PDF to .png image BufferedImage image = pdf.saveAsImage(0); File file = new File( String.format("ToImage.png")); ImageIO.write(image, "PNG", file); } }
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.