I have a requirement where I have to open an existing pdf in a canvas and draw resizable and draggable rectangles in the pdf and get coordinates of these rectangles drawn. This is my code to display the pdf.
import React, { PureComponent } from "react";
import { Document, Page, pdfjs } from "react-pdf/dist/entry.webpack";
import PdfComponents from "../../../Components/PdfComponents/PdfComponent";
import "./PDFdemo.css";
import requiredFile from "./test_form.pdf";
pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${
pdfjs.version
}/pdf.worker.js`;
export default class PdfViewer extends PureComponent {
state = {
numPages: null,
pageNumber: 1,
rotate: 0,
scale: 1
};
onDocumentLoadSuccess = ({ numPages }) => {
console.log('this function was triggered')
this.setState({ numPages });
};
render() {
const { pageNumber, scale, pdf } = this.state;
console.log("pdf", pdf);
return (
<React.Fragment>
<div className="myDoc">
<div>
<PdfComponents />
</div>
<div id="ResumeContainer">
<div className="canvasStyle">
<Document
className="PDFDocument"
file={requiredFile}
onLoadError={(error) => {
console.log("Load error", error)
}}
onSourceSuccess={() => {
console.log("Source success")
}}
onSourceError={(error) => {
console.error("Source error", error)
}}
onLoadSuccess={this.onDocumentLoadSuccess}
>
{window === undefined ? <div>nothing here</div> : <Page
pageNumber={pageNumber}
className="PDFPage PDFPageOne"
scale={scale}
>
</Page>}
</Document>
</div>
</div>
</div>
</React.Fragment>
);
}
}
This is the code I am using to get a resizable rectangle
import React from 'react';
import "./pdfComponent.css";
import ResizableRect from 'react-resizable-rotatable-draggable'
class PDFComponent extends React.Component {
constructor() {
super()
this.state = {
width: 100,
height: 100,
top: 100,
left: 100,
rotateAngle: 0
}
}
handleResize = (style, isShiftKey, type) => {
// type is a string and it shows which resize-handler you clicked
// e.g. if you clicked top-right handler, then type is 'tr'
let { top, left, width, height } = style
top = Math.round(top)
left = Math.round(left)
width = Math.round(width)
height = Math.round(height)
this.setState({
top,
left,
width,
height
})
}
handleDrag = (deltaX, deltaY) => {
this.setState({
left: this.state.left + deltaX,
top: this.state.top + deltaY
})
}
render() {
const { width, top, left, height, rotateAngle } = this.state
return (
<div className="cursor">
<ResizableRect
id="parameters"
left={left}
top={top}
width={width}
height={height}
rotateAngle={rotateAngle}
// aspectRatio={false}
// minWidth={10}
// minHeight={10}
zoomable='n, w, s, e, nw, ne, se, sw'
// rotatable={true}
// onRotateStart={this.handleRotateStart}
onRotate={this.handleRotate}
// onRotateEnd={this.handleRotateEnd}
// onResizeStart={this.handleResizeStart}
onResize={this.handleResize}
// onResizeEnd={this.handleUp}
// onDragStart={this.handleDragStart}
onDrag={this.handleDrag}
// onDragEnd={this.handleDragEnd}
/>
</div>
);
}
}
export default PDFComponent;
I need to place this rectangle on the pdf and get its coordinates. Is there any other way I can draw rectangles in the pdf and get its coordinates?
All help is appreciated.