1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. var __generator = (this && this.__generator) || function (thisArg, body) {
  12. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
  13. return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  14. function verb(n) { return function (v) { return step([n, v]); }; }
  15. function step(op) {
  16. if (f) throw new TypeError("Generator is already executing.");
  17. while (g && (g = 0, op[0] && (_ = 0)), _) try {
  18. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  19. if (y = 0, t) op = [op[0] & 2, t.value];
  20. switch (op[0]) {
  21. case 0: case 1: t = op; break;
  22. case 4: _.label++; return { value: op[1], done: false };
  23. case 5: _.label++; y = op[1]; op = [0]; continue;
  24. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  25. default:
  26. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  27. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  28. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  29. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  30. if (t[2]) _.ops.pop();
  31. _.trys.pop(); continue;
  32. }
  33. op = body.call(thisArg, _);
  34. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  35. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  36. }
  37. };
  38. Object.defineProperty(exports, "__esModule", { value: true });
  39. exports.PDFDocument = void 0;
  40. var react_native_1 = require("react-native");
  41. var AnnotationModels_1 = require("../annotations/AnnotationModels");
  42. var Annotation_1 = require("../annotations/Annotation");
  43. /**
  44. * @class PDFDocument
  45. * @description The current document object loaded in the PSPDFKitView.
  46. *
  47. */
  48. var PDFDocument = /** @class */ (function () {
  49. /**
  50. * @ignore
  51. */
  52. function PDFDocument(pdfViewRef) {
  53. this.pdfViewRef = pdfViewRef;
  54. }
  55. /**
  56. * @method getDocumentId
  57. * @memberof PDFDocument
  58. * @description Returns a document identifier (inferred from a document provider if possible).
  59. * A permanent identifier based on the contents of the file at the time it was originally created.
  60. * If a document identifier is not available, a generated UID value is returned.
  61. * @example
  62. * const documentId = await this.pdfRef.current?.getDocument().getDocumentId();
  63. * @returns { Promise<string> } A promise containing the document identifier.
  64. */
  65. PDFDocument.prototype.getDocumentId = function () {
  66. return react_native_1.NativeModules.PDFDocumentManager.getDocumentId((0, react_native_1.findNodeHandle)(this.pdfViewRef));
  67. };
  68. /**
  69. * @method getPageCount
  70. * @memberof PDFDocument
  71. * @description Returns the number of pages in the document.
  72. * @example
  73. * const documentId = await this.pdfRef.current?.getDocument().getPageCount();
  74. * @returns { Promise<string> } A promise containing the document page count.
  75. */
  76. PDFDocument.prototype.getPageCount = function () {
  77. return react_native_1.NativeModules.PDFDocumentManager.getPageCount((0, react_native_1.findNodeHandle)(this.pdfViewRef));
  78. };
  79. /**
  80. * @method isEncrypted
  81. * @memberof PDFDocument
  82. * @description Indicates if the PDF document is encrypted (password protected).
  83. * @example
  84. * const documentId = await this.pdfRef.current?.getDocument().isEncrypted();
  85. * @returns { Promise<string> } A promise containing whether the document is encrypted.
  86. */
  87. PDFDocument.prototype.isEncrypted = function () {
  88. return react_native_1.NativeModules.PDFDocumentManager.isEncrypted((0, react_native_1.findNodeHandle)(this.pdfViewRef));
  89. };
  90. /**
  91. * @method invalidateCacheForPage
  92. * @memberof PDFDocument
  93. * @param {number} pageIndex Zero-based page index of the page to refresh.
  94. * @description Invalidates the rendered cache of the given pageIndex for this document.
  95. * Use this method if a single page of the document is not updated after a change, or changed externally, and needs to be re-rendered.
  96. * @example
  97. * const result = await this.pdfRef.current?.getDocument().invalidateCacheForPage(0);
  98. * @returns { Promise<boolean> } A promise containing the result of the operation. true if the cache was invalidated, false otherwise.
  99. */
  100. PDFDocument.prototype.invalidateCacheForPage = function (pageIndex) {
  101. return react_native_1.NativeModules.PDFDocumentManager.invalidateCacheForPage((0, react_native_1.findNodeHandle)(this.pdfViewRef), pageIndex);
  102. };
  103. /**
  104. * @method invalidateCache
  105. * @memberof PDFDocument
  106. * @description Invalidates the rendered cache of all the pages for this document.
  107. * Use this method if the document is not updated after a change, or changed externally, and needs to be re-rendered.
  108. * @example
  109. * const result = await this.pdfRef.current?.getDocument().invalidateCache();
  110. * @returns { Promise<boolean> } A promise containing the result of the operation. true if the cache was invalidated, false otherwise.
  111. */
  112. PDFDocument.prototype.invalidateCache = function () {
  113. return react_native_1.NativeModules.PDFDocumentManager.invalidateCache((0, react_native_1.findNodeHandle)(this.pdfViewRef));
  114. };
  115. /**
  116. * @method save
  117. * @memberof PDFDocument
  118. * @description Saves the document asynchronously.
  119. * @example
  120. * const result = await this.pdfRef.current?.getDocument().save();
  121. * @returns { Promise<boolean> } A promise containing the result of the operation. true if the document was saved, false otherwise.
  122. */
  123. PDFDocument.prototype.save = function () {
  124. return react_native_1.NativeModules.PDFDocumentManager.save((0, react_native_1.findNodeHandle)(this.pdfViewRef));
  125. };
  126. /**
  127. * @method getAllUnsavedAnnotations
  128. * @memberof PDFDocument
  129. * @description Gets all the unsaved changes to annotations in the document.
  130. * @example
  131. * const result = await this.pdfRef.current?.getDocument().getAllUnsavedAnnotations();
  132. * @returns { Promise<Record<string, any> | DocumentJSON> } A promise containing the unsaved annotations as an array, wrapped in a DocumentJSON object.
  133. */
  134. PDFDocument.prototype.getAllUnsavedAnnotations = function () {
  135. return react_native_1.NativeModules.PDFDocumentManager.getAllUnsavedAnnotations((0, react_native_1.findNodeHandle)(this.pdfViewRef));
  136. };
  137. /**
  138. * @method getAnnotations
  139. * @memberof PDFDocument
  140. * @param {string | Annotation.Type} [type] The type of annotation to get. If not specified, all annotation types are returned.
  141. * @description Gets all the annotations in the document for a specified type.
  142. * @example
  143. * const result = await this.pdfRef.current?.getDocument().getAnnotations(Annotation.Type.INK);
  144. * @returns { Promise<Array<AnnotationType | any>> } A promise containing the annotations of the document as an array of Annotation objects.
  145. */
  146. PDFDocument.prototype.getAnnotations = function (type) {
  147. return __awaiter(this, void 0, void 0, function () {
  148. var annotations;
  149. return __generator(this, function (_a) {
  150. switch (_a.label) {
  151. case 0: return [4 /*yield*/, react_native_1.NativeModules.PDFDocumentManager.getAnnotations((0, react_native_1.findNodeHandle)(this.pdfViewRef), type)];
  152. case 1:
  153. annotations = _a.sent();
  154. // For backwards compatibility, return raw results if type is not an Annotation.Type value
  155. if (type && !Object.values(Annotation_1.Annotation.Type).includes(type)) {
  156. return [2 /*return*/, annotations];
  157. }
  158. return [2 /*return*/, annotations.map(function (annotation) {
  159. // Convert each annotation based on its type
  160. switch (annotation.type) {
  161. case 'pspdfkit/comment-marker':
  162. case 'comment-marker':
  163. return new AnnotationModels_1.CommentMarkerAnnotation(annotation);
  164. case 'pspdfkit/shape/ellipse':
  165. case 'shape/ellipse':
  166. case 'ellipse':
  167. return new AnnotationModels_1.EllipseShapeAnnotation(annotation);
  168. case 'pspdfkit/markup/highlight':
  169. case 'markup/highlight':
  170. case 'highlight':
  171. return new AnnotationModels_1.HighlightMarkupAnnotation(annotation);
  172. case 'pspdfkit/image':
  173. case 'image':
  174. return new AnnotationModels_1.ImageAnnotation(annotation);
  175. case 'pspdfkit/ink':
  176. case 'ink':
  177. return new AnnotationModels_1.InkAnnotation(annotation);
  178. case 'pspdfkit/shape/line':
  179. case 'shape/line':
  180. case 'line':
  181. return new AnnotationModels_1.LineShapeAnnotation(annotation);
  182. case 'pspdfkit/link':
  183. case 'link':
  184. return new AnnotationModels_1.LinkAnnotation(annotation);
  185. case 'pspdfkit/media':
  186. case 'media':
  187. return new AnnotationModels_1.MediaAnnotation(annotation);
  188. case 'pspdfkit/note':
  189. case 'note':
  190. return new AnnotationModels_1.NoteAnnotation(annotation);
  191. case 'pspdfkit/shape/polygon':
  192. case 'shape/polygon':
  193. case 'polygon':
  194. return new AnnotationModels_1.PolygonShapeAnnotation(annotation);
  195. case 'pspdfkit/shape/polyline':
  196. case 'shape/polyline':
  197. case 'polyline':
  198. return new AnnotationModels_1.PolylineShapeAnnotation(annotation);
  199. case 'pspdfkit/shape/rectangle':
  200. case 'shape/rectangle':
  201. case 'rectangle':
  202. return new AnnotationModels_1.RectangleShapeAnnotation(annotation);
  203. case 'pspdfkit/markup/redaction':
  204. case 'markup/redaction':
  205. case 'redaction':
  206. return new AnnotationModels_1.RedactionMarkupAnnotation(annotation);
  207. case 'pspdfkit/markup/squiggly':
  208. case 'markup/squiggly':
  209. case 'squiggly':
  210. return new AnnotationModels_1.SquigglyMarkupAnnotation(annotation);
  211. case 'pspdfkit/stamp':
  212. case 'stamp':
  213. return new AnnotationModels_1.StampAnnotation(annotation);
  214. case 'pspdfkit/markup/strikeout':
  215. case 'markup/strikeout':
  216. case 'strikeout':
  217. return new AnnotationModels_1.StrikeOutMarkupAnnotation(annotation);
  218. case 'pspdfkit/text':
  219. case 'text':
  220. return new AnnotationModels_1.TextAnnotation(annotation);
  221. case 'pspdfkit/markup/underline':
  222. case 'markup/underline':
  223. case 'underline':
  224. return new AnnotationModels_1.UnderlineMarkupAnnotation(annotation);
  225. case 'pspdfkit/widget':
  226. case 'widget':
  227. return new AnnotationModels_1.WidgetAnnotation(annotation);
  228. default:
  229. return undefined;
  230. }
  231. }).filter(Boolean)]; // Filter out undefined values
  232. }
  233. });
  234. });
  235. };
  236. /**
  237. * @method getAnnotationsForPage
  238. * @memberof PDFDocument
  239. * @param {number} pageIndex The page index to get annotations for. Starts at 0.
  240. * @param {string | Annotation.Type} [type] The type of annotation to get. If not specified, all annotation types are returned.
  241. * @description Gets all the annotations in the document for a specified type.
  242. * @example
  243. * const result = await this.pdfRef.current?.getDocument().getAnnotationsForPage(0, Annotation.Type.INK);
  244. * @returns { Promise<Array<AnnotationType | any>> } A promise containing the annotations for the specified page of the document as an array.
  245. */
  246. PDFDocument.prototype.getAnnotationsForPage = function (pageIndex, type) {
  247. return __awaiter(this, void 0, void 0, function () {
  248. var annotations;
  249. return __generator(this, function (_a) {
  250. switch (_a.label) {
  251. case 0: return [4 /*yield*/, react_native_1.NativeModules.PDFDocumentManager.getAnnotationsForPage((0, react_native_1.findNodeHandle)(this.pdfViewRef), pageIndex, type)];
  252. case 1:
  253. annotations = _a.sent();
  254. // For backwards compatibility, return raw results if type is not an Annotation.Type value
  255. if (type && !Object.values(Annotation_1.Annotation.Type).includes(type)) {
  256. return [2 /*return*/, annotations];
  257. }
  258. return [2 /*return*/, annotations.map(function (annotation) {
  259. // Convert each annotation based on its type
  260. switch (annotation.type) {
  261. case 'pspdfkit/comment-marker':
  262. case 'comment-marker':
  263. return new AnnotationModels_1.CommentMarkerAnnotation(annotation);
  264. case 'pspdfkit/shape/ellipse':
  265. case 'shape/ellipse':
  266. case 'ellipse':
  267. return new AnnotationModels_1.EllipseShapeAnnotation(annotation);
  268. case 'pspdfkit/markup/highlight':
  269. case 'markup/highlight':
  270. case 'highlight':
  271. return new AnnotationModels_1.HighlightMarkupAnnotation(annotation);
  272. case 'pspdfkit/image':
  273. case 'image':
  274. return new AnnotationModels_1.ImageAnnotation(annotation);
  275. case 'pspdfkit/ink':
  276. case 'ink':
  277. return new AnnotationModels_1.InkAnnotation(annotation);
  278. case 'pspdfkit/shape/line':
  279. case 'shape/line':
  280. case 'line':
  281. return new AnnotationModels_1.LineShapeAnnotation(annotation);
  282. case 'pspdfkit/link':
  283. case 'link':
  284. return new AnnotationModels_1.LinkAnnotation(annotation);
  285. case 'pspdfkit/media':
  286. case 'media':
  287. return new AnnotationModels_1.MediaAnnotation(annotation);
  288. case 'pspdfkit/note':
  289. case 'note':
  290. return new AnnotationModels_1.NoteAnnotation(annotation);
  291. case 'pspdfkit/shape/polygon':
  292. case 'shape/polygon':
  293. case 'polygon':
  294. return new AnnotationModels_1.PolygonShapeAnnotation(annotation);
  295. case 'pspdfkit/shape/polyline':
  296. case 'shape/polyline':
  297. case 'polyline':
  298. return new AnnotationModels_1.PolylineShapeAnnotation(annotation);
  299. case 'pspdfkit/shape/rectangle':
  300. case 'shape/rectangle':
  301. case 'rectangle':
  302. return new AnnotationModels_1.RectangleShapeAnnotation(annotation);
  303. case 'pspdfkit/markup/redaction':
  304. case 'markup/redaction':
  305. case 'redaction':
  306. return new AnnotationModels_1.RedactionMarkupAnnotation(annotation);
  307. case 'pspdfkit/markup/squiggly':
  308. case 'markup/squiggly':
  309. case 'squiggly':
  310. return new AnnotationModels_1.SquigglyMarkupAnnotation(annotation);
  311. case 'pspdfkit/stamp':
  312. case 'stamp':
  313. return new AnnotationModels_1.StampAnnotation(annotation);
  314. case 'pspdfkit/markup/strikeout':
  315. case 'markup/strikeout':
  316. case 'strikeout':
  317. return new AnnotationModels_1.StrikeOutMarkupAnnotation(annotation);
  318. case 'pspdfkit/text':
  319. case 'text':
  320. return new AnnotationModels_1.TextAnnotation(annotation);
  321. case 'pspdfkit/markup/underline':
  322. case 'markup/underline':
  323. case 'underline':
  324. return new AnnotationModels_1.UnderlineMarkupAnnotation(annotation);
  325. case 'pspdfkit/widget':
  326. case 'widget':
  327. return new AnnotationModels_1.WidgetAnnotation(annotation);
  328. default:
  329. return undefined;
  330. }
  331. }).filter(Boolean)]; // Filter out undefined values
  332. }
  333. });
  334. });
  335. };
  336. /**
  337. * @method removeAnnotations
  338. * @memberof PDFDocument
  339. * @param {Array<any> | Array<AnnotationType>} annotations An array of the annotations to remove in InstantJSON format. Should not include the "annotations" key as used in the addAnnotations API.
  340. * @description Removes all the specified annotations from the document.
  341. * @example
  342. * const result = await this.pdfRef.current?.getDocument().removeAnnotations(annotations);
  343. * @returns { Promise<boolean> } A promise containing the result of the operation.
  344. */
  345. PDFDocument.prototype.removeAnnotations = function (annotations) {
  346. return react_native_1.NativeModules.PDFDocumentManager.removeAnnotations((0, react_native_1.findNodeHandle)(this.pdfViewRef), annotations);
  347. };
  348. /**
  349. * @method addAnnotations
  350. * @memberof PDFDocument
  351. * @param {Array<any> | Array<AnnotationType> | Record<string, any>} annotations Array of annotations in Instant JSON format to add to the document.
  352. * @description Adds all the specified annotations to the document. For complex annotations or annotations with attachments, use the applyInstantJSON API.
  353. * @example
  354. * const result = await this.pdfRef.current?.getDocument().addAnnotations(annotations);
  355. * @returns { Promise<boolean> } A promise containing the result of the operation. true if the annotations were added, and false if an error occurred.
  356. */
  357. PDFDocument.prototype.addAnnotations = function (annotations) {
  358. return react_native_1.NativeModules.PDFDocumentManager.addAnnotations((0, react_native_1.findNodeHandle)(this.pdfViewRef), annotations);
  359. };
  360. /**
  361. * @method applyInstantJSON
  362. * @memberof PDFDocument
  363. * @param {Annotation.DocumentJSON} documentJSON The full Instant JSON object to add to the document as a DocumentJSON object.
  364. * @description Adds the specified Document JSON data to the document.
  365. * @example
  366. * const result = await this.pdfRef.current?.getDocument().applyInstantJSON(documentJSON);
  367. * @returns { Promise<boolean> } A promise containing the result of the operation. true if the document JSON was applied, and false if an error occurred.
  368. */
  369. PDFDocument.prototype.applyInstantJSON = function (documentJSON) {
  370. return react_native_1.NativeModules.PDFDocumentManager.applyInstantJSON((0, react_native_1.findNodeHandle)(this.pdfViewRef), documentJSON);
  371. };
  372. /**
  373. * @method importXFDF
  374. * @memberof PDFDocument
  375. * @param {string} filePath The path to the XFDF file to import.
  376. * @description Imports the supplied XFDF file into the current document.
  377. * @example
  378. * const result = await this.pdfRef.current?.getDocument().importXFDF('path/to/XFDF.xfdf');
  379. * @returns { Promise<any> } A promise containing an object with the result. true if the xfdf file was imported successfully, and false if an error occurred.
  380. */
  381. PDFDocument.prototype.importXFDF = function (filePath) {
  382. return react_native_1.NativeModules.PDFDocumentManager.importXFDF((0, react_native_1.findNodeHandle)(this.pdfViewRef), filePath);
  383. };
  384. /**
  385. * @method exportXFDF
  386. * @memberof PDFDocument
  387. * @param {string} filePath The path where the XFDF file should be exported to.
  388. * @description Exports the annotations from the current document to a XFDF file.
  389. * @example
  390. * const result = await this.pdfRef.current?.getDocument().exportXFDF('path/to/XFDF.xfdf');
  391. * @returns { Promise<any> } A promise containing an object with the exported file path and result. true if the xfdf file was exported successfully, and false if an error occurred.
  392. */
  393. PDFDocument.prototype.exportXFDF = function (filePath) {
  394. return react_native_1.NativeModules.PDFDocumentManager.exportXFDF((0, react_native_1.findNodeHandle)(this.pdfViewRef), filePath);
  395. };
  396. return PDFDocument;
  397. }());
  398. exports.PDFDocument = PDFDocument;