Every paragraph in a Word document uses a paragraph style, either intentionally or unintentionally. The paragraph style can be a built-in style, such as Heading 1 and Heading 2, or it can be a customized style. This article introduces how we can extract paragraphs that use a specific style by using Spire.Doc for Java.

The table below lists the style names in MS Word and their corresponding names in Spire.Doc. A very simple rule is that the style name returned by programming does not contain spaces.

Style Name in MS Word Style Name in Spire.Doc
Title Title
Subtitle Subtitle
Heading 1 Heading1
Heading 2 Heading2
Heading 3 Heading3
No Spacing NoSpacing
Quote Quote
Intense Quote IntenseQuote
List Paragraph ListParagraph
Normal Normal
Custom Name CustomName

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 Paragraphs that Use a Specific Style

The style name of a specific paragraph can be obtained by Paragraph.getStyleName() method. If a paragraph’s style name is exactly what you want to query, you can get the paragraph content using Paragraph.getText() method. The following are the steps to extract paragraphs that use a specific style.

  • Load a sample Word document while initializing the Document object.
  • Loop through the sections in the document.
  • Get a specific paragraph from a certain section using Section.getParagraphs().get() method.
  • Get the paragraph's style name using Paragraph.getStyleName() method and determine if the style is "Heading 1".
  • If yes, extract the text of the paragraph using Paragraph.getText() method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.documents.Paragraph;

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

        //Load a sample Word document while initializing the Document object
        Document doc = new Document("C:\\Users\\Administrator\\Desktop\\Styles.docx");

        //Declare a variable
        Paragraph paragraph;

        //Loop through the sections
        for (int i = 0; i < doc.getSections().getCount(); i++) {

            //Loop through the paragraphs of a specific section
            for (int j = 0; j < doc.getSections().get(i).getParagraphs().getCount(); j++) {

                //Get a specific paragraph
                paragraph = doc.getSections().get(i).getParagraphs().get(j);

                //Determine if the paragraph style is "Heading 1"
                if (paragraph.getStyleName().equals("Heading1")) {

                    //Get the text of the paragraph in "Heading 1"
                    System.out.println("Heading 1: " + paragraph.getText() + "\n");
                }

                //Determine if the paragraph style is "My Custom Style"
                if (paragraph.getStyleName().equals("MyCustomStyle")) {

                    //Get the text of the paragraph in "My Custom Style"
                    System.out.println("My Custom Style: " + paragraph.getText());
                }
            }
        }
    }
}

Java: Extract Word Paragraphs that Use a Specific Style

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.

Published in Paragraph
Thursday, 18 November 2021 02:19

Java: Hide a Specific Paragraph in Word

In the process of manipulating Word documents, it is sometimes necessary to keep some important information from others. For this reason, we can hide them to ensure confidentiality. This article shows how to hide a specific paragraph 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>
    

Hide a Specific Paragraph in Word

Spire.Doc for Java supports hiding a specific paragraph in Word by using TextRange.getCharacterFormat().setHidden(boolean value) method. Here are detailed steps to follow.

  • Create a Document instance.
  • Load a sample Word document using Document.loadFromFile() method.
  • Get a specific section of the Word document using Document.getSections().get() method.
  • Get a specific paragraph of the section using Section.getParagraphs().get() method.
  • Loop through the child objects of the paragraph, and convert each child object as a text range if it is plain text. Then hide the text range using TextRange.getCharacterFormat().setHidden(boolean value) method.
  • 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.*;

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

        //Load a sample Word document
        document.loadFromFile("C:\\Users\\Test1\\Desktop\\sample.docx");

        //Get a specific section of Word
        Section sec = document.getSections().get(0);

        //Get a specific paragraph of the section
        Paragraph para = sec.getParagraphs().get(1);

        //Loop through the child objects
        for (Object docObj : para.getChildObjects()) {
            DocumentObject obj = (DocumentObject)docObj;

            //Determine if a child object is an instance of TextRange
            if ((obj instanceof TextRange)) {
                TextRange range = ((TextRange)(obj));

                //Hide the text range
                range.getCharacterFormat().setHidden(true);
            }
        }

        //Save the document to another file
        document.saveToFile("output/hideParagraph.docx", FileFormat.Docx_2013);
    }
}

Java: Hide a Specific Paragraph 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.

Published in Paragraph
Thursday, 06 April 2023 05:59

Java: Remove Paragraphs in Word

