Skip to content

Instantly share code, notes, and snippets.

@GroupDocsGists
Last active May 20, 2021 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GroupDocsGists/d5a680abe2fd15a68e8ceca277e3b59c to your computer and use it in GitHub Desktop.
Save GroupDocsGists/d5a680abe2fd15a68e8ceca277e3b59c to your computer and use it in GitHub Desktop.
Convert Images to PDF in Java
// Convert Images to PDF in Java. PNG, WebP, GIF, BMP, TGA and many more ...
Converter converter = new Converter("path/image.png");
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);
// Convert JPG Images to PDF in Java.
Converter converter = new Converter("path/image.jpg");
converter.convert("output/convertedJpg.pdf", new PdfConvertOptions());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment