Open PDF document programmatically

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:

  1. Open a Document directly from a file path.
  2. Open a Document from an InputStream.
  3. 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();
}