冰蓝科技
|
028-81705109
|
|
微信扫一扫
|

Spire.Cloud 纯前端文档控件

扁平化 PDF 表单域意味着将交互式表单域转换为不可编辑或填写的静态内容。一旦表单域被扁平化,用户将无法修改或更改其中已填写的数据。这种转换过程可以确保已填写的表单数据的永久性和不可篡改性,为将来的使用提供了可靠的参考。本文将介绍如何使用 Python 和 Spire.PDF for Python 扁平化 PDF 中的表单域。

安装 Spire.PDF for Python

本教程需要用到 Spire.PDF for Python 和 plum-dispatch v1.7.4。可以通过以下 pip 命令将它们轻松安装到 Windows 中。

pip install Spire.PDF

如果您不清楚如何安装,请参考此教程: 如何在 Windows 中安装 Spire.PDF for Python

Python 扁平化 PDF 中的所有表单域

Spire.PDF for Python 提供的 PdfDocument.Form.IsFlatten 属性,支持扁平化PDF文件中的所有表单域。具体步骤如下。

  • Python
from spire.pdf.common import *
from spire.pdf import *

# 指定输入和输出PDF文件路径
input_file = "表单.pdf"
output_file = "扁平所有域.pdf"

# 创建PdfDocument类的对象
doc = PdfDocument()
# 加载PDF文件
doc.LoadFromFile(input_file)

# 扁平化PDF文件中的所有表单
doc.Form.IsFlatten = True

# 保存结果文件
doc.SaveToFile(output_file)
doc.Close()

Python 扁平化 PDF 表单域

Python 扁平化 PDF 中的特定表单域

要在 PDF 文件中扁平化特定的表单域,你可以使用 PdfField.Flatten 属性。具体步骤如下。

  • Python
from spire.pdf.common import *
from spire.pdf import *

# 指定输入和输出PDF文件路径
input_file = "表单.pdf"
output_file = "扁平特定域.pdf"

# 创建PdfDocument类的对象
doc = PdfDocument()
# 加载PDF文件
doc.LoadFromFile(input_file)

# 获取PDF文件的表单
loadedForm = doc.Form

# 通过索引或名称获取特定的表单
formWidget = PdfFormWidget(loadedForm)
form = formWidget.FieldsWidget.get_Item(1)
# form = formWidget.FieldsWidget.get_Item("地址")

# 扁平化特定的表单
form.Flatten = True

# 保存结果文件
doc.SaveToFile(output_file)
doc.Close()

Python 扁平化 PDF 表单域

申请临时 License

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

Spire.Presentation 9.3.4 已发布。该版本新增 SaveToSvgOption 用于设置 PPTX 转 Svg 的转换选项,同时也支持获取 PPT 默认字体样式。除此之外,一些在转换 PPTX 到 PDF/SVG 以及加载、保存文档时出现的问题也得以成功解决。详情见下文。

新功能:

问题修复:


获取 Spire.Presentation 9.3.4,请点击:

https://www.e-iceblue.cn/Downloads/Spire-Presentation-NET.html

在 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>14.9.5</version>
    </dependency>
</dependencies>

Java 在 Word 文档页面的上端添加装订线

在 Word 文档中,要设置装订线位于页面上端的关键设置是 section.getPageSetup().isTopGutter(true)。默认情况下,装订线区域是显示为空白没有内容的。此示例还包括了如何向装订线区域添加文本的步骤,可以在装订线周围自定义内容。以下是详细的步骤: 
  • 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 文档页面上添加装订线

Java 在 Word 文档页面的左侧添加装订线

要在页面左侧设置装订线,关键在于将 Section.getPageSetup().isTopGutter() 设为 false。以下是详细的步骤:

  • 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);
    }
}

Java 在 Word 文档页面上添加装订线

申请临时 License

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

在 Word 文档中,页眉位于页面顶部,通常包含文档标题、章节标题、作者姓名等信息;页脚位于页面底部,通常包含页码、日期、版权信息等内容。通过在文档中插入页眉和页脚,可以使文档更具结构性和完整性,为读者提供更好的阅读体验,同时也有助于文档的管理和传达信息。在本文中,我们将解释如何使用 Spire.Doc for Python 在 Python 中插入页眉页脚到 Word 文档。

安装 Spire.Doc for Python

