Rotacionar Texto PDF em Python
Gire fragmentos de texto em um documento PDF usando Aspose.PDF for Python via .NET. Esta página mostra como controlar a posição e a rotação do texto usando TextFragment, TextState, e TextBuilder. Ao ajustar ângulos de rotação, você pode criar layouts como cabeçalhos diagonais, rótulos verticais e anotações rotacionadas.
Girar Fragmentos de Texto Usando TextBuilder em PDF
Cria um arquivo PDF chamado rotated_fragments.pdf contendo três fragmentos de texto alinhados horizontalmente:
- o primeiro texto não está rotacionado
- o segundo está rotacionado 45°
- o terceiro está rotacionado 90°
- Crie um novo documento PDF.
- Insira uma nova página para hospedar o texto rotacionado.
- Crie o primeiro fragmento de texto (sem rotação).
- Crie o segundo fragmento de texto (rotação de 45°).
- Crie o terceiro fragmento de texto (rotação de 90°).
- Adicionar fragmentos de texto usando
TextBuilder. - Salvar o documento.
import aspose.pdf as ap
def rotate_text_inside_pdf_1(outfile):
# 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(outfile)
Rotacionar fragmentos de texto individuais dentro de um parágrafo no PDF
Gire fragmentos de texto individuais dentro de um parágrafo. Ele demonstra como criar um parágrafo de várias linhas (TextParagraph) contendo múltiplos fragmentos (TextFragment), cada um com seu próprio ângulo de rotação. Esta técnica é útil para criar documentos visualmente ricos que combinam texto orientado horizontalmente e diagonalmente — por exemplo, cabeçalhos estilizados, diagramas ou rótulos anotados.
Cria um PDF chamado rotated_paragraph_fragments.pdf contendo um parágrafo com três linhas de texto, cada linha rotacionada de forma diferente:
- a primeira linha está rotacionada 45°
- a segunda linha permanece horizontal (0°)
- a terceira linha está rotacionada -45°
- Crie um novo documento PDF.
- Adicione uma página em branco onde o texto girado aparecerá.
- Criar um
TextParagraph. - Crie e configure o primeiro fragmento de texto (rotação de +45°).
- Crie o segundo fragmento de texto (sem rotação).
- Crie o terceiro fragmento de texto (-45° rotação).
- Anexe fragmentos de texto ao parágrafo.
- Adicione o parágrafo à página usando
TextBuilder. - Salvar o documento.
import aspose.pdf as ap
def rotate_text_inside_pdf_2(outfile):
# 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(outfile)
Rotacionar Texto Usando Parágrafos de Página no PDF
Esta seção demonstra um método simplificado para girar texto dentro de um PDF usando Aspose.PDF for Python via .NET.
Ao contrário de abordagens de nível inferior com TextBuilder ou TextParagraph, este método adiciona fragmentos de texto rotacionados diretamente à coleção de parágrafos da página (page.paragraphs). É ideal quando você precisa de rotação de texto básica, mas não requer posicionamento preciso ou estruturação de parágrafos.
Gera um arquivo chamado simple_rotated_text.pdf contendo:
- um fragmento de texto horizontal principal com rotação de 0°
- fragmento rotacionado 315°
- fragmento girado 270°
- Inicialize um novo PDF Document.
- Crie uma página onde o texto girado será colocado.
- Crie o primeiro fragmento de texto (sem rotação).
- Crie o segundo fragmento de texto (rotação de 315°).
- Crie o terceiro fragmento de texto (rotação de 270°).
- Adicione fragmentos de texto diretamente aos parágrafos da página.
- Salve o documento PDF.
import aspose.pdf as ap
def rotate_text_inside_pdf_3(outfile):
# 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(outfile)
Rotacionar parágrafos inteiros em um PDF
Este exemplo demonstra rotação avançada de texto em nível de parágrafo em um PDF. Ao contrário da rotação em nível de fragmento (onde cada trecho de texto é rotacionado individualmente), este método rotaciona parágrafos inteiros como blocos unificados em ângulos diferentes. Cada parágrafo contém múltiplos fragmentos de texto estilizados, e o parágrafo completo é girado em ângulos específicos — permitindo transformações de layout complexas e consistentes. Isso é ideal para layouts artísticos, marcas d’água ou PDFs com muito design, onde seções inteiras de texto precisam ser orientadas em várias direções.
Cria rotated_paragraphs.pdf, contendo quatro parágrafos totalmente estilizados e girados:
- cada um girado em um ângulo único (45°, 135°, 225° e 315°)
- cada parágrafo tem três linhas de texto com fundos coloridos, sublinhado e estilo consistente
- Crie um novo documento PDF.
- Adicione uma página em branco para conter os parágrafos girados.
- Iterar para criar vários parágrafos.
- Crie e posicione o parágrafo.
- Crie fragmentos de texto com formatação.
- Aplicar formatação de texto.
- Adicionar fragmentos de texto ao parágrafo.
- Anexe o parágrafo à página usando
TextBuilder. - Repita para todas as quatro rotações.
- Salve o documento PDF.
import aspose.pdf as ap
def rotate_text_inside_pdf_4(outfile):
# 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(outfile)