*这么优秀的国产工具怎能错过呢!在线下单专享“一口价”,查看优惠价格!想要获取更多福利的朋友可以咨询在线客服哦~
在 Word 中隐藏特定段落
Spire.Doc for Java 支持使用TextRange.getCharacterFormat().setHidden(boolean value)方法隐藏 Word 中的特定段落。以下是要遵循的详细步骤。
- 创建一个文档实例。
- 使用Document.loadFromFile()方法加载示例 Word 文档。
- 使用Document.getSections().get()方法获取Word 文档的特定部分。
- 使用Section.getParagraphs().get()方法获取该部分的特定段落。
- 循环遍历段落的子对象,如果是纯文本,则将每个子对象转换为文本范围。然后使用TextRange.getCharacterFormat().setHidden(boolean value)方法隐藏文本范围。
- 使用Document.saveToFile()方法将文档保存到另一个文件。
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); }}

整合所有格式API处理套包Spire.office for Java正在 火热销售中!联系客服立马1分钟了解全部咨询!

标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!