Change the document title in the PDF metadata on Android
By default, Nutrient parses documents and extracts the PDF title from the PDF metadata. If there is no metadata or the PDF name equals Untitled, then it’ll fall back to the file name and remove the .pdf
file type.
Nutrient exposes various metadata keys:
public abstract class DocumentMetadata { @Nullable public abstract String getTitle(); @Nullable public abstract String getAuthor(); @Nullable public abstract String getSubject(); @Nullable public abstract String getKeywords(); @Nullable public abstract String getCreator(); @Nullable public abstract String getProducer(); @Nullable public abstract String getCreationDate(); @Nullable public abstract String getModDate(); }
You can learn more about PDF metadata in the PDF Reference 1.7, Table 317.
Action bar changes
By default, Nutrient sets the title of the action bar to the document title. To avoid this behavior and customize the action bar title, you’ll have to extend PdfActivity
and override the onSetActivityTitle()
method:
override fun onSetActivityTitle(configuration: PdfActivityConfiguration, document: PdfDocument?) { // Set the required title in the action bar. supportActionBar?.title = "Custom title"
@Override public void onSetActivityTitle(@NonNull PdfActivityConfiguration configuration, @Nullable PdfDocument document) { // Set the required title in the action bar. ActionBar actionBar = getSupportActionBar(); if (actionBar != null) { actionBar.setTitle("Custom title"); } }
Custom document title
If you want to set a custom document title but don’t want to change the document title inside the document metadata, then:
-
Create a
DocumentDescriptor
for your document -
Set the required custom title via
DocumentDescriptor#setTitle()
. -
Open the document descriptor in
PdfActivity
.
// Create the document descriptor and set the custom title. val documentDescriptor = DocumentDescriptor.fromUri(documentUri) documentDescriptor.setTitle("Custom title") // Start the activity with the document loaded from the document descriptor. val intent = PdfActivityIntentBuilder.fromDocumentDescriptor(context, documentDescriptor) .configuration(configuration) .build() context.startActivity(intent)
// Create the document descriptor and set the custom title. DocumentDescriptor documentDescriptor = DocumentDescriptor.fromUri(documentUri); documentDescriptor.setTitle("Custom title"); // Start the activity with the document loaded from the document descriptor. Intent intent PdfActivityIntentBuilder.fromDocumentDescriptor(context, documentDescriptor) .configuration(configuration) .build(); context.startActivity(intent)
Alternatively, if you are not using tabs, you can use a simpler but less generic API, PdfActivityConfiguration.Builder#title()
. The custom title set in PdfActivityConfiguration
is displayed inside the action bar or the document title view (whichever is enabled). Note that this configuration is ignored when displaying multiple documents in a single activity (for example, when using tabs).