我正在使用 icepdf 将 pdf 转换为图像。这是代码。
import org.icepdf.core.exceptions.PDFException;
import org.icepdf.core.exceptions.PDFSecurityException;
import org.icepdf.core.pobjects.Document;
import org.icepdf.core.pobjects.Page;
import org.icepdf.core.util.GraphicsRenderingHints;
import javax.imageio.ImageIO;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
public class PageCapture {
private static ArrayList<File> fileNames = new ArrayList<File>();
public static void main(String[] args) {
// Get a file from the command line to open
String filePath = "example.pdf";
// open the file
Document document = new Document();
RenderedImage rendImages = null;
File file = null;
BufferedImage bi = null;
int height = 0, width = 0, i = 0, x = 0, y = 0;
try {
document.setFile(filePath);
} catch (PDFException ex) {
System.out.println("Error parsing PDF document " + ex);
} catch (PDFSecurityException ex) {
System.out.println("Error encryption not supported " + ex);
} catch (FileNotFoundException ex) {
System.out.println("Error file not found " + ex);
} catch (IOException ex) {
System.out.println("Error IOException " + ex);
}
// save page captures to file.
float scale = 5.0f;
float rotation = 0f;
// Paint each pages content to an image and
// write the image to file
for (i = 0; i < document.getNumberOfPages(); i++) {
BufferedImage image = (BufferedImage) document.getPageImage(i,
GraphicsRenderingHints.PRINT, Page.BOUNDARY_CROPBOX,
rotation, scale);
height = image.getHeight();
width = image.getWidth();
rendImages = image;
file = new File("C:/INC/imageCapture1_" + i + ".tif");
try {
fileNames.add(file);
ImageIO.write(rendImages, "tif", file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
image.flush();
}
try {
BufferedImage result = new BufferedImage(width, height * i, // work
// these
// out
BufferedImage.TYPE_INT_RGB);
Graphics g = result.getGraphics();
for (int j = 0; j < fileNames.size(); j++) {
bi = ImageIO.read(fileNames.get(j));
g.drawImage(bi, x, y, null);
y += height;
}
file = new File("C:/INC/imageCapture_new_.tif");
ImageIO.write(result, "tif", file);
} catch (IOException e) {
e.printStackTrace();
}
// clean up resources
document.dispose();
}
}
上面的代码适用于 true type 字体,但 type3 字体没有显示在图像中。
- 谁能告诉如何将带有type3字体的pdf转换为图像。
- 上面的代码为每个 pdf 页面生成每个图像。无论如何都要为所有 pdf 页面生成单个图像。