在 Word 文档页面上添加装订线可以帮助用户更好地进行页面布局和设计,特别是在准备打印或制作书籍等需要装订的文档时。装订线可以指示页面上的装订位置,有助于用户在设计时留出合适的空白区域,避免文字或图像被裁切。通过设置装订线,用户可以更好地控制文档的外观和版式,确保最终输出符合预期的装订要求,提升文档的专业性和可读性。本文将介绍如何使用 Spire.Doc for Java 在 Java 项目中给 Word 文档页面添加装订线。
安装 Spire.Doc for Java
首先,您需要在 Java 程序中添加 Spire.Doc.jar 文件作为依赖项。您可以从此链接下载 JAR 文件;如果您使用 Maven,则可以通过在 pom.xml 文件中添加以下代码导入 JAR 文件。
<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>13.10.6</version>
    </dependency>
</dependencies>
Java 在 Word 文档页面的上端添加装订线
在 Word 文档中,要设置装订线位于页面上端的关键设置是 section.getPageSetup().isTopGutter(true)。默认情况下,装订线区域是显示为空白没有内容的。此示例还包括了如何向装订线区域添加文本的步骤,可以在装订线周围自定义内容。以下是详细的步骤:
- 创建一个 Document 对象。
 - 使用 Document.loadFromFile() 方法加载一个文档。
 - 使用 for 循环遍历文档的所有节集合 Document.getSections()。
 - 设置 Section.getPageSetup().isTopGutter(true),让装订线在页面上端显示。
 - 使用 Section.getPageSetup().setGutter() 设置装订线的宽度。
 - 调用自定义的 addTopGutterText() 方法将文本添加到装订线区域。
 - 使用 Document.saveToFile() 方法保存到文档。
 
- 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) {
        // 创建一个文档对象
        Document document = new Document();
        // 加载文档
        document.loadFromFile("示例1.docx");
        // 遍历文档的所有节
        for (int i = 0; i < document.getSections().getCount(); i++) {
            // 获取当前节
            Section section = document.getSections().get(i);
            // 设置是否在页面的上端添加装订线为true
            section.getPageSetup().isTopGutter(true);
            // 设置装订线的宽度为100f
            section.getPageSetup().setGutter(100f);
            // 调用方法,在上端装订线上添加文本
            addTopGutterText(section);
        }
        // 将修改后的文档保存为文件
        document.saveToFile("在页面的上端添加装订线.docx", FileFormat.Docx_2016);
        // 释放文档资源
        document.dispose();
    }
    // 在上端装订线上添加文本的方法
    static void addTopGutterText(Section section)
    {
        // 获取节的页眉
        HeaderFooter header = section.getHeadersFooters().getHeader();
        // 设置文本框的宽度为页面宽度
        float width = (float)section.getPageSetup().getPageSize().getWidth();
        // 设置文本框的高度为40
        float height = 40;
        // 在页眉中添加文本框
        TextBox textBox = header.addParagraph().appendTextBox(width, height);
        // 设置文本框无边框
        textBox.getFormat().setNoLine(true);
        // 设置文本框垂直起始位置为页边距顶部
        textBox.setVerticalOrigin(VerticalOrigin.Top_Margin_Area);
        // 设置文本框垂直位置
        textBox.setVerticalPosition(140);
        // 设置文本框水平对齐方式为左对齐
        textBox.setHorizontalAlignment(ShapeHorizontalAlignment.Left);
        // 设置文本框水平起始位置为页边距左侧
        textBox.setHorizontalOrigin(HorizontalOrigin.Left_Margin_Area);
        // 设置文本锚点为底部
        textBox.getFormat().setTextAnchor(ShapeVerticalAlignment.Bottom);
        // 设置文本环绕样式为文字前
        textBox.getFormat().setTextWrappingStyle(TextWrappingStyle.In_Front_Of_Text);
        // 设置文本环绕类型为两侧
        textBox.getFormat().setTextWrappingType(TextWrappingType.Both);
        // 创建一个段落对象
        Paragraph paragraph = new Paragraph(section.getDocument());
        // 设置段落水平居中
        paragraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        // 创建一个字体对象
        Font font = new Font("宋体", 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=" - ";
        String text2="装订线";
        int textWidth1 = fontMetrics.stringWidth(text1);
        int textWidth2 = fontMetrics.stringWidth(text2);
        int count = (int)((textBox.getWidth() -textWidth2)/textWidth1);
        StringBuilder stringBuilder=new StringBuilder();
        for (int i = 1; i < count/2; i++) {
            stringBuilder.append(text1);
        }
        //创建一个字符格式对象
        CharacterFormat characterFormat =new CharacterFormat(section.getDocument());
        characterFormat.setFontName(font.getFontName());
        characterFormat.setFontSize(font.getSize());
        TextRange textRange = paragraph.appendText(stringBuilder.toString());
        textRange.applyCharacterFormat(characterFormat);
        textRange = paragraph.appendText(text2);
        textRange.applyCharacterFormat(characterFormat);
        textRange = paragraph.appendText(stringBuilder.toString());
        textRange.applyCharacterFormat(characterFormat);
        // 将段落添加到文本框中
        textBox.getChildObjects().add(paragraph);
    }
}

