2012-12-05 92 views
6

我想從JPEG,BMP類型的圖像生成PDF,但我gettng圖像的一部分總是被切斷。我正在使用其中一個默認窗口圖片Sunset.jpg。PDFBox - 從圖像生成PDF的問題

下面是代碼:

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

    import javax.imageio.ImageIO; 
    import javax.imageio.stream.FileImageInputStream; 
    import org.apache.pdfbox.exceptions.COSVisitorException; 
    import org.apache.pdfbox.io.RandomAccessFile; 
    import org.apache.pdfbox.pdmodel.PDDocument; 
    import org.apache.pdfbox.pdmodel.PDPage; 
    import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; 
    import org.apache.pdfbox.pdmodel.graphics.xobject.PDCcitt; 
    import org.apache.pdfbox.pdmodel.graphics.xobject.PDJpeg; 
    import org.apache.pdfbox.pdmodel.graphics.xobject.PDPixelMap; 
    import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectImage; 

    public class ImageToPDF 
    { 
     public void createPDFFromImage(String file, String image) throws IOException, COSVisitorException 
     { 
      PDDocument doc = null; 
      try 
      { 
       doc = new PDDocument(); 
       PDPage page = new PDPage(); 
       doc.addPage(page); 
       PDXObjectImage ximage = null; 
       if(image.toLowerCase().endsWith(".jpg") || image.toLowerCase().endsWith(".jpeg")) 
       {   
        BufferedImage awtImage = ImageIO.read(new File(image));    
      ximage = new PDJpeg(doc, awtImage, 0); 
       } 
       else if (image.toLowerCase().endsWith(".tif") || image.toLowerCase().endsWith(".tiff")) 
       { 
        ximage = new PDCcitt(doc, new RandomAccessFile(new File(image),"r")); 
       } 
       else 
       { 
         BufferedImage awtImage = new BufferedImage(1000, 800, BufferedImage.TYPE_INT_RGB);    
        awtImage = ImageIO.read(new FileImageInputStream(new File(image)));        
        ximage = new PDPixelMap(doc, awtImage); 
       } 
       System.out.println(" Width of the image.... "+ximage.getWidth()); 
       PDPageContentStream contentStream = new PDPageContentStream(doc, page);    
       contentStream.drawImage(ximage, 20, 20); 
       contentStream.close(); 
       doc.save(file); 
      } 
      finally 
      { 
       if(doc != null) 
       { 
        doc.close(); 
       } 
      } 
     } 

     public static void main(String[] args) 
     { 
      ImageToPDF app = new ImageToPDF(); 
      try 
      { 
       app.createPDFFromImage("C:\\test1.pdf", "C:\\Sunset.jpg");       
      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 
     } 

}

請幫我糾正我在做什麼錯。

+0

你有沒有找到一個答案,你的問題在這裏?如果是的話,請分享一下,因爲我面臨同樣的問題。 – Anuj

+0

你試過最新的?你有適當的所有圖書館作爲jemp和tempbox包括? – MemLeak

回答

0

您是否考慮過裁剪圖像以防止它被切斷?

contentStream.drawImage(ximage, 20, 20, croppedWidth, croppedHeight); 
0

請嘗試修改爲如下的drawImage聲明:

content.drawImage(ximage,0 /*or your preferred indent*/,(700-ximage.getHeight())); 
8

這些代碼可能對您有所幫助,它的工作原理。

public void createPDFFromImage(String pdfFile, 
     List<String> imgList,int x, int y, float scale) throws IOException, COSVisitorException { 
    // the document 
    PDDocument doc = null; 
    try { 
     doc = new PDDocument(); 
     Iterator iter = imgList.iterator(); 
     int imgIndex=0; 
     while(iter.hasNext()) { 
      PDPage page = new PDPage(); 
      doc.addPage(page); 

      BufferedImage tmp_image = ImageIO.read(new File(iter.next().toString())); 
      BufferedImage image = new BufferedImage(tmp_image.getWidth(), tmp_image.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);   
      image.createGraphics().drawRenderedImage(tmp_image, null); 

      PDXObjectImage ximage = new PDPixelMap(doc, image); 

      imgIndex++; 


      PDPageContentStream contentStream = new PDPageContentStream(
        doc, page,true,true); 

      contentStream.drawXObject(ximage, x, y, ximage.getWidth()*scale, ximage.getHeight()*scale); 

      contentStream.close(); 
     } 
     doc.save(pdfFile); 
    } finally { 
     if (doc != null) { 
      doc.close(); 
     } 
    } 
} 
+1

+10這個代碼,我在PDFBox的代碼示例中包含了「contentStream.drawXObject」這一行。 –

+1

v2.0.0:drawXObject - 「@deprecated改爲使用drawImage或drawForm。」 –

-2

請添加下面的代碼片段,並嘗試:需要

導入:

import org.apache.pdfbox.pdmodel.common.PDRectangle; 

代碼追加:

PDXObjectImage image = new PDJpeg(pdfDoc, filePath)); 

float w = image.getWidth(); 
float h = image.getHeight(); 

PDPage page = new PDPage(new PDRectangle (w,h));