8

我正在尝试在 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);
    }
}   
4

1 回答 1

6

我知道一个技巧可以帮助您在某些情况下使用 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());
    }
} 

*请让我知道这是否有帮助

于 2013-02-13T04:33:24.867 回答