Add Image to PDF using Java
Contents
[
Hide
]
Aspose.PDF for Java supports both high-level image placement and low-level operator-based drawing.
Add an image with page coordinates
Use this example when you need to place an image at a fixed position on a PDF page.
- Create a new PDF Document and add a page.
- Call
page.addImage()with the source image path and target rectangle. - Save the generated PDF file.
public static void addImage(Path imageFile, Path outputFile) {
try (Document document = new Document()) {
Page page = document.getPages().add();
page.addImage(imageFile.toString(), new Rectangle(20, 730, 120, 830, true));
document.save(outputFile.toString());
}
}
Add an image with page operators
Use this example when you need low-level control over image placement and scaling through page operators.
- Create a new PDF Document and open the source image stream.
- Add the image to the page resources and calculate the target rectangle.
- Write the required graphics operators and save the document.
public static void addImageUsingOperators(Path imageFile, Path outputFile) throws Exception {
try (Document document = new Document();
InputStream imageStream = Files.newInputStream(imageFile)) {
Page page = document.getPages().add();
page.setPageSize(842, 595);
XImageCollection resourcesImages = page.getResources().getImages();
String imageId = resourcesImages.add(imageStream);
XImage xImage = resourcesImages.get_Item(resourcesImages.size());
Rectangle rectangle = new Rectangle(
0,
0,
page.getMediaBox().getWidth(),
(page.getMediaBox().getWidth() * xImage.getHeight()) / xImage.getWidth(),
true);
page.getContents().add(new GSave());
Matrix matrix = new Matrix(
rectangle.getURX() - rectangle.getLLX(),
0,
0,
rectangle.getURY() - rectangle.getLLY(),
rectangle.getLLX(),
rectangle.getLLX() + (page.getMediaBox().getHeight() - rectangle.getHeight()) / 2);
page.getContents().add(new ConcatenateMatrix(matrix));
page.getContents().add(new Do(imageId));
page.getContents().add(new GRestore());
document.save(outputFile.toString());
}
}
Add an image and set alternative text
Use this example when the image should include accessibility metadata for screen readers.
- Create a new PDF Document and add the image to the page.
- Get the inserted XImage from page resources.
- Set the alternative text and save the PDF.
public static void addImageSetAlternativeTextForImage(Path imageFile, Path outputFile) {
try (Document document = new Document()) {
Page page = document.getPages().add();
page.setPageSize(842, 595);
page.addImage(imageFile.toString(), new Rectangle(0, 0, 842, 595, true));
XImage xImage = page.getResources().getImages().get_Item(1);
boolean result = xImage.trySetAlternativeText("Alternative text for image", page);
if (result) {
System.out.println("Text has been added successfuly");
}
document.save(outputFile.toString());
}
}
Add an image with Flate compression
Use this example when you want to embed image data by using Flate compression.
- Create a new PDF Document and open the image stream.
- Add the image to page resources with
ImageFilterType.Flate. - Draw the image through page operators and save the result.
public static void addImageToPdfWithFlateCompression(Path imageFile, Path outputFile) throws Exception {
try (Document document = new Document();
InputStream imageStream = Files.newInputStream(imageFile)) {
Page page = document.getPages().add();
XImageCollection resourcesImages = page.getResources().getImages();
String imageId = resourcesImages.add(imageStream, ImageFilterType.Flate);
page.getContents().add(new GSave());
Rectangle rectangle = new Rectangle(0, 0, 600, 600, true);
Matrix matrix = new Matrix(
rectangle.getURX() - rectangle.getLLX(),
0,
0,
rectangle.getURY() - rectangle.getLLY(),
rectangle.getLLX(),
rectangle.getLLY());
page.getContents().add(new ConcatenateMatrix(matrix));
page.getContents().add(new Do(imageId));
page.getContents().add(new GRestore());
document.save(outputFile.toString());
}
}