Writing image to pdf file in java

I am writing code to convert Microsoft Power Point (ppt) slides into images and to write generated images to a PDF file. The following code generates and writes images to a PDF file, but the problem I am having is when I write an image to a PDF file that is larger than the pdf page size, and I can see that only 75% of the rest of the image is invisible. Another note that can be noticed here is that the recorded pdf images look like enlarged or expanded. Take a look at the following code snippet:

for (int i = 0; i < slide.length; i++) { BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB); Graphics2D graphics = img.createGraphics(); graphics.setPaint(Color.white); graphics.fill(new Rectangle(0, 0, pgsize.width, pgsize.height)); slide[i].draw(graphics); fileName="C:/DATASTORE/slide-"+(i+1)+".png"; FileOutputStream out = new FileOutputStream(fileName); javax.imageio.ImageIO.write(img, "png", out); out.flush(); out.close(); com.lowagie.text.Image image =com.lowagie.text.Image.getInstance(fileName); image.setWidthPercentage(40.0f); doc.add((image)); } doc.close(); } catch(DocumentException de) { System.err.println(de.getMessage()); } 

If anyone knows a solution, please help me fix it. Thanks.

Here is the code that he is doing the job that I wanted. Now I get the desired results after the following recommendations of Bruno Loudzhi.

But, as Bruno Loudzhi previously pointed out, their problem is related to the created png image. The generated png image is incorrect because the shape or image in the slide overlaps with the text of the slide. Can you help me identify and correct the error?

 import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.Rectangle; import com.itextpdf.text.Image; import java.awt.image.BufferedImage; import java.io.FileInputStream; import java.io.FileOutputStream; import org.apache.poi.hslf.model.Slide; import org.apache.poi.hslf.usermodel.SlideShow; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.PageSize; import com.itextpdf.text.pdf.PdfWriter; public class ConvertSlidesIntoImages { public static void main(String[] args){ try { FileInputStream is = new FileInputStream("C:/DATASTORE/testPPT.ppt"); SlideShow ppt = new SlideShow(is); is.close(); String fileName; Dimension pgsize = ppt.getPageSize(); Slide[] slide = ppt.getSlides(); Document doc=new Document(); PdfWriter.getInstance(doc, new FileOutputStream("c:/DATASTORE/convertPPTSlidesIntoPDFImages.pdf")); doc.open(); for (int i = 0; i < slide.length; i++) { BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB); Graphics2D graphics = img.createGraphics(); graphics.setPaint(Color.white); graphics.fill(new Rectangle(0, 0, pgsize.width, pgsize.height)); slide[i].draw(graphics); fileName="C:/DATASTORE/slide-"+(i+1)+".png"; FileOutputStream out = new FileOutputStream(fileName); javax.imageio.ImageIO.write(img, "png", out); out.flush(); out.close(); com.itextpdf.text.Image image =com.itextpdf.text.Image.getInstance(fileName); doc.setPageSize(new com.itextpdf.text.Rectangle(image.getScaledWidth(), image.getScaledHeight())); doc.newPage(); image.setAbsolutePosition(0, 0); doc.add(image); } doc.close(); }catch(DocumentException de) { System.err.println(de.getMessage()); } catch(Exception ex) { ex.printStackTrace(); } } 

thanks

+6
source share
1 answer

First it is: If the png saved as "C:/DATASTORE/slide-"+(i+1)+".png" is incorrect, the slide in the PDF will also not be right.

And this:. Your piece of code does not show us how you create the Document object. The default page size is A4 in portrait orientation. It goes without saying that images larger than 595 x 842 do not fit this page.

Now the answer: There are two ways to solve this problem.

Or you will resize the image ( not with setWidthPercentage() if you have not calculated the actual percentage) and you will add it to the position (0, 0) so that it does not take the fields into account. For instance:

 image.scaleToFit(595, 842); image.setAbsolutePosition(0, 0); doc.add(image); doc.newPage(); 

The best solution would be to adapt the page size to the image size.

 Document doc = new Document(new Rectangle(image.getScaledWidth(), image.getScaledHeight())); // create a writer, open the document image.setAbsolutePosition(0, 0); doc.add(image); doc.newPage(); 

If the image size changes, you can resize the page when adding such images:

 doc.setPageSize(new Rectangle(image.getScaledWidth(), image.getScaledHeight())); doc.newPage(); image.setAbsolutePosition(0, 0); doc.add(image); 

It is important to understand that the new page size will only take effect after doc.newPage();

CAVEAT 1: If your PDF file contains only the last slide, you probably put all the slides on one page, and the last slide covers them all. You should call the newPage() method every time you add an image (as it was done in the code snippet in my answer).

CAVEAT 2: Your statement is incorrect. According to the API docs, there is a setPageSize(Rectangle rect) method, maybe you are using the wrong Rectangle class. If you have not followed my advice (which IMHO would not have been reasonable), you are probably looking for com.lowagie.text.Rectangle instead of java.awt.Rectangle .

CAVEAT 3: This is similar to CAVEAT 2, there really are no such methods in the java.awt.Image class, but, as described in the API docs, the com.itextpdf.text.Image class has getScaleWidth() and getScaledHeight() .

+10
source

Source: https://habr.com/ru/post/945990/