Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert Images to PDF in Java
// Convert Images to PDF in Java. JPG, PNG, WebP, GIF, TGA and many more ...
Converter converter = new Converter("path/image.jpg");
converter.convert("output/convertedImage.pdf", new PdfConvertOptions());
// Convert JPG, PNG or other Images to PDF in Java. Apply Watermark, Resize, Set DPI and set margins.
Converter converter = new Converter("path/image.jpg", new ImageLoadOptions());
// Set the PDF conversion options
PdfConvertOptions options = new PdfConvertOptions();
options.setDpi(200);
// Set Margins
options.setMarginBottom(10);
options.setMarginLeft(10);
options.setMarginRight(10);
options.setMarginTop(10);
//options.setRotate(Rotation.On90); // Rotation
options.setWidth(640);
options.setHeight(426);
// Apply watermark to Image in PDF
WatermarkOptions watermarkOptions = new WatermarkOptions();
watermarkOptions.setText("Watermark");
watermarkOptions.setColor(Color.WHITE);
watermarkOptions.setRotationAngle(-45);
watermarkOptions.setTransparency(0.1);
watermarkOptions.setLeft(10);
watermarkOptions.setTop(75);
options.setWatermark(watermarkOptions);
// Save the converted PDF file
converter.convert("output/convertedJpgToPdfAdv.pdf", options);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment