2014-01-11 102 views
6

我想提高圖像的分辨率。我用了PDFRenderer-0.9.0罐子。它是從java.net 下載的PDF頁面轉換爲圖像。如何提高PDF渲染器中圖像的分辨率?

我想將46_2.pdf PDF文件轉換爲圖像。轉換的46_2.png圖像是小尺寸612 x 792 [ width x height ],

所以我想增加圖像尺寸爲1200 x 1400 [ width x height]

我以前試過PdfBox用於將PDF頁轉換成PNG圖像文件。有一個問題only page is converted but text is missing。所以我試圖PdfRenderer圖像轉換。

代碼:

package com.pdfrenderer.examples; 

import java.awt.Graphics2D; 
import java.awt.Image; 
import java.awt.Rectangle; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.RandomAccessFile; 
import java.nio.ByteBuffer; 
import java.nio.channels.FileChannel; 

import javax.imageio.ImageIO; 

import com.sun.pdfview.PDFFile; 
import com.sun.pdfview.PDFPage; 

public class ConvertPdfPagesToImage { 
    public static void main(String[] args) { 
     try { 
      String sourceDir = "C:/PDFCopy/46_2.pdf"; 
      String destinationDir = "C:/PDFCopy/"; 
      File sourceFile = new File(sourceDir); 
      String fileName = sourceFile.getName().replace(".pdf", ""); 
      if (sourceFile.exists()) { 
       RandomAccessFile raf = new RandomAccessFile(sourceFile, "r"); 
       FileChannel channel = raf.getChannel(); 
       ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); 
       PDFFile pdf = new PDFFile(buf); 
       int pageNumber = 1; 
       for (int i = 0; i < pdf.getNumPages(); i++) { 
        PDFPage page = pdf.getPage(i); 
        // create the image 
        Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), (int) page.getBBox().getHeight()); 
        BufferedImage bufferedImage = new BufferedImage(rect.width, rect.height, BufferedImage.TYPE_INT_RGB); 
        // image width, // image height, // clip rect, // null for the ImageObserver, // fill background with white, // block until drawing is done 
        Image image = page.getImage(rect.width, rect.height, rect, null, true, true); 
        Graphics2D bufImageGraphics = bufferedImage.createGraphics(); 
        bufImageGraphics.drawImage(image, 0, 0, null); 
        ImageIO.write(bufferedImage, "png", new File(destinationDir + fileName +"_"+ pageNumber +".png")); 
        pageNumber++; 
       } 
      } else { 
       System.err.println(sourceFile.getName() +" File not exists"); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 
+0

如果圖像的寬度和高度加倍,會發生什麼情況。這不會使它的分辨率翻倍嗎? – geedubb

+0

@geedubb更改後寬度和高度圖像大小增加但分辨率相同。如果我們自動增加分辨率,它會改變它們的高度和寬度。 – pudaykiran

+0

嗯..你想自動增加像素圖像的分辨率?你確實知道你在「CSI」中看到的那些「圖像增強」功能是假的,不是嗎? – usr2564301

回答

5

從PDF文件46_2.pdf轉換所選pageNumber成圖像格式46_2.png與期望的尺寸。圖像分辨率增加。

如何將具有分辨率的單個PDF頁面轉換爲PNG或JPEG格式的圖像格式。

代碼:

package com.pdfrenderer.examples; 

import java.awt.Graphics2D; 
import java.awt.Image; 
import java.awt.Rectangle; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.RandomAccessFile; 
import java.nio.ByteBuffer; 
import java.nio.channels.FileChannel; 

import javax.imageio.ImageIO; 

import com.sun.pdfview.PDFFile; 
import com.sun.pdfview.PDFPage; 

public class PdfToImageWithDimensions { 
    public static void main(String[] args) { 
     try { 
     String sourceDir = "C:/PDFCopy/46_2.pdf";// PDF file must be placed in DataGet folder 
     String destinationDir = "C:/PDFCopy/Converted/";//Converted PDF page saved in this folder 

     File sourceFile = new File(sourceDir); 
     File destinationFile = new File(destinationDir); 

     String fileName = sourceFile.getName().replace(".pdf", ""); 
     if (sourceFile.exists()) { 
      if (!destinationFile.exists()) { 
       destinationFile.mkdir(); 
       System.out.println("Folder created in: "+ destinationFile.getCanonicalPath()); 
      } 

      RandomAccessFile raf = new RandomAccessFile(sourceFile, "r"); 
      FileChannel channel = raf.getChannel(); 
      ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); 
      PDFFile pdf = new PDFFile(buf); 

      int pageNumber = 1;// which PDF page to be convert 
      PDFPage page = pdf.getPage(pageNumber); 

      // image dimensions 
      int width = 1200; 
      int height = 1400; 

      // create the image 
      Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), (int) page.getBBox().getHeight()); 
      BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 

      // width & height, // clip rect, // null for the ImageObserver, // fill background with white, // block until drawing is done 
      Image image = page.getImage(width, height, rect, null, true, true); 
      Graphics2D bufImageGraphics = bufferedImage.createGraphics(); 
      bufImageGraphics.drawImage(image, 0, 0, null); 

      File imageFile = new File(destinationDir + fileName +"_"+ pageNumber +".png");// change file format here. Ex: .png, .jpg, .jpeg, .gif, .bmp 

      ImageIO.write(bufferedImage, "png", imageFile); 

      System.out.println(imageFile.getName() +" File created in: "+ destinationFile.getCanonicalPath()); 
     } else { 
      System.err.println(sourceFile.getName() +" File not exists"); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
} 

輸出:轉換的文件保存在文件夾C:\PDFCopy\Converted。下面一個是控制檯輸出。根據您的JRebel的的example

46_2_1.png File created in: C:\PDFCopy\Converted 

感謝Jeff Friesen我試圖在pdfrenderer

另一種解決方案:

如何將所有的PDF頁面轉換成PNG/JPEG/JPG/GIF/BMP圖片使用PDF渲染器在java中解析格式。要轉換的文件04-Request-Headers.pdf

代碼:

package com.pdfrenderer.examples; 

import java.awt.Graphics2D; 
import java.awt.Image; 
import java.awt.Rectangle; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.RandomAccessFile; 
import java.nio.ByteBuffer; 
import java.nio.channels.FileChannel; 

import javax.imageio.ImageIO; 

import com.sun.pdfview.PDFFile; 
import com.sun.pdfview.PDFPage; 

public class ConvertAllPDFPagesToImageWithDimenstions { 
    public static void main(String[] args) { 
     try { 
      String sourceDir = "C:/Documents/04-Request-Headers.pdf";// PDF file must be placed in DataGet folder 
      String destinationDir = "C:/Documents/Converted/";//Converted PDF page saved in this folder 

     File sourceFile = new File(sourceDir); 
     File destinationFile = new File(destinationDir); 

     String fileName = sourceFile.getName().replace(".pdf", ""); 
     if (sourceFile.exists()) { 
      if (!destinationFile.exists()) { 
       destinationFile.mkdir(); 
       System.out.println("Folder created in: "+ destinationFile.getCanonicalPath()); 
      } 

      RandomAccessFile raf = new RandomAccessFile(sourceFile, "r"); 
      FileChannel channel = raf.getChannel(); 
      ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); 
      PDFFile pdf = new PDFFile(buf); 
      System.out.println("Total Pages: "+ pdf.getNumPages()); 
      int pageNumber = 1; 
      for (int i = 0; i < pdf.getNumPages(); i++) { 
       PDFPage page = pdf.getPage(i); 

       // image dimensions 
       int width = 1200; 
       int height = 1400; 

       // create the image 
       Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), (int) page.getBBox().getHeight()); 
       BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 

       // width & height, // clip rect, // null for the ImageObserver, // fill background with white, // block until drawing is done 
       Image image = page.getImage(width, height, rect, null, true, true); 
       Graphics2D bufImageGraphics = bufferedImage.createGraphics(); 
       bufImageGraphics.drawImage(image, 0, 0, null); 

       File imageFile = new File(destinationDir + fileName +"_"+ pageNumber +".png");// change file format here. Ex: .png, .jpg, .jpeg, .gif, .bmp 

       ImageIO.write(bufferedImage, "png", imageFile); 
       pageNumber++; 

       System.out.println(imageFile.getName() +" File created in Folder: "+ destinationFile.getCanonicalPath()); 
      } 
     } else { 
      System.err.println(sourceFile.getName() +" File not exists"); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
} 

輸出:所有的文件都保存在文件夾C:/Documents/Converted/。下面一個是控制檯輸出

Total Pages: 13 
04-Request-Headers_1.png File created in Folder: C:\Documents\Converted 
04-Request-Headers_2.png File created in Folder: C:\Documents\Converted 
04-Request-Headers_3.png File created in Folder: C:\Documents\Converted 
04-Request-Headers_4.png File created in Folder: C:\Documents\Converted 
04-Request-Headers_5.png File created in Folder: C:\Documents\Converted 
04-Request-Headers_6.png File created in Folder: C:\Documents\Converted 
04-Request-Headers_7.png File created in Folder: C:\Documents\Converted 
04-Request-Headers_8.png File created in Folder: C:\Documents\Converted 
04-Request-Headers_9.png File created in Folder: C:\Documents\Converted 
04-Request-Headers_10.png File created in Folder: C:\Documents\Converted 
04-Request-Headers_11.png File created in Folder: C:\Documents\Converted 
04-Request-Headers_12.png File created in Folder: C:\Documents\Converted 
04-Request-Headers_13.png File created in Folder: C:\Documents\Converted 
+0

使用Rectangle,Image,BufferedImage,Graphics和ImageIO的線條看起來很奇怪/有異味。爲什麼不做以下操作:'BufferedImage buffImage =(BufferedImage)page.getImage(rect.width,rect。然後將圖像寫入文件:ImageIO.write(buffImage,「png」,destImage.toFile());' - 排除'Image'和'Graphics' – mmcrae

+0

我已經使用你的解決方案,並得到一個問題,它工作正常時,PDF包含文本,但當我已經嘗試使用圖像創建的PDF它沒有發現工作給這條線上的錯誤圖像= page.getImage(寬度,高度,矩形,null,true,true); – Vishnu

相關問題