Product: @pdftron/webviewer
Product Version:^11.7.1
After loading a document if I select shapes and then Rectangle or any other tools, if new document loaded how to reset that selection on documentload
Product: @pdftron/webviewer
Product Version:^11.7.1
After loading a document if I select shapes and then Rectangle or any other tools, if new document loaded how to reset that selection on documentload
Hi there,
If you want a set tool to be loaded every time whenever a document is loaded, you can use the documentLoaded event and set any tool, such as the Edit tool:
documentViewer.addEventListener('documentLoaded', () => {
documentViewer.setToolMode(documentViewer.getTool('AnnotationEdit'));
});
Best regards,
Kevin
I written the below code to process the document after uploading into viwer done. The issue i found is pagesUpdated event is not firing for the particular document.
this.viewerInstance.Core.documentViewer.addEventListener(‘documentLoaded’, async () => {
if (!this.file) return;
const doc = this.viewerInstance.Core.documentViewer.getDocument();
const fileName = this.file.name;
// Helper to process the document once it’s fully ready
const processDocument = async () => {
const pageCount = doc.getPageCount();
if (pageCount === 0) {
console.error("No pages found, aborting.");
return;
}
// Get fully converted PDF (works for DOCX too)
const xfdfString = await annotationManager.exportAnnotations();
const options = {
filename: fileName,
xfdfString,
flags: instance.Core.SaveOptions.LINEARIZED,
downloadType: 'pdf'
};
const pdfBuffer = await doc.getFileData(options);
const convertedBlob = new Blob(\[new Uint8Array(pdfBuffer)\], { type: "application/pdf" });
const pageNumber = 1; // 1-based
try {
doc.loadThumbnail(pageNumber, (thumbnail: HTMLCanvasElement | HTMLImageElement) => {
let data = {
Doc: '{0} of {1}',
DocID: this.docsData.Records.length + 1,
File: fileName,
TotalPages: pageCount,
Front: convertedBlob,
thumbSrc: (thumbnail instanceof HTMLCanvasElement)
? thumbnail.toDataURL("image/png")
: (thumbnail as HTMLImageElement).src
};
// this.changeDocTypeinThumb(\[data\]);
this.disableSave = false;
if(this.selectedRecord){
data = { ...data, ...this.selectedRecord }
}
this.mapDocData(data);
this.currentFileIndex++;
this.loadNextFile();
});
} catch (err) {
console.error("Failed to load thumbnail:", err);
}
};
// If page count is > 0 right away (PDF case), process immediately
if (doc.type === 'office' || doc.type === 'officeEditor') {
// Otherwise (DOCX/PPTX case), wait for pagesUpdated event
const onPagesUpdated = async () => {
if (doc.getPageCount() > 0) {
await processDocument();
this.viewerInstance.Core.documentViewer.removeEventListener('pagesUpdated', onPagesUpdated);
}
};
this.viewerInstance.Core.documentViewer.addEventListener('pagesUpdated', onPagesUpdated);
} else {
await processDocument();
}
});
Hi there,
You should be able to use the documentLoaded event instead of the pagesUpdated event.
If the full document isn’t loaded, I recommend using the getDocumentCompletePromise event and then process the document.
Example:
documentViewer.addEventListener('documentLoaded', async () => {
const doc = documentViewer.getDocument();
await doc.getDocumentCompletePromise();
console.log('document fully ready'); // process document here
});
best regards,
Kevin