PDFBox blurry image when inserted on pdf in Java -


I am trying to insert images inside the PDF but images in quality are not readable. How can the quality of the final PDF document be improved?

I have tried other non-GPL licensing libraries and I think that PDF is the best, so I want to be able to use the PDF box. 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 zero WritteBufferedImageToPDF (BufferedImage buff) {PDDocument doc = null; Pdpj page = null; PDXObjectImage ximage = null; Try {Doc = new PDDocument (); Page = new PDPage (); Doc.addPage (page); Ximage = new PDJpeg (doctor, buff, 1.0f); PDPageContentStream content = new PDPageContentStream (doctor, page); Content.drawImage (ximage, 0, 0); Content.close (); Doc.save ("C: / User / Crusader / Desktop / Hello World. PDF"); Doc.close (); } Hold (IOException i.e.) {ie.printStackTrace (); // handle exception} // save and hold off (COSVisitorException E) {// todo automatically generated blocking block e.printStackTrace (); }} Public static zero main (string [] args) {BufferedImage buff = null; Try {buff = imageIO.read (new file ("C: / user / crusader / desktop", "tests.jpg")); } Hold (IOException pre) {ex.printStackTrace (); } System.out.println (buff.getWidth ()); Println (buff.getHeight ()); PdfBoxTest.WritteBufferedImageToPDF (Fond); }}

I know that in some scenarios you can help to increase the quality PDFBOX, which is to scale the image (I know that this is not a smart solution, using CPU, file size increases ... but it works).

  public class PdfBoxTest {public static zero WritteBufferedImageToPDF (BufferedImage buff, integer width, height of intensity) {PDDocument doc = null; Pdpj page = null; PDXObjectImage ximage = null; Try {Doc = new PDDocument (); Page = new PDPage (); Doc.addPage (page); Ximage = new PDJpeg (doctor, buff, 1.0f); PDPageContentStream content = new PDPageContentStream (doctor, page); Content.drawXObject (ximage, 0, 0, width, height); Content.close (); Doc.save ("C: / user / crusader / desktophallow world.pdf"); Doc.close (); } Hold (IOException i.e.) {ie.printStackTrace (); // handle exception} // save and hold off (COSVisitorException E) {// todo automatically generated blocking block e.printStackTrace (); }} Private static final interval SCALE_FACTOR = 3; // increases file size and quality public static zero main (string [] args) {BufferedImage buff = null; Buffer's size changed to = null; Try {buff = imageIO.read (new file ("C: / user / crusader / desktop", "tests.jpg")); Resized = new bufferedImage (buff.getWidth () * SCALE_FACTOR, buff.getHeight () * SCALE_FACTOR, buff.getType ()); Graphics 2DG = resized. CreatGraphics (); G.setRenderingHint (Rendering Hints. KEY_INTERPOLATION, Rendering Hints. VALUE_INTERPOLATION_BILINEAR); G.drawImage (Fond, 0, resized.getWidth (), resized.getHeight (), 0, 0, buff.getWidth (), buff.getHeight (), Faucet); G.dispose (); } Hold (IOException pre) {ex.printStackTrace (); } System.out.println (buff.getWidth ()); Println (buff.getHeight ()); PdfBoxTest.WritteBufferedImageToPDF (resized, buff.getWidth), buff.getHeight ()); }}   

* Please, I know that this is somehow useful

Comments