Answer the question
In order to leave comments, you need to log in
How to display links in PDF.js?
Good evening guys. On the project, you need to view pdf files. I used the pdf.js library .
I made a page-by-page output, displayed the text in a separate layer, but ran into a problem.
The problem is that there are links in the pdf files. And I can't display these links.
Here is an example function for drawing text on a separate layer:
pageTextLayer (page) {
return page
.getTextContent()
.then(textContent => {
let container = this.$refs.pdfTextLayer
container.innerHTML = ''
PDF.renderTextLayer({
textContent,
container,
viewport: this.viewport,
textDivs: []
})
})
},
pageAnnotationLayer (page) {
return page
.getAnnotations()
.then(annotations => {
annotations.forEach(annotation => {
// нужны только ссылки
if (annotation.subtype !== 'Link' && !annotation.url) {
return
}
// Но вот тут дальше не пойму что делать, если с выводом слоя с текстом есть отдельный метод, который создаёт div элементы с нужным позиционированием и размером, то с аннотациями я так и не нашёл нужного метода
})
})
},
Answer the question
In order to leave comments, you need to log in
Found this solution
// Loading document.
PDFJS.getDocument(DEFAULT_URL).then(function (pdfDocument) {
// Document loaded, retrieving the page.
return pdfDocument.getPage(PAGE_TO_VIEW).then(function (pdfPage) {
// Creating the page view with default parameters.
var pdfPageView = new PDFJS.PDFPageView({
container: container,
id: PAGE_TO_VIEW,
scale: SCALE,
defaultViewport: pdfPage.getViewport(SCALE),
// We can enable text/annotations layers, if needed
textLayerFactory: new PDFJS.DefaultTextLayerFactory(),
annotationLayerFactory: new PDFJS.DefaultAnnotationLayerFactory()
});
// Associates the actual page with the view, and drawing it
pdfPageView.setPdfPage(pdfPage);
return pdfPageView.draw();
});
});
import { AnnotationLayerBuilder } from 'pdfjs-dist/lib/web/annotation_layer_builder';
import { SimpleLinkService} from 'pdfjs-dist/lib/web/pdf_link_service';
import NullL10n from 'pdfjs-dist/lib/web/ui_utils.js';
var annotateMeta = page.getAnnotations().then(function (data) {
var annotation = new AnnotationLayerBuilder({
pageDiv: textcontainer,
linkService: new SimpleLinkService(),
pdfPage: page,
l10n: NullL10n
})
annotation .render(viewport);
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question