In MS Word, it is quite essential to delete paragraphs that contain duplicate content or information that is not relevant to the subject matter. By doing so, you can simplify your document and ensure the accuracy of the information. In this article, you will learn how to programmatically remove paragraphs 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>
    

Remove All Paragraphs in a Word Document in Java

To remove all paragraphs, you need to loop through all sections in a document and then delete all paragraphs in each section using Section.getParagraphs().clear() method. The following are the detailed steps.

  • Create a Document instance.
  • Load a Word document using Document.loadFromFile() method.
  • Traverse through each section of the document and then remove all paragraphs in the section using Section.getParagraphs().clear() method.
  • Save the result document using Document.saveToFile() method.
  • Java
import com.spire.doc.*;

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

        //Load a sample document from disk
        document.loadFromFile("E:\\Files\\test23.docx");

        //Remove paragraphs from every section in the document
        for ( Object sectionObj: document.getSections()) {
            Section section = (Section)sectionObj;
            section.getParagraphs().clear();
        }

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

Java: Remove Paragraphs in Word

Remove a Specific Paragraph in a Word Document in Java

If you find a paragraph that contains duplicate or useless information, Spire.Doc for Java allows you to delete the specified paragraph using Section.getParagraphs().removeAt() method. The following are the detailed steps.

  • Create a Document instance.
  • Load a Word document using Document.loadFromFile() method.
  • Get a specified section of the document using Document.getSections().get() method.
  • Remove a specified paragraph in the section using Section.getParagraphs().removeAt() method.
  • Save the result document using Document.saveToFile() method.
  • Java
import com.spire.doc.*;

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

        //Load a sample document from disk
        document.loadFromFile("E:\\Files\\test23.docx");

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

        //Remove the third paragraph in the section
        section.getParagraphs().removeAt(2);

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

Java: Remove Paragraphs in Word

Remove Blank Paragraphs in a Word Document in Java

When there are many empty paragraphs/lines in a document, it's necessary to remove them to improve readability. The following are the steps to remove all blank paragraphs/lines in a Word document.

  • Create a Document instance.
  • Load a Word document using Document.loadFromFile() method.
  • Traverse through all paragraphs in the document and determine whether the paragraph is a blank paragraph.
  • Remove blank paragraphs from the document using Section.getBody().getChildObjects().remove() method.
  • Save the result document using Document.saveToFile() method.
  • Java
import com.spire.doc.*;
import com.spire.doc.documents.*;

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

        //Load a sample document from disk
        document.loadFromFile("E:\\Files\\test230.docx");

        //Traverse each paragraph in the Word document
        for (Object sectionObj : document.getSections()) {
            Section section=(Section)sectionObj;
            for (int i = 0; i < section.getBody().getChildObjects().getCount(); i++) {
                if ((section.getBody().getChildObjects().get(i).getDocumentObjectType().equals(DocumentObjectType.Paragraph) )) {
                    String s= ((Paragraph)(section.getBody().getChildObjects().get(i))).getText().trim();

                    //Determine if the paragraph is a blank paragraph
                    if (s.isEmpty()) {

                        //Remove blank paragraphs
                        section.getBody().getChildObjects().remove(section.getBody().getChildObjects().get(i));
                        i--;
                    }
                }
            }
        }

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

Java: Remove Paragraphs 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.

Published in Paragraph

This article shows you how to set ASCII characters (special symbols) as bullet points in Word documents 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.ListStyle;
import com.spire.doc.documents.ListType;
import com.spire.doc.documents.Paragraph;

public class SetBulletPoints {

