Rotate Text Inside PDF using Python
Contents
[
Hide
]
Rotate Text Inside PDF using Rotation Property
By using the Rotation property of TextFragment Class, you can rotate text at various angles. The text rotation can be used in different scenarios of document generation. You can specify the rotation angle in degrees to rotate the text as per your requirement. Please check the following different scenarios, in which you can implement text rotation.
Implement Rotation using TextFragment and TextBuilder
- Create a New PDF Document.
- Add a Page.
- Create and Position Text Fragments.
- Set Text Properties.
- Build and Append Text.
- Save the Document.
import aspose.pdf as ap
# Create PDF document
with ap.Document() as document:
# Get particular page
page = document.pages.add()
# Create text fragment
text_fragment_1 = ap.text.TextFragment("main text")
text_fragment_1.position = ap.text.Position(100, 600)
# Set text properties
text_fragment_1.text_state.font_size = 12
text_fragment_1.text_state.font = ap.text.FontRepository.find_font("TimesNewRoman")
# Create rotated text fragment
text_fragment_2 = ap.text.TextFragment("rotated text")
text_fragment_2.position = ap.text.Position(200, 600)
# Set text properties
text_fragment_2.text_state.font_size = 12
text_fragment_2.text_state.font = ap.text.FontRepository.find_font("TimesNewRoman")
text_fragment_2.text_state.rotation = 45
# Create rotated text fragment
text_fragment_3 = ap.text.TextFragment("rotated text")
text_fragment_3.position = ap.text.Position(300, 600)
# Set text properties
text_fragment_3.text_state.font_size = 12
text_fragment_3.text_state.font = ap.text.FontRepository.find_font("TimesNewRoman")
text_fragment_3.text_state.rotation = 90
# create TextBuilder object
builder = ap.text.TextBuilder(page)
# Append the text fragment to the PDF page
builder.append_text(text_fragment_1)
builder.append_text(text_fragment_2)
builder.append_text(text_fragment_3)
# Save the document
document.save(path_outfile)
Implement Rotation using TextParagraph and TextBuilder (Rotated Fragments)
- Create a New PDF Document.
- A new page is appended to the document using document.pages.add().
- A TextParagraph object is created and positioned at (200, 600).
- Create Text Fragments.
- Append Text Fragments to Paragraph.
- Build and Add Paragraph to the PDF.
- Save the Document.
import aspose.pdf as ap
# Create PDF document
with ap.Document() as document:
# Get particular page
page = document.pages.add()
paragraph = ap.text.TextParagraph()
paragraph.position = ap.text.Position(200, 600)
# Create text fragment
text_fragment_1 = ap.text.TextFragment("rotated text")
# Set text properties
text_fragment_1.text_state.font_size = 12
text_fragment_1.text_state.font = ap.text.FontRepository.find_font("TimesNewRoman")
# Set rotation
text_fragment_1.text_state.rotation = 45
# Create text fragment
text_fragment_2 = ap.text.TextFragment("main text")
# Set text properties
text_fragment_2.text_state.font_size = 12
text_fragment_2.text_state.font = ap.text.FontRepository.find_font("TimesNewRoman")
# Create text fragment
text_fragment_3 = ap.text.TextFragment("another rotated text")
# Set text properties
text_fragment_3.text_state.font_size = 12
text_fragment_3.text_state.font = ap.text.FontRepository.find_font("TimesNewRoman")
# Set rotation
text_fragment_3.text_state.rotation = -45
# Append the text fragments to the paragraph
paragraph.append_line(text_fragment_1)
paragraph.append_line(text_fragment_2)
paragraph.append_line(text_fragment_3)
# Create TextBuilder object
text_builder = ap.text.TextBuilder(page)
# Append the text paragraph to the PDF page
text_builder.append_paragraph(paragraph)
# Save the document
document.save(path_outfile)
Implement Rotation using TextFragment and ‘page.paragraphs’
- Create a New PDF Document.
- A new page is appended to the document using document.pages.add().
- A TextParagraph object is created and positioned at (200, 600).
- Create Text Fragments.
- Append Text Fragments to Paragraph.
- Save the Document.
import aspose.pdf as ap
# Create PDF document
with ap.Document() as document:
# Get particular page
page = document.pages.add()
# Create text fragment
text_fragment_1 = ap.text.TextFragment("main text")
# Set text properties
text_fragment_1.text_state.font_size = 12
text_fragment_1.text_state.font = ap.text.FontRepository.find_font("TimesNewRoman")
# Create text fragment
text_fragment_2 = ap.text.TextFragment("rotated text")
# Set text properties
text_fragment_2.text_state.font_size = 12
text_fragment_2.text_state.font = ap.text.FontRepository.find_font("TimesNewRoman")
# Set rotation
text_fragment_2.text_state.rotation = 315
# Create text fragment
text_fragment_3 = ap.text.TextFragment("rotated text")
# Set text properties
text_fragment_3.text_state.font_size = 12
text_fragment_3.text_state.font = ap.text.FontRepository.find_font("TimesNewRoman")
# Set rotation
text_fragment_3.text_state.rotation = 270
page.paragraphs.add(text_fragment_1)
page.paragraphs.add(text_fragment_2)
page.paragraphs.add(text_fragment_3)
# Save the document
document.save(path_outfile)
Implement Rotation using TextParagraph and TextBuilder (Whole Paragraph Rotated)
- Create a new PDF document.
- A new page is appended to the document using document.pages.add().
- Loop to create multiple paragraphs.
- Define text fragments.
- Set text styles.
- Append text fragments to paragraph.
- Build and append paragraphs to PDF.
- Save the document.
import aspose.pdf as ap
# Create PDF document
with ap.Document() as document:
# Get particular page
page = document.pages.add()
for i in range(4):
paragraph = ap.text.TextParagraph()
paragraph.position = ap.text.Position(200, 600)
# Specify rotation
paragraph.rotation = i * 90 + 45
# Create text fragment
text_fragment_1 = ap.text.TextFragment("Paragraph Text")
# Create text fragment
text_fragment_1.text_state.font_size = 12
text_fragment_1.text_state.font = ap.text.FontRepository.find_font("TimesNewRoman")
text_fragment_1.text_state.background_color = ap.Color.light_gray
text_fragment_1.text_state.foreground_color = ap.Color.blue
# Create text fragment
text_fragment_2 = ap.text.TextFragment("Second line of text")
# Set text properties
text_fragment_2.text_state.font_size = 12
text_fragment_2.text_state.font = ap.text.FontRepository.find_font("TimesNewRoman")
text_fragment_2.text_state.background_color = ap.Color.light_gray
text_fragment_2.text_state.foreground_color = ap.Color.blue
# Create text fragment
text_fragment_3 = ap.text.TextFragment("And some more text...")
# Set text properties
text_fragment_3.text_state.font_size = 12
text_fragment_3.text_state.font = ap.text.FontRepository.find_font("TimesNewRoman")
text_fragment_3.text_state.background_color = ap.Color.light_gray
text_fragment_3.text_state.foreground_color = ap.Color.blue
text_fragment_3.text_state.underline = True
paragraph.append_line(text_fragment_1)
paragraph.append_line(text_fragment_2)
paragraph.append_line(text_fragment_3)
# Create TextBuilder object
builder = ap.text.TextBuilder(page)
# Append the text fragment to the PDF page
builder.append_paragraph(paragraph)
# Save the document
document.save(path_outfile)