如何根据行号在ReactJS中突出显示PDF页面上的文本

huangapple go评论75阅读模式
英文:

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: &quot;The requirements include...&quot;,
    sourceDocuments: [
      {
        pageContent:&quot;Functionality requirements, backend functionality\nUser data information\nAbility to collect and sort...&quot;,
        metadata: { &quot;loc.lines.from&quot;: 161, &quot;loc.lines.to&quot;: 173 }
      },
    ],
  };

This is my react code to open the pdf:

import { Worker, Viewer } from &quot;@react-pdf-viewer/core&quot;;
import { defaultLayoutPlugin } from &quot;@react-pdf-viewer/default-layout&quot;;
import &quot;@react-pdf-viewer/core/lib/styles/index.css&quot;;
import &quot;@react-pdf-viewer/default-layout/lib/styles/index.css&quot;;

export default function App() {
   const data = {
    text: &quot;The requirements include...&quot;,
    sourceDocuments: [
      {
        pageContent:&quot;Functionality requirements, backend functionality\nUser data information\nAbility to collect and sort...&quot;,
        metadata: { &quot;loc.lines.from&quot;: 161, &quot;loc.lines.to&quot;: 173 }
      },
    ],
  };

  const defaultLayoutPluginInstance = defaultLayoutPlugin();

  return (
    &lt;Worker workerUrl=&quot;https://unpkg.com/pdfjs-dist@3.4.120/build/pdf.worker.min.js&quot;&gt;
      &lt;div style={{ height: &quot;720px&quot; }}&gt;
        &lt;Viewer fileUrl={&quot;Brief.pdf&quot;} plugins={[defaultLayoutPluginInstance]} /&gt;
      &lt;/div&gt;
    &lt;/Worker&gt;
  );
}

答案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 &quot;@react-pdf-viewer/core&quot;;
import { defaultLayoutPlugin } from &quot;@react-pdf-viewer/default-layout&quot;;
import { highlightPlugin, Trigger } from &#39;@react-pdf-viewer/highlight&#39;;
import type { HighlightArea, RenderHighlightsProps } from &#39;@react-pdf-viewer/highlight&#39;;

import &quot;@react-pdf-viewer/core/lib/styles/index.css&quot;;
import &quot;@react-pdf-viewer/default-layout/lib/styles/index.css&quot;;
import &#39;@react-pdf-viewer/highlight/lib/styles/index.css&#39;;

export default function App() {
  const data = {
    text: &quot;The requirements include...&quot;,
    sourceDocuments: [
      {
        pageContent:&quot;Functionality requirements, backend functionality\nUser data information\nAbility to collect and sort...&quot;,
        metadata: { &quot;loc.lines.from&quot;: 161, &quot;loc.lines.to&quot;: 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 = &#39;Brief.pdf&#39;;

  const defaultLayoutPluginInstance = defaultLayoutPlugin();

  const renderHighlights = (props: RenderHighlightsProps) =&gt; (
    &lt;div&gt;
      {areas
        .filter((area) =&gt; area.pageIndex === props.pageIndex)
        .map((area, idx) =&gt; (
          &lt;div
            key={idx}
            className=&quot;highlight-area&quot;
            style={Object.assign(
              {},
              {
                background: &#39;yellow&#39;,
                opacity: 0.4,
              },
              props.getCssProperties(area, props.rotation)
            )}
          /&gt;
        ))}
    &lt;/div&gt;
  );
  const highlightPluginInstance = highlightPlugin({
    renderHighlights,
    trigger: Trigger.None,
  });

  return (
    &lt;Worker workerUrl=&quot;https://unpkg.com/pdfjs-dist@3.4.120/build/pdf.worker.min.js&quot;&gt;
      &lt;div style={{ height: &quot;720px&quot; }}&gt;
        &lt;Viewer fileUrl={fileUrl} plugins={[defaultLayoutPluginInstance, highlightPluginInstance]} /&gt;
      &lt;/div&gt;
    &lt;/Worker&gt;
  );
}

This is based on the example shown here. Kindly replace the hardcoded data with the ones that you have on your metadata.

huangapple
  • 本文由 发表于 2023年7月3日 12:55:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76601925.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定