C#を使用してWordの空白ページを削除する方法

このクイックチュートリアルは、DOCXDOCなどの** C#を使用してWordの空白ページを削除する方法を説明するように設計されています。環境を構成するための詳細と、タスクと、 C#を使用してWordの空白ページを消去する**ための完全なロジックを実装するサンプルコード。このコードは、Windows、Linux、macOSなどのオペレーティングシステムを使用して、サードパーティのソフトウェアをインストールせずに実行できます。

C#を使用してWordで空のページを削除する手順

  1. Aspose.Wordsライブラリをプロジェクトに追加するようにプロジェクトを構成します
  2. ソースWordファイルをDocumentクラスオブジェクトにロードして、空のページを削除します
  3. ソースドキュメントのすべてのページを解析し、各ページを個別にフェッチします
  4. ソースドキュメントからのみ、すべてのページで個別のドキュメントを作成します
  5. この単一ページのドキュメントを分析し、sectionにテキスト/図形ノードが含まれているかどうかを確認します
  6. 元のドキュメントの空でないすべてのページのリストを維持する
  7. 空でないすべてのページを新しいWordファイルに追加します
  8. 一部のテキストまたは形状のみのページを含むこのファイルを保存します

これらの手順は、* C#を使用してWordの空のページを削除する*ための詳細を提供します。まず、ソースWordファイルをロードしてから、その中の各ページを解析して、ページごとに新しいWordドキュメントが作成されるようにします。各ページを個別に分析する必要があり、Documentクラスの機能は、空でないページを識別するのに役立つため、これが必要です。解析中に、空でない各ページが、そのようなすべてのページを含む新しいWordファイルに追加されます。

C#を使用してWordの空白ページを削除するコード

using System.Collections;
using Aspose.Words;
namespace DeleteABlankPageInWordUsingCSharp
{
class Program
{
static void Main(string[] args) // Main function to delete a blank page in Word using CSharp
{
// Instantiate the license to avoid any trial version limitations
// and watermark in the output Word file
Aspose.Words.License licDeleteBlankPages= new Aspose.Words.License();
licDeleteBlankPages.SetLicense("Aspose.Words.lic");
// Load the Word file having blank pages in it
Document originalDocWithFewBlankPages = new Document("WordFileWithBlankPages.docx");
// Declare an array to store page numbers of the empty or blank pages
ArrayList listOfBlankPageNumbers = new ArrayList();
listOfBlankPageNumbers.Add(-1);
// Extract each page of the loaded document as a separate Word document
int totalPagesInOriginalDoc = originalDocWithFewBlankPages.PageCount;
for (int iCount = 0; iCount < totalPagesInOriginalDoc; iCount++)
{
// Create a new Word document containing the single page
Document DocWithOnePage = originalDocWithFewBlankPages.ExtractPages(iCount, 1);
// Parse through all the sections of the newly created document
// to count text segments and shapes if any
int shapesCounter = 0;
string pageText = "";
foreach (Section docSection in DocWithOnePage.Sections)
{
// Extract text from the section and also get shapes count in the newly created document
pageText = pageText + docSection.Body.ToString(SaveFormat.Text);
shapesCounter += docSection.Body.GetChildNodes(NodeType.Shape, true).Count;
}
// Check if the extracted page is null or not and also check if shapes count is 0 or not
// If text is blank and shapes count is 0, it means the page is blank
if (string.IsNullOrEmpty(pageText.Trim()) && shapesCounter == 0)
listOfBlankPageNumbers.Add(iCount);
}
listOfBlankPageNumbers.Add(totalPagesInOriginalDoc);
// Add all the non-empty pages to the final document
Document nonEmptyDocument = (Document)originalDocWithFewBlankPages.Clone(false);
nonEmptyDocument.RemoveAllChildren();
for (int iCount = 1; iCount < listOfBlankPageNumbers.Count; iCount++)
{
int index = (int)listOfBlankPageNumbers[iCount - 1] + 1;
int count = (int)listOfBlankPageNumbers[iCount] - index;
if (count > 0)
nonEmptyDocument.AppendDocument(originalDocWithFewBlankPages.ExtractPages(index, count), ImportFormatMode.KeepSourceFormatting);
}
// Save the output file having all the non-empty pages
nonEmptyDocument.Save(@"NonEmptyPages.docx");
System.Console.WriteLine("Done");
}
}
}
  • C#を使用してWordドキュメントから空白ページを削除する*このコードは、ソースWordファイルの各ページが新しいWordファイルに保存され、その中のコンテンツを分析する完全なロジックを記述しています。このプロセスでは、ヘッダーとフッターは考慮されませんが、それらを含めることもできます。すべてのページが分析され、それらの番号が配列に保存されると、この配列を使用して、それらすべてのページが新しいWordファイルに追加されます。

このチュートリアルでは、Wordファイルからすべての空白ページを削除しました。 Word文書をHTMLに変換するなど、他の機能について知りたい場合は、C#を使用してWord文書をHTMLに変換する方法の記事を参照してください。

 日本語