Java: Insert WordArt in Word

2022-06-24 06:51:00 Written by Koohji

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>13.11.2</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.

The properties of a document are a set of information about the document and its content, such as title, subject, author's name, manager, company, category, keywords (also known as tags), and comments. This information does not appear in the content of the document, but it can help you better search, sort, and filter files. In this article, you will learn how to add document properties to Word documents in Java using 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>13.11.2</version>
    </dependency>
</dependencies>

Add Built-in Document Properties to a Word Document in Java

A built-in document property (also known as a standard document property) consists of a name and a value. You cannot set or change the name of a built-in document property as it’s predefined by Microsoft Word, but you can set or change its value. The following steps demonstrate how to set values for built-in document properties in a Word document in Java using Spire.Doc for Java:

  • Initialize an instance of Document class.
  • Load a Word document using Document.loadFromFile() method.
  • Access the built-in document properties of the document using Document.getBuiltinDocumentProperties() method.
  • Set the values of specific document properties such as title, subject and author using setTitle(), setSubject() and setAuthor() methods provided by BuiltinDocumentProperties class.
  • Save the result document using Document.saveToFile() method.
  • Java
import com.spire.doc.BuiltinDocumentProperties;
import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class AddBuiltinDocumentProperties {
    public static void main(String []args) throws Exception {
        //Create a Document instance
        Document document = new Document();
        //Load a Word document
        document.loadFromFile("Sample.docx");

        //Access the built-in document properties of the document
        BuiltinDocumentProperties standardProperties = document.getBuiltinDocumentProperties();
        //Set the values of specific built-in document properties 
        standardProperties.setTitle("Add Document Properties");
        standardProperties.setSubject("Java Example");
        standardProperties.setAuthor("James");
        standardProperties.setCompany("Eiceblue");
        standardProperties.setManager("Michael");
        standardProperties.setCategory("Document Manipulation");
        standardProperties.setKeywords("Java, Word, Document Properties");
        standardProperties.setComments("This article shows how to add document properties");

        //Save the result document
        document.saveToFile("AddStandardDocumentProperties.docx", FileFormat.Docx_2013);
    }
}

Java: Add Document Properties to Word Documents

Add Custom Document Properties to a Word Document in Java

A custom document property can be defined by a document author or user. Each custom document property should contain a name, a value and a data type. The data type can be one of these four types: Text, Date, Number and Yes or No. The following steps demonstrate how to add custom document properties with different data types to a Word document in Java using Spire.Doc for Java:

  • Initialize an instance of Document class.
  • Load a Word document using Document.loadFromFile() method.
  • Access the custom document properties of the document using Document.getCustomDocumentProperties() method.
  • Add custom document properties with different data types to the document using CustomDocumentProperties.add(String, Object) method.
  • Save the result document using Document.saveToFile() method.
  • Java
import com.spire.doc.CustomDocumentProperties;
import com.spire.doc.Document;
import com.spire.doc.FileFormat;

import java.util.Date;

public class AddCustomDocumentProperties {
    public static void main(String []args) throws Exception {
        //Create a Document instance
        Document document = new Document();
        //Load a Word document
        document.loadFromFile("Sample.docx");

        //Access the custom document properties of the document
        CustomDocumentProperties customProperties = document.getCustomDocumentProperties();
        //Add custom document properties with different data types to the document
        customProperties.add("Document ID", 1);
        customProperties.add("Authorized", true);
        customProperties.add("Authorized By", "John Smith");
        customProperties.add("Authorized Date", new Date());

        //Save the result document
        document.saveToFile("AddCustomDocumentProperties.docx", FileFormat.Docx_2013);
    }
}

Java: Add Document Properties 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.

Java: Merge Word Documents

2022-07-04 09:15:00 Written by zaki zou

Sometimes we need to merge several related Word documents to make a more complete one. To merge documents with MS Word, you need to manually copy and paste contents or import contents from other documents, which can be tedious. Luckily, Spire.Doc for Java provides 2 easy ways to merge Word documents by programming. This article will show the detailed steps to merge Word documents.

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>13.11.2</version>
    </dependency>
</dependencies>

Merge Documents by Inserting the Entire File

The method Document.insertTextFromFile() provided by Spire.Doc allows merging Word documents by inserting other documents entirely into a document with the inserted contents starting from a new page.

The detailed steps of merging documents by inserting the entire file are as follows:

  • Create an object of Document class and load a Word document from disk.
  • Insert another Word document entirely to the loaded document using Document.insertTextFromFile() method.
  • Save the result document using Document.saveToFile() method.
  • Java
import com.spire.doc.*;

public class merge {
    public static void main(String[] args) {
        //Create an object of Document and load a Word document from disk
        Document document = new Document("C:/Samples/Sample1.docx");

        //Insert another Word document entirely to the document
        document.insertTextFromFile("C:/Samples/Sample2.docx", FileFormat.Docx_2013);

        //Save the result document
        document.saveToFile("MergingResult.docx", FileFormat.Docx_2013);
    }
}

Java: Merge Word Documents

Merge Documents by Cloning Contents

If you want to merge documents without starting a new page, you can clone the contents of other documents to add to the end of a document.

The detailed steps of merging documents by cloning contents are as follows:

  • Create two objects of Document and load the two Word documents from disk.
  • Loop through the second document to get all the sections using Document.getSections() method, then loop through all the sections to get their child objects using Section.getBod().getChildObjects() method, then get the last section of the first document using Document.getLastSection() method, and then add the child objects to the last section of the first document using Body.getChildObjects().add() method.
  • Save the result document using Document.saveToFile() method.
  • Java
import com.spire.doc.*;

public class mergeDocuments {
    public static void main(String[] args){
        //Create two Document objects and load two Word documents from disk
        Document document1 = new Document("C:/Samples/Sample1.docx");
        Document document2 = new Document("C:/Samples/Sample2.docx");

        //Loop through the second document to get all the sections
        for (Object sectionObj : (Iterable) document2.getSections()) {
            Section sec=(Section)sectionObj;
            //Loop through the sections of the second document to get their child objects
            for (Object docObj :(Iterable ) sec.getBody().getChildObjects()) {
                DocumentObject obj=(DocumentObject)docObj;

                //Get the last section of the first document
                Section lastSection = document1.getLastSection();

                //Add the child objects to the last section of the first document
                Body body = lastSection.getBody();
                body.getChildObjects().add(obj.deepClone());
            }
        }

        //Save the result document
        document1.saveToFile("MergingResult.docx", FileFormat.Docx_2013);
    }
}

Java: Merge 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.

page 66