1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-26 10:08:06 +02:00

[Annotations] Some annotations can have their values stored in the xfa:datasets

- it aims to fix #14685;
- add a basic object to get values from the parsed datasets;
- these annotations don't have an appearance so we must create one when printing or saving.
This commit is contained in:
Calixte Denizet 2022-03-31 19:18:30 +02:00
parent d6592b5e37
commit 0b597304c1
6 changed files with 178 additions and 13 deletions

View file

@ -72,14 +72,16 @@ class AnnotationFactory {
static create(xref, ref, pdfManager, idFactory, collectFields) {
return Promise.all([
pdfManager.ensureCatalog("acroForm"),
pdfManager.ensureDoc("xfaDatasets"),
collectFields ? this._getPageIndex(xref, ref, pdfManager) : -1,
]).then(([acroForm, pageIndex]) =>
]).then(([acroForm, xfaDatasets, pageIndex]) =>
pdfManager.ensure(this, "_create", [
xref,
ref,
pdfManager,
idFactory,
acroForm,
xfaDatasets,
collectFields,
pageIndex,
])
@ -95,6 +97,7 @@ class AnnotationFactory {
pdfManager,
idFactory,
acroForm,
xfaDatasets,
collectFields,
pageIndex = -1
) {
@ -119,6 +122,7 @@ class AnnotationFactory {
id,
pdfManager,
acroForm: acroForm instanceof Dict ? acroForm : Dict.empty,
xfaDatasets,
collectFields,
pageIndex,
};
@ -1237,7 +1241,7 @@ class WidgetAnnotation extends Annotation {
);
}
const fieldValue = getInheritableProperty({
let fieldValue = getInheritableProperty({
dict,
key: "V",
getArray: true,
@ -1251,6 +1255,15 @@ class WidgetAnnotation extends Annotation {
});
data.defaultFieldValue = this._decodeFormValue(defaultFieldValue);
if (fieldValue === undefined && params.xfaDatasets) {
// Try to figure out if we have something in the xfa dataset.
const path = this._title.str;
if (path) {
this._hasValueFromXFA = true;
data.fieldValue = fieldValue = params.xfaDatasets.getValue(path);
}
}
// When no "V" entry exists, let the fieldValue fallback to the "DV" entry
// (fixes issue13823.pdf).
if (fieldValue === undefined && data.defaultFieldValue !== null) {
@ -1401,17 +1414,20 @@ class WidgetAnnotation extends Annotation {
}
async save(evaluator, task, annotationStorage) {
if (!annotationStorage) {
return null;
}
const storageEntry = annotationStorage.get(this.data.id);
const value = storageEntry && storageEntry.value;
const storageEntry = annotationStorage
? annotationStorage.get(this.data.id)
: undefined;
let value = storageEntry && storageEntry.value;
if (value === this.data.fieldValue || value === undefined) {
return null;
if (!this._hasValueFromXFA) {
return null;
}
value = value || this.data.fieldValue;
}
// Value can be an array (with choice list and multiple selections)
if (
!this._hasValueFromXFA &&
Array.isArray(value) &&
Array.isArray(this.data.fieldValue) &&
value.length === this.data.fieldValue.length &&
@ -1493,14 +1509,23 @@ class WidgetAnnotation extends Annotation {
async _getAppearance(evaluator, task, annotationStorage) {
const isPassword = this.hasFieldFlag(AnnotationFieldFlag.PASSWORD);
if (!annotationStorage || isPassword) {
if (isPassword) {
return null;
}
const storageEntry = annotationStorage.get(this.data.id);
const storageEntry = annotationStorage
? annotationStorage.get(this.data.id)
: undefined;
let value = storageEntry && storageEntry.value;
if (value === undefined) {
// The annotation hasn't been rendered so use the appearance
return null;
if (!this._hasValueFromXFA || this.appearance) {
// The annotation hasn't been rendered so use the appearance.
return null;
}
// The annotation has its value in XFA datasets but not in the V field.
value = this.data.fieldValue;
if (!value) {
return "";
}
}
value = value.trim();