Skip to main content

ImageFragment Class

An image fragment on a PDF page.

Declaration

public class ImageFragment extends VisualFragment

Remarks

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.

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);
            }
        }
    }
}

Refer to the following help topic for additional information: Add Images to PDF Documents.

Inherited Members

com.devexpress.system.JavaObject.clone()
com.devexpress.system.JavaObject.equals(java.lang.Object)
com.devexpress.system.JavaObject.hashCode()
com.devexpress.system.JavaObject.memberwiseClone()
com.devexpress.system.JavaObject.toString()
java.lang.Object.finalize()
java.lang.Object.getClass()
java.lang.Object.notify()
java.lang.Object.notifyAll()
java.lang.Object.wait()
java.lang.Object.wait(long)
java.lang.Object.wait(long,int)

Inheritance

Object
JavaObject

ImageFragment(DXImage image)

Initializes a new instance of the ImageFragment class with specified settings.

Declaration

public ImageFragment(DXImage image)

Parameters

Name Type Description
image DXImage

The image assigned to the fragment.

Methods

getImage() Method

Returns the image assigned to this fragment.

Declaration

public DXImage getImage()

Returns

Type Description
DXImage

The image assigned to this fragment.

setImage(DXImage value) Method

Sets the image assigned to this fragment.

Declaration

public void setImage(DXImage value)

Parameters

Name Type Description
value DXImage

The image assigned to this fragment.