pdf 文档的版式特性使其适用于输出不可变格式的报告与合同。但若要在此类文档中插入或修改文本,常规方式难以实现。借助python,我们可以高效地向 pdf 添加文本,实现从文档生成到内容管理的自动化流程。
本文将从以下方面介绍python实现pdf中文本的添加:
本文使用的方法需要用到免费的free spire.pdf for python,可通过pip安装:
pip install spire.pdf.free
新建pdf并添加段落文本
在构建系统报告或生成模板文档时,通常需要从空白页插入一段段文本,保持排版一致性。
from spire.pdf import pdfdocument, pdftruetypefont, pdffontstyle, pdfsolidbrush, pdfrgbcolor, pointf, rectanglef, \ pdfstringformat, pdftextalignment, pdfverticalalignment # 创建新的pdf文档并添加页面 pdf = pdfdocument() page = pdf.pages.add() # 待插入的文本内容 text = ("the purpose of this document is to provide an overview of the company's financial highlights for the fiscal year 2024. " "it includes revenue trends, operational costs, and net income summaries. " "the following sections will outline each metric in more detail.") # 设置字体、画刷(颜色)和排版区域 font = pdftruetypefont("arial", 14.0, pdffontstyle.regular, true) brush = pdfsolidbrush(pdfrgbcolor(0, 0, 0)) # 黑色文本 layout_area = rectanglef(50.0, 50.0, page.getclientsize().width - 100.0, page.getclientsize().height) string_format = pdfstringformat(pdftextalignment.left, pdfverticalalignment.top) # 添加文字内容 page.canvas.drawstring(text, font, brush, layout_area, string_format, false) # 保存并关闭 pdf.savetofile("output/new.pdf") pdf.close()
技术细节说明:
pdftruetypefont
可嵌入外部字体文件,适用于需要控制字体兼容性的场景;rectanglef
定义文本区域边界(支持自定义段落框);drawstring()
支持超出区域自动换行;- 使用
pdfstringformat
设定水平方向与垂直方向的对齐方式。
生成的pdf文档:
向现有pdf中插入文本内容
当你已有一份pdf文件,但希望在某一页添加标记或文字说明,例如审批标语、状态说明等,可以使用如下方式:
from spire.pdf import pdfdocument, pdffontstyle, pdfsolidbrush, pdfrgbcolor, pointf, pdffont, pdffontfamily # 加载已有pdf文档 pdf = pdfdocument() pdf.loadfromfile("pdf.pdf") page = pdf.pages[0] # 设置字体、颜色和插入位置 font = pdffont(pdffontfamily.timesroman, 12.0, pdffontstyle.bold) brush = pdfsolidbrush(pdfrgbcolor(0, 128, 0)) # 深绿色 location = pointf(130.0, 90.0) # 插入文本 page.canvas.drawstring("verified by qa department", font, brush, location) # 保存结果 pdf.savetofile("output/modifiedpdf.pdf") pdf.close()
技术细节说明:
pdffontfamily.timesroman
属于 pdf 标准内置字体(无需嵌入);- 坐标
pointf(x, y)
单位为磅(1 pt ≈ 0.3528 mm),以页面左上角为 (0,0); - 无需设置区域框时,适用于短文本、标签插入、动态盖章等场景。
修改的pdf文档:
复杂格式设置:添加透明旋转水印文本
在文档审阅或内部版本发布场景中,常见的做法是添加一个带透明度与旋转角度的水印文字,以提示机密性或防止误传播。spire.pdf 提供了画布状态保存、旋转、透明度控制等接口,可用于实现这种复杂的格式需求。
from spire.pdf import pdfdocument, pdftruetypefont, pdffontstyle, pdfsolidbrush, pdfrgbcolor, pointf from spire.pdf.common import color # 加载已有pdf文档 pdf = pdfdocument() pdf.loadfromfile("input1.pdf") page = pdf.pages[0] # 设置水印文本内容 text = "internal use only" # 设置字体样式与大小 font = pdftruetypefont("arial", 40.0, pdffontstyle.bold, true) # 设置画刷颜色为深红色 brush = pdfsolidbrush(pdfrgbcolor(color.get_darkred())) # 测量文本大小以便居中放置 size = font.measurestring(text) x = (page.canvas.clientsize.width - size.width) / 2 y = (page.canvas.clientsize.height - size.height) / 2 # 保存当前画布状态 state = page.canvas.save() # 设置透明度 page.canvas.settransparency(0.3) # 平移至文本中心位置 page.canvas.translatetransform(x + size.width / 2, y + size.height / 2) # 旋转文本(-45度斜着显示) page.canvas.rotatetransform(-45.0) # 绘制文本水印(左上角为起始点,因此需偏移) page.canvas.drawstring(text, font, brush, pointf(-size.width / 2, -size.height / 2)) # 恢复画布状态 page.canvas.restore(state) # 保存文档 pdf.savetofile("output/with_watermark.pdf") pdf.close()
技术细节说明:
- 使用
settransparency(0.3)
设置文本透明度,增强水印效果但不遮挡内容; translatetransform()
将画布原点移动至文本中心;rotatetransform(-45)
实现对角线旋转,常用于“confidential”或“draft”类水印;- 水印文本使用
drawstring()
绘制,pointf(-size.width / 2, -size.height / 2)
保证以中心为参考点; - 通过
canvas.save()
与canvas.restore()
管理局部画布状态,避免影响页面中其他元素。
修改后的pdf文档:
总结
通过 spire.pdf for python,你可以使用简洁的 python 代码在 pdf 中添加各类文本,功能包括:
功能项 | 方法与说明 |
---|---|
从零创建文档 | pdfdocument.pages.add() + drawstring() |
插入文本到现有文档 | loadfromfile() + pointf 定位文本插入点 |
控制排版样式 | 使用 rectanglef + pdfstringformat 控制对齐与区域 |
行文本支持 | 文本中添加 \n,自动换行渲染 |
字体与颜色控制 | 支持 truetype 与 标准字体,自定义颜色 rgb |
这些操作覆盖了 pdf 文本添加的主要应用场景,从文档批量生成到人工标注支持,适合用于自动归档、审批流程、内容输出等业务场景。
到此这篇关于一文详解如何使用python向pdf精确添加文本的文章就介绍到这了,更多相关python pdf添加文本内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论