英文:
How to highlight the pdf page text based upon the line number in reactjs
问题
这是基于我需要用来突出显示文本的元数据的数据:
const data = {
text: "The requirements include...",
sourceDocuments: [
{
pageContent: "功能要求,后端功能\n用户数据信息\n能够收集和排序...",
metadata: { "loc.lines.from": 161, "loc.lines.to": 173 }
},
],
};
这是我的 React 代码以打开 PDF:
import { Worker, Viewer } from "@react-pdf-viewer/core";
import { defaultLayoutPlugin } from "@react-pdf-viewer/default-layout";
import "@react-pdf-viewer/core/lib/styles/index.css";
import "@react-pdf-viewer/default-layout/lib/styles/index.css";
export default function App() {
const data = {
text: "The requirements include...",
sourceDocuments: [
{
pageContent: "功能要求,后端功能\n用户数据信息\n能够收集和排序...",
metadata: { "loc.lines.from": 161, "loc.lines.to": 173 }
},
],
};
const defaultLayoutPluginInstance = defaultLayoutPlugin();
return (
<Worker workerUrl="https://unpkg.com/pdfjs-dist@3.4.120/build/pdf.worker.min.js">
<div style={{ height: "720px" }}>
<Viewer fileUrl="Brief.pdf" plugins={[defaultLayoutPluginInstance]} />
</div>
</Worker>
);
}
英文:
This is the data, based upon the metadata I need to use to highlight the text:
const data = {
text: "The requirements include...",
sourceDocuments: [
{
pageContent:"Functionality requirements, backend functionality\nUser data information\nAbility to collect and sort...",
metadata: { "loc.lines.from": 161, "loc.lines.to": 173 }
},
],
};
This is my react code to open the pdf:
import { Worker, Viewer } from "@react-pdf-viewer/core";
import { defaultLayoutPlugin } from "@react-pdf-viewer/default-layout";
import "@react-pdf-viewer/core/lib/styles/index.css";
import "@react-pdf-viewer/default-layout/lib/styles/index.css";
export default function App() {
const data = {
text: "The requirements include...",
sourceDocuments: [
{
pageContent:"Functionality requirements, backend functionality\nUser data information\nAbility to collect and sort...",
metadata: { "loc.lines.from": 161, "loc.lines.to": 173 }
},
],
};
const defaultLayoutPluginInstance = defaultLayoutPlugin();
return (
<Worker workerUrl="https://unpkg.com/pdfjs-dist@3.4.120/build/pdf.worker.min.js">
<div style={{ height: "720px" }}>
<Viewer fileUrl={"Brief.pdf"} plugins={[defaultLayoutPluginInstance]} />
</div>
</Worker>
);
}
答案1
得分: 1
你试过使用 @react-pdf-viewer/highlight
吗?
文档在这里
我还没有测试过这个,但大致是这样的:
import { Worker, Viewer } from "@react-pdf-viewer/core";
import { defaultLayoutPlugin } from "@react-pdf-viewer/default-layout";
import { highlightPlugin, Trigger } from '@react-pdf-viewer/highlight';
import type { HighlightArea, RenderHighlightsProps } from '@react-pdf-viewer/highlight';
import "@react-pdf-viewer/core/lib/styles/index.css";
import "@react-pdf-viewer/default-layout/lib/styles/index.css";
import '@react-pdf-viewer/highlight/lib/styles/index.css';
export default function App() {
const data = {
text: "The requirements include...",
sourceDocuments: [
{
pageContent:"Functionality requirements, backend functionality\nUser data information\nAbility to collect and sort...",
metadata: { "loc.lines.from": 161, "loc.lines.to": 173 }
},
],
};
// 我暂时使用了硬编码的区域数据,请用您的 sourceDocument 元数据替换它
const areas: HighlightArea[] = [{
pageIndex: 3,
height: 1.55401,
width: 28.1674,
left: 27.5399,
top: 15.0772,
},
{
pageIndex: 3,
height: 1.32637,
width: 37.477,
left: 55.7062,
top: 15.2715,
},
{
pageIndex: 3,
height: 1.55401,
width: 28.7437,
left: 16.3638,
top: 16.6616,
}];
const fileUrl: string = 'Brief.pdf';
const defaultLayoutPluginInstance = defaultLayoutPlugin();
const renderHighlights = (props: RenderHighlightsProps) => (
<div>
{areas
.filter((area) => area.pageIndex === props.pageIndex)
.map((area, idx) => (
<div
key={idx}
className="highlight-area"
style={Object.assign(
{},
{
background: 'yellow',
opacity: 0.4,
},
props.getCssProperties(area, props.rotation)
)}
/>
))}
</div>
);
const highlightPluginInstance = highlightPlugin({
renderHighlights,
trigger: Trigger.None,
});
return (
<Worker workerUrl="https://unpkg.com/pdfjs-dist@3.4.120/build/pdf.worker.min.js">
<div style={{ height: "720px" }}>
<Viewer fileUrl={fileUrl} plugins={[defaultLayoutPluginInstance, highlightPluginInstance]} />
</div>
</Worker>
);
}
这基于这里显示的示例。请将硬编码的数据替换为您的元数据中的数据。
英文:
Have you tried using @react-pdf-viewer/highlight
?
Documentation here
I haven't tested this but It goes something like:
import { Worker, Viewer } from "@react-pdf-viewer/core";
import { defaultLayoutPlugin } from "@react-pdf-viewer/default-layout";
import { highlightPlugin, Trigger } from '@react-pdf-viewer/highlight';
import type { HighlightArea, RenderHighlightsProps } from '@react-pdf-viewer/highlight';
import "@react-pdf-viewer/core/lib/styles/index.css";
import "@react-pdf-viewer/default-layout/lib/styles/index.css";
import '@react-pdf-viewer/highlight/lib/styles/index.css';
export default function App() {
const data = {
text: "The requirements include...",
sourceDocuments: [
{
pageContent:"Functionality requirements, backend functionality\nUser data information\nAbility to collect and sort...",
metadata: { "loc.lines.from": 161, "loc.lines.to": 173 }
},
],
};
// I am temporarily using hardcoded area data, replace this with your sourceDocument metadata
const areas: HighlightArea[] = {[
{
pageIndex: 3,
height: 1.55401,
width: 28.1674,
left: 27.5399,
top: 15.0772,
},
{
pageIndex: 3,
height: 1.32637,
width: 37.477,
left: 55.7062,
top: 15.2715,
},
{
pageIndex: 3,
height: 1.55401,
width: 28.7437,
left: 16.3638,
top: 16.6616,
},
]};
const fileUrl: string = 'Brief.pdf';
const defaultLayoutPluginInstance = defaultLayoutPlugin();
const renderHighlights = (props: RenderHighlightsProps) => (
<div>
{areas
.filter((area) => area.pageIndex === props.pageIndex)
.map((area, idx) => (
<div
key={idx}
className="highlight-area"
style={Object.assign(
{},
{
background: 'yellow',
opacity: 0.4,
},
props.getCssProperties(area, props.rotation)
)}
/>
))}
</div>
);
const highlightPluginInstance = highlightPlugin({
renderHighlights,
trigger: Trigger.None,
});
return (
<Worker workerUrl="https://unpkg.com/pdfjs-dist@3.4.120/build/pdf.worker.min.js">
<div style={{ height: "720px" }}>
<Viewer fileUrl={fileUrl} plugins={[defaultLayoutPluginInstance, highlightPluginInstance]} />
</div>
</Worker>
);
}
This is based on the example shown here. Kindly replace the hardcoded data with the ones that you have on your metadata.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论