1- import { useCallback } from 'react' ;
1+ import { useCallback , useImperativeHandle , useRef } from 'react' ;
22import type {
33 LayoutChangeEvent ,
44 NativeSyntheticEvent ,
55 ViewStyle ,
66} from 'react-native' ;
77
8- import PdfViewNative from './PdfViewNativeComponent' ;
8+ import PdfViewNative , { Commands } from './PdfViewNativeComponent' ;
99import { asPath } from './Util' ;
1010
1111export type ErrorEvent = { message : string } ;
@@ -14,6 +14,8 @@ export type LoadCompleteEvent = { height: number; width: number };
1414
1515export type ResizeMode = 'contain' | 'fitWidth' ;
1616
17+ export type PdfViewRef = { setAnnotation ( annotation : string ) : void } ;
18+
1719export type PdfViewProps = {
1820 /**
1921 * PAS v1 annotation JSON string.
@@ -49,6 +51,8 @@ export type PdfViewProps = {
4951 */
5052 page : number ;
5153
54+ ref ?: React . RefObject < PdfViewRef | null > ;
55+
5256 /**
5357 * How pdf page should be scaled to fit in view dimensions.
5458 *
@@ -79,6 +83,16 @@ export type PdfViewProps = {
7983export function PdfView ( props : PdfViewProps ) {
8084 const { onError, onLayout, onLoadComplete } = props ;
8185
86+ const nativeRef = useRef < React . ComponentRef < typeof PdfViewNative > > ( null ) ;
87+
88+ useImperativeHandle ( props . ref , ( ) => ( {
89+ setAnnotation : ( annotation : string ) => {
90+ if ( nativeRef . current != null ) {
91+ Commands . imperativeApplyAnnotation ( nativeRef . current , annotation ) ;
92+ }
93+ } ,
94+ } ) ) ;
95+
8296 const onPdfError = useCallback (
8397 ( event : NativeSyntheticEvent < ErrorEvent > ) => {
8498 if ( onError != null ) {
@@ -104,6 +118,7 @@ export function PdfView(props: PdfViewProps) {
104118 onPdfError = { onPdfError }
105119 onPdfLoadComplete = { onPdfLoadComplete }
106120 page = { props . page }
121+ ref = { nativeRef }
107122 resizeMode = { props . resizeMode }
108123 source = { asPath ( props . source ) }
109124 style = { props . style }
0 commit comments