Open PDF document programmatically
Contents
[
Hide
]
Aspose.PDF for Java supports several ways to load an existing PDF document depending on where the source data comes from.
Open a PDF document in Java
You can open a PDF document:
- Open a Document directly from a file path.
- Open a Document from an
InputStream. - Open an encrypted Document by supplying the password.
Open document from file
public static void openDocumentFromFile(Path inputFile) {
Document document = new Document(inputFile.toString());
System.out.println("Pages: " + document.getPages().size());
document.close();
}
Open document from stream
public static void openDocumentFromStream(Path inputFile) throws Exception {
try (InputStream stream = Files.newInputStream(inputFile)) {
Document document = new Document(stream);
System.out.println("Pages: " + document.getPages().size());
document.close();
}
}
Open an encrypted document
public static void openDocumentEncrypted(Path inputFile) {
Document document = new Document(inputFile.toString(), "P@ssw0rd");
System.out.println("Pages: " + document.getPages().size());
document.close();
}