PDF-Renderer库中的缩放选项

时间:2010-08-17 23:02:06

标签: java pdfrenderer

我正在检查https://pdf-renderer.dev.java.net/的api 我想将PDF转换为100%缩放级别的图像。

有人试过吗?

3 个答案:

答案 0 :(得分:1)

    int widthPage  = (int)page.getBBox().getWidth();
    int heightPage = (int)page.getBBox().getHeight();

    if(page.getAspectRatio()<1){
        widthPage  =  (int)(widthPage / page.getAspectRatio() );
        heightPage =  (int)(heightPage / page.getAspectRatio() );
    }

    // get the width and height for the doc at the default zoom
    Rectangle rect = new Rectangle(0, 0, (int) widthPage, (int) heightPage);
    // generate the image
    Image img = page.getImage(rect.width, rect.height, // width & height
            rect, // clip rect
            null, // null for the ImageObserver
            true, // fill background with white
            true // block until drawing is done
            );
    // save it as a file

答案 1 :(得分:0)

我建议使用iText进行PDF查看。到目前为止,这对我来说非常好。

答案 2 :(得分:0)

只需更改图像尺寸即可。缩放图像时,如果图像尺寸较大,则所有字符都以清晰视图显示。下载04-Request-Headers.pdf文件并将其粘贴到C盘中,转换后的文件保存在PDFConvertedFiles文件夹中。需要的罐子PDFRenderer-0.9.0

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:/04-Request-Headers.pdf";// PDF file must be placed in DataGet folder
            String destinationDir = "C:/PDFConvertedFiles/";//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();
}
}
}