News Category

Image and Shape

Image and Shape (7)

Stamps can guarantee the authenticity and validity of a document and also make the document look more professional. Since Microsoft Word doesn't provide a built-in stamp feature, you can add an image to your Word documents to mimic the stamp effect. This is useful when the document will be printed to paper or PDF. In this article, you will learn how to add a "stamp" to a Word document using Spire.Doc for Java.

Install Spire.Doc for Java

First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>12.4.6</version>
    </dependency>
</dependencies>
    

Add an Image Stamp to Word Document

Spire.Doc for Java allow developers to use the core classes and method listed in the below table to add and format an image to make it look like a stamp in the Word document.

Name Description
DocPicture Class Represents a picture in a Word document.
Paragraph.appendPicture() Method Appends an image to end of paragraph.
DocPicture.setHorizontalPosition() Method Sets absolute horizontal position of the picture.
DocPicture.setVerticalPosition() Method Sets absolute vertical position of the picture.
DocPicture.setWidth() Method Sets picture width.
DocPicture.setHeight Method Sets picture height.
DocPicture.setTextWrappingStyle() Method Sets text wrapping type of the picture.

The detailed steps are as follows:

  • Create a Document instance.
  • Load a Word document using Document.loadFromFile() method.
  • Get the specific paragraph using ParagraphCollection.get() method.
  • Add an image to the Word document using Paragraph.appendPicture() method.
  • Set position, size and wrapping style of the image using the methods offered by DocPicture class.
  • Save the document to another file using Document.saveToFile() method.
  • Java
import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextWrappingStyle;
import com.spire.doc.fields.DocPicture;

public class AddStamp {
    public static void main(String[] args) {
        //Create a Document instance
        Document doc = new Document();

        //Load a Word document
        doc.loadFromFile("test.docx");

        //Get the specific paragraph
        Section section = doc.getSections().get(0);
        Paragraph paragraph = section.getParagraphs().get(4);

        //Add an image 
        DocPicture picture = paragraph.appendPicture("cert.png");

        //Set the position of the image
        picture.setHorizontalPosition(240f);
        picture.setVerticalPosition(120f);

        //Set width and height of the image
        picture.setWidth(150);
        picture.setHeight(150);

        //Set wrapping style of the image to In_Front_Of_Text, so that it looks like a stamp
        picture.setTextWrappingStyle(TextWrappingStyle.In_Front_Of_Text);

        //Save the document to file
        doc.saveToFile("AddStamp.docx", FileFormat.Docx);
        doc.dispose();
    }
}

Java: Add an Image Stamp to a Word Document

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

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

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

Java: Insert WordArt in Word

2022-06-24 06:51:00 Written by support iceblue

In MS Word, WordArt is used to insert text with special effects such as shadows, outlines, colors, gradients, and 3D effects. It can be helpful when creating stylish headlines or highlighting specific content. In this article, you will learn how to programmatically insert WordArt in a Word document using Spire.Doc for Java.

Install Spire.Doc for Java

First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>12.4.6</version>
    </dependency>
</dependencies>
    

Insert WordArt in Word

The ShapeType enumeration provided by Spire.Doc for Java defines a variety of WordArt shape types whose names begin with "Text". In order to create a WordArt in Word, you need to initialize an instance of ShapeObject and specify the WordArt type and text content. The detailed steps are as follows:

  • Create a Document instance.
  • Add a section to the document using Document.addSection() method, and then add a paragraph to the section using Section.addParagraph() method.
  • Append a shape to the paragraph and specify the shape size and type using Paragraph.appendShape(float width, float height, ShapeType shapeType) method.
  • Set the position of the shape using ShapeObject.setVerticalPosition() and ShapeObject.setHorizontalPosition() methods.
  • Set the text of WordArt using ShapeObject.getWordArt().setText() method.
  • Set the fill color and stroke color of WordArt using ShapeObject.setFillColor() and ShapeObject.setStrokeColor() methods.
  • Save the document to another file using Document.saveToFile() method.
  • 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 Document instance
        Document doc = new Document();

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

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

        //Add a shape to the paragraph and specify the shape size and type
        ShapeObject shape = paragraph.appendShape(400, 150, ShapeType.Text_Deflate_Bottom);

        //Set the position of the shape
        shape.setVerticalPosition(60);
        shape.setHorizontalPosition(60);

        //set the text of WordArt
        shape.getWordArt().setText("Create WordArt in Word");

        // Set the fill color and stroke color of WordArt
        shape.setFillColor(Color.CYAN);
        shape.setStrokeColor(Color.BLUE);

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

