简体   繁体   中英

Pdf(with type 3 fonts) to Image conversion in java

I am using icepdf to convert the pdf to image. Here is the code.

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();
    }
}

The above code is working fine for true type fonts but type3 fonts are not getting displayed in the image.

  1. Can anyone tell how to convert the pdf with type3 fonts to image.
  2. The above code generates each image for each pdf page. Is there anyway to generate single image for all the pdf pages.

The open source version of ICEpdf does not support Type 3 fonts. The PRO version does support Type 3 and can be licensed if needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM