PNG
JPG
BMP
TIFF
PDF
How to add Text to PDF via Cloud Python SDK
以下代码片段由 Aspose.PDF 提供,加载一个 PDF 和自定义字体文件,定义带有自定义样式(文本颜色、背景颜色、字体、旋转、对齐和间距)的项目,并指定段落应放置在页面上的矩形区域。这对于在 PDF 页面某个区域添加样式化文本非常有用。 为了将文本添加到 PDF 中,我们将使用 Aspose.PDF Cloud Python SDK。这个云 SDK 帮助 Python 程序员使用 Python 编程语言通过 Aspose.PDF REST API 开发基于云的 PDF 创建、注释、编辑和转换应用程序。只需在 Aspose for Cloud 创建一个帐户并获取您的应用程序信息。一旦您拥有了 App SID 和密钥,您就可以准备使用 Aspose.PDF Cloud Python SDK。如果 Python 包托管在 Github 上,您可以直接从 Github 安装:
Installation from Github
pip install git+https://github.com/aspose-pdf-cloud/aspose-pdf-cloud-python.git
Package Manager Console Command
pip install asposepdfcloud
Steps to add Text into PDF via Python SDK
Aspose.PDF Cloud 开发人员可以轻松地在几行代码中加载并添加文本到 PDF。
- 安装 Python SDK。
- 前往 Aspose Cloud Dashboard。
- 创建一个新的 Account 以访问所有应用程序和服务,或登录到您的帐户。
- 单击左侧菜单中的 Applications 以获取 Client Id 和 Client Secret。
- 查看 开发者指南 以通过 Python 添加文本到 PDF。
- 查看我们的 GitHub 仓库 以获取完整的 API 列表及工作示例。
- 查看 API 参考页面 以了解 API 参数的描述。
Add Text to PDF using Python
file_name = 'pages.pdf'
self.uploadFile(file_name)
# Upload font file
font_file = 'Righteous-Regular.ttf'
self.uploadFile(font_file)
# Specify the page number and define the text area (rectangle)
page_number = 1
rectangle = asposepdfcloud.models.Rectangle(100, 100, 300, 300)
# Define colors for text foreground and background
foreground_color = asposepdfcloud.models.Color(a=0x00, r=0x00, g=0xFF, b=0x00) # Blue foreground
background_color = asposepdfcloud.models.Color(a=0x00, r=0xFF, g=0x00, b=0x00) # Green background
# Define text properties using TextState
text_state = asposepdfcloud.models.TextState(
font_size=10,
font='Righteous',
foreground_color=foreground_color,
background_color=background_color,
font_style=asposepdfcloud.models.FontStyles.REGULAR,
font_file=self.temp_folder + '/' + font_file
)
# Create a text segment with specified text and text state
segment = asposepdfcloud.models.Segment(value='segment 1', text_state=text_state)
# Add the segment to a text line with right horizontal alignment
text_line = asposepdfcloud.models.TextLine(
horizontal_alignment=asposepdfcloud.models.TextHorizontalAlignment.RIGHT,
segments=[segment]
)
# Create a paragraph with the text line and set additional properties
paragraph = asposepdfcloud.models.Paragraph(lines=[text_line])
paragraph.rectangle = rectangle
paragraph.left_margin = 10
paragraph.right_margin = 10
paragraph.top_margin = 20
paragraph.bottom_margin = 20
paragraph.horizontal_alignment = asposepdfcloud.models.TextHorizontalAlignment.FULLJUSTIFY
paragraph.line_spacing = asposepdfcloud.models.LineSpacing.FONTSIZE
paragraph.rotation = 10
paragraph.subsequent_lines_indent = 20
paragraph.vertical_alignment = asposepdfcloud.models.VerticalAlignment.CENTER
paragraph.wrap_mode = asposepdfcloud.models.WrapMode.BYWORDS
# Set the options for adding the paragraph
opts = {
"paragraph": paragraph,
"folder": self.temp_folder
}
Work with Text in PDF
文本可以动态添加以自定义 PDF 以适应特定用户,例如添加他们的姓名或个性化信息。这通常用于营销材料或个性化报告。以特定字体、颜色和对齐方式添加文本可以使某些部分更易读和更具视觉吸引力,从而创造更好的用户体验。文本添加可以帮助在不同的 PDF 版本中保持一致的信息,避免手动编辑并确保关键信息保留在适当位置。 使用 Aspose.PDF Cloud Python SDK 将文本添加到 PDF 文档中。
使用我们的 Python 库,您可以:
- 合并 PDF 文档。
- 拆分 PDF 文件。
- 将 PDF 转换为其他格式,反之亦然。
- 操作注释。
- 处理 PDF 中的图像,等等。
- 您可以试用我们的 免费应用程序 在线添加文本到 PDF 文件并测试功能。