在Java中插入pdf时,PDFBox模糊图像

时间:2012-07-10 09:25:41

标签: java image pdfbox

我正在尝试在PDF中插入图像,但质量会使图像无法读取。如何提高最终PDF文档的质量?

我已尝试过其他免费的非GPL许可证库,我认为pdfbox是最好的,所以我希望能够使用pdfbox。

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

import javax.imageio.ImageIO;

import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDJpeg;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectImage;


public class pdfBoxTest {


    public static void WritteBufferedImageToPDF(BufferedImage buff)
    {

        PDDocument doc = null;
        PDPage page = null;
        PDXObjectImage ximage = null;
        try {
            doc = new PDDocument();
            page = new PDPage();
            doc.addPage(page);

            ximage = new PDJpeg(doc, buff, 1.0f);

            PDPageContentStream content = new PDPageContentStream(doc, page);


            content.drawImage(ximage, 0, 0);
            content.close();
            doc.save("C:/Users/crusader/Desktop/Hello World.pdf");
            doc.close();

        }
        catch (IOException ie){
            ie.printStackTrace();
            //handle exception
        }
        //save and close
 catch (COSVisitorException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    public static void main(String []args)
    {
        BufferedImage buff= null;

        try{

            buff = ImageIO.read(new File("C:/Users/crusader/Desktop","tests.jpg"));

        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }

        System.out.println(buff.getWidth());
        System.out.println(buff.getHeight());
        pdfBoxTest.WritteBufferedImageToPDF(buff);
    }
}   

1 个答案:

答案 0 :(得分:5)

我知道一个技巧可以帮助你提高PDFBOX在某些情况下的质量,这是为了扩展图像(我知道它不是一个非常聪明的解决方案,增加了CPU的使用,文件大小......但它确实有效)。

public class PdfBoxTest {

    public static void WritteBufferedImageToPDF(BufferedImage buff, int width, int height)
    {

        PDDocument doc = null;
        PDPage page = null;
        PDXObjectImage ximage = null;
        try {
            doc = new PDDocument();
            page = new PDPage();
            doc.addPage(page);

            ximage = new PDJpeg(doc, buff, 1.0f);

            PDPageContentStream content = new PDPageContentStream(doc, page);

            content.drawXObject(ximage, 0, 0, width, height);
            content.close();
            doc.save("C:/Users/crusader/DesktopHello World.pdf");
            doc.close();

        }
        catch (IOException ie){
            ie.printStackTrace();
            //handle exception
        }
        //save and close
        catch (COSVisitorException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    private static final int SCALE_FACTOR = 3;//increases file size and quality

    public static void main(String []args)
    {
        BufferedImage buff= null;
        BufferedImage resized = null;
        try{
            buff = ImageIO.read(new File("C:/Users/crusader/Desktop","tests.jpg"));
            resized = new BufferedImage(buff.getWidth()*SCALE_FACTOR, buff.getHeight()*SCALE_FACTOR, buff.getType());
            Graphics2D g = resized.createGraphics();
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g.drawImage(buff, 0, 0, resized.getWidth(), resized.getHeight(), 0, 0, buff.getWidth(), buff.getHeight(), null);
            g.dispose();        
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }

        System.out.println(buff.getWidth());
        System.out.println(buff.getHeight());

        PdfBoxTest.WritteBufferedImageToPDF(resized, buff.getWidth(), buff.getHeight());
    }
} 

*请告诉我这是否有所帮助