This blog demonstrates how to convert PDF to Image using Free Spire.PDF for Java library. Free Spire.PDF is a Java library which supports converting PDF to multiple commonly used image formats such as .png, .jpg, .tiff and .svg.
Free Spire.PDF for Java provide a saveAsImage method in PdfDocument class which enables us to save PDF to image. In the following example, we will see how to convert a PDF file to .png images using Free Spire.PDFfor Java.
Before start with coding, you need to Download Free Spire.PDF for Java package, unzip it and import Spire.Doc.jar file from the lib folder in your project as a denpendency.
Before start with coding, you need to Download Free Spire.PDF for Java package, unzip it and import Spire.Doc.jar file from the lib folder in your project as a denpendency.
import java.awt.image.BufferedImage;
import java.io.File; import java.io.IOException; import com.spire.pdf.PdfDocument; import javax.imageio.ImageIO;
public class PDFtoImage { public static void main(String[] args) throws IOException { //load a PDF file PdfDocument pdf = new PdfDocument(); pdf.loadFromFile("Sample.pdf");
//save the pdf to .png images BufferedImage image; for (int i = 0; i < pdf.getPages().getCount(); i++) { image = pdf.saveAsImage(i); File file = new File( String.format("ToImage-img-%d.png", i)); ImageIO.write(image, "png", file); } pdf.close(); } }
Comments
Post a Comment