UNPKG

@compdfkit_pdf_sdk/react_native

Version:

ComPDFKit for React Native is a comprehensive SDK that allows you to quickly add PDF functionality to Android, iOS, and React Native applications.

184 lines (179 loc) 7.72 kB
/** * Copyright © 2014-2025 PDF Technologies, Inc. All Rights Reserved. * * THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW * AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT. * UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES. * This notice may not be removed from this file. */ import { NativeModules, findNodeHandle } from 'react-native'; import { CPDFAnnotationFactory } from '../annotation/CPDFAnnotationFactory'; import { CPDFWidgetFactory } from '../annotation/form/CPDFWidgetFactory'; const { CPDFViewManager } = NativeModules; /** * @class CPDFPage * @memberof CPDFPage * The `CPDFPage` class represents a page in a PDF document. * It provides methods to retrieve annotations and form widgets present on the page. * @example * // Get the first page * const pageIndex = 0; * const cpdfPage : CPDFPage = pdfReaderRef?.current?._pdfDocument.pageAtIndex(pageIndex); * */ export class CPDFPage { constructor(viewerRef, pageIndex) { this.pageIndex = pageIndex; this._viewerRef = viewerRef; } /** * Retrieves all annotations on the current page. * * This method fetches all annotations present on the current page of the PDF document * and returns a list of corresponding CPDFAnnotation instances. * * @example * const pageIndex = 0; * const page = pdfReaderRef?.current?._pdfDocument.pageAtIndex(pageIndex); * const annotations = await page?.getAnnotations(); * * @returns {Promise<CPDFAnnotation[]>} A promise that resolves with all annotations on the current page, * or an empty array if retrieval fails. */ getAnnotations = async () => { const tag = findNodeHandle(this._viewerRef); if (tag != null) { try { const data = await CPDFViewManager.getAnnotations(tag, this.pageIndex); return CPDFAnnotationFactory.createFromArray(this._viewerRef, data); } catch (e) { return []; } } return Promise.reject(new Error('Unable to find the native view reference')); }; /** * Retrieves all form widgets on the current page. * * This method fetches all form widgets available on the current page of the PDF document * and returns a list of corresponding `CPDFWidget` instances. * * @example * const pageIndex = 0; * const page = pdfReaderRef?.current?._pdfDocument.pageAtIndex(pageIndex); * const widgets = await page?.getWidgets(); * * @see CPDFWidget - Base class for all form widgets * @see CPDFTextWidget - Text input field widget * @see CPDFSignatureWidget - Signature widget * @see CPDFRadiobuttonWidget - Radio button widget * @see CPDFPushbuttonWidget - Button widget * @see CPDFListboxWidget - List box widget * @see CPDFComboboxWidget - Combo box widget * @see CPDFCheckboxWidget - Checkbox widget * * @returns {Promise<CPDFWidget[]>} A promise that resolves to an array of `CPDFWidget` instances * representing form widgets on the current page. Returns an empty array if retrieval fails. * Rejects with an error if the native view reference is unavailable. */ getWidgets = async () => { const tag = findNodeHandle(this._viewerRef); if (tag != null) { try { const data = await CPDFViewManager.getForms(tag, this.pageIndex); return CPDFWidgetFactory.createFromArray(this._viewerRef, data); } catch (e) { console.error('Failed to retrieve form widgets:', e); return []; } } return Promise.reject(new Error('Unable to find the native view reference')); }; /** * Removes an annotation from the current page. * @param annotation The annotation to be removed. * @example * const pageIndex = 0; * const page = pdfReaderRef?.current?._pdfDocument.pageAtIndex(pageIndex); * const annotations = await page?.getAnnotations(); * const annotationToRemove = annotations[0]; * await page?.removeAnnotation(annotationToRemove); */ removeAnnotation(annotation) { const tag = findNodeHandle(this._viewerRef); if (tag != null) { return CPDFViewManager.removeAnnotation(tag, annotation.page, annotation.uuid); } return Promise.reject(new Error('Unable to find the native view reference')); } /** * Removes a form widget from the current page. * @example * const pageIndex = 0; * const page = pdfReaderRef?.current?._pdfDocument.pageAtIndex(pageIndex); * const widgets = await page?.getWidgets(); * const widgetToRemove = widgets[0]; * await page?.removeWidget(widgetToRemove); * @see CPDFWidget - Base class for all form widgets * @param widget The widget to be removed. * @returns */ removeWidget(widget) { const tag = findNodeHandle(this._viewerRef); if (tag != null) { return CPDFViewManager.removeWidget(tag, widget.page, widget.uuid); } return Promise.reject(new Error('Unable to find the native view reference')); } } export class CPDFPageSize { constructor(name, width, height, isCustom = false) { this.name = name; this.width = width; this.height = height; this.isCustom = isCustom; } static letter = new CPDFPageSize('letter', 612, 792); static note = new CPDFPageSize('note', 540, 720); static legal = new CPDFPageSize('legal', 612, 1008); static a0 = new CPDFPageSize('a0', 2380, 3368); static a1 = new CPDFPageSize('a1', 1684, 2380); static a2 = new CPDFPageSize('a2', 1190, 1684); static a3 = new CPDFPageSize('a3', 842, 1190); static a4 = new CPDFPageSize('a4', 595, 842); static a5 = new CPDFPageSize('a5', 421, 595); static a6 = new CPDFPageSize('a6', 297, 421); static a7 = new CPDFPageSize('a7', 210, 297); static a8 = new CPDFPageSize('a8', 148, 210); static a9 = new CPDFPageSize('a9', 105, 148); static a10 = new CPDFPageSize('a10', 74, 105); static b0 = new CPDFPageSize('b0', 2836, 4008); static b1 = new CPDFPageSize('b1', 2004, 2836); static b2 = new CPDFPageSize('b2', 1418, 2004); static b3 = new CPDFPageSize('b3', 1002, 1418); static b4 = new CPDFPageSize('b4', 709, 1002); static b5 = new CPDFPageSize('b5', 501, 709); static archE = new CPDFPageSize('archE', 2592, 3456); static archD = new CPDFPageSize('archD', 1728, 2592); static archC = new CPDFPageSize('archC', 1296, 1728); static archB = new CPDFPageSize('archB', 864, 1296); static archA = new CPDFPageSize('archA', 648, 864); static flsa = new CPDFPageSize('flsa', 612, 936); static flse = new CPDFPageSize('flse', 612, 936); static halfLetter = new CPDFPageSize('halfLetter', 396, 612); static s11x17 = new CPDFPageSize('11x17', 792, 1224); static ledger = new CPDFPageSize('ledger', 1224, 792); static values = [CPDFPageSize.letter, CPDFPageSize.note, CPDFPageSize.legal, CPDFPageSize.a0, CPDFPageSize.a1, CPDFPageSize.a2, CPDFPageSize.a3, CPDFPageSize.a4, CPDFPageSize.a5, CPDFPageSize.a6, CPDFPageSize.a7, CPDFPageSize.a8, CPDFPageSize.a9, CPDFPageSize.a10, CPDFPageSize.b0, CPDFPageSize.b1, CPDFPageSize.b2, CPDFPageSize.b3, CPDFPageSize.b4, CPDFPageSize.b5, CPDFPageSize.archE, CPDFPageSize.archD, CPDFPageSize.archC, CPDFPageSize.archB, CPDFPageSize.archA, CPDFPageSize.flsa, CPDFPageSize.flse, CPDFPageSize.halfLetter, CPDFPageSize.s11x17, CPDFPageSize.ledger]; static custom(width, height) { if (width <= 0 || height <= 0) { throw new Error('Width and height must be positive numbers.'); } return new CPDFPageSize('custom', width, height, true); } toString() { return `${this.name} (${this.width} x ${this.height})${this.isCustom ? ' [custom]' : ''}`; } } //# sourceMappingURL=CPDFPage.js.map