本快速教程指导如何使用 Java 删除 Word 中的空白页。它包含设置开发环境的详细信息、应用程序的编程任务列表以及演示如何使用 Java 删除 Word 中的空白页的可运行示例代码。您将学习从页面上的部分正文访问不同类型的内容,并验证其中文本或形状是否存在。
使用 Java 删除 Word 中的空页的步骤
- 设置环境使用Aspose.Words for Java删除空白页
- 加载源Word document 并获取其页数
- 解析所有页面并获取其 text 和形状
- 保存不存在文本或形状的页面索引列表
- 创建一个新的Word文档并使用空白页码列表添加其中的所有非空页面
- 保存生成的 Word 文件,其中不包含空白页
这些步骤总结了如何使用 Java 删除 Word DOC 中的空白页。采用的逻辑是加载源word文件并解析其每个页面以保存不存在文本或形状的空白页面的索引。随后,生成一个新的Word文件,并将源Word文件中的所有页面添加到新Word文件中,其索引不存在于空页面索引列表中。
使用Java删除Word DOC中的空白页的代码
import java.util.ArrayList; | |
import com.aspose.words.*; | |
public class Main | |
{ | |
public static void main(String[] args) throws Exception // Delete blank pages in Word files using Java | |
{ | |
// Set the licenses | |
new License().setLicense("License.lic"); | |
// Load the source Word file | |
Document originalDocWithFewBlankPages = new Document("WordFileWithBlankPages.docx"); | |
// Declare list for empty pages | |
ArrayList<Integer> listOfBlankPageNumbers = new ArrayList<>(); | |
listOfBlankPageNumbers.add(-1); | |
// Get the page count of the existing Word document | |
int totalPagesInOriginalDoc = originalDocWithFewBlankPages.getPageCount(); | |
// Iterate through all the pages | |
for (int iCount = 0; iCount < totalPagesInOriginalDoc; iCount++) | |
{ | |
// Create a new document using each page | |
Document DocWithOnePage = originalDocWithFewBlankPages.extractPages(iCount, 1); | |
// Get text and shapes count on the page | |
int shapesCounter = 0; | |
String pageText = ""; | |
for (Section docSection : DocWithOnePage.getSections()) | |
{ | |
pageText = pageText + docSection.getBody().toString(SaveFormat.TEXT); | |
shapesCounter += docSection.getBody().getChildNodes(NodeType.SHAPE, true).getCount(); | |
} | |
// Check if the text is empty and no shape is there, save the page index in the list | |
pageText = pageText.trim(); | |
if((pageText.isEmpty() || pageText == null || pageText.length() == 0) && shapesCounter == 0) | |
listOfBlankPageNumbers.add(iCount); | |
} | |
listOfBlankPageNumbers.add(totalPagesInOriginalDoc); | |
// Create a new document where pages with some content are added | |
Document nonEmptyDocument = (Document)originalDocWithFewBlankPages.deepClone(false); | |
nonEmptyDocument.removeAllChildren(); | |
for (int iCount = 1; iCount < listOfBlankPageNumbers.size(); iCount++) | |
{ | |
int index = (int)listOfBlankPageNumbers.get(iCount - 1) + 1; | |
int count = (int)listOfBlankPageNumbers.get(iCount) - index; | |
if (count > 0) | |
nonEmptyDocument.appendDocument(originalDocWithFewBlankPages.extractPages(index, count), ImportFormatMode.KEEP_SOURCE_FORMATTING); | |
} | |
// Save the output file having all the non-empty pages | |
nonEmptyDocument.save("NonEmptyPages.docx"); | |
System.out.println("Done"); | |
} | |
} |
此示例代码演示如何使用 Java 删除 Word 中的空白页。我们使用Document类中的extractPages()方法来访问页面,使用Section.getBody()方法来提取文本,使用Section.getChildNodes()方法和NodeType.SHAPE来提取页面上的形状。如果需要,您可以修改此代码以根据其索引删除单个空白页。
本教程指导我们删除 Word 中的空白页。如果您想删除Word文件中的所有注释,请参阅Java删除Word文档注释的方法上的文章。