如何在PDF渲染器中提高图像的分辨率



我想提高图像的分辨率。我使用了 PDFRenderer-0.9.0 jar 。它是从 java.net 下载的 用于将 PDF 页面转换为图像。

我想将 46_2.pdf PDF 文件转换为图像。转换后的 46_2.png 图像很小,尺寸612 x 792 [ width x height ]

所以我想将图像尺寸增加到1200 x 1400 [ width x height].

我以前尝试过将PDF页面转换为PNG图像文件的PdfBoxonly 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();
        }
    }
}
将 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:PDFCopyConverted文件夹中。下面是控制台输出。

46_2_1.png File created in: C:PDFCopyConverted

谢谢杰夫弗里森基于你的jRebel示例,我在pdfrenderer中尝试过

另一种解决方案:

如何使用PDF渲染器将所有PDF页面转换为png/jpeg/jpg/gif/bmp图像格式。要转换的文件 04-请求-标头.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:DocumentsConverted
04-Request-Headers_2.png File created in Folder: C:DocumentsConverted
04-Request-Headers_3.png File created in Folder: C:DocumentsConverted
04-Request-Headers_4.png File created in Folder: C:DocumentsConverted
04-Request-Headers_5.png File created in Folder: C:DocumentsConverted
04-Request-Headers_6.png File created in Folder: C:DocumentsConverted
04-Request-Headers_7.png File created in Folder: C:DocumentsConverted
04-Request-Headers_8.png File created in Folder: C:DocumentsConverted
04-Request-Headers_9.png File created in Folder: C:DocumentsConverted
04-Request-Headers_10.png File created in Folder: C:DocumentsConverted
04-Request-Headers_11.png File created in Folder: C:DocumentsConverted
04-Request-Headers_12.png File created in Folder: C:DocumentsConverted
04-Request-Headers_13.png File created in Folder: C:DocumentsConverted

相关内容

  • 没有找到相关文章

最新更新