Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created February 28, 2021 06:04
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 aspose-com-gists/8cc0eeecadf75ea93d5d8d6237c8a940 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/8cc0eeecadf75ea93d5d8d6237c8a940 to your computer and use it in GitHub Desktop.
Generate PDF Files from Images Java
// Instantiate Document Object
Document doc = new Document();
// Access image files in the folder
String imageDir = "D:/Images/";
File file = new File(imageDir);
String[] fileList = file.list();
for (String fileName : fileList) {
// Add a page to pages collection of document
Page page = doc.getPages().add();
// Load the source image file to Stream object
java.io.FileInputStream fs = new java.io.FileInputStream(imageDir + fileName);
// Set margins so image will fit, etc.
page.getPageInfo().getMargin().setBottom(0);
page.getPageInfo().getMargin().setTop(0);
page.getPageInfo().getMargin().setLeft(0);
page.getPageInfo().getMargin().setRight(0);
page.setCropBox(new com.aspose.pdf.Rectangle(0, 0, 400, 400));
// Create an image object
Image image1 = new Image();
// Add the image into paragraphs collection of the section
page.getParagraphs().add(image1);
// Set the image file stream
image1.setImageStream(fs);
}
// Save resultant PDF file
doc.save("document.pdf");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment