Word 文書ページにとじしろを追加することで、文書のプロフェッショナリズムと美観を向上させることができます。とじしろは文書をより整然とした印象にするだけでなく、印刷時にガイドとしても機能し、読者が内容をナビゲートしやすくします。文書ページにとじしろを追加することで、物理的な文書で見られる一般的な製本ライン効果を模倣し、文書に印刷されたような品質を与えることができます。この記事では、C# プロジェクト内で Spire.Doc for .NET を使用して、Word 文書ページにとじしろを追加する方法を説明します。 C# を使用して Word 文書ページの上部にとじしろを追加する C# を使用して Word 文書ページの左側にとじしろを追加する Spire.Doc for .NET をインストールします まず、Spire.Doc for .NET パッケージに含まれている DLL ファイルを .NET プロジェクトの参照として追加する必要があります。DLL ファイルは、このリンクからダウンロードするか、NuGet を介してインストールできます。 PM> Install-Package Spire.Doc C# を使用して Word 文書ページの上部にとじしろマージンを追加する ページの上部にとじしろを有効にするには、section.PageSetup.IsTopGutter = true を設定します。デフォルトのとじしろエリアは空白でコンテンツが表示されません。この例では、とじしろエリア内にテキストを追加する方法も含まれています。以下は詳細な手順です。 Document オブジェクトを作成します。 Document.LoadFromFile() メソッドを使用して Word 文書をロードします。 Document.Sections コレクションに対して for ループを使用して文書のすべてのセクションを反復処理します。 Section.PageSetup.IsTopGutter を true に設定してページの上部にとじしろを表示します。 Section.PageSetup.Gutter プロパティを使用してとじしろの幅を設定します。 カスタムメソッド AddTopGutterText() を呼び出してとじしろエリアにテキストを追加します。 Document.SaveToFile() メソッドを使用して文書を保存します。 C# using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using Spire.Doc.Formatting; using System.Drawing; using System.Text; namespace SpireDocDemo { internal class Program { static void Main(string[] args) { // ドキュメントオブジェクトを作成 Document document = new Document(); // ドキュメントをロード document.LoadFromFile("Sample.docx"); // ドキュメントの全セクションを繰り返し処理 for (int i = 0; i < document.Sections.Count; i++) { // 現在のセクションを取得 Section section = document.Sections[i]; // ページの上部にガターを追加するかどうかをtrueに設定 section.PageSetup.IsTopGutter = true; // ガターの幅を100fに設定 section.PageSetup.Gutter = 100f; // 上部ガターにテキストを追加するメソッドを呼び出す AddTopGutterText(section); } // 修正されたドキュメントをファイルに保存 document.SaveToFile("上部とじしろの追加.docx", FileFormat.Docx2016); // ドキュメントリソースを解放 document.Dispose(); } // 上部ガターにテキストを追加するメソッド static void AddTopGutterText(Section section) { // セクションのヘッダーを取得 HeaderFooter header = section.HeadersFooters.Header; // テキストボックスの幅をページの幅に設定 float width = section.PageSetup.PageSize.Width; // テキストボックスの高さを40に設定 float height = 40; // ヘッダーにテキストボックスを追加 TextBox textBox = header.AddParagraph().AppendTextBox(width, height); // テキストボックスの枠線をなしに設定 textBox.Format.NoLine = true; // テキストボックスの垂直開始位置を上マージンエリアに設定 textBox.VerticalOrigin = VerticalOrigin.TopMarginArea; // テキストボックスの垂直位置を設定 textBox.VerticalPosition = 140; // テキストボックスの水平配置を左に設定 textBox.HorizontalAlignment = ShapeHorizontalAlignment.Left; // テキストボックスの水平開始位置を左マージンエリアに設定 textBox.HorizontalOrigin = HorizontalOrigin.LeftMarginArea; // テキストアンカーを下に設定 textBox.Format.TextAnchor = ShapeVerticalAlignment.Bottom; // テキストの折り返しスタイルをテキストの前に設定 textBox.Format.TextWrappingStyle = TextWrappingStyle.InFrontOfText; // テキストの折り返しタイプを両側に設定 textBox.Format.TextWrappingType = TextWrappingType.Both; // パラグラフオブジェクトを作成 Paragraph paragraph = new Paragraph(section.Document); // パラグラフを水平に中央揃えに設定 paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center; // フォントオブジェクトを作成 Font font = new Font("Yu Gothic UI", 8); // ドローイングオブジェクトを作成 Graphics graphics = Graphics.FromImage(new Bitmap(1, 1)); string text1 = " - "; SizeF size1 = graphics.MeasureString(text1, font); float textWidth1 = size1.Width / 96 * 72; int count = (int)(textBox.Width / textWidth1); StringBuilder stringBuilder = new StringBuilder(); for (int i = 1; i < count; i++) { stringBuilder.Append(text1); } // 文字フォーマットオブジェクトを作成 CharacterFormat characterFormat = new CharacterFormat(section.Document); characterFormat.FontName = font.Name; characterFormat.FontSize = font.Size; TextRange textRange = paragraph.AppendText(stringBuilder.ToString()); textRange.ApplyCharacterFormat(characterFormat); // パラグラフをテキストボックスに追加 textBox.ChildObjects.Add(paragraph); } } } C# を使用して Word 文書ページの左側にとじしろを追加する ページの左側にとじしろを設定するには、Section.PageSetup.IsTopGutter プロパティを false に設定する必要があります。以下は詳細な手順です。 Document オブジェクトを作成します。 Document.LoadFromFile() メソッドを使用して Word 文書をロードします。 Document.Sections コレクションに対して for ループを使用して文書のすべてのセクションを反復処理します。 Section.PageSetup.IsTopGutter を false に設定してページの左側にとじしろを表示します。 Section.PageSetup.Gutter プロパティを使用してとじしろの幅を設定します。 カスタムメソッド AddLeftGutterText() を呼び出してとじしろエリアにテキストを追加します。 Document.SaveToFile() メソッドを使用して文書を保存します。 C# using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using Spire.Doc.Formatting; using System.Drawing; using System.Text; namespace SpireDocDemo { internal class Program { static void Main(string[] args) { // ドキュメントオブジェクトを作成 Document document = new Document(); // ドキュメントをロード document.LoadFromFile("Sample.docx"); // ドキュメントの全セクションを繰り返し処理 for (int i = 0; i < document.Sections.Count; i++) { // 現在のセクションを取得 Section section = document.Sections[i]; // ページの上部にガターを追加するかどうかをfalseに設定。ガターはページの左側に追加される section.PageSetup.IsTopGutter = false; // ガターの幅を100fに設定 section.PageSetup.Gutter = 100f; // 左側ガターにテキストを追加するメソッドを呼び出す AddLeftGutterText(section); } // 修正されたドキュメントをファイルに保存 document.SaveToFile("左とじしろの追加.docx", FileFormat.Docx2016); // ドキュメントリソースを解放 document.Dispose(); } // 左側ガターにテキストを追加するメソッド static void AddLeftGutterText(Section section) { // セクションのヘッダーを取得 HeaderFooter header = section.HeadersFooters.Header; // テキストボックスの幅を40に設定 float width = 40; // ページの高さを取得 float height = section.PageSetup.PageSize.Height; // ヘッダーにテキストボックスを追加 TextBox textBox = header.AddParagraph().AppendTextBox(width, height); // テキストボックスの枠線をなしに設定 textBox.Format.NoLine = true; // テキストボックス内のテキスト方向を右から左に設定 textBox.Format.LayoutFlowAlt = TextDirection.RightToLeft; // テキストボックスの水平開始位置を設定 textBox.HorizontalOrigin = HorizontalOrigin.LeftMarginArea; // テキストボックスの水平位置を設定 textBox.HorizontalPosition = 140; // テキストボックスの垂直配置を上に設定 textBox.VerticalAlignment = ShapeVerticalAlignment.Top; // テキストボックスの垂直開始位置を上マージンエリアに設定 textBox.VerticalOrigin = VerticalOrigin.TopMarginArea; // テキストアンカーを上に設定 textBox.Format.TextAnchor = ShapeVerticalAlignment.Top; // テキストの折り返しスタイルをテキストの前に設定 textBox.Format.TextWrappingStyle = TextWrappingStyle.InFrontOfText; // テキストの折り返しタイプを両側に設定 textBox.Format.TextWrappingType = TextWrappingType.Both; // パラグラフオブジェクトを作成 Paragraph paragraph = new Paragraph(section.Document); // パラグラフを水平に中央揃えに設定 paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center; // フォントオブジェクトを作成 Font font = new Font("Yu Gothic UI", 8); // ドローイングオブジェクトを作成 Graphics graphics = Graphics.FromImage(new Bitmap(1, 1)); string text1 = " - "; // テキストのサイズを測定 SizeF size1 = graphics.MeasureString(text1, font); float textWidth1 = size1.Width / 96 * 72; int count = (int)(textBox.Height / textWidth1); StringBuilder stringBuilder = new StringBuilder(); for (int i = 1; i < count; i++) { stringBuilder.Append(text1); } // 文字フォーマットオブジェクトを作成 CharacterFormat characterFormat = new CharacterFormat(section.Document); characterFormat.FontName = font.Name; characterFormat.FontSize = font.Size; TextRange textRange = paragraph.AppendText(stringBuilder.ToString()); textRange.ApplyCharacterFormat(characterFormat); // パラグラフをテキストボックスに追加 textBox.ChildObjects.Add(paragraph); } } } 一時ライセンスを申請する 結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。