C#: Add Gutters on Word Document Pages

2024-03-27 01:31:57 Written by  support iceblue
Rate this item
(0 votes)

Adding gutters on Word document pages can enhance the professionalism and aesthetics of the document. Gutters not only make the document appear neater and more organized but also serve as a guide when printing the document, making it easier for readers to navigate and browse through the content. By adding gutters on the document pages, you can simulate the common binding line effect found in physical documents, giving the document a more printed quality. This article will explain how to use Spire.Doc for .NET to add gutters on Word document pages within a C# project.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Add a Gutter at the Top of a Word Document Page using C#

To enable the top gutter on a page, you can set the section.PageSetup.IsTopGutter = true. The default gutter area displays blank without content, and this example also includes how to add text within the gutter area. Here are the detailed steps:

  • Create a Document object.
  • Load a document using the Document.LoadFromFile() method.
  • Iterate through all sections of the document using a for loop over the Document.Sections collection.
  • Set Section.PageSetup.IsTopGutter to true to display the gutter at the top of the page.
  • Use the Section.PageSetup.Gutter property to set the width of the gutter.
  • Call the custom AddTopGutterText() method to add text to the gutter area.
  • Save the document using the Document.SaveToFile() method.
  • 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)
		{
			// Create a document object
			Document document = new Document();

			// Load the document
			document.LoadFromFile("Sample1.docx");

			// Iterate through all sections of the document
			for (int i = 0; i < document.Sections.Count; i++)
			{
				// Get the current section
				Section section = document.Sections[i];

				// Set whether to add a gutter at the top of the page to true
				section.PageSetup.IsTopGutter = true;

				// Set the width of the gutter to 100f
				section.PageSetup.Gutter = 100f;

				// Call a method to add text on the top gutter
				AddTopGutterText(section);
			}

			// Save the modified document to a file
			document.SaveToFile("Add Gutter Line at the Top of the Page.docx", FileFormat.Docx2016);

			// Release document resources
			document.Dispose();
		}
		// Method to add text on the top gutter 
		static void AddTopGutterText(Section section)
		{
			// Get the header of the section
			HeaderFooter header = section.HeadersFooters.Header;

			// Set the width of the text box to the page width
			float width = section.PageSetup.PageSize.Width;

			// Set the height of the text box to 40
			float height = 40;

			// Add a text box in the header
			TextBox textBox = header.AddParagraph().AppendTextBox(width, height);

			// Set the text box without border
			textBox.Format.NoLine = true;

			// Set the vertical starting position of the text box to the top margin area
			textBox.VerticalOrigin = VerticalOrigin.TopMarginArea;

			// Set the vertical position of the text box
			textBox.VerticalPosition = 140;

			// Set the horizontal alignment of the text box to left
			textBox.HorizontalAlignment = ShapeHorizontalAlignment.Left;

			// Set the horizontal starting position of the text box to the left margin area
			textBox.HorizontalOrigin = HorizontalOrigin.LeftMarginArea;

			// Set the text anchor to bottom
			textBox.Format.TextAnchor = ShapeVerticalAlignment.Bottom;

			// Set the text wrapping style to in front of text
			textBox.Format.TextWrappingStyle = TextWrappingStyle.InFrontOfText;

			// Set the text wrapping type to both sides
			textBox.Format.TextWrappingType = TextWrappingType.Both;

			// Create a paragraph object
			Paragraph paragraph = new Paragraph(section.Document);

			// Set the paragraph to be horizontally centered
			paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center;

			// Create a font object
			Font font = new Font("Times New Roman", 8);

			// Create a drawing object
			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);
			}

			// Create a character format object
			CharacterFormat characterFormat = new CharacterFormat(section.Document);
			characterFormat.FontName = font.Name;
			characterFormat.FontSize = font.Size;
			TextRange textRange = paragraph.AppendText(stringBuilder.ToString());
			textRange.ApplyCharacterFormat(characterFormat);

			// Add the paragraph to the text box
			textBox.ChildObjects.Add(paragraph);
		}
	}
}

