@@ -636,62 +636,36 @@ const copyAnno = (idPath: string, fileName: string, pdf: any) => {
636
636
} , Constants . TIMEOUT_DBLCLICK ) ;
637
637
} ;
638
638
639
-
640
- const getPage = ( element : Element ) : Element | null => {
641
- if ( ! element ) {
642
- return null ;
643
- } else if ( element . classList . contains ( "page" ) ) {
644
- return element ;
645
- }
646
- return getPage ( element . parentElement ) ;
647
- } ;
648
-
649
- const getCaptureCanvas = async ( page : any , captureScale : number ) => {
650
- const viewport = page . getViewport ( { scale : captureScale * window . pdfjsLib . PixelsPerInch . PDF_TO_CSS_UNITS } ) ;
651
- // Support HiDPI-screens.
652
- const outputScale = window . devicePixelRatio || 1 ;
653
-
639
+ const getCaptureCanvas = async ( pdfObj : any , pageNumber : number ) => {
640
+ const pdfPage = await pdfObj . pdfDocument . getPage ( pageNumber ) ;
641
+ const viewport = pdfPage . getViewport ( { scale : window . devicePixelRatio || 1 } ) ;
654
642
const canvas = document . createElement ( "canvas" ) ;
655
- canvas . width = Math . floor ( viewport . width * outputScale ) ;
656
- canvas . height = Math . floor ( viewport . height * outputScale ) ;
657
- canvas . style . width = Math . floor ( viewport . width ) + "px" ;
658
- canvas . style . height = Math . floor ( viewport . height ) + "px" ;
643
+ canvas . width = Math . floor ( viewport . width ) ;
644
+ canvas . height = Math . floor ( viewport . height ) ;
659
645
660
- await page . render ( {
646
+ await pdfPage . render ( {
661
647
canvasContext : canvas . getContext ( "2d" ) ,
662
- transform : outputScale !== 1
663
- ? [ outputScale , 0 , 0 , outputScale , 0 , 0 ]
664
- : null ,
665
648
viewport : viewport
666
649
} ) . promise ;
667
650
668
651
return canvas ;
669
652
} ;
670
653
671
654
async function getRectImgData ( pdfObj : any ) {
672
- const pageElement = getPage ( rectElement . firstElementChild ) ;
655
+ const pageElement = hasClosestByClassName ( rectElement , "page" ) ;
673
656
if ( ! pageElement ) {
674
657
return ;
675
658
}
676
- const cavasElement = pageElement . querySelector ( ".canvasWrapper canvas" ) as HTMLCanvasElement ;
677
- let scale ;
678
- const trueWith = cavasElement . getBoundingClientRect ( ) . width ;
679
- if ( trueWith <= 0 ) {
680
- scale = window . devicePixelRatio || 1 ;
681
- } else {
682
- scale = cavasElement . width / trueWith ;
683
- }
659
+
660
+ const captureCanvas = await getCaptureCanvas ( pdfObj , parseInt ( pageElement . getAttribute ( "data-page-number" ) ) ) ;
661
+
684
662
const rectStyle = ( rectElement . firstElementChild as HTMLElement ) . style ;
685
- const captureLocation = {
686
- width : scale * parseFloat ( rectStyle . width ) ,
687
- height : scale * parseFloat ( rectStyle . height ) ,
688
- top : scale * parseFloat ( rectStyle . top ) ,
689
- left : scale * parseFloat ( rectStyle . left ) ,
690
- } ;
691
-
692
- const pdfPage = await pdfObj . pdfDocument . getPage ( parseInt ( pageElement . getAttribute ( "data-page-number" ) ) ) ;
693
- const captureCanvas = await getCaptureCanvas ( pdfPage , pdfObj . pdfViewer . currentScale ) ;
694
- const captureImageData = captureCanvas . getContext ( "2d" ) . getImageData ( captureLocation . left , captureLocation . top , captureLocation . width , captureLocation . height ) ;
663
+ const scale = ( window . devicePixelRatio || 1 ) / pdfObj . pdfViewer . currentScale / window . pdfjsLib . PixelsPerInch . PDF_TO_CSS_UNITS ;
664
+ const captureImageData = captureCanvas . getContext ( "2d" ) . getImageData (
665
+ scale * parseFloat ( rectStyle . left ) ,
666
+ scale * parseFloat ( rectStyle . top ) ,
667
+ scale * parseFloat ( rectStyle . width ) ,
668
+ scale * parseFloat ( rectStyle . height ) ) ;
695
669
696
670
const tempCanvas = document . createElement ( "canvas" ) ;
697
671
tempCanvas . width = captureImageData . width ;
0 commit comments