Image and Shape

Image and Shape (6)

Friday, 01 November 2019 09:12

Replace Image with New Image in Word in Java

Written by support iceblue

This article demonstrates how to replace an existing image in a Word document with a new image using Spire.Doc for Java.

Below is the screenshot of the original Word document before replacing image:

Replace Image with New Image in Word in Java

import com.spire.doc.Document;
import com.spire.doc.DocumentObject;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.DocPicture;

public class ReplaceImages {
    public static void main(String[] args){
        //Load the Word document
        Document doc = new Document();
        doc.loadFromFile("Images.docx");

        //Get the first section
        Section section = doc.getSections().get(0);

        //Loop through the paragraphs in the section
        for (Paragraph para:(Iterable) section.getParagraphs()
             ) {
            //Loop through the child object in the paragraph
            for (DocumentObject obj:(Iterable) para.getChildObjects()
                 ) {
                //Replace image with new image
                if(obj instanceof DocPicture){
                    DocPicture pic = (DocPicture)obj;
                    pic.loadImage("Hydrangeas.jpg");
                }
            }
        }

        //Save the resultant document
        doc.saveToFile("ReplaceWithImage.docx", FileFormat.Docx_2013);
    }
}

Output:

Replace Image with New Image in Word in Java

Tuesday, 29 October 2019 08:33

Rotate shapes on Word document in Java

Written by support iceblue

This article demonstrates how to rotate shapes on a Word document using Spire.Doc for Java.

import com.spire.doc.Document;
import com.spire.doc.DocumentObject;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.*;
import com.spire.doc.fields.ShapeObject;

public class RotateShape {
    public static void main(String[] args) throws Exception {

               //Load the Sample Word document.
                Document doc = new Document();
                doc.loadFromFile("InsertShapes.docx");

               //Get the first section
                Section sec = doc.getSections().get(0);

                //Traverse every paragraphs to get the shapes and rotate them
                 for ( Paragraph para: (Iterable) sec.getParagraphs()) {
                    for (DocumentObject obj : (Iterable) para.getChildObjects()) {

                      if (obj instanceof ShapeObject) {
                       ((ShapeObject) obj).setRotation(20);

                  }
             }
        }
                //Save to file
                doc.saveToFile("output/RotateShape.docx", FileFormat.Docx);

    }
}

Effective screenshot after rotating the shapes on word:

Rotate shapes on Word document in Java

This article will show you how to set the wrapping style to adjust the position of the image in Java applications with the help of Spire.Doc for Java.

import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextWrappingStyle;
import com.spire.doc.documents.TextWrappingType;
import com.spire.doc.fields.DocPicture;

public class ImageWrappingStyle {
    public static void main(String[] args) throws Exception {

        Document doc = new Document();
        doc.loadFromFile("Sample.docx");

        Section sec = doc.getSections().get(0);

        Paragraph para = sec.getParagraphs().get(0);
        DocPicture picture = para.appendPicture("logo.png");

        //Set image width and height
        picture.setWidth(150f);
        picture.setHeight(125f);

        //Set text wrapping style to Behind
        picture.setTextWrappingStyle(TextWrappingStyle.Behind);
        picture.setTextWrappingType(TextWrappingType.Both);

        //Save the document to file
        doc.saveToFile("Output/WrapStyle.docx");
        doc.close();

    }
}

Effective screenshot after setting the wrapping style for image:

How to wrap text around image on Word document in Java

Tuesday, 11 June 2019 06:51

Java insert WordArt to word document

Written by support iceblue

This article demonstrates how to add WordArt in a Word document in Java applications by using Spire.Doc for Java.

import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.ShapeObject;
import java.awt.*;


public class WordArt{
    public static void main(String[] args) throws Exception {
        //create a Word document.
        Document doc = new Document();

        //add a section
        Section section = doc.addSection();

        //add a paragraph.
        Paragraph paragraph = section.addParagraph();

        //add a shape
        ShapeObject shape = paragraph.appendShape(250, 70, ShapeType.Text_Wave_4);

        //set the position of the shape
        shape.setVerticalPosition(80);
        shape.setHorizontalPosition(100);

        //set the text of WordArt
        shape.getWordArt().setText("Thanks for reading");

        //set the fill color
        shape.setFillColor(Color.RED);

        //set the border color of the text
        shape.setStrokeColor(Color.GRAY);

        //save the document to file.
        doc.saveToFile("output/wordart.docx", FileFormat.Docx_2013);
    }
}

Java insert WordArt to word document

Tuesday, 14 May 2019 05:57

Insert Shapes in Word in Java

Written by support iceblue

This article demonstrates how to add various kinds of shapes and how to group shapes in a Word document using Spire.Doc for Java.

Insert Shapes

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.*;
import com.spire.doc.fields.ShapeObject;

import java.awt.*;

public class InsertShapes {

