Example of Hello World using Python
A “Hello World” example shows the simplest syntax and the simplest program in any given programming language. Developers are introduced to the basic programming language syntax by learning how to print “Hello World” on the device screen. Therefore, we will traditionally start our acquaintance with our library from it.
In this article, we are creating a PDF document containing text “Hello World!”. After installing Aspose.PDF for Python via .NET in your environment, you can execute below code sample to see how Aspose.PDF API works.
Below code snippet follows these steps:
- Instantiate a Document object
- Add a Page to document object
- Create a TextFragment object
- Set Text Colors
- Create a Text Builder
- Add TextFragment to the Page
- save() resultant PDF document
Following code snippet is a “Hello World” program to exhibit working of Aspose.PDF for Python via .NET API.
from datetime import timedelta
import aspose.pdf as apdf
# Initialize document object
document = apdf.Document()
# Add page
page = document.pages.add()
# Add text to new page
textFragment = apdf.text.TextFragment("Hello, world!")
textFragment.position = apdf.text.Position(100, 600)
textFragment.TextState.FontSize = 12
textFragment.TextState.Font = apdf.apdf.text.FontRepository.find_font(
"TimesNewRoman"
)
textFragment.TextState.background_color = apdf.Color.blue
textFragment.TextState.foreground_color = apdf.Color.yellow
# Create TextBuilder object
textBuilder = apdf.text.TextBuilder(page)
# Append the text fragment to the PDF page
textBuilder.append_text(textFragment)
document.save("HelloWorld_out.pdf")