Am 27.04.2015 um 15:58 schrieb Rohail Kidwai:
Hey,  I am trying to figure out how to write a bufferedimage to a pdf in pdfbox 
2.0 and am having some trouble.

Since the PDPixelMap has been removed in 2.0. Can you please give me some 
guidance here?
Use LosslessFactory.createFromImage(...)

See also the ImageToPDF.java example in the source code:


            PDPage page = new PDPage();
            doc.addPage(page);

            PDImageXObject pdImage;
            if (imagePath.toLowerCase().endsWith(".jpg"))
            {
pdImage = JPEGFactory.createFromStream(doc, new FileInputStream(imagePath));
            }
            else if (imagePath.toLowerCase().endsWith(".tif") ||
                     imagePath.toLowerCase().endsWith(".tiff"))
            {
pdImage = CCITTFactory.createFromFile(doc, new File(imagePath));
            }
            else if (imagePath.toLowerCase().endsWith(".gif") ||
                     imagePath.toLowerCase().endsWith(".bmp") ||
                     imagePath.toLowerCase().endsWith(".png"))
            {
                BufferedImage bim = ImageIO.read(new File(imagePath));
                pdImage = LosslessFactory.createFromImage(doc, bim);
            }
            else
            {
throw new IOException("Image type not supported: " + imagePath);
            }

PDPageContentStream contents = new PDPageContentStream(doc, page);

            // draw the image at full size at (x=20, y=20)
            contents.drawImage(pdImage, 20, 20);

            // to draw the image at half size at (x=20, y=20) use
// contents.drawImage(pdImage, 20, 20, pdImage.getWidth() / 2, pdImage.getHeight() / 2);

            contents.close();
            doc.save(pdfPath);



Tilman



Thanks.


code that used to work in 1.8

BufferedImage img;


PDDocument doc = new PDDocument();


PDRectangle pageSize = new PDRectangle(width, height);


PDPage pdfPage = PDPage(pageSize);

PDPixelMap pdPixelMap = new PDPixelMap(doc, img);
PDPageContentStream contents = new PDPageContentStream(doc, pdfPage);
contents.drawXObject(pdPixelMap, 0, 0, img.getWidth(), img.getHeight());
contents.close();


doc.addPage(pdfPage);




---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@pdfbox.apache.org
For additional commands, e-mail: users-h...@pdfbox.apache.org

Reply via email to