【发布时间】:2016-06-13 07:48:55
【问题描述】:
我正在尝试使用 java 将 pdf 转换为图像,但是当我使用 pdf 渲染器将其转换时,图像文件中的文本呈现并且它不是英语语言无法读取,因为这里的图像 ->
public static void main(String[] args) {
File file = new File("path file");
RandomAccessFile raf;
try {
raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdffile = new PDFFile(buf);
// draw the first page to an image
int num = pdffile.getNumPages();
for (int i = 1; i <= num; i++) {
PDFPage page = pdffile.getPage(i);
// get the width and height for the doc at the default zoom
int width = (int) page.getBBox().getWidth();
int height = (int) page.getBBox().getHeight();
Rectangle rect = new Rectangle(0, 0, width, height);
int rotation = page.getRotation();
Rectangle rect1 = rect;
if (rotation == 90 || rotation == 270)
rect1 = new Rectangle(0, 0, rect.height, rect.width);
// generate the image
BufferedImage img = (BufferedImage) page.getImage(rect.width, rect.height, // width
// &
// height
rect1, // clip rect
null, // null for the ImageObserver
true, // fill background with white
true // block until drawing is done
);
ImageIO.write(img, "png", new File("path file" + i + ".png"));
}
} catch (FileNotFoundException e1) {
System.err.println(e1.getLocalizedMessage());
} catch (IOException e) {
System.err.println(e.getLocalizedMessage());
}
}
有人知道如何用英语以外的语言将 pdf 渲染成图像吗?
【问题讨论】: