Rendering pages
Render PDF pages and thumbnails to PNG on iOS and Android with NexPDF for React Native, and map PDF coordinates to screen points.
License module
Rendering is part of the viewer module and works without a key. Unlicensed and trial renders carry
a watermark; commercial keys render without one.
Render a page
renderPage(page, dpi) resolves to a base64 PNG of the page. At 72 dpi one pixel is one PDF point;
the default is 150 dpi. Multiply by PixelRatio.get() for sharp pages on high-density screens.
import { Image, PixelRatio } from 'react-native';
import { NexPdf } from '@nexdevit/nexpdf-react-native';
const doc = await NexPdf.open(base64Pdf);
const png = await doc.renderPage(0, 72 * PixelRatio.get());
<Image source={{ uri: `data:image/png;base64,${png}` }} style={{ width: 360, height: 509 }} />;Render a thumbnail
renderThumbPng(page, maxDim) fits the page into a square of maxDim pixels, keeping the aspect
ratio:
const thumb = await doc.renderThumbPng(0, 256);Page size and coordinates
pageSize(page) resolves to the displayed size in points, with the crop box, /Rotate and
/UserUnit applied. pageView(page) resolves to a PageView that maps between PDF points and the
displayed page, whose origin is the top-left corner with y pointing down:
const view = await doc.pageView(0);
const scale = 360 / view.width; // screen points per PDF point
const box = view.rectToView([72, 700, 300, 716], scale); // { left, top, width, height }
const [x, y] = view.toPdf(event.nativeEvent.locationX, event.nativeEvent.locationY, scale); // a tap| Method | Converts |
|---|---|
toView(x, y, scale) | A PDF point to display coordinates |
toPdf(vx, vy, scale) | Display coordinates (a tap) to a PDF point |
deltaToPdf(dx, dy, scale) | A drag on screen to a displacement in PDF space |
rectToView(rect, scale) | A PDF rectangle to { left, top, width, height } |
rectToPdf(x1, y1, x2, y2, scale) | Two screen corners to a PDF rectangle |
Overprint simulation
Print-oriented PDF/X files can use overprint. setOverprint(mode) sets how later renders treat it:
0 simulates it for PDF/X documents only (the default), 1 always, 2 never.
await doc.setOverprint(1);
const png = await doc.renderPage(0, 300);Memory
memoryUsageBytes() reports the memory the document uses. Large renders are base64 strings in
JavaScript memory, so render at the size you display, and close documents you no longer need.
Related
- Reader for a ready-made page viewer
- Text extraction