2012-08-08 122 views
1

我們如何將pdf文件轉換爲單個jpeg?現在我已經使用下面的代碼轉換爲不同的圖像..但我怎麼能作爲一個單一的JPEG圖像 package packagename;將pdf文件轉換爲單個jpeg?

import antlr.ByteBuffer; 
import com.sun.pdfview.PDFFile; 
import com.sun.pdfview.PDFPage; 
import java.awt.Image; 
import java.awt.Rectangle; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.PrintWriter; 
import java.io.RandomAccessFile; 
import java.nio.MappedByteBuffer; 
import java.nio.channels.FileChannel; 
import javax.imageio.ImageIO; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

/** 
* 
* @author Praveen 
*/ 
public class Pdf2ImageCon extends HttpServlet { 

/** 
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. 
* @param request servlet request 
* @param response servlet response 
* @throws ServletException if a servlet-specific error occurs 
* @throws IOException if an I/O error occurs 
*/ 
protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException { 
response.setContentType("text/html;charset=UTF-8"); 
PrintWriter out = response.getWriter(); 
String pdfname="C:/san.pdf"; 
    try { 
     BufferedImage img=null; 

     File file = new File(pdfname); 
    RandomAccessFile raf; 
    try { 
     raf = new RandomAccessFile(file, "r"); 
     FileChannel channel = raf.getChannel(); 
     MappedByteBuffer 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(); 

int length; 
length=num; 
     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 == 180) { 
       rect1 = 
new Rectangle(0, 0, rect.height, rect.width); 
      } 
      //generate the image 


        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 
        ); 

     } 
     ImageIO.write(img, "png", new File("C:\\newone.png")); 
    } catch (FileNotFoundException e1) { 
     System.err.println(e1.getLocalizedMessage()); 
    } catch (IOException e) { 
     System.err.println(e.getLocalizedMessage()); 
    } 



    } finally { 
     out.close(); 
    } 
} 

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> 
/** 
* Handles the HTTP <code>GET</code> method. 
* @param request servlet request 
* @param response servlet response 
* @throws ServletException if a servlet-specific error occurs 
* @throws IOException if an I/O error occurs 
*/ 

protected void doGet(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException { 
    processRequest(request, response); 
} 

/** 
* Handles the HTTP <code>POST</code> method. 
* @param request servlet request 
* @param response servlet response 
* @throws ServletException if a servlet-specific error occurs 
* @throws IOException if an I/O error occurs 
*/ 

protected void doPost(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException { 
    processRequest(request, response); 
} 

/** 
* Returns a short description of the servlet. 
* @return a String containing servlet description 
*/ 

public String getServletInfo() { 
    return "Short description"; 
}// </editor-fold> 

} 

用這個每個pdf文件頁面生成一個新的jpeg圖像。如何我可以得到一個JPEG格式的單個JPEG/PNG

回答

0

JPEG不是一個多頁圖像格式(而例如TIFF是,或動畫GIF)。

  • 那麼你究竟想要什麼?
  • 將所有PDF頁面「拼貼」成一個巨大的JPEG?

例如,如果您想要將官方ISO 3200_2008 PDF-1.7規範的前12頁平鋪爲4列和3行('4x3'),並在每頁之間留出2像素的額外間距) ,運行這個ImageMagick命令:

montage \ 
    http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/PDF32000_2008.pdf[1-12] \ 
    -tile 4x3 -geometry +2+2 \ 
    -background white \ 
    tiled.png 

它不應該是一個問題,你將它翻譯成你的代碼。

1

首先,看起來相當複雜。 Ghost4J達到轉換好一點恕我直言:

// load PDF document 
PDFDocument document = new PDFDocument(); 
document.load(new File("input.pdf")); 

// create renderer 
SimpleRenderer renderer = new SimpleRenderer(); 

// set resolution (in DPI) 
renderer.setResolution(300); 

// render 
List<Image> images = renderer.render(document); 

然後,看看這個博客員額(還沒有嘗試過,但看起來這是你想要的東西:http://kalanir.blogspot.de

它使使用Java的ImageIO的:

int rows = images.size(); 
int cols = 1; 
int chunks = rows * cols; 

int chunkWidth, chunkHeight; 
int type; 
//fetching image files 
File[] imgFiles = new File[chunks]; 
for (int i = 0; i < chunks; i++) { 
     imgFiles[i] = new File("archi" + i + ".jpg"); 
    } 

    //creating a bufferd image array from image files 
    BufferedImage[] buffImages = new BufferedImage[chunks]; 
    for (int i = 0; i < chunks; i++) { 
     buffImages[i] = ImageIO.read(imgFiles[i]); 
    } 
    type = buffImages[0].getType(); 
    chunkWidth = buffImages[0].getWidth(); 
    chunkHeight = buffImages[0].getHeight(); 

    //Initializing the final image 
    BufferedImage finalImg = new BufferedImage(chunkWidth*cols, chunkHeight*rows, type); 

    int num = 0; 
    for (int i = 0; i < rows; i++) { 
     for (int j = 0; j < cols; j++) { 
      finalImg.createGraphics().drawImage(buffImages[num], chunkWidth * j, chunkHeight * i, null); 
      num++; 
     } 
    } 
    System.out.println("Image concatenated....."); 
    ImageIO.write(finalImg, "jpeg", new File("finalImg.jpg")); 

當然,你有圖像了,沒有必要從文件中獲取它就像這個例子