Independentsoft
- any library, any programming language
Home
Purchase
Support
Company
Contact
JWord
>
Tutorial
> Scale image
The example shows you how to scale/resize image.
import com.independentsoft.office.Unit; import com.independentsoft.office.UnitType; import com.independentsoft.office.drawing.Extents; import com.independentsoft.office.drawing.FillRectangle; import com.independentsoft.office.drawing.Offset; import com.independentsoft.office.drawing.Picture; import com.independentsoft.office.drawing.PresetGeometry; import com.independentsoft.office.drawing.ShapeType; import com.independentsoft.office.drawing.Stretch; import com.independentsoft.office.drawing.Transform2D; import com.independentsoft.office.word.DrawingObject; import com.independentsoft.office.word.Paragraph; import com.independentsoft.office.word.Run; import com.independentsoft.office.word.WordDocument; import com.independentsoft.office.word.drawing.DrawingObjectSize; import com.independentsoft.office.word.drawing.Inline; public class Example { public static void main(String[] args) { try { WordDocument doc = new WordDocument(); Picture picture = new Picture("c:\\test\\image1.png"); Unit pictureWidth = new Unit(180, UnitType.PIXEL); Unit pictureHeight = new Unit(240, UnitType.PIXEL); Offset offset = new Offset(0, 0); Extents extents = new Extents(pictureWidth, pictureHeight); picture.getShapeProperties().setPresetGeometry(new PresetGeometry(ShapeType.RECTANGLE)); picture.getShapeProperties().setTransform2D(new Transform2D(offset, extents)); picture.setID("1"); picture.setName("image1.png"); Stretch stretch = new Stretch(); //important to scale image stretch.setFillRectangle(new FillRectangle()); picture.setStretch(stretch); Inline inline = new Inline(picture); inline.setSize(new DrawingObjectSize(pictureWidth, pictureHeight)); inline.setID("1"); inline.setName("Picture 1"); inline.setDescription("image1.png"); DrawingObject drawingObject = new DrawingObject(inline); Run imageRun = new Run(); imageRun.add(drawingObject); Paragraph imageParagraph = new Paragraph(); imageParagraph.add(imageRun); doc.getBody().add(imageParagraph); doc.save("c:\\test\\output.docx", true); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } }
Need help? Ask our developers:
Name*
Email*
Message*