Java: Insert WordArt in Word

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Insert Shapes in Word in Java

2019-05-14 05:57:22 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

MS Word allows users to insert a lot of elements in Word documents, including text, images, charts, and files. Images are frequently used in Word documents to make them intuitive, to express ideas that are hard to express in words, or just to make them beautiful. This article provides you a convenient and efficient way to insert images to Word documents under the help of Spire.Doc for Java.

Install Spire.Doc for Java

First, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>12.4.6</version>
    </dependency>
</dependencies>
    

Insert an Image to a Word Document with Specified Text Wrapping Style

The detailed steps of inserting images with specified wrapping style are as follows:

  • Create an object of Document class.
  • Load a Word document from disk using Document.loadFromFile() method.
  • Create an object of DocPicture class.
  • Load an image from disk using DocPicture.loadImage() method.
  • Set the size of the image using DocPicture.setWidth() and DocPicture.setHeight() method.
  • Set the text wrapping style of the image as Square using DocPicture.setTextWrappingStyle() method.
  • Insert the image at the start of the second paragraph using Paragraph.getChildObjects().insert() method.
  • Save the document using Document.saveToFile method.
  • Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;

public class insertImage {
    public static void main(String[] args) throws Exception {
        //Create an object of Document class
        Document doc = new Document();

        //Load a Word document from disk
        doc.loadFromFile("D:/Samples/Sample.docx");

        //Create an object of DocPicture class
        DocPicture picture = new DocPicture(doc);

        //Load an image from disk
        picture.loadImage("D:/Samples/System.png");

        //Set the size of the image
        picture.setWidth(75);
        picture.setHeight(90);

        //Set the text wrapping style of the image as Square
        picture.setTextWrappingStyle( TextWrappingStyle.Square);

        //Insert the image at the start of the second paragraph
        doc.getSections().get(0).getParagraphs().get(1).getChildObjects().insert(0,picture);

        //Save the document
        doc.saveToFile("D:/javaOutput/insertImage.docx", FileFormat.Docx);
    }
}

Java: Insert Images to Word Documents

Insert an Image to a Word Document at a Specific Location

The detailed steps of inserting images at a specific location are as follows:

  • Create an object of Document class.
  • Load a Word document from disk using Document.loadFromFile() method.
  • Create an object of DocPicture class.
  • Load an image from disk using DocPicture.loadImage() method.
  • Set the size of the image using DocPicture.setWidth() and DocPicture.setHeight() method.
  • Set the text wrapping style of the image as Tight using DocPicture.setTextWrappingStyle() method.
  • Insert the image into the third paragraph using Paragraph.getChildObjects().insert() method.
  • Set the position of the image using DocPicture.setHorizontalPosition() and DocPicture.setVerticalPositon() method. The original location is at the start of the selected paragraph.
  • Save the document using Document.saveToFile method.
  • Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;

public class insertImage {
    public static void main(String[] args) throws Exception {
        //Create an object of Document class
        Document doc = new Document();

        //Load a Word document from disk
        doc.loadFromFile("D:/Samples/Sample.docx");

        //Create an object of DocPicture class
        DocPicture picture = new DocPicture(doc);

        //Load an image from disk
        picture.loadImage("D:/Samples/PDF.png");

        //Set the size of the image
        picture.setWidth(75);
        picture.setHeight(90);

        //Set the text wrapping style of the image as Tight
        picture.setTextWrappingStyle( TextWrappingStyle.Tight);

        //Insert the image into the Third paragraph
        doc.getSections().get(0).getParagraphs().get(2).getChildObjects().insert(0,picture);

        //Set the position of the image
        picture.setHorizontalPosition(370.0F);
        picture.setVerticalPosition(10.0F);

        //Save the document
        doc.saveToFile("D:/javaOutput/insertImage.docx", FileFormat.Docx);
    }
}

Java: Insert Images to Word Documents

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.