##テストドキュメント、期待される目標ドキュメントの削除した結果
テストに使用されたWord文書を以下のように示します。含まれている空白の段落は、記事の全体的なレイアウトと美的な効果に影響を与えます。
目標ドキュメントの削除した結果
##補助ツール
クラスライブラリを使用する:Free Spire.Doc for Java(無料版)
クラスライブラリjarファイルのインポート(参照用の2つのインポートメソッド)
1 公式Webサイトからjarパッケージをダウンロードして解凍し、libフォルダー内のSpire.Doc.jarをJavaプログラムに手動でインポートします。
2 Mavenプログラムにjarをインポートするには、最初にpom.xmlファイルを構成してから、プログラムをインポートし、次のように構成する必要があります。
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>
##Javaコード例
import com.spire.doc.*;
import com.spire.doc.documents.DocumentObjectType;
import com.spire.doc.documents.Paragraph;
public class DeleteBlankParas {
public static void main(String[] args) {
//Wordテストドキュメントをロードする
Document doc = new Document();
doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.docx");
//セクションをトラバースする
for(int i = 0; i< doc.getSections().getCount();i++)
{
//sectionを取得する
Section section = doc.getSections().get(i);
//セクション内のオブジェクトをトラバースする
for (int j = 0;j<section.getBody().getChildObjects().getCount();j++)
{
//オブジェクトのタイプを取得する
Object object = section.getBody().getChildObjects().get(j).getDocumentObjectType();
//段落をトラバースする
for(int z = 0 ; z<section.getParagraphs().getCount();z++)
{
//段落を取得する
Paragraph paragraph = section.getParagraphs().get(z);
//オブジェクトタイプが段落かどうかを判断する
if(object.equals(DocumentObjectType.Paragraph))
{
//段落の内容が空かどうかを判断する
if(paragraph.getChildObjects().getLastItem() == null)
{
//空白の段落を削除する
section.getBody().getParagraphs().remove(paragraph);
z--;
}
}
}
}
}
//ドキュメントを保存する
doc.saveToFile("DeleteBlankParas.docx",FileFormat.Docx_2013);
doc.dispose();
}
}
以上は今回の記事でした、最後まで読んでいただきありがとうございます。