Skip to main content

Add Images to PDF Documents

  • 2 minutes to read

The PDF Document API allows you to add raster and vector images to PDF pages. An ImageFragment object specifies an image and its position, size, and transformations.

Supported Image Formats

  • BMP
  • EMF, EMF+
  • GIF
  • JPEG
  • PNG
  • SVG
  • TIFF

Add an Image

Use the addImageFragment(DXImage image, float x, float y) method to add an image with its original size at the specified location.

The following code snippet creates a PDF document, loads an image from a file, places it on a Letter-size page at a specified position, and saves the result to a PDF file:

import com.devexpress.docs.pdf.*;
import com.devexpress.drawing.*;
import com.devexpress.drawing.printing.*;

import java.io.ByteArrayInputStream;
import java.nio.channels.*;
import java.nio.file.*;

public class Main {
    public static void main(String[] args) throws Exception {
        byte[] imageData = Files.readAllBytes(Path.of("devexpress-logo.png"));

        try (PdfDocument pdfDocument = new PdfDocument();
            DXImage image = DXImage.fromStream(new ByteArrayInputStream(imageData))) {

            // Add a Letter-size page to the document.
            Page page = pdfDocument.getPages().add(DXPaperKind.LETTER);

            page.addImageFragment(image, 100, 500);

            // Save the document to a PDF file.
            try (WritableByteChannel writableByteChannel =
                     FileChannel.open(Path.of("result.pdf"),
                         StandardOpenOption.CREATE,
                         StandardOpenOption.WRITE,
                         StandardOpenOption.TRUNCATE_EXISTING)) {

                pdfDocument.save(writableByteChannel);
            }
        }
    }
}

Customize Image Appearance and Apply Transformations

Create an ImageFragment object to specify the image source, position, size, and transformations such as scaling, rotation, and skew.

Use the Page.addFragment() method to add the image fragment to the page.

import com.devexpress.docs.pdf.*;
import com.devexpress.drawing.*;
import com.devexpress.system.drawing.*;
import com.devexpress.drawing.printing.*;

import java.io.ByteArrayInputStream;
import java.nio.channels.*;
import java.nio.file.*;

public class Main {
    public static void main(String[] args) throws Exception {
        byte[] imageData = Files.readAllBytes(Path.of("devexpress-logo.png"));

        try (PdfDocument pdfDocument = new PdfDocument();
            DXImage image = DXImage.fromStream(new ByteArrayInputStream(imageData))) {

            // Add a Letter page to the document.
            Page page = pdfDocument.getPages().add(DXPaperKind.LETTER);

            // Create and customize an image fragment.
            ImageFragment imageFragment = new ImageFragment(image);
            imageFragment.setImage(image);
            imageFragment.setLocation(new PointF(100, 500));
            imageFragment.setSkewX(20);

            // Add the image fragment to the page.
            page.addFragment(imageFragment);

            // Save the document to a PDF file.
            try (WritableByteChannel writableByteChannel =
                     FileChannel.open(Path.of("result.pdf"),
                         StandardOpenOption.CREATE,
                         StandardOpenOption.WRITE,
                         StandardOpenOption.TRUNCATE_EXISTING)) {

                pdfDocument.save(writableByteChannel);
            }
        }
    }
}

Next Steps