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