本教程需要 Spire.Doc for Python 和 plum-dispatch v1.7.4。您可以通过以下 pip 命令将它们轻松安装到 Windows 中。

pip install Spire.Doc

如果您不确定如何安装,请参考此教程: 如何在 Windows 中安装 Spire.Doc for Python

Python 插入页眉页脚到 Word 文档

您可以使用 Spire.Doc for Python 提供的 Section.HeadersFooters.Header 和 Section.HeadersFooters.Footers 属性获取 Word 文档的页眉页脚,然后在页眉页脚中添加段落,再将文本、图片、页码等添加到段落中。详细步骤如下:

  • Python
from spire.doc import *
from spire.doc.common import *

# 定义一个函数,用于在节中插入页眉和页脚
def InsertHeaderAndFooter(section):
    # 获取节的页眉和页脚对象
    header = section.HeadersFooters.Header
    footer = section.HeadersFooters.Footer

    # 在页眉中添加段落
    headerParagraph = header.AddParagraph()

    # 在页眉段落中插入图片
    headerPicture = headerParagraph.AppendPicture("Header.png")

    # 在页眉段落中插入文本
    text = headerParagraph.AppendText("添加页眉")
    text.CharacterFormat.FontName = "宋体"
    text.CharacterFormat.FontSize = 10
    text.CharacterFormat.Italic = True
    headerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Right

    # 设置页眉段落的底部边框样式
    headerParagraph.Format.Borders.Bottom.BorderType = BorderStyle.Single
    headerParagraph.Format.Borders.Bottom.Space = 0.05

    # 设置图片的文本环绕样式和位置
    headerPicture.TextWrappingStyle = TextWrappingStyle.Behind
    headerPicture.HorizontalOrigin = HorizontalOrigin.Page
    headerPicture.HorizontalAlignment = ShapeHorizontalAlignment.Left
    headerPicture.VerticalOrigin = VerticalOrigin.Page
    headerPicture.VerticalAlignment = ShapeVerticalAlignment.Top

    # 在页脚中添加段落
    footerParagraph = footer.AddParagraph()

    # 在页脚段落中插入图片
    footerPicture = footerParagraph.AppendPicture("Footer.png")

    # 设置页脚图片的文本环绕样式和位置
    footerPicture.TextWrappingStyle = TextWrappingStyle.Behind
    footerPicture.HorizontalOrigin = HorizontalOrigin.Page
    footerPicture.HorizontalAlignment = ShapeHorizontalAlignment.Left
    footerPicture.VerticalOrigin = VerticalOrigin.Page
    footerPicture.VerticalAlignment = ShapeVerticalAlignment.Bottom

    # 在页脚段落中插入文本和字段
    footerParagraph.AppendText("第 ")
    footerParagraph.AppendField("页码", FieldType.FieldPage)
    footerParagraph.AppendText("页 共 ")
    footerParagraph.AppendField("页数", FieldType.FieldNumPages)
    footerParagraph.AppendText("页")
    footerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Center

    # 设置页脚段落的顶部边框样式
    footerParagraph.Format.Borders.Top.BorderType = BorderStyle.Single
    footerParagraph.Format.Borders.Top.Space = 0.05

# 创建一个新的文档对象
document = Document()
# 从现有文件加载文档内容
document.LoadFromFile("示例文档.docx")
# 获取第一个节
section = document.Sections.get_Item(0)
# 在节中插入页眉和页脚
InsertHeaderAndFooter(section)
# 将文档保存为"添加页眉页脚.docx"文件,使用Docx格式
document.SaveToFile("添加页眉页脚.docx", FileFormat.Docx)
# 关闭文档对象
document.Close()

Python 添加页眉页脚到 Word 文档

Python 插入表格到 Word 文档的页眉

Word 文档的页眉也支持添加表格作为其内容;Spire.Doc for Python 提供了 Header.AddTable(True) 方法添加表格到页眉。详细步骤如下:

  • Python
from spire.doc import *
from spire.doc.common import *

