@digital-blueprint/annotpdf
Version:
Javascript library for creating annotations in PDF documents
97 lines • 4.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const util_1 = require("./util");
class WriterUtil {
/**
* Writes a reference pointer
*
* <obj_id> <generation> R
*
* The 'R' and the preceding space is only written in case 'referenced' is true
* */
static writeReferencePointer(ref, referenced = false) {
let ret = util_1.Util.convertNumberToCharArray(ref.obj);
ret.push(util_1.Util.SPACE);
ret = ret.concat(util_1.Util.convertNumberToCharArray(ref.generation));
if (referenced) {
ret.push(util_1.Util.SPACE);
ret.push(...util_1.Util.R);
}
return ret;
}
/**
* Adds preceding zeros (0) in front of the 'value' to match the length
* */
static pad(length, value) {
value = String(value);
let ret = [];
for (let i = 0; i < length - value.length; ++i) {
ret.push(48);
}
ret = ret.concat(util_1.Util.convertNumberToCharArray(value));
return ret;
}
/**
* Writes a nested number array
* */
static writeNestedNumberArray(array) {
let ret = util_1.Util.ARRAY_START;
for (let subArray of array) {
ret = ret.concat(WriterUtil.writeNumberArray(subArray));
ret.push(util_1.Util.SPACE);
}
ret.push(...util_1.Util.ARRAY_END);
return ret;
}
/**
* Writes a javascript number array to a PDF number array
* */
static writeNumberArray(array) {
let ret = util_1.Util.ARRAY_START;
for (let i of array) {
ret = ret.concat(util_1.Util.convertNumberToCharArray(i));
ret.push(util_1.Util.SPACE);
}
ret.push(...util_1.Util.ARRAY_END);
return ret;
}
/**
* Replaces the /Annots field in an page object
*
* ptr : Pointer to the page object
* annot_array_reference : The reference to the annotation array
* */
static replaceAnnotsFieldInPageObject(data, page, page_ptr, annot_array_reference) {
let ptr_objend = util_1.Util.locateSequence(util_1.Util.ENDOBJ, data, page_ptr, true);
let complete_page_object_data = data.slice(page_ptr, ptr_objend + util_1.Util.ENDOBJ.length);
let ret = [];
if (page.hasAnnotsField) {
// in this case the page object directly contains an array of references and
// does not point to an array array object -- we replace the array of references with a pointer
// to the reference array
let ptr_annots = util_1.Util.locateSequence(util_1.Util.ANNOTS, complete_page_object_data, 0, true);
ret = Array.from(complete_page_object_data.slice(0, ptr_annots + util_1.Util.ANNOTS.length));
ret.push(util_1.Util.SPACE);
ret = ret.concat(WriterUtil.writeReferencePointer(annot_array_reference, true));
ret.push(util_1.Util.SPACE);
let ptr_annots_array_end = util_1.Util.locateSequence(util_1.Util.ARRAY_END, complete_page_object_data, ptr_annots, true) + util_1.Util.ARRAY_END.length;
ret = ret.concat(Array.from(complete_page_object_data.slice(ptr_annots_array_end, complete_page_object_data.length)));
}
else {
let ptr_dict_end = util_1.Util.locateSequenceReversed(util_1.Util.DICT_END, complete_page_object_data, complete_page_object_data.length - 1);
if (-1 === ptr_dict_end)
throw Error("Could not identify dictionary end");
ret = Array.from(complete_page_object_data.slice(0, ptr_dict_end));
ret = ret.concat(util_1.Util.ANNOTS);
ret.push(util_1.Util.SPACE);
ret = ret.concat(WriterUtil.writeReferencePointer(annot_array_reference, true));
ret.push(util_1.Util.SPACE);
ret = ret.concat(Array.from(complete_page_object_data.slice(ptr_dict_end, complete_page_object_data.length)));
}
ret.push(util_1.Util.CR);
ret.push(util_1.Util.LF);
return ret;
}
}
exports.WriterUtil = WriterUtil;
//# sourceMappingURL=writer-util.js.map