Replies: 6 comments 12 replies
-
The storage is just used to add the new properties which will be used when the pdf is printed/saved. |
Beta Was this translation helpful? Give feedback.
-
Yeah you need to handle that yourself. I'm just iterating through all inputs, textareas etc. and updating the value that way, so that it's the same as in the annotationstorage. |
Beta Was this translation helpful? Give feedback.
-
@calixteman I am trying to store annotations into localStorage so that I can keep them between site reloads, how would I do that? |
Beta Was this translation helpful? Give feedback.
-
Not really a javascript guy. I'm trying to be able to reload annotations in a maui web view that loads the pdfjs official viewer. I can save and restore annotationstorage from the database (JSON.stringify/JSON.parse). I know I need to re-populate the inputs. I don't need to check the user. I allready know it is the correct user. I have tried to use your code but haven't figured out what I should use for character and fields. I have a loaded a pdf and the restored data in the annotationstorage. |
Beta Was this translation helpful? Give feedback.
-
Has anyone an idea how I can access |
Beta Was this translation helpful? Give feedback.
-
@Excubitor Update: Found a way to programmatically add fields/editors/annotations (however you want to call them): /**
* @param {number} pageNum - starts at 1, represents the page
* @param {string} defaultContent - inital text content
* @returns editor
*/
function addTextEditor(pageNum, defaultContent = "unset") {
const pageEditorLayer = PDFViewerApplication.pdfViewer._pages[pageNum - 1].annotationEditorLayer.annotationEditorLayer
pageEditorLayer.pasteEditor(pdfjsLib.AnnotationEditorType.FREETEXT, {}) // Open editor layer.
// Workaround to update the mode, this creates an empty editor that gets removed later since it has no content
let editor = pageEditorLayer.createAndAddNewEditor(
{ offsetX: 0, offsetY: 0 },
/* isCentered = */ false
);
editor.setContentText = function (text) {
console.log(editor.contentDiv)
editor.contentDiv.innerHTML = `<div>` + defaultContent + `</div>`
}
editor.setContentText(defaultContent)
pageEditorLayer.pasteEditor(0, {}) // Close editor layer
return editor
}
function removeTextEditor(pageNum, editor) {
const pageEditorLayer = PDFViewerApplication.pdfViewer._pages[pageNum - 1].annotationEditorLayer.annotationEditorLayer
pageEditorLayer.pasteEditor(pdfjsLib.AnnotationEditorType.FREETEXT, {}) // Open editor layer.
pageEditorLayer.remove(editor);
pageEditorLayer.pasteEditor(0, {}) // Close editor layer
return editor
}
/**
* Seems like the only option since pageEditorLayer.#editors is a private field
* thus make sure to keep track of the actual editor object if you really need it (gets returned by addTextEditor)
* also sets/updates the editors page attribute.
* @returns editors divs of all pages
*/
function getEditors() {
let all = []
let editorLayers = document.querySelectorAll("div.annotationEditorLayer")
let pageNum = 1; // Assumes pages have ascending order, like any sane person would
// and that there is one editorLayer per page (these both are usually the defaults),
// otherwise pageNum will be wrong
for (layer of editorLayers) {
for (editor of layer.children) {
editor.setAttribute("page", pageNum);
all.push(editor)
}
pageNum++;
}
return all
} Usage: TODO for motivated people: make add function support drawings and images. Tested on: PDF 110dd61fd57444010b1ab5ff38782f0f [1.4 pdfeTeX-1.21a / TeX] (PDF.js: 4.1.378 [a208d6b]) |
Beta Was this translation helpful? Give feedback.
-
is there currently a way to make annotations manually added to the annotationstorage visible in the pdfjs viewer?
I can’t quite grasp how to reinitialize the page so that annotation in the storage gets rendered on screen.
Beta Was this translation helpful? Give feedback.
All reactions