# 定义一个函数,用于在页眉中插入表格
def InsertTableToHeader(section):
    # 获取节的页眉对象
    header = section.HeadersFooters.Header

    # 在页眉中添加一个表格
    tableHeader = header.AddTable(True)
    tableHeader.ResetCells(2, 2)
    data = [["出版号", "日期"], ["SC12546", "2019年7月24日"]]
    i = 0
    while i < len(data):
        dataRow = tableHeader.Rows.get_Item(i)
        dataRow.Height = 20
        dataRow.HeightType = TableRowHeightType.Exactly
        dataRow.RowFormat.BackColor = Color.Empty()
        c = 0
        while c < len(data[i]):
            dataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle
            range = dataRow.Cells[c].AddParagraph().AppendText(data[i][c])
            range.CharacterFormat.FontName = "宋体"
            dataRow.Cells[c].CellFormat.HorizontalAlignment = HorizontalAlignment.Center
            c += 1
        i += 1

# 创建一个新的文档对象
document = Document()
# 从现有文件加载文档内容
document.LoadFromFile("示例文档.docx")
# 获取第一个节
section = document.Sections.get_Item(0)
# 在节的页眉中插入表格
InsertTableToHeader(section)
# 将文档保存为"添加表格页眉.docx"文件,使用Docx格式
document.SaveToFile("添加表格页眉.docx", FileFormat.Docx)
document.Close()

Python 添加页眉页脚到 Word 文档

Python 设置 Word 文档首页页眉页脚不同

Spire.Doc for Python 提供了一个 Section.PageSetup.DifferentFirstPageHeaderFooter 属性可以设置首页页眉页脚与其他页不同。详细步骤如下:

  • Python
from spire.doc import *
from spire.doc.common import *

# 定义输入和输出文件名
inputFile = "输入文档.docx"
outputFile = "首页不同.docx"

# 创建一个文档对象
doc = Document()
# 从文件加载文档内容
doc.LoadFromFile(inputFile)

# 获取第一个节
section = doc.Sections.get_Item(0)
# 设置不同的首页页眉和页脚
section.PageSetup.DifferentFirstPageHeaderFooter = True

# 在第一页的页眉中添加段落
paragraph1 = section.HeadersFooters.FirstPageHeader.AddParagraph()
paragraph1.Format.HorizontalAlignment = HorizontalAlignment.Left
range1 = paragraph1.AppendText("首页页眉")
range1.CharacterFormat.FontName = "宋体"
range1.CharacterFormat.FontSize = 12

# 在第一页的页脚中添加段落
paragraph2 = section.HeadersFooters.FirstPageFooter.AddParagraph()
paragraph2.Format.HorizontalAlignment = HorizontalAlignment.Left
range2 = paragraph2.AppendText("首页页脚")
range2.CharacterFormat.FontName = "宋体"
range2.CharacterFormat.FontSize = 12

# 在非首页页眉中添加段落
paragraph3 = section.HeadersFooters.Header.AddParagraph()
paragraph3.Format.HorizontalAlignment = HorizontalAlignment.Right
range3 = paragraph3.AppendText("非首页页眉")
range3.CharacterFormat.FontName = "微软雅黑"
range3.CharacterFormat.FontSize = 14

# 在非首页页脚中添加段落
paragraph4 = section.HeadersFooters.Footer.AddParagraph()
paragraph4.Format.HorizontalAlignment = HorizontalAlignment.Right
range4 = paragraph4.AppendText("非首页页脚")
range4.CharacterFormat.FontName = "微软雅黑"
range4.CharacterFormat.FontSize = 14

# 将文档保存为指定的输出文件
doc.SaveToFile(outputFile, FileFormat.Docx)
# 关闭文档对象
doc.Close()

Python 添加页眉页脚到 Word 文档

Python 设置 Word 文档奇偶页页眉页脚不同

Spire.Doc for Python 提供了 Section.PageSetup.DifferentOddAndEvenPagesHeaderFooter 属性来设置奇数页和偶数页不同的页眉页脚内容。详细步骤如下:

  • Python
from spire.doc import *
from spire.doc.common import *

# 定义输入和输出文件名
inputFile = "示例文档.docx"
outputFile = "奇偶页眉页脚.docx"

# 创建一个文档对象
doc = Document()
# 从文件加载文档内容
doc.LoadFromFile(inputFile)

# 获取第一个节
section = doc.Sections.get_Item(1)
# 设置不同的奇偶页页眉和页脚
section.PageSetup.DifferentOddAndEvenPagesHeaderFooter = True

