Example of Hello World using Python

Contents
[ ]

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:

  1. Instantiate a Document object
  2. Add a Page to document object
  3. Create a TextFragment object
  4. Set Text Colors
  5. Create a Text Builder
  6. Add TextFragment to the Page
  7. document.save() resultant PDF document

The following code snippet is a “Hello World” program that demonstrates the functionality of Aspose.PDF for Python via the .NET API.


from datetime import timedelta
import aspose.pdf as ap

def run_simple(self):
    # Initialize document object
    document = ap.Document()
    # Add page
    page = document.pages.add()
    # Add text to new page
    textFragment = ap.text.TextFragment("Hello, world!")
    textFragment.position = ap.text.Position(100, 600)

    textFragment.text_state.font_size = 12
    textFragment.text_state.font = ap.text.FontRepository.find_font(
        "TimesNewRoman"
    )
    textFragment.text_state.background_color = ap.Color.blue
    textFragment.text_state.foreground_color = ap.Color.yellow

    # Create TextBuilder object
    textBuilder = ap.text.TextBuilder(page)

    # Append the text fragment to the PDF page
    textBuilder.append_text(textFragment)

    document.save(self.data_dir + "HelloWorld_out.pdf")