Java 在 Word 文档页面的左侧添加装订线
要在页面左侧设置装订线,关键在于将 Section.getPageSetup().isTopGutter() 设为 false。以下是详细的步骤:
- 创建一个 Document 对象。
 - 使用 Document.loadFromFile() 方法加载一个文档。
 - 使用 for 循环遍历文档的所有节集合 Document.getSections()。
 - 设置 Section.getPageSetup().isTopGutter(false),让装订线在页面左侧显示。
 - 使用 Section.getPageSetup().setGutter() 设置装订线的宽度。
 - 调用自定义的 addLeftGutterText() 方法将文本添加到装订线区域。
 - 使用 Document.saveToFile() 方法保存到文档。
 
- 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) {
        // 创建一个文档对象
        Document document = new Document();
        // 加载文档
        document.loadFromFile("示例1.docx");
        // 遍历文档的所有节
        for (int i = 0; i < document.getSections().getCount(); i++) {
            // 获取当前节
            Section section = document.getSections().get(i);
            // 设置是否在页面的上端添加装订线为false, 则会添加到页面的左侧
            section.getPageSetup().isTopGutter(false);
            // 设置装订线的宽度为100f
            section.getPageSetup().setGutter(100f);
            // 调用方法,在左侧装订线上添加文本
            AddLeftGutterText(section);
        }
        // 将修改后的文档保存为文件
        document.saveToFile("在页面的左侧添加装订线.docx", FileFormat.Docx_2016);
        // 释放文档资源
        document.dispose();
    }
    // 在左侧装订线上添加文本的方法
    static void AddLeftGutterText(Section section) {
        // 获取节的页眉
        HeaderFooter header = section.getHeadersFooters().getHeader();
        // 设置文本框的宽度为40
        float width = 40;
        // 获取页面高度
        float height = (float)section.getPageSetup().getPageSize().getHeight();
        // 在页眉中添加文本框
        TextBox textBox = header.addParagraph().appendTextBox(width, height);
        // 设置文本框无边框
        textBox.getFormat().setNoLine(true);
        // 设置文本框中文本方向为从右向左
        textBox.getFormat().setLayoutFlowAlt(TextDirection.Right_To_Left);
        // 设置文本框水平起始位置
        textBox.setHorizontalOrigin(HorizontalOrigin.Left_Margin_Area);
        // 设置文本框水平位置
        textBox.setHorizontalPosition(140);
        // 设置文本框垂直对齐方式为顶部
        textBox.setVerticalAlignment(ShapeVerticalAlignment.Top);
        // 设置文本框垂直起始位置为页边距顶部
        textBox.setVerticalOrigin(VerticalOrigin.Top_Margin_Area);
        // 设置文本锚点为顶部
        textBox.getFormat().setTextAnchor(ShapeVerticalAlignment.Top);
        // 设置文本环绕样式为文字前
        textBox.getFormat().setTextWrappingStyle(TextWrappingStyle.In_Front_Of_Text);
        // 设置文本环绕类型为两侧
        textBox.getFormat().setTextWrappingType(TextWrappingType.Both);
        // 创建一个段落对象
        Paragraph paragraph = new Paragraph(section.getDocument());
        // 设置段落水平居中
        paragraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        // 创建字体对象,宋体,大小8
        Font font = new Font("宋体", 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=" - ";
        String text2="装订线";
        int textWidth1 = fontMetrics.stringWidth(text1);
        int textWidth2 = fontMetrics.stringWidth(text2);
        int count = (int)((textBox.getHeight() -textWidth2)/textWidth1);
        StringBuilder stringBuilder=new StringBuilder();
        for (int i = 1; i < count/2; i++) {
            stringBuilder.append(text1);
        }
        // 创建字符格式对象
        CharacterFormat characterFormat =new CharacterFormat(section.getDocument());
        characterFormat.setFontName(font.getFontName());
        characterFormat.setFontSize(font.getSize());
        TextRange textRange = paragraph.appendText(stringBuilder.toString());
        textRange.applyCharacterFormat(characterFormat);
        textRange = paragraph.appendText(text2);
        textRange.applyCharacterFormat(characterFormat);
        textRange = paragraph.appendText(stringBuilder.toString());
        textRange.applyCharacterFormat(characterFormat);
        // 将段落添加到文本框中
        textBox.getChildObjects().add(paragraph);
    }
}

申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
    


					



