Getting started
Activate a license, open a PDF, count its pages, render a page and extract its text in a React Native app with NexPDF.
This example opens a PDF that the app already has as base64, shows the first page and its text. It runs without a license key too: viewing is always allowed, and rendered pages then carry a watermark.
Install the SDK
Add @nexdevit/nexpdf-react-native, the Android SDK and the iOS pods as described in
Installation.
Write the component
import { useEffect, useState } from 'react';
import { Image, ScrollView, Text } from 'react-native';
import { NexPdf } from '@nexdevit/nexpdf-react-native';
export function FirstPage({ base64Pdf, licenseXml }: { base64Pdf: string; licenseXml?: string }) {
const [png, setPng] = useState<string>();
const [text, setText] = useState('');
useEffect(() => {
let cancelled = false;
(async () => {
// 1. Activate the license once per app launch (optional for viewing).
if (licenseXml) await NexPdf.activateLicense(licenseXml, 'com.acme.reader');
// 2. Open the document.
const doc = await NexPdf.open(base64Pdf);
try {
// 3. Page count and size.
const count = await doc.pageCount();
const { width, height } = await doc.pageSize(0);
console.log(`${count} pages, first page ${width.toFixed(0)} x ${height.toFixed(0)} pt`);
// 4. Render the first page at 150 dpi, as base64 PNG.
const page = await doc.renderPage(0, 150);
// 5. Extract the text of the first page.
const first = await doc.extractPageText(0);
if (!cancelled) { setPng(page); setText(first); }
} finally {
await doc.close();
}
})().catch((e) => console.error(e));
return () => { cancelled = true; };
}, [base64Pdf, licenseXml]);
return (
<ScrollView>
{png && <Image source={{ uri: `data:image/png;base64,${png}` }} style={{ width: '100%', aspectRatio: 0.707 }} />}
<Text>{text}</Text>
</ScrollView>
);
}Run it
Rebuild the app on a device or simulator, and pass the component a PDF as base64, for example one read with your file-system library.
What the code does
NexPdf.activateLicense(keyOrXml, appId)accepts the license XML or the bare key. The app id is your bundle id on iOS and package name on Android. See License.NexPdf.open(base64, password?)opens a PDF. Word, Excel and PowerPoint files are converted when the license includesconversion; JPEG, PNG and TIFF pictures become one page each.pageSize(i)is the displayed size in points: crop box, rotation and/UserUnitapplied.renderPage(i, dpi)returns a base64 PNG. 72 dpi is one pixel per point; the default is 150.extractPageText(i)returns the page text in reading order.close()frees the native document.