@ironsoftware/ironpdf
Version:
IronPDF for Node
161 lines • 7.82 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAnnotationCount = exports.addLinkAnnotation = exports.getAnnotations = void 0;
const access_1 = require("../../access");
const annotation_1 = require("../../../public/annotation");
const util_1 = require("../util");
/**
* URL prefix used to encode an internal hyperlink (goto-page) target inside the
* existing {@code Pdfium_Annotation_AddLinkAnnotation} RPC. Must match the
* {@code InternalLinkPrefixes.InternalLinkPrefix} constant in the C#
* {@code IronPdf.GrpcLayer.InternalPrefixes} / {@code IronPdfServiceHandler}.
*
* <p>The {@code x-} prefix follows RFC 6648 conventions for experimental /
* private URL schemes and cannot collide with any real URL scheme a user would
* construct.</p>
*/
const INTERNAL_LINK_PREFIX = "x-ironpdf-goto-page:";
/**
* Retrieve all heterogeneous annotations (text, free-text, link, ...) on a single page
* via the {@code Pdfium_Annotation_GetAnnotations} unary RPC.
*
* The returned wrapped annotations carry one of the proto sub-types ({@code text},
* {@code freetext}, {@code link}). Callers inspect the {@code annotations} oneof to
* dispatch by sub-type.
*/
function getAnnotations(id, pageIndex) {
return __awaiter(this, void 0, void 0, function* () {
const client = yield access_1.Access.ensureConnection();
return new Promise((resolve, reject) => {
client.Pdfium_Annotation_GetAnnotationsRequestP({
document: { documentId: id },
pageIndex: pageIndex,
}, (err, value) => {
var _a, _b;
if (err) {
reject(`${err.name}/n${err.message}`);
return;
}
if (!value) {
reject("No response from IronPdfEngine for getAnnotations");
return;
}
if (value.exception) {
(0, util_1.handleRemoteException)(value.exception, reject);
return;
}
resolve((_b = (_a = value.result) === null || _a === void 0 ? void 0 : _a.annotations) !== null && _b !== void 0 ? _b : []);
});
});
});
}
exports.getAnnotations = getAnnotations;
/**
* Add an internal hyperlink annotation to the document. The link navigates to a
* destination page within the same PDF when clicked.
*
* <p>Reuses the existing {@code Pdfium_Annotation_AddLinkAnnotation} RPC by encoding
* the destination parameters into a special URL format:
* {@code x-ironpdf-goto-page:{destPage},{destType},{left},{right},{top},{bottom},{zoom},{showBorder}}.
* The engine detects this prefix and routes the request to its internal link handler.</p>
*
* <p>Mirrors {@code IronPdf.Engines.Pdfium.GrpcPdfClient.AddInternalLinkAnnotation}
* on the C# side.</p>
*/
function addLinkAnnotation(id, link) {
var _a, _b, _c, _d, _e, _f;
return __awaiter(this, void 0, void 0, function* () {
if (link.pageIndex < 0) {
throw new Error("Invalid page index when adding link annotation");
}
if (link.destinationPageIndex < 0) {
throw new Error("Invalid destination page index when adding link annotation");
}
const client = yield access_1.Access.ensureConnection();
const destType = (_a = link.destinationType) !== null && _a !== void 0 ? _a : annotation_1.BookmarkDestinations.Page;
const destLeft = (_b = link.destinationLeft) !== null && _b !== void 0 ? _b : 0;
const destRight = (_c = link.destinationRight) !== null && _c !== void 0 ? _c : 0;
const destTop = (_d = link.destinationTop) !== null && _d !== void 0 ? _d : 0;
const destBottom = (_e = link.destinationBottom) !== null && _e !== void 0 ? _e : 0;
const destZoom = (_f = link.destinationZoom) !== null && _f !== void 0 ? _f : 0;
const showBorderFlag = link.showBorder ? 1 : 0;
// Matches the 8-field encoding produced by C# AddInternalLinkAnnotation:
// {prefix}{destPageIndex},{destType},{destLeft},{destRight},{destTop},{destBottom},{destZoom},{showBorder}
const encodedUrl = `${INTERNAL_LINK_PREFIX}${link.destinationPageIndex},${destType},` +
`${destLeft},${destRight},${destTop},${destBottom},${destZoom},${showBorderFlag}`;
// Color fallback: the engine parses `color_code` into IronSoftware.Drawing.Color and
// throws on an empty string. Match the C# AddLinkAnnotation behavior by always
// sending a valid color — default to black when the caller hasn't specified one.
const colorCode = link.colorCode && link.colorCode.length > 0 ? link.colorCode : "#000000";
return new Promise((resolve, reject) => {
var _a, _b;
client.Pdfium_Annotation_AddLinkAnnotation({
document: { documentId: id },
name: (_a = link.title) !== null && _a !== void 0 ? _a : "",
url: encodedUrl,
pageIndex: link.pageIndex,
rectangle: {
x: link.x,
y: link.y,
width: link.width,
height: link.height,
},
colorCode: colorCode,
Hidden: (_b = link.hidden) !== null && _b !== void 0 ? _b : false,
}, (err, value) => {
if (err) {
reject(`${err.name}/n${err.message}`);
return;
}
if (!value) {
reject("No response from IronPdfEngine for addLinkAnnotation");
return;
}
(0, util_1.handleEmptyResultP__Output)(value, reject);
resolve();
});
});
});
}
exports.addLinkAnnotation = addLinkAnnotation;
/**
* Retrieve the number of annotations contained on the specified page via the
* {@code Pdfium_Annotation_GetAnnotationCount} unary RPC.
*/
function getAnnotationCount(id, pageIndex) {
return __awaiter(this, void 0, void 0, function* () {
const client = yield access_1.Access.ensureConnection();
return new Promise((resolve, reject) => {
client.Pdfium_Annotation_GetAnnotationCountRequestP({
document: { documentId: id },
pageIndex: pageIndex,
}, (err, value) => {
var _a;
if (err) {
reject(`${err.name}/n${err.message}`);
return;
}
if (!value) {
reject("No response from IronPdfEngine for getAnnotationCount");
return;
}
if (value.exception) {
(0, util_1.handleRemoteException)(value.exception, reject);
return;
}
resolve((_a = value.result) !== null && _a !== void 0 ? _a : 0);
});
});
});
}
exports.getAnnotationCount = getAnnotationCount;
//# sourceMappingURL=annotations.js.map