Save PDF annotations on Android
By default, Nutrient will save annotations asynchronously and automatically when the onStop()
lifecycle event is called.
Nutrient can write annotations into a PDF under the following conditions:
-
The PDF must be in a writeable location. This means that it either has to be in a writeable directory on the file system, or the
ContentProvider
from which the file is loaded must allow saving. For maximum performance, ask forWRITE_EXTERNAL_STORAGE
permission when opening files from external storage. This will allow Nutrient to bypass the slowContentProvider
APIs. -
If the PDF was opened from a custom
DataProvider
, it has to implementWriteableDataProvider
and properly handle writing to a file. -
The PDF must be valid according to the Adobe PDF specification. Some PDFs are broken but still work somewhat, so Nutrient can render the content. If Nutrient detects a mismatch in the object tree or is unable to find objects, annotation saving will be stopped, since there would be a risk of damaging the document.
Annotation saving
By default, Nutrient auto saves changes to a document and to annotations inside PdfFragment#onStop
— effectively, this means every time the fragment is sent to the background, e.g. when switching to another application or when leaving the viewer activity. You can disable auto saving via the #autosaveEnabled
setter on the PdfConfiguration.Builder
:
// By default, auto save is enabled. val config = PdfConfiguration.Builder() .autosaveEnabled(false) .build() val fragment = PdfFragment.newInstance(documentUri, config) ...
// By default, auto save is enabled. final PdfConfiguration config = new PdfConfiguration.Builder() .autosaveEnabled(false) .build(); final PdfFragment fragment = PdfFragment.newInstance(documentUri, config); ...
If you’re using the PdfActivity
, you can also deactivate auto save via the #autosaveEnabled
setter of the PdfActivityConfiguration.Builder
:
// By default, auto save is enabled. val config = PdfActivityConfiguration.Builder(context) .autosaveEnabled(false) .build() PdfActivity.showDocument(context, documentUri, config) ...
// By default, auto save is enabled. final PdfActivityConfiguration config = new PdfActivityConfiguration.Builder(context) .autosaveEnabled(false) .build(); PdfActivity.showDocument(context, documentUri, config); ...
Modifying and saving annotations
If an annotation is modified (i.e. if it has been changed since the document has been loaded) a call to Annotation#isModified
will return true
. Furthermore, the PdfDocument#wasModified
method will return true
if annotations were added, changed, or removed. Once you save the document and its annotations, they’re no longer marked as modified.
![]()
If you’re editing annotations using one of the annotation tools, modifications to the edited annotation and document will only be visible after you exit the current tool mode by calling
PdfFragment#exitCurrentlyActiveMode
. If the annotation tool is still active (i.e. the tool is selected in the annotation creation toolbar),