Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将PDF转换为具有透明度的png文件(保留alpha)_Java_Pdf_Png_Pdfbox - Fatal编程技术网

Java 将PDF转换为具有透明度的png文件(保留alpha)

Java 将PDF转换为具有透明度的png文件(保留alpha),java,pdf,png,pdfbox,Java,Pdf,Png,Pdfbox,我一直试图将PDF转换为png文件,但没有成功。 我试图用多种方法解决它,但没有成功。 我在写我的方式,希望有人能发现哪里出了问题: 一, 但是我得到了白色背景的png。。。 有什么想法吗? 提前感谢 更改此行 BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB); 到 这将获得一个透明的图像。尝试将ImageType.RGB更改为ImageType.RGBA。效果很好!!非常感谢。我应该把它改

我一直试图将PDF转换为png文件,但没有成功。 我试图用多种方法解决它,但没有成功。 我在写我的方式,希望有人能发现哪里出了问题:

一,

但是我得到了白色背景的png。。。 有什么想法吗? 提前感谢

更改此行

BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB);


这将获得一个透明的图像。

尝试将
ImageType.RGB
更改为
ImageType.RGBA
。效果很好!!非常感谢。我应该把它改成RGBA
        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=0;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
                );
             Graphics2D graphics = (Graphics2D)img.getGraphics();
             graphics.setBackground( new Color( 255, 255, 255, 0 ) );

            ImageIO.write(img, "png", new File(imageConverted));
        }
    } 
    catch (FileNotFoundException e1) {
        System.err.println(e1.getLocalizedMessage());
    } catch (IOException e) {
        System.err.println(e.getLocalizedMessage());
    }
// Instantiating the PDFRenderer class
        PDFRenderer renderer = new PDFRenderer(document);

        // Rendering an image from the PDF document
        BufferedImage image = null;
        try {
             image= renderer.renderImage(0);
        } catch (IOException e1) {
            return "N/A";
        }

        // Writing the image to a file
        try {
            ImageIO.write(image, "png", new File(imageConverted));
        } catch (IOException e) {
            return "N/A";
        }
BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB);
BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGBA);