News Category

Text

Text (6)

This article demonstrates how to apply a border around a set of charaters and how to apply a border around a whole paragraph, by using Spire.Doc for Java.

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.BorderStyle;
import com.spire.doc.documents.BreakType;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.TextRange;

import java.awt.*;

public class AddBorders {

    public static void main(String[] args) {

        //Create a Document instance
        Document doc = new Document();

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

        //Add a border to a set of characters
        Paragraph para = section.addParagraph();
        TextRange tr = para.appendText("Spire.Doc for Java");
        tr.getCharacterFormat().getBorder().setBorderType(BorderStyle.Single);
        tr.getCharacterFormat().getBorder().setColor(Color.BLACK);
        String text = " is a professional Java library specifically designed for developers to create, read, " +
                "write, convert and print Word document files on Java platform.";
        para.appendText(text);
        para.appendBreak(BreakType.Line_Break);

        //Add a border to a paragraph
        para = section.addParagraph();
        String text2 = "A plenty of Word document processing tasks can be performed by Spire.Doc for Java, such as " +
                "creating, reading, editing, converting and printing Word documents." ;
        para.appendText(text2);
        para.getFormat().getBorders().setBorderType(BorderStyle.Single);
        para.getFormat().getBorders().setColor(Color.BLACK);

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

Add Borders to Some Text in Word in Java

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

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.TextSelection;
import com.spire.doc.fields.DocPicture;
import com.spire.doc.fields.TextRange;

public class ReplaceTextWithImage {

    public static void main(String[] args) {

        //Load a sample Word file
        Document document = new Document();
        document.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.docx");

        //Find the string 'E-iceblue' in the document
        TextSelection[] selections = document.findAllString("E-iceblue", true, true);

        //Replace the string with an image
        int index = 0;
        TextRange range = null;
        for (Object obj : selections) {

            TextSelection textSelection = (TextSelection)obj;
            DocPicture pic = new DocPicture(document);
            pic.loadImage("C:\\Users\\Administrator\\Desktop\\e-iceblue-logo.png");
            range = textSelection.getAsOneRange();
            index = range.getOwnerParagraph().getChildObjects().indexOf(range);
            range.getOwnerParagraph().getChildObjects().insert(index,pic);
            range.getOwnerParagraph().getChildObjects().remove(range);
        }

        //Save the document
        document.saveToFile("output/ReplaceTextWithImage.docx", FileFormat.Docx_2013);
    }
}

Replace Text with Image in Word in Java

There are always troublesome situations in the process of creating Word documents. For example, after completing a large Word document, it is discovered that someone’s name or a term, which appears multiple times in the document, was misspelled. Fortunately, there are some simple ways to solve such problems. MS Word has a find-and-replace feature to help users to find the misspelled words in a Word document and replace them with the right ones. However, if you don’t have MS Word, or if you want your application to implement this feature, Spire.Doc for Java is also a good choice. This article shows how to use the professional development component Spire.Doc for Java to find and replace text with new text or images in a Word document.

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>
    

Find Text and Replace All Matches with New Text

Finding and replacing text in Word documents can be done by Spire.Doc for Java with a simple method Document.replace(). This method replaces all matches of the searched text with new text, and you can decide whether to consider the case and whether to search for whole words.

The detailed steps of finding text and replacing all matches are as follows:

  • Create a Document class instance.
  • Load a Word document using Document.loadFromFile() method.
  • Replace all the matches of text "deer" in the document with new text "buffalo" using Document.replace() method.
  • Save the result document using Document.saveToFile() method.
  • Java
import com.spire.doc.Document;

public class replaceText {
    public static void main(String[] args) {

        //Create a Document class instance
        Document document = new Document();

        //Load a Word document
        document.loadFromFile("Cave Art.docx");

        //Replace all the matches of text “deer” in the document with new text “buffalo”
        document.replace("deer", "buffalo", false, true);

        //Save the result document
        document.saveToFile("Find&Replace.docx");
    }
}

Java: Find and Replace Text in Word Documents

Find Text and Replace the First Match with New Text

Spire.Doc for Java also provides the Document.setReplaceFirst() method to change the replacement mode of the Document.replace() method to replace the first match or replace all matches.

The detailed steps of finding text and replacing the first match are as follows:

  • Create a Document class instance.
  • Load a Word document using Document.loadFromFile() method.
  • Change the replacement mode as replacing only the first match using Document.setReplaceFirst() method.
  • Replace the first match of text "deer" in the document with new text "buffalo" using Document.replace() method.
  • Save the result document using Document.saveToFile() method.
  • Java
import com.spire.doc.Document;

public class replaceText {
    public static void main(String[] args) {

        //Create a Document class instance
        Document document = new Document();

        //Load a Word document
        document.loadFromFile("Cave Art.docx");

        //Set the replacement mode as replacing only the first match
        document.setReplaceFirst(true);

        //Replace all the matches of text “deer” in the document with new text “buffalo”
        document.replace("deer", "buffalo", false, true);

        //Save the result document
        document.saveToFile("Find&Replace.docx");
    }
}

Java: Find and Replace Text in Word Documents

Find and Replace Text with a Picture

Spire.Doc for Java supports searching text and replacing all matches as pictures as well. To do so, you need to search for the text and get all matches. Then load a picture as a document object, insert it to where the matching text is, and delete the text.

The detailed steps of finding and replacing text with images are as follows:

  • Create a Document class instance.
  • Load a Word document using Document.loadFromFile() method.
  • Find all the strings matching 'deer' in the document using Document.findString() method.
  • Loop through the matches to replace all of them with the picture.
  • Save the document using Document.saveToFile() method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.TextSelection;
import com.spire.doc.fields.DocPicture;
import com.spire.doc.fields.TextRange;

public class replaceTextWithImage {

    public static void main(String[] args) {

        //Create a Document class instance
        Document document = new Document();

        //Load a Word document
        document.loadFromFile("Cave Art.docx");

        //Find all the strings matching 'deer' in the document
        TextSelection[] selections = document.findAllString("deer", true, true);
        
        //Replace all the matches with the image
        int index = 0;
        TextRange range = null;
        for (Object obj : selections) {

            TextSelection textSelection = (TextSelection)obj;
            
            //Create an object of DocPicture class and load a picture
            DocPicture pic = new DocPicture(document);
            pic.loadImage("deer.png");
            
            range = textSelection.getAsOneRange();
            index = range.getOwnerParagraph().getChildObjects().indexOf(range);
            range.getOwnerParagraph().getChildObjects().insert(index,pic);
            range.getOwnerParagraph().getChildObjects().remove(range);
        }
        
        //Replace a specific match with the picture
        //Create an object of DocPicture class and load a picture
        //DocPicture pic = new DocPicture(document);
        //pic.loadImage("deer.png");
        //Object object = selections[1];
        //TextSelection selection = (TextSelection) object;
        //TextRange textRange = selection.getAsOneRange();
        //int i = textRange.getOwnerParagraph().getChildObjects().indexOf(textRange);
        //textRange.getOwnerParagraph().getChildObjects().insert(i,pic);
        //textRange.getOwnerParagraph().getChildObjects().remove(textRange);

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

Java: Find and Replace Text in 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.

The "Find" function in MS Word allows users to search for specific text or phrases in a document quickly, and users can also highlight the found text in yellow or other bright colors to make them visible to readers at a glance. In this article, you will learn how to programmatically find and highlight text in a Word document 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>12.4.6</version>
    </dependency>
</dependencies>
    

Find and Highlight Text in Word

The detailed steps are as follows.

  • Create a Document instance
  • Load a sample Word document using Document.loadFromFile() method.
  • Find all matching text in the document using Document.findAllString(java.lang.String matchString, boolean caseSensitive, boolean wholeWord) method.
  • Loop through all matching text in the document.
  • Get the text range of a specific matching text using TextSelection.getAsOneRange() method, and then set its highlight color using TextRange.getCharacterFormat().setHighlightColor() method.
  • Save the result document using Document.saveToFile() method.
  • Java
import com.spire.doc.*;
import com.spire.doc.documents.TextSelection;

import java.awt.*;
public class FindAndHighlight {
    public static void main(String[] args){
        //Create a Document instance
        Document document = new Document();

        //Load a sample Word document
        document.loadFromFile("E:\\Files\\input1.docx");

        //Find all matching text in the document
        TextSelection[] textSelections = document.findAllString("transcendentalism", false, true);

        //Loop through all matching text and set highlight color for them
        for (TextSelection selection : textSelections) {
            selection.getAsOneRange().getCharacterFormat().setHighlightColor(Color.YELLOW);
        }

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

Java: Find and Highlight Text 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.

Text and images are crucial elements that can enrich the content of a Word document. When users need to manipulate text or images separately of the document, programmatically extracting them from a Word document provides an optimal solution. Extracting text guarantees greater convenience and efficiency when dealing with large documents compared to manually copying text. Additionally, image extraction enables users to perform further editing on the images of the document or effortlessly share them with others. In this article, we will demonstrate how to extract text and images from Word in Java by using Spire.Doc for Java library.

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>
    

Extract Text from Word in Java

Spire.Doc for Java supports extracting text from Word documents and saving it as a text file format, which allows users to view text content without device restrictions. Below are detailed steps for extracting text from a Word document.

  • Create a Document object.
  • Load a word document using Document.loadFromFile method.
  • Get text from document as string using Document.getText() method.
  • Call writeStringToTxt() method to write string to a specified text file.
  • Java
import com.spire.doc.Document;
import java.io.FileWriter;
import java.io.IOException;

public class ExtractText {

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

        //Create a Document object and load a Word document
        Document document = new Document();
        document.loadFromFile("sample1.docx");

        //Get text from document as string
        String text=document.getText();

        //Write string to a .txt file
        writeStringToTxt(text," ExtractedText.txt");
    }
    public static void writeStringToTxt(String content, String txtFileName) throws IOException{
        FileWriter fWriter= new FileWriter(txtFileName,true);
        try {
            fWriter.write(content);
        }catch(IOException ex){
            ex.printStackTrace();
        }finally{
            try{
                fWriter.flush();
                fWriter.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}

Java: Extract Text and Images from Word

Extract Images from Word in Java

By extracting images, users are able to import image data into other applications for further processing without difficulty. Spire.Doc for Java allows users to extract images from Word documents and save them to the specified path. The following are detailed steps.

  • Create a Document object.
  • Load a Word document using Document.loadFromFile() method.
  • Create a queue of composite objects.
  • Add the root document element to the traversal queue using Queue<ICompositeObject>.add(ICompositeObject e) method.
  • Create a ArrayList object to store extracted images.
  • Traverse the document tree and check for composite or picture objects by iterating over the children node of each node.
  • Check if the child element is a composite object. If so, add it to the queue for further processing.
  • Check if the child element is a picture object. If so, extract its image data and add it to the extracted image list.
  • Save images to the specific folder using ImageIO.write(RenderedImage im, String formatName, File output) method.
  • Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import com.spire.doc.interfaces.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;

public class ExtractImage {
    public static void main(String[] args) throws IOException {

        //Create a Document object and load a Word document
        Document document = new Document();
        document.loadFromFile("sample2.docx");

        //Create a queue and add the root document element to it
        Queue<ICompositeObject> nodes = new LinkedList<>();
        nodes.add(document);

        //Create a ArrayList object to store extracted images
        List<BufferedImage> images = new ArrayList<>();

        //Traverse the document tree
        while (nodes.size() > 0) {
            ICompositeObject node = nodes.poll();
            for (int i = 0; i < node.getChildObjects().getCount(); i++)
            {
                IDocumentObject child = node.getChildObjects().get(i);
                if (child instanceof ICompositeObject)
                {
                    nodes.add((ICompositeObject) child);
                }
                else if (child.getDocumentObjectType() == DocumentObjectType.Picture)
                {
                    DocPicture picture = (DocPicture) child;
                    images.add(picture.getImage());
                }
            }
        }

        //Save images to the specific folder
        for (int i = 0; i < images.size(); i++) {
            File file = new File(String.format("output/extractImage-%d.png", i));
            ImageIO.write(images.get(i), "PNG", file);
        }
    }
}

Java: Extract Text and Images from 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 appearance of a document conveys more than just document's message; it also reveals the information about the creator. A well-organized document that includes consistently formatted content and appropriate graphics, and that contains no spelling or grammatical mistakes, inspires greater trust in your ability to deliver a product or service.

Using Spire.Doc for Java, you're able to format entire paragraphs as well as individual words or phrases. This article focuses on introducing how to apply various types of formatting to characters in a Word document in Java using Spire.Doc for Java.

  • Font Name
  • Font Size
  • Font Color
  • Highlight Color
  • Bold
  • Italic
  • Underline
  • Strikethrough
  • Border
  • Shadow Effect
  • Emphasis Mark
  • Subscript and Superscript

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>
    

Apply Formatting to Characters in Word in Java

In order to apply formatting to a piece of text, you need to get the text in a TextRange and then format the characters within the TextRange using the methods under CharacterFormat class. The following are the steps to set character formatting in a Word document using Spire.Doc for Java.

  • Create a Document object.
  • Add a section to the document using Document.addSection() method.
  • Add a paragraph to the section using Section.addParagraph() method.
  • Append text to the paragraph using Paragraph.appendText() method and return a TextRange object.
  • Apply formatting such as font name, font size, border and highlight color to the characters within the text range using the methods under CharacterFormat class.
  • Save the document to a Word file using Document.saveToFile() method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.BorderStyle;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.SubSuperScript;
import com.spire.doc.documents.UnderlineStyle;
import com.spire.doc.fields.TextRange;
import com.spire.doc.fields.shape.Emphasis;

import java.awt.*;

public class ApplyFormattingToCharacters {

    public static void main(String[] args) {

        //Create a Document object
        Document document = new Document();

        //Add a section
        Section sec = document.addSection();

        //Add a paragraph
        Paragraph paragraph = sec.addParagraph();
        paragraph.appendText("Here is a paragraph with various character styles. This is ");

        //Append text to the paragraph and return a TextRange object
        TextRange tr = paragraph.appendText("text with strikeout");

        //Set the character format to strikeout via TextRange object
        tr.getCharacterFormat().isStrikeout(true);

        //Apply shadow effect to text
        paragraph.appendText(". This is ");
        tr = paragraph.appendText("text with shadow");
        tr.getCharacterFormat().isShadow (true);

        //Set font size
        paragraph.appendText(". This is ");
        tr = paragraph.appendText("text in a large font size");
        tr.getCharacterFormat().setFontSize(20);

        //Set font name
        paragraph.appendText(". This is ");
        tr = paragraph.appendText("text in the font of Arial Black");
        tr.getCharacterFormat().setFontName("Arial Black");

        //Set font color
        paragraph.appendText(". This is ");
        tr = paragraph.appendText("text in red");
        tr.getCharacterFormat().setTextColor(Color.red);

        //Apply bold & italic to text
        paragraph.appendText(". This is ");
        tr = paragraph.appendText("text in bold & italic");
        tr.getCharacterFormat().setBold(true);
        tr.getCharacterFormat().setItalic(true);

        //Apply underline to text
        paragraph.appendText(". This is ");
        tr = paragraph.appendText("underlined text");
        tr.getCharacterFormat().setUnderlineStyle(UnderlineStyle.Single);

        //Apply background color to text
        paragraph.appendText(". This is ");
        tr = paragraph.appendText("text with highlight color");
        tr.getCharacterFormat().setHighlightColor(Color.green);

        //Apply border to text
        paragraph.appendText(". This is ");
        tr = paragraph.appendText("text with border");
        tr.getCharacterFormat().getBorder().setBorderType(BorderStyle.Single);
        tr.getCharacterFormat().getBorder().setColor(Color.black);

        //Apply emphasis mark to text
        paragraph.appendText(". This is ");
        tr = paragraph.appendText("text with emphasis mark");
        tr.getCharacterFormat().setEmphasisMark(Emphasis.Dot_Below);

        //Apply superscript to text
        paragraph.appendText(". This is a math formula: a");
        tr = paragraph.appendText("2");
        tr.getCharacterFormat().setSubSuperScript(SubSuperScript.Super_Script);
        paragraph.appendText(" + b");
        tr = paragraph.appendText("2");
        tr.getCharacterFormat().setSubSuperScript(SubSuperScript.Super_Script);
        paragraph.appendText(" = c");
        tr = paragraph.appendText("2");
        tr.getCharacterFormat().setSubSuperScript(SubSuperScript.Super_Script);
        paragraph.appendText(".");

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

Java: Apply Formatting to Characters 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.