余白とは、ページの端からコンテンツ領域までの距離のことです。ドキュメントの見栄えを整え、専門的にするのに役立ちます。Microsoft Word でドキュメントを作成する場合、ドキュメントの見栄えをよくするために余白を設定する必要がある場合があります。この記事では、C# と VB.NET で Spire.Doc for .NET を使用して Word ドキュメントの余白を設定する方法を説明します。 Spire.Doc for .NET をインストールします まず、Spire.Doc for .NET パッケージに含まれている DLL ファイルを .NET プロジェクトの参照として追加する必要があります。DLL ファイルは、このリンクからダウンロードするか、NuGet を介してインストールできます。 PM> Install-Package Spire.Doc Word ドキュメントのページ余白を設定する Spire.Doc for .NET では、Section.PageSetup.Margins プロパティの下に、Word ドキュメントの上下左右の余白を設定するプロパティを用意しています。Word ドキュメントの余白を設定するための詳細な手順は以下の通りです。 Document のインスタンスを作成します。 Document.LoadFromFile() メソッドを使用して Word ドキュメントを読み込みます。 Document.Sections[sectionIndex] プロパティを使用して、目的のセクションを取得します。 Section.PageSetup.Margins.Top、Section.PageSetup.Margins.Bottom、Section.PageSetup.Margins.Left、Section.PageSetup.Margins.Right プロパティを使用して、セクション内のページの上下左右の余白を設定します。 Document.SaveToFile() メソッドを使用して、ドキュメントを保存します。 C# VB.NET using Spire.Doc; namespace PageMargins { class Program { static void Main(string[] args) { //Documentのインスタンスを作成する Document document = new Document(); //Wordドキュメントを読み込む document.LoadFromFile("C:/サンプル.docx"); //最初のセクションを取得する Section section = document.Sections[0]; //セクションの上下左右の余白を設定する section.PageSetup.Margins.Top = 17.9f; section.PageSetup.Margins.Bottom = 17.9f; section.PageSetup.Margins.Left = 17.9f; section.PageSetup.Margins.Right = 17.9f; //ドキュメントを保存する document.SaveToFile("余白の設定.docx", FileFormat.Docx2013); } } } Imports Spire.Doc Namespace PageMargins Class Program Shared Sub Main(ByVal args() As String) 'Documentのインスタンスを作成する Dim document As Document = New Document() 'Wordドキュメントを読み込む document.LoadFromFile("C:/サンプル.docx") '最初のセクションを取得する Dim section As Section = document.Sections(0) 'セクションの上下左右の余白を設定する section.PageSetup.Margins.Top = 17.9F section.PageSetup.Margins.Bottom = 17.9F section.PageSetup.Margins.Left = 17.9F section.PageSetup.Margins.Right = 17.9F 'ドキュメントを保存する document.SaveToFile("余白の設定.docx", FileFormat.Docx2013) End Sub End Class End Namespace 一時ライセンスを申請する 結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。