この短いチュートリアルでは、構成要件と機能を実現するために使用するロジックを共有することにより、** C#を使用してWordにページ番号を追加する方法について説明します。 DOCXドキュメント内を移動したり、フッターにフィールドを追加しながら条件を適用したり、最後に空白ページを追加して効果を示したりする手順について説明します。この記事を使用すると、必要に応じて、フッターまたはヘッダーの C#**のWord文書にページ番号を適用できます。
C#を使用してWordにページ番号を追加する手順
- Wordファイルにページ番号を追加するためのAspose.Wordsを追加するようにプロジェクトを構成します
- WordファイルをDocumentクラスオブジェクトにロードして、ページ番号を追加します
- ロードされたドキュメントのDocumentBuilderクラスオブジェクトを作成します
- ロードされたドキュメントのフッターにコントロールを移動します
- 特定のページの後に番号を追加するには、条件付きページ番号フィールドを追加します
- テストページを追加するために、コントロールをドキュメントの開始に移動します
- 上で定義したように、結果のWordファイルをフッターのページ番号とともに保存します
これらの手順では、* C#を使用してページ番号をWordに挿入する*プロセスの論理フローについて説明します。これは、最初にWordファイルをロードしてから、手動プロセスと同様に、カーソルをフッターセクションに移動してから、フィールドを追加する必要があることを示しています。特定のページから始まるページ番号を追加するための条件を追加するための詳細もコードに示されています。
C#を使用してフッターにWordページ番号を追加するコード
namespace AddPageNumbersInWordUsingCSharp | |
{ | |
class Program | |
{ | |
static void Main(string[] args) // Main function to Add Page Numbers in Word using C# | |
{ | |
// Load the license to avoid a watermark in the output Word file | |
// after adding the page numbers in the footer | |
Aspose.Words.License licAddPageNumber = new Aspose.Words.License(); | |
licAddPageNumber.SetLicense("Aspose.Word.lic"); | |
// Load the input Word file for adding page numbers | |
Aspose.Words.Document wordFile = new Aspose.Words.Document("word.docx"); | |
// Instantiate the DocumentBuilder object to move around and insert contents | |
Aspose.Words.DocumentBuilder fileBuilder = new Aspose.Words.DocumentBuilder(wordFile); | |
// Using the builder, move to the primary footer section | |
fileBuilder.MoveToHeaderFooter(Aspose.Words.HeaderFooterType.FooterPrimary); | |
// Add the page number field along with the condition using IF | |
Aspose.Words.Fields.Field field = fileBuilder.InsertField("IF ", null); | |
fileBuilder.MoveTo(field.Start.NextSibling.NextSibling); | |
// Insert the field in to the moved location i.e. footer | |
fileBuilder.InsertField(Aspose.Words.Fields.FieldType.FieldPage, false); | |
// add the IF expression to be checked before inserting page number | |
fileBuilder.Write(" > 4 \""); | |
// In the TRUE segment of the IF condition add another field | |
fileBuilder.InsertField(Aspose.Words.Fields.FieldType.FieldPage, false); | |
// In the FALSE part of the IF condition insert blank string | |
fileBuilder.Write("\" \"\""); | |
// Move to the start of the document for adding blank pages | |
fileBuilder.MoveToDocumentStart(); | |
// Insert a defined number of blank pages | |
for (int page = 0; page < 15; page++) | |
fileBuilder.InsertBreak(Aspose.Words.BreakType.PageBreak); | |
// Save to output Word file with page number in the DOCX format | |
wordFile.Save("show hide page numbers .docx"); | |
System.Console.WriteLine("Done"); | |
} | |
} | |
} |
このコードは、DocumentBuilderクラスオブジェクトを使用してフッターに移動するなど、Wordドキュメントのさまざまな部分に移動するコマンドを提供することにより、* C#を使用して特定のページからページ番号を開始する方法*を示しています。同じビルダーを使用して、フィールドタイプを指定し、テキストをフォーマットすることで、ヘッダーまたはフッターにフィールドを挿入できます。フッターにページ番号が追加されると、ビルダーを使用してドキュメントの先頭に移動し、機能をテストするために空白のページを追加します。
- C#でWord文書にページ番号を追加する*ことを学びましたが、Wordファイルに画像を追加する方法を確認したい場合は、記事C#を使用してWord文書に画像を追加する方法を参照してください。