Load PDF Documents
- 3 minutes to read
Use PdfDocument constructors to load an existing PDF document from an InputStream or ReadableByteChannel. You can also specify a password, configure document loading behavior, and synchronize document metadata during loading.
Note
Examples in this help topic use a FileChannel, which implements ReadableByteChannel. You can use any ReadableByteChannel implementation to load a PDF document.
Load a PDF Document
Use one of the following constructors to load a PDF document with default settings:
| Source | Constructor |
|---|---|
InputStream |
PdfDocument(InputStream stream) |
ReadableByteChannel |
PdfDocument(ReadableByteChannel channel) |
The following code snippet loads a PDF document from a file:
import com.devexpress.docs.pdf.*;
import java.nio.channels.*;
import java.nio.file.*;
try (FileChannel channel = FileChannel.open(Path.of("document.pdf"));
PdfDocument pdfDocument = new PdfDocument(channel)) {
// Process the document.
}
Load a Password-Protected Document
Specify the password to load a password-protected PDF document. If the password is incorrect or missing, the API throws a PdfIncorrectPasswordException.
Use one of the following constructors:
| Source | Constructor |
|---|---|
InputStream |
PdfDocument(InputStream stream, String password) |
ReadableByteChannel |
PdfDocument(ReadableByteChannel channel, String password) |
The following code snippet loads a password-protected PDF document:
import com.devexpress.docs.pdf.*;
import java.nio.channels.*;
import java.nio.file.*;
try (FileChannel channel = FileChannel.open(Path.of("protected.pdf"));
PdfDocument pdfDocument = new PdfDocument(channel, "password")) {
// Process the document.
}
Refer to the following help topic for additional information: Protect PDF Documents with Passwords.
Specify Load Options
Create a LoadOptions object to customize how the document is loaded.
Use one of the following constructors:
| Source | Constructor |
|---|---|
InputStream |
PdfDocument(InputStream stream, LoadOptions loadOptions) |
ReadableByteChannel |
PdfDocument(ReadableByteChannel channel, LoadOptions loadOptions) |
The LoadOptions class includes the following settings:
| Method | Description |
|---|---|
| setPassword() | Sets the password used to open a password-protected document. |
| setDetachStreamAfterLoadComplete() | Sets whether the source stream is detached after the load operation completes. |
| setSyncMetadata() | Sets whether to synchronize basic document information with XMP metadata when the document is loaded. |
| setMetadataSyncMode() | Sets the metadata synchronization mode when metadata synchronization is enabled.. |
The following code snippet loads a document with custom load options:
import com.devexpress.docs.pdf.*;
import java.nio.channels.*;
import java.nio.file.*;
try (FileChannel channel = FileChannel.open(Path.of("document.pdf"))) {
LoadOptions options = new LoadOptions();
options.setPassword("password");
options.setDetachStreamAfterLoadComplete(true);
options.setSyncMetadata(true);
options.setMetadataSyncMode(MetadataSyncMode.AUTO);
try (PdfDocument pdfDocument = new PdfDocument(channel, options)) {
// Process the document.
}
}
Detach the Source Stream After Loading
The PDF Document API reads data from the input stream or channel and expects the source to remain open and unchanged while the document is in use.
Call the setDetachStreamAfterLoadComplete(true) method, or use the corresponding constructor overload with the detachStreamAfterLoadComplete parameter set to true, to force the PDF Document API to read the entire document during loading and detach it from the source. After the document is loaded, you can safely close or reuse the input stream or channel.
Synchronize Document Metadata
If a PDF document contains both document properties and XMP metadata, you can synchronize these metadata formats when you load the document.
To enable synchronization, do the following:
- Call setSyncMetadata(true).
- Use setMetadataSyncMode() to specify the synchronization direction.
The following synchronization modes are available:
| Mode | Description |
|---|---|
| AUTO | Synchronizes metadata in a direction that depends on the document content. If the document contains XMP metadata, copies XMP metadata to basic document information (as XMP_TO_INFO does). Otherwise, copies basic document information to XMP metadata (as INFO_TO_XMP does). |
| XMP_TO_INFO | Copies XMP metadata to the corresponding basic document information properties. The API clears basic information properties that have no XMP value. |
| INFO_TO_XMP | Copies basic document information to the corresponding XMP metadata properties (the API creates XMP metadata if the document has none). The API clears XMP properties that have no basic information value. |