有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java中的Pdf(带3类字体)到图像转换

我正在使用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字体不会显示在图像中

  1. 有人能告诉我如何将带有type3字体的pdf转换成图像吗
  2. 上面的代码为每个pdf页面生成每个图像。是否存在为所有pdf页面生成单个图像的方法

共 (1) 个答案

  1. # 1 楼答案

    ICEpdf的开源版本不支持类型3字体。PRO版本支持类型3,如果需要,可以获得许可