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.
source share