    public static void main(String[] args) {

        //Create a Document object and add a section
        Document doc = new Document();
        Section section = doc.addSection();

        //Create four list styles based on different ASCII characters
        ListStyle listStyle1 = new ListStyle(doc, ListType.Bulleted);
        listStyle1.getLevels().get(0).setBulletCharacter("\u006e");
        listStyle1.getLevels().get(0).getCharacterFormat().setFontName("Wingdings");
        listStyle1.setName("liststyle1");
        doc.getListStyles().add(listStyle1);
        ListStyle listStyle2 = new ListStyle(doc, ListType.Bulleted);
        listStyle2.getLevels().get(0).setBulletCharacter("\u0075");
        listStyle2.getLevels().get(0).getCharacterFormat().setFontName("Wingdings");
        listStyle2.setName("liststyle2");
        doc.getListStyles().add(listStyle2);
        ListStyle listStyle3 = new ListStyle(doc, ListType.Bulleted);
        listStyle3.getLevels().get(0).setBulletCharacter("\u00b2");
        listStyle3.getLevels().get(0).getCharacterFormat().setFontName("Wingdings");
        listStyle3.setName("liststyle3");
        doc.getListStyles().add(listStyle3);
        ListStyle listStyle4 = new ListStyle(doc, ListType.Bulleted);
        listStyle4 .getLevels().get(0).setBulletCharacter("\u00d8");
        listStyle4 .getLevels().get(0).getCharacterFormat().setFontName("Wingdings");
        listStyle4.setName("liststyle4");
        doc.getListStyles().add(listStyle4);

        //Add four paragraphs and apply list style separately
        Paragraph p1 = section.getBody().addParagraph();
        p1.appendText("Spire.Doc for .NET");
        p1.getListFormat().applyStyle(listStyle1.getName());
        Paragraph p2 = section.getBody().addParagraph();
        p2.appendText("Spire.PDF for .NET");
        p2.getListFormat().applyStyle(listStyle2.getName());
        Paragraph p3 = section.getBody().addParagraph();
        p3.appendText("Spire.XLS for .NET");
        p3.getListFormat().applyStyle(listStyle3.getName());
        Paragraph p4= section.getBody().addParagraph();
        p4.appendText("Spire.Presentation for .NET");
        p4.getListFormat().applyStyle(listStyle4.getName());

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

Set ASCII Characters as Bullet Points in Word in Java

Published in Paragraph
Tuesday, 24 November 2020 07:03

Remove Blank Lines in Word Document in Java

This article demonstrates how to remove blank lines/empty paragraphs in a Word document by using Spire.Doc for Java.

Below is the sample document which contains many blank lines:

Remove Blank Lines in Word Document in Java

import com.spire.doc.*;
import com.spire.doc.documents.*;

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

        //Load the sample document
        Document document = new Document();
        document.loadFromFile("sample.docx");

        //Traverse every section in the word document and remove the null and empty paragraphs
        for (Object sectionObj : document.getSections()) {
            Section section=(Section)sectionObj;
            for (int i = 0; i < section.getBody().getChildObjects().getCount(); i++) {
                if ((section.getBody().getChildObjects().get(i).getDocumentObjectType().equals(DocumentObjectType.Paragraph) )) {
                    String s= ((Paragraph)(section.getBody().getChildObjects().get(i))).getText().trim();
                    if (s.isEmpty()) {
                        section.getBody().getChildObjects().remove(section.getBody().getChildObjects().get(i));
                        i--;
                    }
                }
            }
        }
        
        //Save the document to file
        String result = "removeBlankLines.docx";
        document.saveToFile(result, FileFormat.Docx_2013);
    }
}

Remove Blank Lines in Word Document in Java

Published in Paragraph

This article demonstrates how to set spacing between paragraphs and how to set line spacing within a paragraph using Spire.Doc for Java.

Set Paragraph Spacing

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;

public class SetParagraphSpacing {

    public static void main(String[] args) {

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

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

        //Add a paragraph
        Paragraph para = section.addParagraph();
        para.appendText("Spire.Doc for Java is a professional Java Word API that enables Java applications to " +
                "manipulate Word documents without using Microsoft Office.");

        //Set paragraph after spacing
        para.getFormat().setAfterSpacing(20f);

        //Add another paragraph
        para = section.addParagraph();
        para.appendText("A plenty of Word document processing tasks can be performed by Spire.Doc for Java, "+
                "such as creating, comparing, reading, editing, converting and printing Word documents, "+
                "inserting image, adding header and footer, creating table and adding form field.");
        
        //Save the document
        doc.saveToFile("output/SetParagraphSpacing.docx", FileFormat.Docx_2013);
    }
}

Set Paragraph Spacing and Line Spacing in Word in Java

Set Line Spacing

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;

public class SetLineSpacing {

