public class ImageSize
Example:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// By default, the image is inserted at 100% scale
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
// Reduce the overall size of the shape by 50%
shape.setWidth(shape.getWidth() * 0.5);
shape.setHeight(shape.getHeight() * 0.5);
Assert.assertEquals(75.0d, shape.getWidth());
Assert.assertEquals(75.0d, shape.getHeight());
// However, we can also go back to the original image size and scale from there, for example, to 110%
ImageSize imageSize = shape.getImageData().getImageSize();
shape.setWidth(imageSize.getWidthPoints() * 1.1);
shape.setHeight(imageSize.getHeightPoints() * 1.1);
Assert.assertEquals(330.0d, shape.getWidth());
Assert.assertEquals(330.0d, shape.getHeight());
doc.save(getArtifactsDir() + "Image.ScaleImage.docx");
Constructor Summary |
---|
ImageSize(intwidthPixels, intheightPixels)
Initializes width and height to the given values in pixels. Initializes resolution to 96 dpi. |
ImageSize(intwidthPixels, intheightPixels, doublehorizontalResolution, doubleverticalResolution)
Initializes width, height and resolution to the given values. |
Property Getters/Setters Summary | ||
---|---|---|
int | getHeightPixels() | |
Gets the height of the image in pixels.
|
||
double | getHeightPoints() | |
Gets the height of the image in points. 1 point is 1/72 inch.
|
||
double | getHorizontalResolution() | |
Gets the horizontal resolution in DPI.
|
||
double | getVerticalResolution() | |
Gets the vertical resolution in DPI.
|
||
int | getWidthPixels() | |
Gets the width of the image in pixels.
|
||
double | getWidthPoints() | |
Gets the width of the image in points. 1 point is 1/72 inch.
|
public ImageSize(int widthPixels, int heightPixels)
widthPixels
- Width in pixels.heightPixels
- Height in pixels.public ImageSize(int widthPixels, int heightPixels, double horizontalResolution, double verticalResolution)
widthPixels
- Width in pixels.heightPixels
- Height in pixels.horizontalResolution
- Horizontal resolution in DPI.verticalResolution
- Vertical resolution in DPI.public int getHeightPixels()
Example:
Shows how to read the attributes of an image in a shape.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert a shape into the document which contains an image taken from our local file system. Shape shape = builder.insertImage(getImageDir() + "Logo.jpg"); // If the shape contains an image, its ImageData property will be valid, // and it will contain an ImageSize object. ImageSize imageSize = shape.getImageData().getImageSize(); // The ImageSize object contains read-only information about the image within the shape. Assert.assertEquals(imageSize.getHeightPixels(), 400); Assert.assertEquals(imageSize.getWidthPixels(), 400); final double delta = 0.05; Assert.assertEquals(imageSize.getHorizontalResolution(), 95.98d, delta); Assert.assertEquals(imageSize.getVerticalResolution(), 95.98d, delta); // We can base the size of the shape on the size of its image to avoid stretching the image. shape.setWidth(imageSize.getWidthPoints() * 2.0); shape.setHeight(imageSize.getHeightPoints() * 2.0); doc.save(getArtifactsDir() + "Drawing.ImageSize.docx");
public double getHeightPoints()
Example:
Shows how to resize a shape with an image.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // By default, the image is inserted at 100% scale Shape shape = builder.insertImage(getImageDir() + "Logo.jpg"); // Reduce the overall size of the shape by 50% shape.setWidth(shape.getWidth() * 0.5); shape.setHeight(shape.getHeight() * 0.5); Assert.assertEquals(75.0d, shape.getWidth()); Assert.assertEquals(75.0d, shape.getHeight()); // However, we can also go back to the original image size and scale from there, for example, to 110% ImageSize imageSize = shape.getImageData().getImageSize(); shape.setWidth(imageSize.getWidthPoints() * 1.1); shape.setHeight(imageSize.getHeightPoints() * 1.1); Assert.assertEquals(330.0d, shape.getWidth()); Assert.assertEquals(330.0d, shape.getHeight()); doc.save(getArtifactsDir() + "Image.ScaleImage.docx");
public double getHorizontalResolution()
Example:
Shows how to read the attributes of an image in a shape.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert a shape into the document which contains an image taken from our local file system. Shape shape = builder.insertImage(getImageDir() + "Logo.jpg"); // If the shape contains an image, its ImageData property will be valid, // and it will contain an ImageSize object. ImageSize imageSize = shape.getImageData().getImageSize(); // The ImageSize object contains read-only information about the image within the shape. Assert.assertEquals(imageSize.getHeightPixels(), 400); Assert.assertEquals(imageSize.getWidthPixels(), 400); final double delta = 0.05; Assert.assertEquals(imageSize.getHorizontalResolution(), 95.98d, delta); Assert.assertEquals(imageSize.getVerticalResolution(), 95.98d, delta); // We can base the size of the shape on the size of its image to avoid stretching the image. shape.setWidth(imageSize.getWidthPoints() * 2.0); shape.setHeight(imageSize.getHeightPoints() * 2.0); doc.save(getArtifactsDir() + "Drawing.ImageSize.docx");
public double getVerticalResolution()
Example:
Shows how to read the attributes of an image in a shape.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert a shape into the document which contains an image taken from our local file system. Shape shape = builder.insertImage(getImageDir() + "Logo.jpg"); // If the shape contains an image, its ImageData property will be valid, // and it will contain an ImageSize object. ImageSize imageSize = shape.getImageData().getImageSize(); // The ImageSize object contains read-only information about the image within the shape. Assert.assertEquals(imageSize.getHeightPixels(), 400); Assert.assertEquals(imageSize.getWidthPixels(), 400); final double delta = 0.05; Assert.assertEquals(imageSize.getHorizontalResolution(), 95.98d, delta); Assert.assertEquals(imageSize.getVerticalResolution(), 95.98d, delta); // We can base the size of the shape on the size of its image to avoid stretching the image. shape.setWidth(imageSize.getWidthPoints() * 2.0); shape.setHeight(imageSize.getHeightPoints() * 2.0); doc.save(getArtifactsDir() + "Drawing.ImageSize.docx");
public int getWidthPixels()
Example:
Shows how to read the attributes of an image in a shape.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert a shape into the document which contains an image taken from our local file system. Shape shape = builder.insertImage(getImageDir() + "Logo.jpg"); // If the shape contains an image, its ImageData property will be valid, // and it will contain an ImageSize object. ImageSize imageSize = shape.getImageData().getImageSize(); // The ImageSize object contains read-only information about the image within the shape. Assert.assertEquals(imageSize.getHeightPixels(), 400); Assert.assertEquals(imageSize.getWidthPixels(), 400); final double delta = 0.05; Assert.assertEquals(imageSize.getHorizontalResolution(), 95.98d, delta); Assert.assertEquals(imageSize.getVerticalResolution(), 95.98d, delta); // We can base the size of the shape on the size of its image to avoid stretching the image. shape.setWidth(imageSize.getWidthPoints() * 2.0); shape.setHeight(imageSize.getHeightPoints() * 2.0); doc.save(getArtifactsDir() + "Drawing.ImageSize.docx");
public double getWidthPoints()
Example:
Shows how to resize a shape with an image.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // By default, the image is inserted at 100% scale Shape shape = builder.insertImage(getImageDir() + "Logo.jpg"); // Reduce the overall size of the shape by 50% shape.setWidth(shape.getWidth() * 0.5); shape.setHeight(shape.getHeight() * 0.5); Assert.assertEquals(75.0d, shape.getWidth()); Assert.assertEquals(75.0d, shape.getHeight()); // However, we can also go back to the original image size and scale from there, for example, to 110% ImageSize imageSize = shape.getImageData().getImageSize(); shape.setWidth(imageSize.getWidthPoints() * 1.1); shape.setHeight(imageSize.getHeightPoints() * 1.1); Assert.assertEquals(330.0d, shape.getWidth()); Assert.assertEquals(330.0d, shape.getHeight()); doc.save(getArtifactsDir() + "Image.ScaleImage.docx");