2012-07-10 46 views
8

我試圖在PDF中插入圖像,但質量使圖像無法讀取。如何提高最終PDF文檔的質量?PDFBox在PDF中插入Java時的模糊圖像

我試過其他免費的非GPL許可證庫,我認爲pdfbox是最好的,所以我想能夠使用pdfbox。

import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 

import javax.imageio.ImageIO; 

import org.apache.pdfbox.exceptions.COSVisitorException; 
import org.apache.pdfbox.pdmodel.PDDocument; 
import org.apache.pdfbox.pdmodel.PDPage; 
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; 
import org.apache.pdfbox.pdmodel.font.PDFont; 
import org.apache.pdfbox.pdmodel.font.PDType1Font; 
import org.apache.pdfbox.pdmodel.graphics.xobject.PDJpeg; 
import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectImage; 


public class pdfBoxTest { 


    public static void WritteBufferedImageToPDF(BufferedImage buff) 
    { 

     PDDocument doc = null; 
     PDPage page = null; 
     PDXObjectImage ximage = null; 
     try { 
      doc = new PDDocument(); 
      page = new PDPage(); 
      doc.addPage(page); 

      ximage = new PDJpeg(doc, buff, 1.0f); 

      PDPageContentStream content = new PDPageContentStream(doc, page); 


      content.drawImage(ximage, 0, 0); 
      content.close(); 
      doc.save("C:/Users/crusader/Desktop/Hello World.pdf"); 
      doc.close(); 

     } 
     catch (IOException ie){ 
      ie.printStackTrace(); 
      //handle exception 
     } 
     //save and close 
catch (COSVisitorException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 
    public static void main(String []args) 
    { 
     BufferedImage buff= null; 

     try{ 

      buff = ImageIO.read(new File("C:/Users/crusader/Desktop","tests.jpg")); 

     } 
     catch(IOException ex) 
     { 
      ex.printStackTrace(); 
     } 

     System.out.println(buff.getWidth()); 
     System.out.println(buff.getHeight()); 
     pdfBoxTest.WritteBufferedImageToPDF(buff); 
    } 
} 
+0

的問題造成的。 我還沒有找到合適的解決方案。 – iddo 2012-07-21 20:40:44

+1

@iddo:試用最新版本的PDPixelMap – Gilead 2013-08-19 16:00:24

+0

@Crusader:我已經試過你的代碼,它的工作很好,但是我只能得到pdf文檔中一半的頁面內容(寬度明智)。你可以幫我解決這個問題。 – 2013-10-09 11:56:17

回答

5

我知道的技巧,可以幫助你PDFBOX,這是將圖像縮放(我知道這是不是一個非常聰明的解決方案,提高了CPU的使用,文件大小增加的質量在某些情況下...但它確實有效)。

public class PdfBoxTest { 

    public static void WritteBufferedImageToPDF(BufferedImage buff, int width, int height) 
    { 

     PDDocument doc = null; 
     PDPage page = null; 
     PDXObjectImage ximage = null; 
     try { 
      doc = new PDDocument(); 
      page = new PDPage(); 
      doc.addPage(page); 

      ximage = new PDJpeg(doc, buff, 1.0f); 

      PDPageContentStream content = new PDPageContentStream(doc, page); 

      content.drawXObject(ximage, 0, 0, width, height); 
      content.close(); 
      doc.save("C:/Users/crusader/DesktopHello World.pdf"); 
      doc.close(); 

     } 
     catch (IOException ie){ 
      ie.printStackTrace(); 
      //handle exception 
     } 
     //save and close 
     catch (COSVisitorException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 

    private static final int SCALE_FACTOR = 3;//increases file size and quality 

    public static void main(String []args) 
    { 
     BufferedImage buff= null; 
     BufferedImage resized = null; 
     try{ 
      buff = ImageIO.read(new File("C:/Users/crusader/Desktop","tests.jpg")); 
      resized = new BufferedImage(buff.getWidth()*SCALE_FACTOR, buff.getHeight()*SCALE_FACTOR, buff.getType()); 
      Graphics2D g = resized.createGraphics(); 
      g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); 
      g.drawImage(buff, 0, 0, resized.getWidth(), resized.getHeight(), 0, 0, buff.getWidth(), buff.getHeight(), null); 
      g.dispose();   
     } 
     catch(IOException ex) 
     { 
      ex.printStackTrace(); 
     } 

     System.out.println(buff.getWidth()); 
     System.out.println(buff.getHeight()); 

     PdfBoxTest.WritteBufferedImageToPDF(resized, buff.getWidth(), buff.getHeight()); 
    } 
} 

*請讓我知道這已經在某種程度上有所幫助從PDFBox的只能夠使用JPEG作爲文件,這是一種有損格式,圖像的圖像格式

+0

我已經嘗試過你的代碼和它的工作良好,但我只能獲得pdf文檔中一半的頁面內容(寬度方面的明智)。你可以幫我解決這個問題 – 2013-10-09 11:55:57

+0

content.drawXObject乘以0.5f的寬度和高度()調用。 – 2014-04-16 10:51:38