Skip to content

Commit 5e64e1b

Browse files
author
Brendan Dahl
committed
[api-minor] Implement API to get MarkInfo from the catalog.
1 parent 8cf2749 commit 5e64e1b

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

src/core/obj.js

+41
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,47 @@ class Catalog {
152152
return shadow(this, "metadata", metadata);
153153
}
154154

155+
get markInfo() {
156+
let markInfo = null;
157+
try {
158+
markInfo = this._readMarkInfo();
159+
} catch (ex) {
160+
if (ex instanceof MissingDataException) {
161+
throw ex;
162+
}
163+
warn("Unable to read mark info.");
164+
}
165+
return shadow(this, "markInfo", markInfo);
166+
}
167+
168+
/**
169+
* @private
170+
*/
171+
_readMarkInfo() {
172+
const obj = this._catDict.get("MarkInfo");
173+
if (!isDict(obj)) {
174+
return null;
175+
}
176+
177+
const markInfo = Object.create({
178+
Marked: false,
179+
UserProperties: false,
180+
Suspects: false,
181+
});
182+
for (const key in markInfo) {
183+
if (!obj.has(key)) {
184+
continue;
185+
}
186+
const value = obj.get(key);
187+
if (!isBool(value)) {
188+
continue;
189+
}
190+
markInfo[key] = value;
191+
}
192+
193+
return markInfo;
194+
}
195+
155196
get toplevelPagesDict() {
156197
const pagesObj = this._catDict.get("Pages");
157198
if (!isDict(pagesObj)) {

src/core/worker.js

+4
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,10 @@ class WorkerMessageHandler {
499499
]);
500500
});
501501

502+
handler.on("GetMarkInfo", function wphSetupGetMarkInfo(data) {
503+
return pdfManager.ensureCatalog("markInfo");
504+
});
505+
502506
handler.on("GetData", function wphSetupGetData(data) {
503507
pdfManager.requestLoadedStream();
504508
return pdfManager.onLoadedStream().then(function (stream) {

src/display/api.js

+22
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,24 @@ class PDFDocumentProxy {
805805
return this._transport.getMetadata();
806806
}
807807

808+
/**
809+
* @typedef {Object} MarkInfo
810+
* Properties correspond to Table 321 of the PDF 32000-1:2008 spec. Values
811+
* are null if not defined by the PDF.
812+
* @property {boolean} Marked
813+
* @property {boolean} UserProperties
814+
* @property {boolean} Suspects
815+
*/
816+
817+
/**
818+
* @returns {Promise<MarkInfo | null>} A promise that is resolved with
819+
* a {MarkInfo} object that contains the MarkInfo flags for the PDF
820+
* document, or `null` when no MarkInfo values are present in the PDF file.
821+
*/
822+
getMarkInfo() {
823+
return this._transport.getMarkInfo();
824+
}
825+
808826
/**
809827
* @returns {Promise<TypedArray>} A promise that is resolved with a
810828
* {TypedArray} that has the raw data from the PDF.
@@ -2646,6 +2664,10 @@ class WorkerTransport {
26462664
});
26472665
}
26482666

2667+
getMarkInfo() {
2668+
return this.messageHandler.sendWithPromise("GetMarkInfo", null);
2669+
}
2670+
26492671
getStats() {
26502672
return this.messageHandler.sendWithPromise("GetStats", null);
26512673
}

test/unit/api_spec.js

+18
Original file line numberDiff line numberDiff line change
@@ -1192,6 +1192,24 @@ describe("api", function () {
11921192
.catch(done.fail);
11931193
});
11941194

1195+
it("gets markInfo", function (done) {
1196+
const loadingTask = getDocument(
1197+
buildGetDocumentParams("annotation-line.pdf")
1198+
);
1199+
1200+
loadingTask.promise
1201+
.then(function (pdfDoc) {
1202+
return pdfDoc.getMarkInfo();
1203+
})
1204+
.then(function (info) {
1205+
expect(info.Marked).toEqual(true);
1206+
expect(info.UserProperties).toEqual(false);
1207+
expect(info.Suspects).toEqual(false);
1208+
done();
1209+
})
1210+
.catch(done.fail);
1211+
});
1212+
11951213
it("gets data", function (done) {
11961214
var promise = pdfDocument.getData();
11971215
promise

0 commit comments

Comments
 (0)