C#: Add Gutters on Word Document Pages

Add a Gutter at the Left of a Word Document Page using C#

To set the left-side gutter on the page, ensure that you set the Section.PageSetup.IsTopGutter property to false. Here are the detailed steps:

  • Create a Document object.
  • Load a document using the Document.LoadFromFile() method.
  • Iterate through all sections of the document using a for loop over the Document.Sections collection.
  • Set Section.PageSetup.IsTopGutter to false to display the gutter on the left side of the page.
  • Use the Section.PageSetup.Gutter property to set the width of the gutter.
  • Call the custom AddLeftGutterText() method to add text to the gutter area.
  • Save the document using the Document.SaveToFile() method.
  • 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)
		{
			// Create a document object
			Document document = new Document();

			// Load the document
			document.LoadFromFile("Sample1.docx");

			// Iterate through all sections of the document
			for (int i = 0; i < document.Sections.Count; i++)
			{
				// Get the current section
				Section section = document.Sections[i];

				// Set whether to add a gutter at the top of the page to false, it will be added to the left side of the page
				section.PageSetup.IsTopGutter = false;

				// Set the width of the gutter to 100f
				section.PageSetup.Gutter = 100f;

				// Call a method to add text on the left gutter 
				AddLeftGutterText(section);
			}

			// Save the modified document to a file
			document.SaveToFile("Add Gutter Line on the Left Side of the Page.docx", FileFormat.Docx2016);

			// Release document resources
			document.Dispose();
		}
		// Method to add text on the left gutter 
		static void AddLeftGutterText(Section section)
		{
			// Get the header of the section
			HeaderFooter header = section.HeadersFooters.Header;

			// Set the width of the text box to 40
			float width = 40;

			// Get the page height
			float height = section.PageSetup.PageSize.Height;

			// Add a text box in the header
			TextBox textBox = header.AddParagraph().AppendTextBox(width, height);

			// Set the text box without border
			textBox.Format.NoLine = true;

			// Set the text direction in the text box from right to left
			textBox.Format.LayoutFlowAlt = TextDirection.RightToLeft;

			// Set the horizontal starting position of the text box
			textBox.HorizontalOrigin = HorizontalOrigin.LeftMarginArea;

			// Set the horizontal position of the text box
			textBox.HorizontalPosition = 140;

			// Set the vertical alignment of the text box to top
			textBox.VerticalAlignment = ShapeVerticalAlignment.Top;

			// Set the vertical starting position of the text box to the top margin area
			textBox.VerticalOrigin = VerticalOrigin.TopMarginArea;

			// Set the text anchor to top
			textBox.Format.TextAnchor = ShapeVerticalAlignment.Top;

			// Set the text wrapping style to in front of text
			textBox.Format.TextWrappingStyle = TextWrappingStyle.InFrontOfText;

			// Set the text wrapping type to both sides
			textBox.Format.TextWrappingType = TextWrappingType.Both;

			// Create a paragraph object
			Paragraph paragraph = new Paragraph(section.Document);

			// Set the paragraph to be horizontally centered
			paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center;

			// Create a font object
			Font font = new Font("Times New Roman", 8);

			// Create a drawing object
			Graphics graphics = Graphics.FromImage(new Bitmap(1, 1));
			string text1 = " - ";

			// Measure the size of the text
			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);
			}

			// Create a character format object
			CharacterFormat characterFormat = new CharacterFormat(section.Document);
			characterFormat.FontName = font.Name;
			characterFormat.FontSize = font.Size;
			TextRange textRange = paragraph.AppendText(stringBuilder.ToString());
			textRange.ApplyCharacterFormat(characterFormat);

			// Add the paragraph to the text box
			textBox.ChildObjects.Add(paragraph);
		}
	}
}

C#: Add Gutters on Word Document Pages

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.