Java: Add Gutters on Word Document Pages

Adding gutters on Word document pages can assist users in better page layout and design, especially when preparing documents for printing or creating books that require gutter. Gutter can indicate the gutter position on the page, helping users to design with appropriate blank areas to avoid text or images being cut off. By setting gutter, users can have better control over the appearance and layout of the document, ensuring that the final output meets the expected gutter requirements, enhancing the professionalism and readability of the document. This article shows how to add gutters on Word document pages by programming using Spire.Doc for Java.

Install Spire.Doc for Java

First, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>12.4.14</version>
    </dependency>
</dependencies>
    

Add a Gutter at the Top of a Word Document Page in Java

In a Word document, you can set section.getPageSetup().isTopGutter(true) to place the gutter at the top of the page. By default, the gutter area is displayed as blank without any content. This example also includes steps on how to add content, such as the dash symbol, to the gutter area to customize content around the gutter. 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 and Document.getSections().
  • Set Section.getPageSetup().isTopGutter(true) to display the gutter at the top of the page.
  • Use Section.getPageSetup().setGutter() to set the width of the gutter.
  • Call the custom addTopGutterContent() method to add content to the gutter area.
  • Save the document using the Document.saveToFile() method.
  • Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import com.spire.doc.formatting.CharacterFormat;
import java.awt.*;

public class AddTopGutter {
    public 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.getSections().getCount(); i++) {
            // Get the current section
            Section section = document.getSections().get(i);

            // Set whether to add a gutter at the top of the page to true
            section.getPageSetup().isTopGutter(true);

            // Set the width of the gutter to 100f
            section.getPageSetup().setGutter(100f);

            // Call the method to add content to the top gutter
            addTopGutterContent(section);
        }

        // Save the modified document to a file
        document.saveToFile("AddGutterOnTop.docx", FileFormat.Docx_2016);

        // Release document resources
        document.dispose();
    }

    // Method to add content to the top gutter
    static void addTopGutterContent(Section section) {
        // Get the header of the section
        HeaderFooter header = section.getHeadersFooters().getHeader();

        // Set the width of the text box to the width of the page
        float width = (float) section.getPageSetup().getPageSize().getWidth();

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

        // Add a text box to the header
        TextBox textBox = header.addParagraph().appendTextBox(width, height);

        // Set the text box without borders
        textBox.getFormat().setNoLine(true);

        // Set the vertical origin of the text box to the top margin area
        textBox.setVerticalOrigin(VerticalOrigin.Top_Margin_Area);

        // Set the vertical position of the text box
        textBox.setVerticalPosition(140);

        // Set the horizontal alignment of the text box to left
        textBox.setHorizontalAlignment(ShapeHorizontalAlignment.Left);

        // Set the horizontal origin of the text box to the left margin area
        textBox.setHorizontalOrigin(HorizontalOrigin.Left_Margin_Area);

        // Set the text anchor to the bottom
        textBox.getFormat().setTextAnchor(ShapeVerticalAlignment.Bottom);

        // Set the text wrapping style to in front of text
        textBox.getFormat().setTextWrappingStyle(TextWrappingStyle.In_Front_Of_Text);

        // Set the text wrapping type to both sides
        textBox.getFormat().setTextWrappingType(TextWrappingType.Both);

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

        // Set the paragraph alignment to center
        paragraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);

        // Create a font object
        Font font = new Font("SimSun", Font.PLAIN, 8);

        Graphics graphics = new java.awt.image.BufferedImage(1, 1, java.awt.image.BufferedImage.TYPE_INT_ARGB).getGraphics();
        graphics.setFont(font);
        FontMetrics fontMetrics = graphics.getFontMetrics();
        String text1 = " - ";
        int textWidth1 = fontMetrics.stringWidth(text1);
        int count = (int) (textBox.getWidth() / 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.getDocument());
        characterFormat.setFontName(font.getFontName());
        characterFormat.setFontSize(font.getSize());
        TextRange textRange = paragraph.appendText(stringBuilder.toString());
        textRange.applyCharacterFormat(characterFormat);

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

Java: Add Gutters on Word document pages

Add a Gutter at the Left of a Word Document Page in Java

To set the gutter on the left side of the page, the key is to set Section.getPageSetup().isTopGutter(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 and Document.getSections().
  • Set Section.getPageSetup().isTopGutter(false) to display the gutter on the left side of the page.
  • Use Section.getPageSetup().setGutter() to set the width of the gutter.
  • Call the custom addLeftGutterContent() method to add content to the gutter area.
  • Save the document using the Document.saveToFile() method.
  • Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import com.spire.doc.formatting.CharacterFormat;
import java.awt.*;

public class AddLeftGutter {
    public 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.getSections().getCount(); i++) {
            // Get the current section
            Section section = document.getSections().get(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.getPageSetup().isTopGutter(false);

            // Set the width of the gutter to 100f
            section.getPageSetup().setGutter(100f);

            // Call the method to add content to the left gutter
            AddLeftGutterContent (section);
        }

        // Save the modified document to a file
        document.saveToFile("AddGutterOnLeft.docx", FileFormat.Docx_2016);

        // Release document resources
        document.dispose();
    }

    // Method to add content to the left gutter
    static void AddLeftGutterContent(Section section) {
        // Get the header of the section
        HeaderFooter header = section.getHeadersFooters().getHeader();

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

        // Get the page height
        float height = (float) section.getPageSetup().getPageSize().getHeight();

        // Add a text box to the header
        TextBox textBox = header.addParagraph().appendTextBox(width, height);

        // Set the text box without borders
        textBox.getFormat().setNoLine(true);

        // Set the text direction in the text box from right to left
        textBox.getFormat().setLayoutFlowAlt(TextDirection.Right_To_Left);

        // Set the horizontal starting position of the text box
        textBox.setHorizontalOrigin(HorizontalOrigin.Left_Margin_Area);

        // Set the horizontal position of the text box
        textBox.setHorizontalPosition(140);

        // Set the vertical alignment of the text box to the top
        textBox.setVerticalAlignment(ShapeVerticalAlignment.Top);

        // Set the vertical origin of the text box to the top margin area
        textBox.setVerticalOrigin(VerticalOrigin.Top_Margin_Area);

        // Set the text anchor to the top
        textBox.getFormat().setTextAnchor(ShapeVerticalAlignment.Top);

        // Set the text wrapping style to in front of text
        textBox.getFormat().setTextWrappingStyle(TextWrappingStyle.In_Front_Of_Text);

        // Set the text wrapping type to both sides
        textBox.getFormat().setTextWrappingType(TextWrappingType.Both);

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

        // Set the paragraph alignment to center
        paragraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);

        // Create a font object, SimSun, size 8
        Font font = new Font("SimSun", Font.PLAIN, 8);

        Graphics graphics = new java.awt.image.BufferedImage(1, 1, java.awt.image.BufferedImage.TYPE_INT_ARGB).getGraphics();
        graphics.setFont(font);
        FontMetrics fontMetrics = graphics.getFontMetrics();
        String text1 = " - ";
        int textWidth1 = fontMetrics.stringWidth(text1);
        int count = (int) (textBox.getHeight() / 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.getDocument());
        characterFormat.setFontName(font.getFontName());
        characterFormat.setFontSize(font.getSize());
        TextRange textRange = paragraph.appendText(stringBuilder.toString());
        textRange.applyCharacterFormat(characterFormat);

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

Java: 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.