Create PDF document programmatically
Contents
[
Hide
]
Creating PDF files in code is a common requirement for reports, invoices, and generated business documents. Aspose.PDF for Java provides a direct way to build a document from scratch.
How to create a PDF file in Java
To create a PDF document programmatically:
- Create a Document object.
- Add a Page to the document.
- Add a TextFragment to the page paragraphs.
- Save the Document to an output file.
Create a simple PDF document
The following Java example is based on CreatePdfDocumentExamples.java.
public static void createNewDocument(Path outputFile) {
try (Document document = new Document()) {
Page page = document.getPages().add();
page.getParagraphs().add(new TextFragment("Hello World!"));
document.save(outputFile.toString());
}
}