public final class

DocumentJsonFormatter

extends Object
java.lang.Object
   ↳ com.pspdfkit.document.formatters.DocumentJsonFormatter

Class Overview

Exports and imports changes to annotations of a PdfDocument into a reliable, cross-platform usable JSON format. JSON strings created and read by this class are also used by PSPDFKit Instant.

The JSON generated by this class represents a set of all annotation changes to the original document (i.e. the document in the state in which it was loaded). It comprises all added annotations, changes to existing annotations, and also deleted annotations. More info on the JSON format is available in our online guides.

Summary

Public Constructors
DocumentJsonFormatter()
Public Methods
static void exportDocumentJson(PdfDocument document, OutputStream outputStream)
Exports all annotation changes of the given document into a JSON string.
static Completable exportDocumentJsonAsync(PdfDocument document, OutputStream outputStream)
Asynchronously exports all annotation changes of the given document into a JSON string.
static void importDocumentJson(PdfDocument document, DataProvider dataProvider)
Imports a document JSON and applies its changes to the given document.
static Completable importDocumentJsonAsync(PdfDocument document, DataProvider dataProvider)
Asynchronously imports a document JSON and applies its changes to the given document.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public DocumentJsonFormatter ()

Public Methods

public static void exportDocumentJson (PdfDocument document, OutputStream outputStream)

Exports all annotation changes of the given document into a JSON string. The generated JSON is written to outputStream. Serialization is performed on the current thread. When trying to export from the main thread, consider using exportDocumentJsonAsync(PdfDocument, OutputStream) instead to avoid that your application becomes unresponsive.

The method will throw a DocumentJsonFormatterException when encountering an unexpected error while exporting the JSON.

Exporting of compound documents changes is not supported. This method will throw an exception if called with a compound document (i.e. a document having more than one document source).

Parameters
document The PdfDocument to serialize. This must not be a compound document.
outputStream The OutputStream to which the JSON should be written.

public static Completable exportDocumentJsonAsync (PdfDocument document, OutputStream outputStream)

Asynchronously exports all annotation changes of the given document into a JSON string. The generated JSON is written to outputStream. Serialization will start upon subscribing to the Completable returned by this method. The returned completable does not operate on any particular scheduler. If you want to run the serialization process on a background thread, consider calling subscribeOn(Scheduler) on the completable before subscribing.

The returned completable will emit a DocumentJsonFormatterException when encountering an unexpected error while exporting the JSON.

Exporting of compound documents is not supported. This method will throw an exception if called with a compound document (i.e. a document having more than one document source).

Parameters
document The PdfDocument to serialize. This must not be a compound document.
outputStream The OutputStream to which the JSON should be written.
Returns

public static void importDocumentJson (PdfDocument document, DataProvider dataProvider)

Imports a document JSON and applies its changes to the given document. The JSON has to be provided by the given dataProvider. Deserialization is performed on the current thread. When trying to import from the main thread, consider using importDocumentJsonAsync(PdfDocument, DataProvider) instead to avoid that your application becomes unresponsive.

The method will throw a DocumentJsonFormatterException when encountering an unexpected error while importing the JSON.

Importing JSON to compound documents is not supported. This method will throw an exception if called with a compound document (i.e. a document having more than one document source).

Parameters
document The PdfDocument to which the JSON should be imported. This must not be a compound document.
dataProvider The DataProvider which serves the document JSON.

public static Completable importDocumentJsonAsync (PdfDocument document, DataProvider dataProvider)

Asynchronously imports a document JSON and applies its changes to the given document. The JSON has to be provided by the given dataProvider. Deserialization will start upon subscribing to the Completable returned by this method. The returned completable does not operate on any particular scheduler. If you want to run the deserialization process on a background thread, consider calling subscribeOn(Scheduler) on the completable before subscribing.

The returned completable will emit a DocumentJsonFormatterException when encountering an unexpected error while importing the JSON.

Importing JSON to compound documents is not supported. This method will throw an exception if called with a compound document (i.e. a document having more than one document source).

Parameters
document The PdfDocument to which the JSON should be imported. This must not be a compound document.
dataProvider The DataProvider which serves the document JSON.
Returns