    public static void main(String[] args) {

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

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

        //Add a paragraph
        Paragraph para = section.addParagraph();
        para.appendText("Spire.Doc for Java is a professional Java Word API that enables Java applications to " +
                "manipulate Word documents without using Microsoft Office. A plenty of Word document " +
                "processing tasks can be performed by Spire.Doc for Java, such as creating, comparing, " +
                "reading, editing, converting and printing Word documents, inserting image, adding header " +
                "and footer, creating table and adding form field.");

        //Set line spacing
        para.getFormat().setLineSpacing(30f);

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

Set Paragraph Spacing and Line Spacing in Word in Java

Published in Paragraph
Thursday, 11 July 2019 09:39

Java: Set Paragraph Alignments in Word

In Microsoft Word, we can set the text alignment of a paragraph as left/center/right/justified/distributed. In this article, we will demonstrate how to achieve this function programmatically in Java 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>
    

Set Paragraph Alignments in Word

Spire.Doc for Java provides the ParagraphFormat class to work with paragraph formatting. You can use Paragraph.getFormat() method to get the ParagraphFormat object, and then use ParagraphFormat.setHorizontalAlignment() method to set the text alignment for the paragraph.

The following are the steps to set the text alignment for a paragraph in a Word document:

  • Create a Document instance.
  • Add a section to the document using Document.addSection() method.
  • Add a paragraph to the section using Section.addParagraph() method, then append text to the paragraph using Paragraph.appendText() method.
  • Get the ParagraphFormat object using Paragraph.getFormat() method.
  • Set text alignment for the paragraph using ParagraphFormat.setHorizontalAlignment() method.
  • Save the result document 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.HorizontalAlignment;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.formatting.ParagraphFormat;

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

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

        //Add a paragraph and make it left-aligned
        Paragraph para = section.addParagraph();
        para.appendText("This paragraph is left-aligned");
        ParagraphFormat format = para.getFormat();
        format.setHorizontalAlignment(HorizontalAlignment.Left);

        //Add a paragraph and make it centered
        para = section.addParagraph();
        para.appendText("This paragraph is centered");
        format = para.getFormat();
        format.setHorizontalAlignment(HorizontalAlignment.Center);

        //Add a paragraph and make it right-aligned
        para = section.addParagraph();
        para.appendText("This paragraph is right-aligned");
        format = para.getFormat();
        format.setHorizontalAlignment(HorizontalAlignment.Right);

        //Add a paragraph and make it justified
        para = section.addParagraph();
        para.appendText("This paragraph is justified");
        format = para.getFormat();
        format.setHorizontalAlignment(HorizontalAlignment.Justify);

        //Add a paragraph and make it distributed
        para = section.addParagraph();
        para.appendText("This paragraph is distributed");
        format = para.getFormat();
        format.setHorizontalAlignment(HorizontalAlignment.Distribute);

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

Java: Set Paragraph Alignments 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.

Published in Paragraph
Tuesday, 21 May 2019 08:53

Java: Set Paragraph Indents in Word

In Microsoft Word, we can set the paragraph indents as left, right, first line or hanging. In this article, we will demonstrate how to achieve this functionality 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>
    

Set Paragraph Indents in Word

Spire.Doc for Java provides a ParagraphFormat class to work with paragraph formatting. You can use Paragraph.getFormat() method to get the ParagraphFormat object, and then use the following methods to set the corresponding paragraph indents with the object:

Indents Methods Descriptions
Left ParagraphFormat.setLeftIndent(float value) Indents the paragraph on the left by the amount you set.
Right ParagraphFormat.setRightIndent(float value) Indents the paragraph on the right by the amount you set.
First line ParagraphFormat.setFirstLineIndent(float value) Indent the first line of a paragraph.
Hanging ParagraphFormat.setFirstLineIndent(float negativeValue) Sets off the first line of a paragraph by positioning it at the margin, and then indenting each subsequent line of the paragraph.

The following are the steps to set paragraph indents:

  • Create a Document instance.
  • Load a Word document using Document.loadFromFile() method.
  • Get the desired section by its index using Document.getSections.get() method.
  • Get the desired paragraph by index using Section.getParagraphs.get() method.
  • Get the ParagraphFormat object using Paragraph.getFormat() method.
  • Call the methods listed in above table to set paragraph indents with the object.
  • Save the result document 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.Paragraph;
import com.spire.doc.formatting.ParagraphFormat;

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

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

        //Get the second paragraph and set left indent
        Paragraph para = section.getParagraphs().get(1);
        ParagraphFormat format = para.getFormat();
        format.setLeftIndent(30);

        //Get the third paragraph and set right indent
        para = section.getParagraphs().get(2);
        format = para.getFormat();
        format.setRightIndent(30);

        //Get the fourth paragraph and set first line indent
        para = section.getParagraphs().get(3);
        format = para.getFormat();
        format.setFirstLineIndent(30);

        //Get the fifth paragraph and set hanging indent
        para = section.getParagraphs().get(4);
        format = para.getFormat();
        format.setFirstLineIndent(-30);

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

The following is the output Word document after setting paragraph indents:

Java: Set Paragraph Indents 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.

Published in Paragraph