Reputation: 93
I'm trying to insert images inside a PDF but the quality makes the images unreadable. How can improve the quality of final PDF document ?
I have tried other free non GPL license libraries and I think pdfbox is the best, so I would like to be able to use 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);
}
}
Upvotes: 8
Views: 7663
Reputation: 7625
I know a trick that can help you increase the quality in some scenarios with PDFBOX, which is to scale the image (I know it's not a very smart solution, increases the CPU use, file size... but it does work).
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());
}
}
*Please, let me know if this has been somehow helpful
Upvotes: 6