Hide a Specific Paragraph in Word Using Java


Method 1: Download the free API (Free Spire.Doc for Java) and unzip it, then add the Spire.Doc.jar file to your project as dependency.


Sample Code

The detailed steps and complete sample code are as follows.

  • 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.
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

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

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

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

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



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store