# 在奇数页的页眉中添加段落
P3 = section.HeadersFooters.OddHeader.AddParagraph()
OH = P3.AppendText("奇数页页眉")
P3.Format.HorizontalAlignment = HorizontalAlignment.Center
OH.CharacterFormat.FontName = "微软雅黑"
OH.CharacterFormat.FontSize = 10

# 在偶数页的页眉中添加段落
P4 = section.HeadersFooters.EvenHeader.AddParagraph()
EH = P4.AppendText("偶数页页眉")
P4.Format.HorizontalAlignment = HorizontalAlignment.Center
EH.CharacterFormat.FontName = "微软雅黑"
EH.CharacterFormat.FontSize = 10

# 在奇数页的页脚中添加段落
P2 = section.HeadersFooters.OddFooter.AddParagraph()
OF = P2.AppendText("奇数页页脚")
P2.Format.HorizontalAlignment = HorizontalAlignment.Center
OF.CharacterFormat.FontName = "微软雅黑"
OF.CharacterFormat.FontSize = 10

# 在偶数页的页脚中添加段落
P1 = section.HeadersFooters.EvenFooter.AddParagraph()
EF = P1.AppendText("偶数页页脚")
EF.CharacterFormat.FontName = "微软雅黑"
EF.CharacterFormat.FontSize = 10
P1.Format.HorizontalAlignment = HorizontalAlignment.Center

# 将文档保存为指定的输出文件
doc.SaveToFile(outputFile, FileFormat.Docx)
# 关闭文档对象
doc.Close()

Python 添加页眉页脚到 Word 文档

Python 添加页眉页脚到 Word 文档

申请临时 License

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

Spire.Doc for Python 12.3.2 已发布。该版本新增支持获取修订的功能。另外还修复了使用 FindString() 未匹配到关键词时,程序抛出 TypeError 错误问题。详情请阅读以下内容。

新功能:

问题修复:


获取 Spire.Doc for Python 12.3.2 请点击:

https://www.e-iceblue.cn/Downloads/Spire-Doc-Python.html

在 Word 文档页面上添加装订线可以提高文档的专业性和美观度。装订线不仅可以使文档看起来更加整洁和有条理,还可以在打印文档时起到指导作用,帮助读者更容易翻阅和浏览文档内容。通过在文档页面上添加装订线,可以模拟出纸质文档中常见的装订线效果,让文档更具印刷品质。本文将介绍如何使用 Spire.Doc for .NET 在 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。默认的装订线区域是显示空白没有内容,此示例也包含了在装订线区域如何添加文本。以下是详细的步骤:

  • 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("示例1.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;

			// 创建字体对象,宋体,大小8
			Font font = new Font("宋体", 8);

			// 创建绘图对象
			Graphics graphics = Graphics.FromImage(new Bitmap(1, 1));
			string text1 = " - ";
			string text2 = "装订线";
			SizeF size1 = graphics.MeasureString(text1, font);
			SizeF size2 = graphics.MeasureString(text2, font);
			float textWidth1 = size1.Width / 96 * 72;
			float textWidth2 = size2.Width / 96 * 72;
			int count = (int)((textBox.Width - textWidth2) / textWidth1);
			StringBuilder stringBuilder = new StringBuilder();
			for (int i = 2; i < count / 2; 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);
			textRange = paragraph.AppendText(text2);
			textRange.ApplyCharacterFormat(characterFormat);
			textRange = paragraph.AppendText(stringBuilder.ToString());
			textRange.ApplyCharacterFormat(characterFormat);

			// 将段落添加到文本框中
			textBox.ChildObjects.Add(paragraph);
		}
	}
}

C# 在 Word 文档页面上添加装订线

C# 在 Word 文档页面的左侧添加装订线

在页面左侧设置装订线的关键是将 Section.PageSetup.IsTopGutter 属性设置为 false。以下是详细的步骤:

  • 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("示例1.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;

            // 创建字体对象,宋体,大小8
            Font font = new Font("宋体", 8);

            // 创建绘图对象
            Graphics graphics = Graphics.FromImage(new Bitmap(1, 1));
            string text1 = " - ";
            string text2 = "装订线";

            // 测量文本的大小
            SizeF size1 = graphics.MeasureString(text1, font);
            SizeF size2 = graphics.MeasureString(text2, font);
            float textWidth1 = size1.Width / 96 * 72;
            float textWidth2 = size2.Width / 96 * 72;
            int count = (int)((textBox.Height - textWidth2) / textWidth1);
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 5; i < count / 2; 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);
            textRange = paragraph.AppendText(text2);
            textRange.ApplyCharacterFormat(characterFormat);
            textRange = paragraph.AppendText(stringBuilder.ToString());
            textRange.ApplyCharacterFormat(characterFormat);

            // 将段落添加到文本框中
            textBox.ChildObjects.Add(paragraph);
        }
    }
}

