Problems with PDFBox Size

I am new to working with PdfBox and I have a little problem displaying images. I can import an image with a size of 800 * 900 pixels and looks great when viewed in an existing pdf at 100%. However, when the resulting PDF is created using the code below, the image becomes blurry and the image extends beyond the borders of the A4 page.

Is there any other way to select / save images so that they display correctly in pdfbox?

public class PDFtest { /** * @param args the command line arguments */ public static void main(String[] args) throws IOException, COSVisitorException { // TODO code application logic here // Create a document and add a page to it PDDocument document = new PDDocument(); PDPage page = new PDPage(PDPage.PAGE_SIZE_A4); document.addPage(page); // Create a new font object selecting one of the PDF base fonts PDFont font = PDType1Font.HELVETICA_BOLD; InputStream in = new FileInputStream(new File("img.jpg")); PDJpeg img = new PDJpeg(document, in); // Start a new content stream which will "hold" the to be created content PDPageContentStream contentStream = new PDPageContentStream(document, page); // Define a text content stream using the selected font, moving the cursor and drawing the text "Hello World" contentStream.drawImage(img, 10, 700); contentStream.beginText(); contentStream.setFont(font, 12); contentStream.moveTextPositionByAmount(10, 650); contentStream.drawString("Hello World"); contentStream.endText(); // Make sure that the content stream is closed: contentStream.close(); // Save the results and ensure that the document is properly closed: document.save("Hello World.pdf"); document.close(); } 
+5
source share
5 answers

If your intention is the size of an A4 image in PDF format, then I think you find the actual size of a typical A4 format in pixels.

You should also know about the image extension you want to view, for example, jpg, gif or bmp ...

from what I saw in your code, the image sizes are 10 X 700, which I think are pretty small.

 contentStream.drawImage(img, 10, 700); 

And image extension: jpg

 InputStream in = new FileInputStream(new File("img.jpg")); 

check them out and come back for more information.

what all. good luck ''

+1
source

I had the same problem asked in this question, but this answer is incorrect.

After some research, I found a solution.

Instead of using the drawImage function, use the drawXObject function

 contentStream.drawXObject( img, 10, 700, 100, 100 ); 

If the last two numbers indicate the size of the image to be drawn.

+6
source

I would like to point out that with 2.0 the call to the contentStream.drawXObject function in Victor's answer is deprecated. If you want to specify the width and height, you should use contentStream.drawImage(image, x, y, width, height)

+3
source

For a similar situation, for me, with PDF 2.0.11 and a tiff file of 1600 x 2100 size, the following code is ideal for A4 image (portrait). Not sure if the PDFRectangle is ok with you.

I got this example directly from PDFBOX - Example

The only thing I corrected / introduced:

 PDRectangle.A4.getWidth(), PDRectangle.A4.getHeight() 

Here is the complete sample:

 public static void main(String[] args) throws IOException { // if (args.length != 2) // { // System.err.println("usage: " + ImageToPDF.class.getName() + " <image> <output-file>"); // System.exit(1); // } String imagePath = "C:/FAX/sample.tiff"; String pdfPath = "C:/FAX/sample.pdf"; if (!pdfPath.endsWith(".pdf")) { System.err.println("Last argument must be the destination .pdf file"); System.exit(1); } try (PDDocument doc = new PDDocument()) { PDPage page = new PDPage(); doc.addPage(page); // createFromFile is the easiest way with an image file // if you already have the image in a BufferedImage, // call LosslessFactory.createFromImage() instead PDImageXObject pdImage = PDImageXObject.createFromFile(imagePath, doc); // draw the image at full size at (x=20, y=20) try (PDPageContentStream contents = new PDPageContentStream(doc, page)) { // draw the image at full size at (x=20, y=20) contents.drawImage(pdImage, 0, 0, PDRectangle.A4.getWidth(), PDRectangle.A4.getHeight()); // to draw the image at half size at (x=20, y=20) use // contents.drawImage(pdImage, 20, 20, pdImage.getWidth() / 2, pdImage.getHeight() / 2); } doc.save(pdfPath); System.out.println("Tiff converted to PDF succussfully..!"); } } 

Hope it helps.

0
source

According to the new API 2.0.x, you can use PDRectangle to get the width and height of the PDF page. You can use PDPageContentStream to draw an image according to the PDF page. For reference:

 try (PDPageContentStream contents = new PDPageContentStream(pdDocument, pdPage)) { final PDRectangle mediaBox = pdPage.getMediaBox(); final PDImageXObject pdImage = PDImageXObject.createFromFile(image, pdDocument); contents.drawImage(pdImage, 0, 0, mediaBox.getWidth(), mediaBox.getHeight()); } 
0
source

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


All Articles