1

I am using pdfbox for converting a pdf to image but I am getting only half image of the original . Can somebody please help

String sourceDir = "D:/pdffiles/Sample_93.pdf"; // Pdf files are read from this folder
String destinationDir = "D:/images/png/"; // converted images from pdf document are saved here

File sourceFile = new File(sourceDir);
File destinationFile = new File(destinationDir);
if (!destinationFile.exists()) {
    destinationFile.mkdir();
    System.out.println("Folder Created -> "+ destinationFile.getAbsolutePath());
}
if (sourceFile.exists()) {
    PDDocument document = PDDocument.loadNonSeq(new File(sourceDir), null);
    List<PDPage> pdPages = document.getDocumentCatalog().getAllPages();
    int page = 0;
    for (PDPage pdPage : pdPages){ 
        ++page;
        BufferedImage bim = pdPage.convertToImage(BufferedImage.TYPE_INT_RGB, 300);
        ImageIOUtil.writeImage(bim, "Report" + "-" + page + ".png", 300);
    }
    document.close();
}

Below was the half image

incomplete image

Edit 1 :

1st page with content came out correctly the 2nd page with the images(its scanned as pdf images) came out only half .

Edit 2

Tried with the below code where the image got turned black areas to white and white areas to black

Total Black Image

public class ImageMain {
public static void setup() throws IOException {
    // load a pdf from a byte buffer
    File file = new File("D:/odimages/selection.pdf");
    RandomAccessFile 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);
    int numPgs = pdffile.getNumPages();
    for (int i = 0; i < numPgs; i++) {
        // draw the first page to an image
        PDFPage page = pdffile.getPage(i);
        // get the width and height for the doc at the default zoom
        Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), (int) page.getBBox().getHeight());
        // generate the image
        Image img = page.getImage(rect.width, rect.height, // width & height
                rect, // clip rect
                null, // null for the ImageObserver
                true, // fill background with white
                true // block until drawing is done
                );
        // save it as a file
        BufferedImage bImg = toBufferedImage(img);
        File yourImageFile = new File("D:/odimages/png/page_" + i + ".png");
        ImageIO.write(bImg, "png", yourImageFile);
    }
}

// This method returns a buffered image with the contents of an image
public static BufferedImage toBufferedImage(Image image) {
    if (image instanceof BufferedImage) {
        return (BufferedImage) image;
    }
    // This code ensures that all the pixels in the image are loaded
    image = new ImageIcon(image).getImage();
    // Determine if the image has transparent pixels; for this method's
    // implementation, see e661 Determining If an Image Has Transparent
    // Pixels
    boolean hasAlpha = hasAlpha(image);
    // Create a buffered image with a format that's compatible with the
    // screen
    BufferedImage bimage = null;
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    try {
        // Determine the type of transparency of the new buffered image
        int transparency = Transparency.OPAQUE;
        if (hasAlpha) {
            transparency = Transparency.BITMASK;
        }
        // Create the buffered image
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gs.getDefaultConfiguration();
        bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
    } catch (HeadlessException e) {
        // The system does not have a screen
    }
    if (bimage == null) {
        // Create a buffered image using the default color model
        int type = BufferedImage.TYPE_INT_RGB;
        if (hasAlpha) {
            type = BufferedImage.TYPE_INT_ARGB;
        }
        bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    }
    // Copy image to buffered image
    Graphics g = bimage.createGraphics();
    // Paint the image onto the buffered image
    g.drawImage(image, 0, 0, null);
    g.dispose();
    return bimage;
}

public static boolean hasAlpha(Image image) {
    // If buffered image, the color model is readily available
    if (image instanceof BufferedImage) {
        BufferedImage bimage = (BufferedImage) image;
        return bimage.getColorModel().hasAlpha();
    }
    // Use a pixel grabber to retrieve the image's color model;
    // grabbing a single pixel is usually sufficient
    PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
    }
    // Get the image's color model
    ColorModel cm = pg.getColorModel();
    return cm.hasAlpha();
}

public static void main(final String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            try {
                ImageMain.setup();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    });
}
}
User146378
  • 195
  • 1
  • 1
  • 10
  • Could you please link to the PDF? What PDFBox version are you using? – Tilman Hausherr Apr 21 '16 at 07:46
  • I had used the http://mirrors.ibiblio.org/apache/pdfbox/1.8.11/pdfbox-1.8.11.jar – User146378 Apr 21 '16 at 13:49
  • I can't help without the PDF. Please try also the 2.0 version, see https://stackoverflow.com/questions/23326562/apache-pdfbox-convert-pdf-to-images – Tilman Hausherr Apr 21 '16 at 16:45
  • @TilmanHausherr - I can't attach the pdf in stackoverflow – User146378 Apr 21 '16 at 17:59
  • You could upload it to a sharehoster (e.g. filedropper.com) and post the link. – Tilman Hausherr Apr 21 '16 at 18:01
  • @TilmanHausherr Version 2.0 really worked as expected . I am getting the full image but the quality of the image wasn't that good . Do you have any suggestion on that – User146378 Apr 21 '16 at 18:37
  • I can't comment without the PDF. – Tilman Hausherr Apr 21 '16 at 18:40
  • @User146378 "Do you have any suggestion on that" - select a higher dpi value. For any other hint, your PDF is required for inspection and analysis. – mkl Apr 22 '16 at 09:17
  • 1
    @User146378 Your code in your second edit is not using PDFBox, is it? – mkl Apr 22 '16 at 09:19
  • @mkl fixed the quality of image with below statement BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 300, ImageType.BINARY); – User146378 Apr 22 '16 at 19:02
  • @mkl . Yes I am not using the PDFBox in the 2nd edit – User146378 Apr 22 '16 at 19:03
  • I assume you have tried PDFRenderer. That project has been inactive for over 4 years. However there's a fork: https://github.com/katjas/PDFrenderer – Tilman Hausherr Apr 23 '16 at 10:41
  • This question is going nowhere. You did post the image but didn't share the PDF (I would have liked to see what the problem is in 1.8), then you tested 2.0 but didn't set the dpi that you used for 1.8, and finally you inserted a question about a different product. – Tilman Hausherr Apr 24 '16 at 11:22
  • @TilmanHausherr - Sorry for the confusion and to clear it out . My edit 2 was the code from a different library which worked but got an image with black back ground ... Its just for a reference about the options that i had tried . Secondly it really worked in version 2.0 and I can't share the pdf as it has some client related content in it .(please excuse me one this) . – User146378 Apr 25 '16 at 19:10

0 Answers0