この簡単なチュートリアルでは、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() メソッドを使用してテキストを抽出し、NodeType.SHAPE を使用して Section.getChildNodes() メソッドを使用してページ上の図形を抽出しました。必要に応じて、このコードを変更して、インデックスに基づいて単一の空白ページを削除することができます。
このチュートリアルでは、Word で空白ページを削除する方法を説明しました。 Word ファイル内のすべてのコメントを削除する場合は、JavaでWord文書からコメントを削除する方法 の記事を参照してください。