Skip to main content

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. The API throws an exception if the specified password in incorrect.

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:

  1. Call setSyncMetadata(true).
  2. Use setMetadataSyncMode() to specify the synchronization direction.

The following synchronization modes are available:

Mode Description
AUTO Synchronizes metadata automatically. If a property exists in both formats, the XMP metadata value takes precedence.
INFO_TO_XMP Copies document properties to the corresponding XMP metadata properties. If a property exists in both formats, the document property value takes precedence.
XMP_TO_INFO Copies XMP metadata to the corresponding document properties. If a property exists in both formats, the XMP metadata value takes precedence.
See Also