Skip to content
ComPDF

Images

Adding Images

java
// Create an image from a file path
CImage image = div.createImageElement("path/to/image.png");

// Set the image size (in points)
image.setWidth(300);
image.setHeight(200);

// Or use a percentage value
image.setWidth(CUnitValue.CreatePercentValue(50));

Image Scaling

java
// Scale proportionally
image.scale(0.5f, 0.5f); // Scale to 50%

// Fit to a specified size (keep aspect ratio)
image.scaleToFit(400, 300);

// Scale to an exact size (may distort the image)
image.scaleAbsolute(400, 300);

// Auto scaling
image.setAutoScale(true);
image.setAutoScaleWidth(true);
image.setAutoScaleHeight(true);

Image Positioning

java
// Absolute positioning
image.setAbsolutePosition(100, 100);

// Set margin
image.setMargin(10);
image.setMargins(10, 20, 10, 20); // Top, Right, Bottom, Left

// Set padding
image.setPadding(5);