pdf 文件中的条形码可以加快数据检索和处理速度。您可以在 pdf 文件中添加包含详细信息的条形码,如文档的唯一标识符、版本号、创建者,甚至整个文档内容。扫描后,所有信息都会立即解码。这种即时访问对于处理大量文件的企业来说非常有用,因为它最大限度地减少了人工搜索和数据录入所需的时间和精力。在本文中,您将学习如何使用 spire.pdf for python 和 spire.barcode for python 在 python 中为 pdf 添加条形码和二维码。
安装 python 库
本教程需要用到 spire.pdf for python 和 spire.barcode for python。可以通过以下 pip 命令将它们轻松安装到 windows 中。
pip install spire.pdf
pip install spire.barcode
如果您不清楚如何安装,请参考: 如何在 windows 中安装 spire.pdf for python
python 在 pdf 中添加条形码
spire.pdf for python 提供了多种类来代表不同的一维条码类型,如 pdfcodabarbarcode、pdfcode11barcode、pdfcode32barcode、pdfcode39barcode、pdfcode93barcode。
每一类都提供了相应的属性,用于设置条形码文本、大小、颜色等。以下是在 pdf 页面指定位置绘制常见的 codabar、code39 和 code93 条码的步骤。
- 创建 pdfdocument 对象。
- 使用 pdfdocument.pages.add() 方法在 pdf 中添加一页。
- 创建 pdftextwidget 对象,然后使用 pdftextwidget.draw() 方法在页面上绘制文本。
- 创建 pdfcodabarbarcode、pdfcode39barcode、pdfcode93barcode 对象。
- 通过相应类的 barcodetotextgapheight 属性设置条形码与显示文本之间的间隙。
- 通过相应类的 textdisplaylocation 属性设置条形码文本的显示位置。
- 通过相应类的 textcolor 属性设置条形码文本的颜色。
- 使用相应类的 draw(page: pdfpagebase, location: pointf) 方法在 pdf 页面的指定位置绘制条形码。
- 使用 pdfdocument.savetofile() 方法保存生成的 pdf 文件。
- python
from spire.pdf.common import *
from spire.pdf import *
# 创建pdf文档
pdf = pdfdocument()
# 添加一页
page = pdf.pages.add(pdfpagesize.a4())
# 初始化y轴
y = 20.0
# 创建字体
font = pdftruetypefont("arial", 12.0, pdffontstyle.bold, true)
# 在页面上绘制文本
text = pdftextwidget()
text.font = font
text.text = "codabar:"
result = text.draw(page, 0.0, y)
page = result.page
y = result.bounds.bottom 2
# 在页面上绘制codabar条码
codabar = pdfcodabarbarcode("00:12-3456/7890")
codabar.barcodetotextgapheight = 1.0
codabar.enablecheckdigit = true
codabar.showcheckdigit = true
codabar.textdisplaylocation = textlocation.bottom
codabar.textcolor = pdfrgbcolor(color.get_blue())
codabar.draw(page, pointf(0.0, y))
y = codabar.bounds.bottom 6
# 在页面上绘制文本
text.text = "code39:"
result = text.draw(page, 0.0, y)
page = result.page
y = result.bounds.bottom 2
# 在页面上绘制code39条码
code39 = pdfcode39barcode("16-273849")
code39.barcodetotextgapheight = 1.0
code39.textdisplaylocation = textlocation.bottom
code39.textcolor = pdfrgbcolor(color.get_blue())
code39.draw(page, pointf(0.0, y))
y = code39.bounds.bottom 6
# 在页面上绘制文本
text.text = "code93:"
result = text.draw(page, 0.0, y)
page = result.page
y = result.bounds.bottom 2
# 在页面上绘制code93条码
code93 = pdfcode93barcode("16-273849")
code93.barcodetotextgapheight = 1.0
code93.textdisplaylocation = textlocation.bottom
code93.textcolor = pdfrgbcolor(color.get_blue())
code93.quietzone.bottom = 5.0
code93.draw(page, pointf(0.0, y))
# 保存文档
pdf.savetofile("添加条形码.pdf")
pdf.close()
python 在 pdf 中添加二维码
要在 pdf 文件中添加二维条码,首先需要使用 spire.barcode for python 库生成二维码,然后再通过 spire.pdf for python 库将二维码图片添加到 pdf 文件中。具体步骤如下:
- 创建 pdfdocument 对象。
- 使用 pdfdocument.pages.add() 方法添加 pdf 页面。
- 创建 barcodesettings 对象。
- 调用 barcodesettings 类的相应属性来设置条形码类型、数据、纠错级别和宽度等。
- 根据设置创建 barcodegenerator 对象。
- 使用 barcodegenerator.generateimage() 方法生成二维码图像。
- 使用 pdfpagebase.canvas.drawimage() 方法在 pdf 页面的指定位置绘制二维码图像。
- 使用 pdfdocument.savetofile() 方法保存生成的 pdf 文件。
- python
from spire.pdf.common import *
from spire.pdf import *
from spire.barcode import *
# 创建pdfdocument对象
pdf = pdfdocument()
# 添加一页
page = pdf.pages.add()
# 创建barcodesettings对象
settings = barcodesettings()
# 将条码类型设置为qrcode
settings.type = barcodetype.qrcode
# 设置二维码数据
settings.data = "e-iceblue"
settings.data2d = "e-iceblue"
# 设置二维码宽度
settings.x = 2
# 设置二维码纠错级别
settings.qrcodeecl = qrcodeecl.m
# 设置底部显示文本
settings.showtextonbottom = true
# 基于以上设置生成二维码图片
barcodegenerator = barcodegenerator(settings)
qrimage = barcodegenerator.generateimage()
# 将二维码图片保存为png格式
with open("qrcode.png", "wb") as file:
file.write(qrimage)
# 初始化y轴
y = 20.0
# 创建字体
font = pdftruetypefont("arial", 12.0, pdffontstyle.bold, true)
# 在页面上绘制文本
text = pdftextwidget()
text.font = font
text.text = "qrcode:"
result = text.draw(page, 0.0, y)
page = result.page
y = result.bounds.bottom 2
# 将二维码图片绘制到页面指定位置
pdfimage = pdfimage.fromfile("qrcode.png")
page.canvas.drawimage(pdfimage, 0.0, y)
# 保存文档
pdf.savetofile("添加二维码.pdf")
pdf.close()
申请临时 license
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用javascript。获取有效期 30 天的临时许可证。