1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-20 15:18:08 +02:00

[Editor] Add the ability to create/update the structure tree when saving a pdf containing newly added annotations (bug 1845087)

When there is no tree, the tags for the new annotions are just put under the root element.
When there is a tree, we insert the new tags at the right place in using the value
of structTreeParentId (added in PR #16916).
This commit is contained in:
Calixte Denizet 2023-09-11 17:51:22 +02:00
parent 7f8de83e96
commit a8573d4e1b
8 changed files with 613 additions and 14 deletions

View file

@ -2297,6 +2297,114 @@ describe("api", function () {
await loadingTask.destroy();
});
it("write a new stamp annotation in a tagged pdf, save and check that the structure tree", async function () {
if (isNodeJS) {
pending("Cannot create a bitmap from Node.js.");
}
const TEST_IMAGES_PATH = "../images/";
const filename = "firefox_logo.png";
const path = new URL(TEST_IMAGES_PATH + filename, window.location).href;
const response = await fetch(path);
const blob = await response.blob();
const bitmap = await createImageBitmap(blob);
let loadingTask = getDocument(buildGetDocumentParams("bug1823296.pdf"));
let pdfDoc = await loadingTask.promise;
pdfDoc.annotationStorage.setValue("pdfjs_internal_editor_0", {
annotationType: AnnotationEditorType.STAMP,
rect: [128, 400, 148, 420],
rotation: 0,
bitmap,
bitmapId: "im1",
pageIndex: 0,
structTreeParentId: "p3R_mc12",
accessibilityData: {
type: "Figure",
alt: "Hello World",
},
});
const data = await pdfDoc.saveDocument();
await loadingTask.destroy();
loadingTask = getDocument(data);
pdfDoc = await loadingTask.promise;
const page = await pdfDoc.getPage(1);
const tree = await page.getStructTree();
const leaf = tree.children[0].children[6].children[1];
expect(leaf).toEqual({
role: "Figure",
children: [
{
type: "annotation",
id: "pdfjs_internal_id_477R",
},
],
alt: "Hello World",
});
await loadingTask.destroy();
});
it("write a new stamp annotation in a non-tagged pdf, save and check that the structure tree", async function () {
if (isNodeJS) {
pending("Cannot create a bitmap from Node.js.");
}
const TEST_IMAGES_PATH = "../images/";
const filename = "firefox_logo.png";
const path = new URL(TEST_IMAGES_PATH + filename, window.location).href;
const response = await fetch(path);
const blob = await response.blob();
const bitmap = await createImageBitmap(blob);
let loadingTask = getDocument(buildGetDocumentParams("empty.pdf"));
let pdfDoc = await loadingTask.promise;
pdfDoc.annotationStorage.setValue("pdfjs_internal_editor_0", {
annotationType: AnnotationEditorType.STAMP,
rect: [128, 400, 148, 420],
rotation: 0,
bitmap,
bitmapId: "im1",
pageIndex: 0,
structTreeParentId: null,
accessibilityData: {
type: "Figure",
alt: "Hello World",
},
});
const data = await pdfDoc.saveDocument();
await loadingTask.destroy();
loadingTask = getDocument(data);
pdfDoc = await loadingTask.promise;
const page = await pdfDoc.getPage(1);
const tree = await page.getStructTree();
expect(tree).toEqual({
children: [
{
role: "Figure",
children: [
{
type: "annotation",
id: "pdfjs_internal_id_18R",
},
],
alt: "Hello World",
},
],
role: "Root",
});
await loadingTask.destroy();
});
describe("Cross-origin", function () {
let loadingTask;
function _checkCanLoad(expectSuccess, filename, options) {