我的任务是将pdf转换为图像,我能够将基于rgb的颜色模型pdf转换为图像,但我无法转换(图像类型设备cmyk) 我使用itext api读取pdf文件并获取pagesize并使用bufferimage来转换图像。我不知道如何查找 解决方案。请帮助我。
代码:
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++;
}
}
}