Images To PDF
Developers can use IronPDF to combine multiple images into a single PDF document.
The PdfDocument.fromImage
method accepts a list of Path
objects, each of which references a valid path to an image stored on a local file system. This gives the developer a lot of freedom in specificity. The developer can build this list to include groups of images spanning multiple directories, to include images matching specific naming or typing criteria, etc. If the images are sourced from the same directory (as is the case in the example below), the developer can simply utilize a DirectoryStream
among other java.nio.file
classes to quickly build up a list of all the images contained in the directory.
PdfDocument.fromImage
will render each image referenced in the list on a separate page in the PDF document.
If the images need to be placed in separate PDF documents (and not combined into one), then developers can do either of the following:
- Iteratively call
PdfDocument.fromImage
using lists containing only onePath
object, e.g.:
// Creating a PDF document for each image individually
List<Path> imagePaths = List.of(
Paths.get("path/to/image1.png"),
Paths.get("path/to/image2.png")
);
for (Path imagePath : imagePaths) {
// Create a new document for each image
PdfDocument document = PdfDocument.fromImage(List.of(imagePath));
// Save the document with a distinct name
document.save("path/to/single/image_" + imagePath.getFileName() + ".pdf");
}
// Creating a PDF document for each image individually
List<Path> imagePaths = List.of(
Paths.get("path/to/image1.png"),
Paths.get("path/to/image2.png")
);
for (Path imagePath : imagePaths) {
// Create a new document for each image
PdfDocument document = PdfDocument.fromImage(List.of(imagePath));
// Save the document with a distinct name
document.save("path/to/single/image_" + imagePath.getFileName() + ".pdf");
}
- Combine all images into one PDF document as shown in the example above, and then copy each page into new PDF documents with the
PdfDocument.copyPage
method.
// Assuming all images are combined into a single PDF document
PdfDocument combinedDocument = PdfDocument.fromImage(imagePaths);
// Create individual documents for each page/image in the combined document
for (int i = 0; i < combinedDocument.getPageCount(); i++) {
PdfDocument singlePageDocument = PdfDocument.create();
// Copy the page from the combined document to the new singlePageDocument
singlePageDocument.copyPage(combinedDocument, i);
// Save individual page document
singlePageDocument.save("path/to/single/imagePage_" + i + ".pdf");
}
// Assuming all images are combined into a single PDF document
PdfDocument combinedDocument = PdfDocument.fromImage(imagePaths);
// Create individual documents for each page/image in the combined document
for (int i = 0; i < combinedDocument.getPageCount(); i++) {
PdfDocument singlePageDocument = PdfDocument.create();
// Copy the page from the combined document to the new singlePageDocument
singlePageDocument.copyPage(combinedDocument, i);
// Save individual page document
singlePageDocument.save("path/to/single/imagePage_" + i + ".pdf");
}
For more detailed information on how to work with PDF documents using IronPDF, visit the IronPDF Developer Documentation.