Add support for checkboxes printing
This commit is contained in:
parent
bf539deada
commit
cb60523a15
@ -49,18 +49,21 @@ class AnnotationFactory {
|
|||||||
* instance.
|
* instance.
|
||||||
*/
|
*/
|
||||||
static create(xref, ref, pdfManager, idFactory) {
|
static create(xref, ref, pdfManager, idFactory) {
|
||||||
return pdfManager.ensure(this, "_create", [
|
return pdfManager.ensureDoc("acroForm").then(acroForm => {
|
||||||
xref,
|
return pdfManager.ensure(this, "_create", [
|
||||||
ref,
|
xref,
|
||||||
pdfManager,
|
ref,
|
||||||
idFactory,
|
pdfManager,
|
||||||
]);
|
idFactory,
|
||||||
|
acroForm,
|
||||||
|
]);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
static _create(xref, ref, pdfManager, idFactory) {
|
static _create(xref, ref, pdfManager, idFactory, acroForm) {
|
||||||
const dict = xref.fetchIfRef(ref);
|
const dict = xref.fetchIfRef(ref);
|
||||||
if (!isDict(dict)) {
|
if (!isDict(dict)) {
|
||||||
return undefined;
|
return undefined;
|
||||||
@ -78,6 +81,7 @@ class AnnotationFactory {
|
|||||||
subtype,
|
subtype,
|
||||||
id,
|
id,
|
||||||
pdfManager,
|
pdfManager,
|
||||||
|
acroForm: acroForm instanceof Dict ? acroForm : Dict.empty,
|
||||||
};
|
};
|
||||||
|
|
||||||
switch (subtype) {
|
switch (subtype) {
|
||||||
@ -514,8 +518,9 @@ class Annotation {
|
|||||||
return Promise.resolve(new OperatorList());
|
return Promise.resolve(new OperatorList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const appearance = this.appearance;
|
||||||
const data = this.data;
|
const data = this.data;
|
||||||
const appearanceDict = this.appearance.dict;
|
const appearanceDict = appearance.dict;
|
||||||
const resourcesPromise = this.loadResources([
|
const resourcesPromise = this.loadResources([
|
||||||
"ExtGState",
|
"ExtGState",
|
||||||
"ColorSpace",
|
"ColorSpace",
|
||||||
@ -533,14 +538,14 @@ class Annotation {
|
|||||||
opList.addOp(OPS.beginAnnotation, [data.rect, transform, matrix]);
|
opList.addOp(OPS.beginAnnotation, [data.rect, transform, matrix]);
|
||||||
return evaluator
|
return evaluator
|
||||||
.getOperatorList({
|
.getOperatorList({
|
||||||
stream: this.appearance,
|
stream: appearance,
|
||||||
task,
|
task,
|
||||||
resources,
|
resources,
|
||||||
operatorList: opList,
|
operatorList: opList,
|
||||||
})
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
opList.addOp(OPS.endAnnotation, []);
|
opList.addOp(OPS.endAnnotation, []);
|
||||||
this.appearance.reset();
|
appearance.reset();
|
||||||
return opList;
|
return opList;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -795,11 +800,16 @@ class WidgetAnnotation extends Annotation {
|
|||||||
getArray: true,
|
getArray: true,
|
||||||
});
|
});
|
||||||
data.alternativeText = stringToPDFString(dict.get("TU") || "");
|
data.alternativeText = stringToPDFString(dict.get("TU") || "");
|
||||||
data.defaultAppearance = getInheritableProperty({ dict, key: "DA" }) || "";
|
data.defaultAppearance =
|
||||||
|
getInheritableProperty({ dict, key: "DA" }) ||
|
||||||
|
params.acroForm.get("DA") ||
|
||||||
|
"";
|
||||||
const fieldType = getInheritableProperty({ dict, key: "FT" });
|
const fieldType = getInheritableProperty({ dict, key: "FT" });
|
||||||
data.fieldType = isName(fieldType) ? fieldType.name : null;
|
data.fieldType = isName(fieldType) ? fieldType.name : null;
|
||||||
this.fieldResources =
|
this.fieldResources =
|
||||||
getInheritableProperty({ dict, key: "DR" }) || Dict.empty;
|
getInheritableProperty({ dict, key: "DR" }) ||
|
||||||
|
params.acroForm.get("DR") ||
|
||||||
|
Dict.empty;
|
||||||
|
|
||||||
data.fieldFlags = getInheritableProperty({ dict, key: "Ff" });
|
data.fieldFlags = getInheritableProperty({ dict, key: "Ff" });
|
||||||
if (!Number.isInteger(data.fieldFlags) || data.fieldFlags < 0) {
|
if (!Number.isInteger(data.fieldFlags) || data.fieldFlags < 0) {
|
||||||
@ -961,6 +971,9 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
|
|||||||
constructor(params) {
|
constructor(params) {
|
||||||
super(params);
|
super(params);
|
||||||
|
|
||||||
|
this.checkedAppearance = null;
|
||||||
|
this.uncheckedAppearance = null;
|
||||||
|
|
||||||
this.data.checkBox =
|
this.data.checkBox =
|
||||||
!this.hasFieldFlag(AnnotationFieldFlag.RADIO) &&
|
!this.hasFieldFlag(AnnotationFieldFlag.RADIO) &&
|
||||||
!this.hasFieldFlag(AnnotationFieldFlag.PUSHBUTTON);
|
!this.hasFieldFlag(AnnotationFieldFlag.PUSHBUTTON);
|
||||||
@ -980,6 +993,40 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getOperatorList(evaluator, task, renderForms, annotationStorage) {
|
||||||
|
if (annotationStorage) {
|
||||||
|
const value = annotationStorage[this.data.id] || false;
|
||||||
|
let appearance;
|
||||||
|
if (value) {
|
||||||
|
appearance = this.checkedAppearance;
|
||||||
|
} else {
|
||||||
|
appearance = this.uncheckedAppearance;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (appearance) {
|
||||||
|
const savedAppearance = this.appearance;
|
||||||
|
this.appearance = appearance;
|
||||||
|
const operatorList = super.getOperatorList(
|
||||||
|
evaluator,
|
||||||
|
task,
|
||||||
|
renderForms,
|
||||||
|
annotationStorage
|
||||||
|
);
|
||||||
|
this.appearance = savedAppearance;
|
||||||
|
return operatorList;
|
||||||
|
}
|
||||||
|
|
||||||
|
// No appearance
|
||||||
|
return Promise.resolve(new OperatorList());
|
||||||
|
}
|
||||||
|
return super.getOperatorList(
|
||||||
|
evaluator,
|
||||||
|
task,
|
||||||
|
renderForms,
|
||||||
|
annotationStorage
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
_processCheckBox(params) {
|
_processCheckBox(params) {
|
||||||
if (isName(this.data.fieldValue)) {
|
if (isName(this.data.fieldValue)) {
|
||||||
this.data.fieldValue = this.data.fieldValue.name;
|
this.data.fieldValue = this.data.fieldValue.name;
|
||||||
@ -1003,6 +1050,14 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
|
|||||||
|
|
||||||
this.data.exportValue =
|
this.data.exportValue =
|
||||||
exportValues[0] === "Off" ? exportValues[1] : exportValues[0];
|
exportValues[0] === "Off" ? exportValues[1] : exportValues[0];
|
||||||
|
|
||||||
|
const normalAppearance = customAppearance.get("N");
|
||||||
|
if (!isDict(normalAppearance)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.checkedAppearance = normalAppearance.get(this.data.exportValue);
|
||||||
|
this.uncheckedAppearance = normalAppearance.get("Off") || null;
|
||||||
}
|
}
|
||||||
|
|
||||||
_processRadioButton(params) {
|
_processRadioButton(params) {
|
||||||
|
@ -542,16 +542,28 @@ class CheckboxWidgetAnnotationElement extends WidgetAnnotationElement {
|
|||||||
* @returns {HTMLSectionElement}
|
* @returns {HTMLSectionElement}
|
||||||
*/
|
*/
|
||||||
render() {
|
render() {
|
||||||
|
const storage = this.annotationStorage;
|
||||||
|
const data = this.data;
|
||||||
|
const id = data.id;
|
||||||
|
const value = storage.getOrCreateValue(
|
||||||
|
id,
|
||||||
|
data.fieldValue && data.fieldValue !== "Off"
|
||||||
|
);
|
||||||
|
|
||||||
this.container.className = "buttonWidgetAnnotation checkBox";
|
this.container.className = "buttonWidgetAnnotation checkBox";
|
||||||
|
|
||||||
const element = document.createElement("input");
|
const element = document.createElement("input");
|
||||||
element.disabled = this.data.readOnly;
|
element.disabled = data.readOnly;
|
||||||
element.type = "checkbox";
|
element.type = "checkbox";
|
||||||
element.name = this.data.fieldName;
|
element.name = this.data.fieldName;
|
||||||
if (this.data.fieldValue && this.data.fieldValue !== "Off") {
|
if (value) {
|
||||||
element.setAttribute("checked", true);
|
element.setAttribute("checked", true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
element.addEventListener("change", function (event) {
|
||||||
|
storage.setValue(id, event.target.checked);
|
||||||
|
});
|
||||||
|
|
||||||
this.container.appendChild(element);
|
this.container.appendChild(element);
|
||||||
return this.container;
|
return this.container;
|
||||||
}
|
}
|
||||||
|
@ -25,18 +25,24 @@ import {
|
|||||||
AnnotationFieldFlag,
|
AnnotationFieldFlag,
|
||||||
AnnotationFlag,
|
AnnotationFlag,
|
||||||
AnnotationType,
|
AnnotationType,
|
||||||
|
OPS,
|
||||||
stringToBytes,
|
stringToBytes,
|
||||||
stringToUTF8String,
|
stringToUTF8String,
|
||||||
} from "../../src/shared/util.js";
|
} from "../../src/shared/util.js";
|
||||||
import { createIdFactory, XRefMock } from "./test_utils.js";
|
import { createIdFactory, XRefMock } from "./test_utils.js";
|
||||||
import { Dict, Name, Ref } from "../../src/core/primitives.js";
|
import { Dict, Name, Ref } from "../../src/core/primitives.js";
|
||||||
import { Lexer, Parser } from "../../src/core/parser.js";
|
import { Lexer, Parser } from "../../src/core/parser.js";
|
||||||
|
import { PartialEvaluator } from "../../src/core/evaluator.js";
|
||||||
import { StringStream } from "../../src/core/stream.js";
|
import { StringStream } from "../../src/core/stream.js";
|
||||||
|
import { WorkerTask } from "../../src/core/worker.js";
|
||||||
|
|
||||||
describe("annotation", function () {
|
describe("annotation", function () {
|
||||||
class PDFManagerMock {
|
class PDFManagerMock {
|
||||||
constructor(params) {
|
constructor(params) {
|
||||||
this.docBaseUrl = params.docBaseUrl || null;
|
this.docBaseUrl = params.docBaseUrl || null;
|
||||||
|
this.pdfDocument = {
|
||||||
|
acroForm: new Dict(),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
ensure(obj, prop, args) {
|
ensure(obj, prop, args) {
|
||||||
@ -49,21 +55,41 @@ describe("annotation", function () {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ensureDoc(prop, args) {
|
||||||
|
return this.ensure(this.pdfDocument, prop, args);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let pdfManagerMock, idFactoryMock;
|
function HandlerMock() {
|
||||||
|
this.inputs = [];
|
||||||
|
}
|
||||||
|
HandlerMock.prototype = {
|
||||||
|
send(name, data) {
|
||||||
|
this.inputs.push({ name, data });
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
let pdfManagerMock, idFactoryMock, partialEvaluator;
|
||||||
|
|
||||||
beforeAll(function (done) {
|
beforeAll(function (done) {
|
||||||
pdfManagerMock = new PDFManagerMock({
|
pdfManagerMock = new PDFManagerMock({
|
||||||
docBaseUrl: null,
|
docBaseUrl: null,
|
||||||
});
|
});
|
||||||
idFactoryMock = createIdFactory(/* pageIndex = */ 0);
|
idFactoryMock = createIdFactory(/* pageIndex = */ 0);
|
||||||
|
partialEvaluator = new PartialEvaluator({
|
||||||
|
xref: new XRefMock(),
|
||||||
|
handler: new HandlerMock(),
|
||||||
|
pageIndex: 0,
|
||||||
|
idFactory: createIdFactory(/* pageIndex = */ 0),
|
||||||
|
});
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
afterAll(function () {
|
afterAll(function () {
|
||||||
pdfManagerMock = null;
|
pdfManagerMock = null;
|
||||||
idFactoryMock = null;
|
idFactoryMock = null;
|
||||||
|
partialEvaluator = null;
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("AnnotationFactory", function () {
|
describe("AnnotationFactory", function () {
|
||||||
@ -1630,6 +1656,62 @@ describe("annotation", function () {
|
|||||||
}, done.fail);
|
}, done.fail);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should render checkboxes for printing", function (done) {
|
||||||
|
const appearanceStatesDict = new Dict();
|
||||||
|
const exportValueOptionsDict = new Dict();
|
||||||
|
const normalAppearanceDict = new Dict();
|
||||||
|
const checkedAppearanceDict = new Dict();
|
||||||
|
|
||||||
|
const stream = new StringStream("0.1 0.2 0.3 rg");
|
||||||
|
stream.dict = checkedAppearanceDict;
|
||||||
|
|
||||||
|
checkedAppearanceDict.set("BBox", [0, 0, 8, 8]);
|
||||||
|
checkedAppearanceDict.set("FormType", 1);
|
||||||
|
checkedAppearanceDict.set("Matrix", [1, 0, 0, 1, 0, 0]);
|
||||||
|
normalAppearanceDict.set("Checked", stream);
|
||||||
|
exportValueOptionsDict.set("Off", 0);
|
||||||
|
exportValueOptionsDict.set("Checked", 1);
|
||||||
|
appearanceStatesDict.set("D", exportValueOptionsDict);
|
||||||
|
appearanceStatesDict.set("N", normalAppearanceDict);
|
||||||
|
|
||||||
|
buttonWidgetDict.set("AP", appearanceStatesDict);
|
||||||
|
|
||||||
|
const buttonWidgetRef = Ref.get(124, 0);
|
||||||
|
const xref = new XRefMock([
|
||||||
|
{ ref: buttonWidgetRef, data: buttonWidgetDict },
|
||||||
|
]);
|
||||||
|
const task = new WorkerTask("test print");
|
||||||
|
|
||||||
|
AnnotationFactory.create(
|
||||||
|
xref,
|
||||||
|
buttonWidgetRef,
|
||||||
|
pdfManagerMock,
|
||||||
|
idFactoryMock
|
||||||
|
)
|
||||||
|
.then(annotation => {
|
||||||
|
const annotationStorage = {};
|
||||||
|
annotationStorage[annotation.data.id] = true;
|
||||||
|
return annotation.getOperatorList(
|
||||||
|
partialEvaluator,
|
||||||
|
task,
|
||||||
|
false,
|
||||||
|
annotationStorage
|
||||||
|
);
|
||||||
|
}, done.fail)
|
||||||
|
.then(opList => {
|
||||||
|
expect(opList.argsArray.length).toEqual(3);
|
||||||
|
expect(opList.fnArray).toEqual([
|
||||||
|
OPS.beginAnnotation,
|
||||||
|
OPS.setFillRGBColor,
|
||||||
|
OPS.endAnnotation,
|
||||||
|
]);
|
||||||
|
expect(opList.argsArray[1]).toEqual(
|
||||||
|
new Uint8ClampedArray([26, 51, 76])
|
||||||
|
);
|
||||||
|
done();
|
||||||
|
}, done.fail);
|
||||||
|
});
|
||||||
|
|
||||||
it("should handle radio buttons without a field value", function (done) {
|
it("should handle radio buttons without a field value", function (done) {
|
||||||
const normalAppearanceStateDict = new Dict();
|
const normalAppearanceStateDict = new Dict();
|
||||||
normalAppearanceStateDict.set("2", null);
|
normalAppearanceStateDict.set("2", null);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user