
Paragraph (10)
Java: Extract Word Paragraphs that Use a Specific Style
Wednesday, 01 December 2021 08:11 Written by support iceblueEvery 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.
- Package Manager
<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>4.11.8</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()); } } } } }
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.
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.
- Package Manager
<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>4.11.3</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); } }
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 shows you how to remove a specific paragraph from Word documents using Spire.Doc for Java.
import com.spire.doc.Document; import com.spire.doc.FileFormat; public class RemoveParagraph { public static void main(String[] args) { //Create a Document object Document document = new Document(); //Load the sample Word document document.loadFromFile("C:\\Users\\Administrator\\Desktop\\Java Doc.docx"); //Remove the second paragraph of the first section document.getSections().get(0).getParagraphs().removeAt(1); //Save the document document.saveToFile("RemoveParagraph.docx", FileFormat.Docx_2013); } }
Set ASCII Characters as Bullet Points in Word in Java
Tuesday, 20 April 2021 03:48 Written by support iceblueThis 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); } }
Remove Blank Lines in Word Document in Java
Tuesday, 24 November 2020 07:03 Written by support iceblueThis 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:
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); } }
Set Paragraph Spacing and Line Spacing in Word in Java
Wednesday, 04 November 2020 07:56 Written by support iceblueThis 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 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); } }
A list can have up to 9 levels. In Spire.Doc for Java API, the ListFormat.setListLevelNumber method is used to set the list level for a paragraph in Word. This method accepts an int parameter which can be set from 0 to 8.
The following example demonstrates how to add a custom list style to a document, apply the list style to paragraphs and set the list levels for paragraphs 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); } }
Create Bulleted and Numbered Lists in Word in Java
Friday, 19 July 2019 08:26 Written by support iceblueThere are two kinds of lists in Microsoft Word: bulleted list and numbered list. Spire.Doc for Java offers ListFormat.applyBulletStyle method and ListFormat.applyNumberedStyle method to create bulleted lists and numbered lists in Word document.
The following example shows how to create a bulleted list and a numbered list 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:
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.
- Package Manager
<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>4.9.0</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.Low_Kashida); //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); } }
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.
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.
- Package Manager
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc</artifactId> <version>4.9.0</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:
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.
