itext pdf to image convert

4.7k views Asked by At

I my task is convert pdf to image,I able to convert rgb based color model pdf to image but i was not able to convert(image type device cmyk) I used itext api for read the pdf file and get the pagesize and used bufferimage to convert the image.I dont know how do find out the solution.plese help me out.

Code:

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.awt.image.ColorModel;
import java.awt.image.PixelGrabber;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Properties;
import java.awt.Toolkit;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

import org.apache.pdfbox.PDFReader;
import org.apache.pdfbox.pdmodel.PDDocument;

import com.google.gson.Gson;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.pdf.PdfPatternPainter;
import com.itextpdf.text.pdf.PdfReader;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;

import java.awt.color.ColorSpace;
import java.awt.font.GlyphVector;

import sun.awt.SunHints;
import sun.java2d.SunGraphics2D;
import sun.font.GlyphList;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;

public class PdfToMultiImage {


    public static void main(String args[]) throws IOException {

        System.out.println("HEEEEEEEEE::::::::::::");
        // private String sourceDir;
        File sourceFile = new File("/home/PDF_Clipping/tp_cmyk.pdf");
        // private String destinationDir;
        File destinationFile = new File("/home/testImage/page_0003");
        String fileName = sourceFile.getName().replace(".pdf", "");

        RandomAccessFile raf = new RandomAccessFile(sourceFile, "r");
        FileChannel channel = raf.getChannel();
        ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0,
                channel.size());
        PDFFile pdf = new PDFFile(buf);

        System.out.println("Total Pages: " + pdf.getNumPages());
        int pageNumber = 1;
        for (int i = 0; i < pdf.getNumPages(); i++) {
              PDFPage page = pdf.getPage(i+1);

            // image dimensions
            int width = 1200;
            int height = 1400;
            // create the image

            Rectangle rect = new Rectangle(0, 0, (int) page.getBBox()
                    .getWidth(), (int) page.getBBox().getHeight());

            BufferedImage bufferedImage = new BufferedImage(width, height,
                    BufferedImage.TYPE_INT_RGB);


            Image image = page.getImage(width, height, rect, null, true, true);

            ColorSpace colorSpace ;
        int a=  bufferedImage.getType();
        System.out.println(a);


            Graphics2D bufImageGraphics = bufferedImage.createGraphics();
            bufImageGraphics.drawImage(image, 0, 0, null);

            File imageFile = new File(destinationFile + "pdfImg" + i + ".png");

            ImageIO.write(bufferedImage, "png", imageFile);
            pageNumber++;
        }
    }



}
0

There are 0 answers