Example of Hello World using Python language
A simple use case can help to demonstrate the features of a programming language or software. This is usually done with a “Hello World” example.
Aspose.PDF for Python via C++ is a powerful PDF API that enables the developers to create, manipulate and convert PDF documents in their Python applications. It supports working with various file formats such as PDF, XFA, TXT, HTML, PCL, XML, XPS, EPUB, TEX and image file formats. In this article, we will show you how to create a PDF document with the text “Hello World!” using Aspose.PDF API. You need to install Aspose.PDF for Python via C++ in your environment before running the following code sample.
- Import the
AsposePdfPython
module. - Create a new PDF document object using the
document_create
function. - Get the pages collection of the document using the
document_get_pages
function. - Add a new page to the pages collection using the
page_collection_add_page
function. - Get the paragraphs collection of the page using the
page_get_paragraphs
function. - Creating a new image object using the
image_create
function. - Setting the file name of the image object to “sample.jpg” using the
image_set_file
function. - Adding the image object to the paragraphs collection using the
paragraphs_add_image
function. - Saving the document to a file named “document.pdf” using the
document_save
function. - Closing the document handle using the
close_handle function
.
The following code snippet is a Hello World program that demonstrates how to Aspose.PDF for Python via C++ works.
from AsposePdfPython import *
doc = document_create()
pages = document_get_pages(doc)
page = page_collection_add_page(pages)
paragraphs = page_get_paragraphs(page)
image = image_create()
image_set_file(image,"sample.jpg")
paragraphs_add_image(paragraphs,image)
document_save(doc, "document.pdf")
close_handle(doc)