-
-
Notifications
You must be signed in to change notification settings - Fork 896
Recipes
Wojciech Maj edited this page Nov 23, 2022
·
15 revisions
import React from 'react';
import { Document, Page } from 'react-pdf';
import samplePDF from './test.pdf';
export default function Test() {
return (
<Document file={samplePDF}>
<Page pageNumber={1} />
</Document>
);
}
Please note that this approach may cause performance issues.
import React, { useState } from 'react';
import { Document, Page } from 'react-pdf';
import samplePDF from './test.pdf';
export default function Test() {
const [numPages, setNumPages] = useState(null);
function onDocumentLoadSuccess({ numPages }) {
setNumPages(numPages);
}
return (
<Document
file={samplePDF}
onLoadSuccess={onDocumentLoadSuccess}
>
{Array.from(
new Array(numPages),
(el, index) => (
<Page
key={`page_${index + 1}`}
pageNumber={index + 1}
/>
),
)}
</Document>
);
}
import React, { useState } from 'react';
import { Document, Page } from 'react-pdf';
import samplePDF from './test.pdf';
export default function Test() {
const [numPages, setNumPages] = useState(null);
const [pageNumber, setPageNumber] = useState(1);
function onDocumentLoadSuccess({ numPages }) {
setNumPages(numPages);
setPageNumber(1);
}
function changePage(offset) {
setPageNumber(prevPageNumber => prevPageNumber + offset);
}
function previousPage() {
changePage(-1);
}
function nextPage() {
changePage(1);
}
return (
<>
<Document
file={samplePDF}
onLoadSuccess={onDocumentLoadSuccess}
>
<Page pageNumber={pageNumber} />
</Document>
<div>
<p>
Page {pageNumber || (numPages ? 1 : '--')} of {numPages || '--'}
</p>
<button
type="button"
disabled={pageNumber <= 1}
onClick={previousPage}
>
Previous
</button>
<button
type="button"
disabled={pageNumber >= numPages}
onClick={nextPage}
>
Next
</button>
</div>
</>
);
}
import React, { useCallback, useState } from 'react';
import { Document, Page } from 'react-pdf';
import samplePDF from './test.pdf';
function highlightPattern(text, pattern) {
return text.replace(pattern, (value) => `<mark>${value}</mark>`);
}
export default function Test() {
const [searchText, setSearchText] = useState('');
const textRenderer = useCallback(
(textItem) => highlightPattern(textItem.str, searchText),
[searchText]
);
function onChange(event) {
setSearchText(event.target.value);
}
return (
<>
<Document file={samplePDF}>
<Page
pageNumber={1}
customTextRenderer={textRenderer}
/>
</Document>
<div>
<label htmlFor="search">Search:</label>
<input type="search" id="search" value={searchText} onChange={onChange} />
</div>
</>
);
}
Note that this example is nearly identical to the next one. onItemClick
can be both used in Document
and Outline
components.
import React, { useState } from 'react';
import { Document, Outline, Page } from 'react-pdf';
import samplePDF from './test.pdf';
export default function Test() {
const [pageNumber, setPageNumber] = useState(1);
function onItemClick({ pageNumber: itemPageNumber }) {
setPageNumber(itemPageNumber);
}
return (
<Document file={samplePDF}>
<Outline onItemClick={onItemClick} />
<Page pageNumber={pageNumber || 1} />
</Document>
);
}
Note that this example is nearly identical to the previous one. onItemClick
can be both used in Document
and Outline
components.
import React, { useState } from 'react';
import { Document, Outline, Page } from 'react-pdf';
import samplePDF from './test.pdf';
export default function Test() {
const [pageNumber, setPageNumber] = useState(1);
function onItemClick({ pageNumber: itemPageNumber }) {
setPageNumber(itemPageNumber);
}
return (
<Document
file={samplePDF}
onItemClick={onItemClick}
>
<Page pageNumber={pageNumber || 1} />
</Document>
);
}