How to add Text to PDF via Cloud Python SDK
The following code snippet, by the Aspose.PDF, loads a PDF and custom font file, defines the item with the custom style (text color, background color, font, rotation, alignment, and spacing), and specifies the rectangle in which the paragraph should be placed on the page. This is useful for adding stylized text to a certain area of the PDF page. To add text into PDF, we’ll use Aspose.PDF Cloud Python SDK. This Cloud SDK assists Python programmers in developing cloud-based PDF creator, annotator, editor, and converter apps using Python programming language via Aspose.PDF REST API. Simply create an account at Aspose for Cloud and get your application information. Once you have the App SID & key, you are ready to give the Aspose.PDF Cloud Python SDK. If the python package is hosted on Github, you can install directly from 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 developers can easily load & add Text to PDF in just a few lines of code.
- Install Python SDK.
- Go to the Aspose Cloud Dashboard.
- Create a new Account to access all applications and services or Sign In to your account.
- Click on Applications in the left menu to get Client Id and Client Secret.
- Check out the Developer Guide to add Text to PDF via Python.
- Check out our GitHub repository for a complete API list along with working examples.
- Check out the API Reference page for the description of APIs parameters.
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
Text can be dynamically added to customize a PDF for a specific user, such as adding their name or personalized message. This is often used in marketing materials or for personalized reports. Adding text in specific fonts, colors, and alignments can make certain sections more readable and visually appealing, creating a better user experience. Text additions can help maintain consistent information across different PDF versions, avoiding manual edits and ensuring that key information remains in place. Add the Text into PDF documents with Aspose.PDF Cloud Python SDK.
With our Python library you can:
- Combine PDF documents.
- Split PDF Files.
- Convert PDF to other formats, and vice versa.
- Manipulate Annotations.
- Work with Images in PDF, etc.
- You can try out our free App to add the text into PDF files online and test the functionality.