/** * Copyright © 2014-2024 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 React, { useState, useEffect, useRef } from 'react'; import { Image, Platform, StyleSheet, Text, View } from 'react-native'; import { CPDFReaderView, ComPDFKit, CPDFToolbarAction, CPDFThemes } from '@compdfkit_pdf_sdk/react_native'; import { useNavigation, useRoute, RouteProp } from '@react-navigation/native'; import { HeaderBackButton } from '@react-navigation/elements'; import { MenuProvider, Menu, MenuTrigger, MenuOptions, MenuOption } from 'react-native-popup-menu'; import { SafeAreaView } from 'react-native-safe-area-context'; import DocumentPicker from 'react-native-document-picker'; type RootStackParamList = { CPDFReaderViewExample: { document?: string }; }; type CPDFReaderViewExampleScreenRouteProp = RouteProp< RootStackParamList, 'CPDFReaderViewExample' >; const CPDFAnnotationsExampleScreen = () => { const pdfReaderRef = useRef(null); const navigation = useNavigation(); const route = useRoute(); const [samplePDF] = useState( route.params?.document || (Platform.OS === 'android' ? 'file:///android_asset/PDF_Document.pdf' : 'PDF_Document.pdf') ); const handleSave = async () => { if (pdfReaderRef.current) { const success = await pdfReaderRef.current.save(); if (success) { console.log('ComPDFKitRN save() : Document saved successfully'); } else { console.log('ComPDFKitRN save() : Failed to save document'); } } }; const menuOptions = [ 'Save', 'Remove All Annotations', 'Import Annotations 1', 'Import Annotations 2', 'Export Annotations']; const handleMenuItemPress = async (action: string) => { switch (action) { case 'Save': handleSave(); break; case 'Remove All Annotations': const removeResult = await pdfReaderRef.current?._pdfDocument.removeAllAnnotations(); console.log('ComPDFKitRN removeAllAnnotations:', removeResult); break; case 'Import Annotations 1': try { // Select an xfdf file from the public directory and import it into the current document const pickerResult = DocumentPicker.pick({ type: [DocumentPicker.types.allFiles], copyTo: 'cachesDirectory' }); pickerResult.then(async (res) => { const file = res[0]; console.log('fileUri:', file?.uri); console.log('fileCopyUri:', file?.fileCopyUri); console.log('fileType:', file?.type); const path = file!!.fileCopyUri!! if (!path?.endsWith('xml') && !path?.endsWith('xfdf')) { console.log('ComPDFKitRN please select xfdf format file'); return; } const importResult = await pdfReaderRef.current?._pdfDocument.importAnnotations(path); console.log('ComPDFKitRN importAnnotations:', importResult); }) } catch (err) { } break; case 'Import Annotations 2': // Android // import xfdf file from android assets directory const testXfdf = Platform.OS === 'android' ? 'file:///android_asset/test.xfdf' : 'test.xfdf' // import xfdf file from file path // const testXfdf = '/data/user/0/com.compdfkit.reactnative.example/xxx/xxx.xfdf'; const importResult = await pdfReaderRef.current?._pdfDocument.importAnnotations(testXfdf); console.log('ComPDFKitRN importAnnotations:', importResult); break; case 'Export Annotations': const exportXfdfFilePath = await pdfReaderRef.current?._pdfDocument.exportAnnotations(); console.log('ComPDFKitRN exportAnnotations:', exportXfdfFilePath); break; default: break; } }; const handleBack = () => { navigation.goBack(); }; const renderToolbar = () => { return ( Annotations Example {menuOptions.map((option, index) => ( handleMenuItemPress(option)}> {option} ))} ); }; return ( {renderToolbar()} ); }; const styles = StyleSheet.create({ toolbar: { height: 56, flexDirection: 'row', alignItems: 'center', justifyContent: 'flex-start', backgroundColor: '#FAFCFF', paddingHorizontal: 4, }, toolbarButton: { padding: 8, }, toolbarTitle: { flex: 1, color: 'black', fontSize: 16, fontWeight: 'bold', marginStart: 8 }, menuOption: { padding: 8, fontSize: 14, color: 'black', }, }); export default CPDFAnnotationsExampleScreen;