custom rendered text position not calculated correctly

I’m creating a basic PDF viewer in Vue.js 2 (since the customer does not want to migrate to Vue 3) using PDF.js (latest version of pdfjs-dist).

The loading and rendering of pages is working perfectly fine.

I am now trying to create a search functionality from scratch, since the latest version of PDF.js does no longer include the PDFViewerApplication and find controllers etc.

For the purpose of being able to highlight matched words, I was thinking of creating the text layers on top of the PDF’s canvas. Retrieving the text contents is alright, but the problem here is that the calculation of the desired position for the text is not correct.

I’ve tried using the transform properties from the text items and doing some calculations with it, I’ve tried scaling, doing some custom calculations but it just doesn’t work out.

My 3 latest attempts were the following (by the way I have created a repo in which the problem is reproduced, you can find it here: https://gitlab.com/dante.cassiman/pdf-viewer-bug-reproduction)

1.

      span.style.left =`${item.transform[4] * scale}px`;
      span.style.top = `${(viewport.height - item.transform[5]) * scale}px`;
      span.style.transform = `rotate(${item.transform[0]}deg)`
      span.style.fontSize = `${item.height * scale}px`;
      span.style.left =${item.transform[4] * scale}px;
      span.style.top = ${(viewport.height - item.transform[5]) * scale}px;
      span.style.transform = rotate(${item.transform[0]}deg)
      span.style.fontSize = ${item.height * scale}px;

      span.style.fontFamily = fontMap[item.fontName].loadedName;
      const [scaleX, , , scaleY, translateX, translateY] = item.transform;

      // calculate left and top positions
      const leftPosition = (translateX * scale) + (item.width * scaleX * scale) / scaleX;
      const topPosition = (viewport.height - translateY * scale) - (item.height * scaleY * scale) / scaleY;

      // set the calculated left and top
      span.style.left = `${leftPosition}px`;
      span.style.top = `${topPosition}px`;

      // calculate font size using the actual item height adjusted by scale
      span.style.fontSize = `${item.height * scaleY * (scale / scaleY)}px`; // Scale the font size appropriately

The scale variable comes from the renderPage function, which calculates it like this:

    const scale = canvasEl.offsetWidth / page.getViewport({ scale: 1 }).width * (window.devicePixelRatio || 1);
    const viewport = page.getViewport({ scale: scale, rotation: page.rotate });

It’s all a lot more clear in the Gitlab repo, so please feel free to take a look at it. When you run the app, the red text is the what’s rendered in the text layers and has to appear on top of it’s corresponding text item in the canvas, which it does not.

This is a screenshot of my latest attempt in the repo:

I also just noticed that zooming out the page does not change the size of the canvas (which is perfect for me), but it does zoom out / scale the text layers on top…

Some help would be greatly appreciated!

EDIT: It appears that I am making it way harder than it has to be. The following calculations seem to be working perfectly fine, the only problem now is that the text seems to be rendered bottom to top (if that makes sense) instead of top to bottom. So I assume that I need some sort of transform or inversion of Y coordinate now?

Also, the zooming problem in the browser still persists.

      span.style.left = `${item.transform[4] * scale / 2}px`;
      span.style.top = `${(viewport.height - item.transform[5]) * scale / 2}px`;
      span.style.fontSize = `${item.height * scale / 2}px`;

0

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật