Paragraph

Paragraph (6)

Tuesday, 24 November 2020 07:03

Remove Blank Lines in Word Document in Java

Written by support iceblue

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

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

Thursday, 25 June 2020 08:07

Insert Multi-level Lists in Word in Java

Written by support iceblue

This article demonstrates how to insert a multi-level list in a Word document 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.*;

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

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

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

        //Create a ListStyle object
        ListStyle listStyle = new ListStyle(document, ListType.Numbered);
        listStyle.setName("CustomStyle");

        //Set the list pattern type and number prefix of each level
        listStyle.getLevels().get(0).setPatternType(ListPatternType.Arabic);
        listStyle.getLevels().get(1).setNumberPrefix("\u0000.");
        listStyle.getLevels().get(1).setPatternType(ListPatternType.Arabic);
        listStyle.getLevels().get(2).setNumberPrefix("\u0000.\u0001.");
        listStyle.getLevels().get(2).setPatternType(ListPatternType.Arabic);

        //Add the custom list style to the list styles collection
        document.getListStyles().add(listStyle);

        //Add first paragraph and apply list style to it
        //The default list level is the first level if you don't set a list level number
        Paragraph paragraph = section.addParagraph();
        paragraph.appendText("The first item");
        paragraph.applyStyle(BuiltinStyle.Heading_1);
        paragraph.getListFormat().applyStyle(listStyle.getName());

        //Add second paragraph and apply list style to it
        paragraph = section.addParagraph();
        paragraph.appendText("The second item");
        paragraph.applyStyle(BuiltinStyle.Heading_1);
        paragraph.getListFormat().applyStyle(listStyle.getName());

        //Add third paragraph, apply list style and set the list level number
        paragraph = section.addParagraph();
        paragraph.appendText("The first sub-item");
        paragraph.applyStyle(BuiltinStyle.Heading_2);
        paragraph.getListFormat().setListLevelNumber(1);
        paragraph.getListFormat().applyStyle(listStyle.getName());

        //Add third paragraph, apply list style and set the list level number
        paragraph = section.addParagraph();
        paragraph.appendText("The second sub-item");
        paragraph.applyStyle(BuiltinStyle.Heading_2);
        paragraph.getListFormat().continueListNumbering();
        paragraph.getListFormat().applyStyle(listStyle.getName());

        //Add forth paragraph, apply list style and set the list level number
        paragraph = section.addParagraph();
        paragraph.appendText("A sub-sub-item");
        paragraph.applyStyle(BuiltinStyle.Heading_5);
        paragraph.getListFormat().setListLevelNumber(2);
        paragraph.getListFormat().applyStyle(listStyle.getName());

        //Add fifth paragraph and apply list style to it
        paragraph = section.addParagraph();
        paragraph.appendText("The third item");
        paragraph.applyStyle(BuiltinStyle.Heading_1);
        paragraph.getListFormat().applyStyle(listStyle.getName());

        //Save the document
        document.saveToFile("MultiLevelList.docx", FileFormat.Docx);
    }
}

Insert Multi-level Lists in Word in Java

This article demonstrates how to create simple bulleted and numbered lists in a Word document using Spire.Doc for Java.

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

public class Bullets {
    public static void main(String[] args){
        //load the Word document
        Document document = new Document();
        //add a section
        Section section = document.addSection();

        //add 8 paragraphs
        Paragraph paragraph1 = section.addParagraph();
        paragraph1.appendText("Bulleted List");
        paragraph1.applyStyle(BuiltinStyle.Heading_1);
        Paragraph paragraph2 = section.addParagraph();
        paragraph2.appendText("Chapter 1");
        Paragraph paragraph3 = section.addParagraph();
        paragraph3.appendText("Chapter 2");
        Paragraph paragraph4 = section.addParagraph();
        paragraph4.appendText("Chapter 3");
        Paragraph paragraph5 = section.addParagraph();
        paragraph5.appendText("Numbered List");
        paragraph5.applyStyle(BuiltinStyle.Heading_1);
        Paragraph paragraph6 = section.addParagraph();
        paragraph6.appendText("Chapter 1");
        Paragraph paragraph7 = section.addParagraph();
        paragraph7.appendText("Chapter 2");
        Paragraph paragraph8 = section.addParagraph();
        paragraph8.appendText("Chapter 3");

        //create bulleted list for the 2-4 paragraphs
        for(int i = 1; i < 4; i++){
            Paragraph para = section.getParagraphs().get(i);
            para.getListFormat().applyBulletStyle();
            para.getListFormat().getCurrentListLevel().setNumberPosition(-10);
        }

        //create numbered list for the 6-8 paragraphs
        for(int i = 5; i < 8; i++){
            Paragraph para = section.getParagraphs().get(i);
            para.getListFormat().applyNumberedStyle();
            para.getListFormat().getCurrentListLevel().setNumberPosition(-10);
        }

        //save the document
        document.saveToFile("CreateLists.docx", FileFormat.Docx_2013);
    }
}

Output:

Create Bulleted and Numbered Lists in Word in Java

Thursday, 11 July 2019 09:39

Set Alignment for Paragraphs in Word in Java

Written by support iceblue

We can set left, centered, right and justified alignment for paragraphs in Word. This article will show you how to set the alignment for paragraphs in a Word document using Spire.Doc for Java.

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

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

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

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

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

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

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

Output:

Set Alignment for Paragraphs in Word in Java

The following code snippets demonstrate how to indent paragraphs for Word document in Java applications. With the help of Spire.Doc for Java, developers can set the paragraph indentation easily. There are four kinds of paragraph indentation style for word document: Left Indentation, Right Indentation, First Line Indentation and Hanging Indentation.

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

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

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

        Section section = document.getSections().get(0);

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

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

        //get the fourth paragraph and set the first line indent
        Paragraph para3 = section.getParagraphs().get(3);
        para3.getFormat().setFirstLineIndent(40);

        //get the fifth paragraph and set the hanging indent
        Paragraph para4 = section.getParagraphs().get(4);
        para4.getFormat().setFirstLineIndent(-40);
        
        //save the document to file
        document.saveToFile("out/WordIndent.docx", FileFormat.Docx_2013);

    }
}

Effective screenshot after setting the word indentation styles for word paragraph:

Java set paragraph indentation for word document