C# 在 Word 文档页面上添加装订线

申请临时 License

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

Spire.Cloud 9.3.5 已发布。该版本在原有 Spire.Cloud.Word 产品基础上,新增了 WordAI 功能。通过向 AI 大模型发送指令,可以实现一键生成文章,以及对现有内容进行编辑、润色、翻译、总结等各项操作。此外,该版本还修复了在 embedded 模式下打开文档时出现的问题。详情请查看以下内容。

新功能:

问题修复:


获取 Spire.Cloud 9.3.5 请点击:

https://cloud.e-iceblue.cn/index.php/privatization-download

PDF 是一种理想的文件共享和存档的格式。在处理文本文件时,将其转换为 PDF 文件可以增强文件的便携性、安全性,且能更好的保留文件的板式、外观等。在本文中,您将学习如何使用 Spire.PDF for Python 在 Python 中将 TXT 文件转换为 PDF。

安装 Spire.PDF for Python

本教程需要用到 Spire.PDF for Python 和 plum-dispatch v1.7.4。可以通过以下 pip 命令将它们轻松安装到 Windows 中。

pip install Spire.PDF

如果您不清楚如何安装,请参考: 如何在 Windows 中安装 Spire.PDF for Python

Python 将 TXT 转换为 PDF

Spire.PDF for Python 可通过读取输入 TXT 文件中的文本内容,然后将其绘制到 PDF 文档的页面上,从而将文本文件转换为 PDF。下面列出了一些使用到的核心类和方法:

以下是使用 Python 将 TXT 文本文件转换为 PDF 的详细步骤:

  • Python
from spire.pdf.common import *
from spire.pdf import *

def ReadFromTxt(fname: str) -> str:
    with open(fname, 'r', encoding='utf-8') as f:
        text = f.read()
    return text

inputFile = "文本.txt"
outputFile = "Text转Pdf.pdf"

# 获取txt文件中的文本
text = ReadFromTxt(inputFile)

# 创建PdfDocument对象
pdf = PdfDocument()

# 添加一页
page = pdf.Pages.Add()

# 创建PDF字体和笔刷
font = PdfTrueTypeFont("宋体", 12.0, PdfFontStyle.Regular, True)
brush = PdfBrushes.get_Black()

# 设置文本对齐方式和行距
strformat = PdfStringFormat()
strformat.LineSpacing = 10.0
strformat.Alignment = PdfTextAlignment.Justify

# 设置文本版式
textLayout = PdfTextLayout()
textLayout.Break = PdfLayoutBreakType.FitPage
textLayout.Layout = PdfLayoutType.Paginate

# 创建一个PdfTextWidget实例来保存文本内容
textWidget = PdfTextWidget(text, font, brush)

# 设置文本样式
textWidget.StringFormat = strformat

# 在页面指定位置绘制文字
bounds = RectangleF(PointF(0.0, 20.0), page.Canvas.ClientSize)
textWidget.Draw(page, bounds, textLayout)

# 保存结果文件
pdf.SaveToFile(outputFile, FileFormat.PDF)
pdf.Close()

Python 将 TXT 文本转换为 PDF

申请临时 License

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

Spire.XLS 14.3.3已发布。本次更新新增支持数据透视表分组功能。另外,还支持对CellRange添加FindAll()方法以及为透视表设置“Repeat All Item Labels”。一些已知问题也在该版本中被成功修复,如保存Excel文件后内容不正确的问题。详情请阅读以下内容。

新功能:

问题修复:


获取Spire.XLS 14.3.3请点击:

https://www.e-iceblue.cn/Downloads/Spire-XLS-NET.html

Spire.DocViewer 8.7.8 已发布。该版本新增支持 WPF 和 MAUI 平台。详情请阅读以下内容。

新功能:


获取 Spire.DocViewer 8.7.8 请点击:

https://www.e-iceblue.cn/Downloads/Spire-DocViewer-NET.html