Example of Hello World using Rust language
Contents
[
Hide
]
A “Hello World” example is traditionally used to introduce features of a programming language or software with a simple use case.
Aspose.PDF for Rust is a feature-rich PDF API that allows developers to embed PDF document creation, manipulation, and conversion capabilities with Rust. It supports many popular file formats, including PDF, TXT, XLSX, EPUB, TEX, DOC, DOCX, PPTX, image formats etc. In this article, we are creating a PDF document containing the text “Hello World!” After installing Aspose.PDF for Rust in your environment, you can execute the code sample to see how the Aspose.PDF API works. Below code snippet follows these steps:
- Create a new PDF document instance.
- Add a new page to the PDF document using page_add function.
- Set the page size using page_set_size.
- Add “Hello World!” text to the first page using page_add_text.
- Save the PDF document using save_as method.
Following code snippet is a Hello World program to exhibit working of Aspose.PDF for Rust API.
use asposepdf::{Document, PageSize};
use std::error::Error;
fn main() -> Result<(), Box<dyn Error>> {
// Create a new PDF-document
let pdf = Document::new()?;
// Add a new page
pdf.page_add()?;
// Set the size of the first page to A4
pdf.page_set_size(1, PageSize::A4)?;
// Add "Hello World!" text to the first page
pdf.page_add_text(1, "Hello World!")?;
// Save the PDF-document as "hello.pdf"
pdf.save_as("hello.pdf")?;
println!("Saved PDF-document: hello.pdf");
Ok(())
}