    public static void main(String[] args) throws Exception {

        //create a Word document.
        Document doc = new Document();

        //add a section and a paragraph
        Section sec = doc.addSection();
        Paragraph para = sec.addParagraph();

        //insert a rectangle
        ShapeObject rectangle = para.appendShape(130, 80, ShapeType.Rectangle);
        rectangle.setFillColor(Color.darkGray);
        rectangle.setStrokeColor(Color.darkGray);
        rectangle.setVerticalPosition(50);

        //insert a triangle
        ShapeObject triangle = para.appendShape((float)(160/Math.sqrt(3)),80, ShapeType.Triangle);
        triangle.setStrokeColor(Color.red);
        triangle.setFillColor(Color.orange);
        triangle.setVerticalPosition(50);
        triangle.setHorizontalPosition(200);

        //insert a ellipse
        ShapeObject circle = para.appendShape(80,80, ShapeType.Ellipse);
        circle.setFillColor(Color.RED);
        circle.setStrokeWeight(15);
        circle.setVerticalPosition(50);
        circle.setHorizontalPosition((float)(270 + 160/Math.sqrt(3)));

        //save to file
        doc.saveToFile("output/InsertShapes.docx", FileFormat.Docx);
    }

}

Insert Shapes in Word in Java

Insert Shape Group

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.ShapeType;
import com.spire.doc.fields.ShapeGroup;
import com.spire.doc.fields.ShapeObject;

import java.awt.*;

public class InsertShapeGroup {

    public static void main(String[] args) {

        //create a Word document
        Document doc = new Document();

        //add a section and a paragraph
        Section sec = doc.addSection();
        Paragraph para = sec.addParagraph();

        //get page width
        float pageWidth = sec.getPageSetup().getClientWidth();

        //add a shape group, specifying width, height and horizontal position
        ShapeGroup shapegroup = para.appendShapeGroup(200, 150);
        shapegroup.setHorizontalPosition((pageWidth - 200) / 2);

        //calculate the scale ratio
        float X = (shapegroup.getWidth() / 1000.0f);
        float Y = (shapegroup.getHeight() / 1000.0f);

        //create a circle
        ShapeObject circle_1 = new ShapeObject(doc, ShapeType.Ellipse);
        circle_1.setWidth(80 / X);
        circle_1.setHeight(80 / Y);
        circle_1.setFillColor(new Color(176, 196, 222));
        circle_1.setStrokeColor(new Color(176, 196, 222));
        circle_1.setHorizontalPosition(60 / X);//set its horizontal position relative to shape group
        
        //add the circle to shape group
        shapegroup.getChildObjects().add(circle_1);

        //add two more circles to shape group
        ShapeObject circle_2 = new ShapeObject(doc, ShapeType.Ellipse);
        circle_2.setWidth(80 / X);
        circle_2.setHeight(80 / Y);
        circle_2.setFillColor(new Color(0, 128, 128));
        circle_2.setStrokeColor(new Color(0, 128, 128));
        circle_2.setHorizontalPosition(30 / X);
        circle_2.setVerticalPosition(50 / Y);
        shapegroup.getChildObjects().add(circle_2);
        ShapeObject circle_3 = new ShapeObject(doc, ShapeType.Ellipse);
        circle_3.setWidth(80 / X);
        circle_3.setHeight(80 / Y);
        circle_3.setFillColor(new Color(72, 61, 139));
        circle_3.setStrokeColor(new Color(72, 61, 139));
        circle_3.setHorizontalPosition(90 / X);
        circle_3.setVerticalPosition(50 / Y);
        shapegroup.getChildObjects().add(circle_3);
        
        //save the document
        doc.saveToFile("output/InsertShapeGroup.docx", FileFormat.Docx_2010);
    }
}

Insert Shapes in Word in Java

Wednesday, 01 May 2019 07:44

Insert Image into Word Document in Java

Written by support iceblue

This article is going to demonstrate how to insert image into a Word document using Spire.Doc for Java.

In Spire.Doc for Java, the appendPicture() method in Paragraph class is used for appending images to Word paragraph. When appending an image, we can customize the image settings like width and height. The image can be from a local file, a byte array or a stream.

import com.spire.doc.*;
import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.ParagraphStyle;
import com.spire.doc.fields.DocPicture;

import java.awt.*;

public class InsertImage {
    public static void main(String[] args){
        //Create a Document object
        Document doc = new Document();
        //Add a section
        Section section = doc.addSection();

        //Add a paragraph to the section
        Paragraph paragraph1 = section.addParagraph();
        //Set paragraph text
        paragraph1.setText("Insert Image Demo by Spire.Doc for Java");

        //Add another paragraph to the section
        Paragraph paragraph2 = section.addParagraph();
        //Append an image from local file to the paragraph
        DocPicture picture = paragraph2.appendPicture("C:\\Users\\Administrator\\Desktop\\Hydrangeas.jpg");
        //Set image width
        picture.setWidth(300f);
        //Set image height
        picture.setHeight(250f);

        //Apply style to paragraph 1
        ParagraphStyle style = new ParagraphStyle(doc);
        style.setName("titleStyle");
        style.getCharacterFormat().setBold(true);
        style.getCharacterFormat().setTextColor(Color.BLUE);
        style.getCharacterFormat().setFontName("Arial");
        style.getCharacterFormat().setFontSize(18f);
        doc.getStyles().add(style);
        paragraph1.applyStyle("titleStyle");

        //Set horizontal alignment for paragraph 1 and paragraph 2
        paragraph1.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        paragraph2.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        
        //Set space after paragraph 1
        paragraph1.getFormat().setAfterSpacing(15f);
        
        //Save the Document object
        doc.saveToFile("InsertImage.docx", FileFormat.Docx_2013);
    }
}

Output:

Insert Image into Word Document in Java