PDF框PNG图像不支持

时间:2014-10-06 04:40:08

标签: java png pdfbox

我正在使用PDF框创建PDF,我正在尝试将图像打印成pdf,它可以处理除PNG之外的所有格式。我的代码如下:

String image = "c:/image.png";                  
                                   PDXObjectImage ximage = null;
                  if( image.toLowerCase().endsWith( ".jpg" ) )
                  {
                      ximage = new PDJpeg(doc, new FileInputStream( image ) );
                  }
                  else if (image.toLowerCase().endsWith(".tif") || image.toLowerCase().endsWith(".tiff"))
                 {
                      ximage = new PDCcitt(doc, new RandomAccessFile(new File(image),"r"));
                 }
                 else
                  {
                      BufferedImage awtImage = ImageIO.read( new File( image ) );
                      ximage = new PDPixelMap(doc, awtImage);
                      throw new IOException( "Image type not supported:" + image );
                  }

                 PDPageContentStream contentStream = new PDPageContentStream(doc, page);
                  contentStream.drawImage( ximage, 20, 20 );

当我给png图像时,它会转到:

else
                      {
                          BufferedImage awtImage = ImageIO.read( new File( image ) );
                          ximage = new PDPixelMap(doc, awtImage);
                          //throw new IOException( "Image type not supported:" + image );
                      }

并显示图像流IO异常无法读取图像文件。我还需要做些什么来接受png图像?请帮忙......

1 个答案:

答案 0 :(得分:0)

String image = "c:/"+rst.getString(8);                  
                                               PDXObjectImage ximage = null;
                                if( image.toLowerCase().endsWith( ".jpg" ) )
                                {
                                    ximage = new PDJpeg(doc, new FileInputStream( image ) );
                                }
                                else if (image.toLowerCase().endsWith(".tif") || image.toLowerCase().endsWith(".tiff"))
                               {
                                    ximage = new PDCcitt(doc, new RandomAccessFile(new File(image),"r"));
                               }
                               else
                                {
                                    BufferedImage awtImage = ImageIO.read( new File( image ) );
                                    ximage = new PDPixelMap(doc, awtImage);

                                }


                               contentStream.drawXObject(ximage, 20, pageYaxis-120, 80, 80);
                                                pageYaxis = pageYaxis-56;