|
@@ -49,6 +49,7 @@ class ComPDFKitViewer {
|
|
|
this.scaleChangedCallback = null
|
|
|
this.annotationsNumChangedCallback = null
|
|
|
this.distanceChangedCallback = null
|
|
|
+ this._pagesCount = 0
|
|
|
this._license = ''
|
|
|
this._token = null
|
|
|
this._pdfId = null
|
|
@@ -301,6 +302,7 @@ class ComPDFKitViewer {
|
|
|
tool,
|
|
|
color
|
|
|
}) {
|
|
|
+ if (!this.pdfDocument) return
|
|
|
this.activeTool = tool
|
|
|
this.color = color
|
|
|
this.eventBus.dispatch("toolChanged", { tool, color });
|
|
@@ -495,7 +497,7 @@ class ComPDFKitViewer {
|
|
|
// }
|
|
|
|
|
|
// 处理传入的PDF
|
|
|
- return await loadingTask.promise.then(
|
|
|
+ return loadingTask.promise.then(
|
|
|
async pdfDocument => {
|
|
|
// 上传pdf
|
|
|
await pdfDocument.getData().then(async (binaryData) => {
|
|
@@ -649,6 +651,7 @@ class ComPDFKitViewer {
|
|
|
}
|
|
|
|
|
|
getSelectedText(pageNumber) {
|
|
|
+ if (!this.pdfDocument) return ''
|
|
|
if (pageNumber) {
|
|
|
const page = this.pdfViewer._pages[pageNumber - 1]
|
|
|
if (page) {
|
|
@@ -953,6 +956,7 @@ class ComPDFKitViewer {
|
|
|
}
|
|
|
|
|
|
getOutlines() {
|
|
|
+ if (!this.pdfDocument) return []
|
|
|
if (this.outlines) {
|
|
|
return this.outlines
|
|
|
}
|
|
@@ -965,6 +969,7 @@ class ComPDFKitViewer {
|
|
|
}
|
|
|
|
|
|
async search(value) {
|
|
|
+ if (!this.pdfDocument) return
|
|
|
const result = await this.messageHandler.sendWithPromise('Search', {
|
|
|
pagesPtr: this.pagesPtr,
|
|
|
value
|
|
@@ -990,6 +995,7 @@ class ComPDFKitViewer {
|
|
|
}
|
|
|
|
|
|
setActiveSearchResult(result = null, activeSearchIndex = 0) {
|
|
|
+ if (!this.pdfDocument) return
|
|
|
if (!this.searchResults) return
|
|
|
const activeResult = result ? result : this.searchResults[0]
|
|
|
const quad = activeResult.quads[0]
|
|
@@ -1014,7 +1020,6 @@ class ComPDFKitViewer {
|
|
|
pageView.setActiveSearchResult(activeResult)
|
|
|
this.#activeSearchIndex = 0
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
clearSearchResults() {
|
|
@@ -1153,12 +1158,14 @@ class ComPDFKitViewer {
|
|
|
}
|
|
|
|
|
|
removeAllAnnotations() {
|
|
|
+ if (!this.pdfDocument) return
|
|
|
if (this.pdfViewer.annotationStorage) {
|
|
|
this.pdfViewer.annotationStorage.cleanup()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
async importAnnotations(data) {
|
|
|
+ if (!this.pdfDocument) return
|
|
|
const source = await new Promise((resolve) => {
|
|
|
const reader = new FileReader();
|
|
|
reader.onload = (event) => {
|
|
@@ -1264,6 +1271,7 @@ class ComPDFKitViewer {
|
|
|
return false
|
|
|
});
|
|
|
} else {
|
|
|
+ if (!this.pdfDocument) return
|
|
|
// 设置密码标志
|
|
|
this.saveAction = 'set'
|
|
|
this.#pwd = value
|
|
@@ -1304,6 +1312,7 @@ class ComPDFKitViewer {
|
|
|
return false
|
|
|
});
|
|
|
} else {
|
|
|
+ if (!this.pdfDocument) return
|
|
|
if (this.#pwd) {
|
|
|
// 移除密码标志
|
|
|
this.saveAction = 'remove'
|
|
@@ -1337,6 +1346,7 @@ class ComPDFKitViewer {
|
|
|
}
|
|
|
|
|
|
toggleSidebar() {
|
|
|
+ if (!this.pdfDocument) return
|
|
|
this.pdfSidebar.toggle()
|
|
|
}
|
|
|
|
|
@@ -1512,10 +1522,15 @@ class ComPDFKitViewer {
|
|
|
this.pdfOutlineViewer?.reset();
|
|
|
this.pdfAttachmentViewer?.reset();
|
|
|
|
|
|
+ this.pagesPtr.length = 0
|
|
|
this.outlines = []
|
|
|
+ this.annotationHistory.length = 0
|
|
|
this.#pwd = ''
|
|
|
this._pdfId = null
|
|
|
this.annotations = null
|
|
|
+ this._docName = ''
|
|
|
+ this._pagesCount = 0
|
|
|
+ annotationStore.annotationsAll = null
|
|
|
|
|
|
await Promise.all(promises);
|
|
|
}
|
|
@@ -1928,6 +1943,7 @@ class ComPDFKitViewer {
|
|
|
return false
|
|
|
});
|
|
|
} else {
|
|
|
+ if (!this.pdfDocument) return
|
|
|
let saveType = 2
|
|
|
if (!this.saveAction || this.saveAction === 'set') {
|
|
|
saveType = 2
|
|
@@ -1954,6 +1970,7 @@ class ComPDFKitViewer {
|
|
|
}
|
|
|
|
|
|
async flattenPdfDownload() {
|
|
|
+ if (!this.pdfDocument) return
|
|
|
const blobData = await this.flattenPdf()
|
|
|
if (blobData) {
|
|
|
saveAs(blobData, this._docName)
|
|
@@ -2046,6 +2063,7 @@ class ComPDFKitViewer {
|
|
|
return false
|
|
|
});
|
|
|
} else {
|
|
|
+ if (!this.pdfDocument) return
|
|
|
const result = await this.messageHandler.sendWithPromise('XFDFExportAnnotations', {
|
|
|
doc: this.doc
|
|
|
})
|
|
@@ -2074,6 +2092,7 @@ class ComPDFKitViewer {
|
|
|
}
|
|
|
|
|
|
getSelectedPage(firstCoordinates, lastCoordinates) {
|
|
|
+ if (!this.pdfDocument) return null
|
|
|
let firstPageIndex = null, lastPageIndex = null
|
|
|
for (let i = 0; i < this.pagesCount; i++) {
|
|
|
const el = this.pdfViewer._pages[i].div
|
|
@@ -2102,6 +2121,7 @@ class ComPDFKitViewer {
|
|
|
}
|
|
|
|
|
|
pageToWindow(coordinates, pageNumber) {
|
|
|
+ if (!this.pdfDocument) return null
|
|
|
if (pageNumber > this.page && pageNumber < 1) return null
|
|
|
const el = this.pdfViewer._pages[pageNumber - 1].div
|
|
|
const left = el.offsetLeft
|
|
@@ -2115,6 +2135,7 @@ class ComPDFKitViewer {
|
|
|
}
|
|
|
|
|
|
windowToPage(coordinates, pageNumber) {
|
|
|
+ if (!this.pdfDocument) return null
|
|
|
if (pageNumber > this.page && pageNumber < 1) return null
|
|
|
const el = this.pdfViewer._pages[pageNumber - 1].div
|
|
|
const left = el.offsetLeft
|
|
@@ -2252,6 +2273,7 @@ class ComPDFKitViewer {
|
|
|
}
|
|
|
|
|
|
requestFullScreenMode() {
|
|
|
+ if (!this.pdfDocument) return
|
|
|
this.pdfPresentationMode?.request();
|
|
|
}
|
|
|
|
|
@@ -2274,7 +2296,8 @@ class ComPDFKitViewer {
|
|
|
this._cleanup()
|
|
|
}
|
|
|
|
|
|
- zoomIn(steps) {
|
|
|
+ zoomIn() {
|
|
|
+ if (!this.pdfDocument) return
|
|
|
if (this.pdfViewer.isInPresentationMode) {
|
|
|
return;
|
|
|
}
|
|
@@ -2283,7 +2306,8 @@ class ComPDFKitViewer {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- zoomOut(steps) {
|
|
|
+ zoomOut() {
|
|
|
+ if (!this.pdfDocument) return
|
|
|
if (this.pdfViewer.isInPresentationMode) {
|
|
|
return;
|
|
|
}
|
|
@@ -2293,14 +2317,17 @@ class ComPDFKitViewer {
|
|
|
}
|
|
|
|
|
|
nextPage() {
|
|
|
+ if (!this.pdfDocument) return
|
|
|
return this.pdfViewer.nextPage()
|
|
|
}
|
|
|
|
|
|
previousPage() {
|
|
|
+ if (!this.pdfDocument) return
|
|
|
return this.pdfViewer.previousPage()
|
|
|
}
|
|
|
|
|
|
pageNumberChanged(value) {
|
|
|
+ if (!this.pdfDocument) return
|
|
|
// Note that for `<input type="number">` HTML elements, an empty string will
|
|
|
// be returned for non-number inputs; hence we simply do nothing in that case.
|
|
|
if (value !== "") {
|
|
@@ -2394,10 +2421,12 @@ class ComPDFKitViewer {
|
|
|
}
|
|
|
|
|
|
webViewerSwitchAnnotationEditorMode(evt) {
|
|
|
+ if (!this.pdfDocument) return
|
|
|
this.pdfViewer.annotationEditorMode = evt.mode;
|
|
|
}
|
|
|
|
|
|
switchTool(mode) {
|
|
|
+ if (!this.pdfDocument) return
|
|
|
this.pdfCursorTools.switchTool(mode);
|
|
|
}
|
|
|
|
|
@@ -2470,6 +2499,7 @@ class ComPDFKitViewer {
|
|
|
}
|
|
|
|
|
|
webViewerPageMode({ mode }) {
|
|
|
+ if (!this.pdfDocument) return
|
|
|
// Handle the 'pagemode' hash parameter, see also `PDFLinkService_setHash`.
|
|
|
let view;
|
|
|
switch (mode) {
|
|
@@ -2497,6 +2527,7 @@ class ComPDFKitViewer {
|
|
|
}
|
|
|
|
|
|
async webViewerNamedAction(evt) {
|
|
|
+ if (!this.pdfDocument) return
|
|
|
// Processing a couple of named actions that might be useful, see also
|
|
|
// `PDFLinkService.executeNamedAction`.
|
|
|
switch (evt.action) {
|
|
@@ -2516,6 +2547,7 @@ class ComPDFKitViewer {
|
|
|
}
|
|
|
|
|
|
triggerPrinting(data) {
|
|
|
+ if (!this.pdfDocument) return
|
|
|
let url = data instanceof Blob ? URL.createObjectURL(data) : data
|
|
|
url = decodeURIComponent(url)
|
|
|
printJS({
|
|
@@ -2569,18 +2601,23 @@ class ComPDFKitViewer {
|
|
|
}
|
|
|
}
|
|
|
webViewerScaleChanged(value) {
|
|
|
+ if (!this.pdfDocument) return
|
|
|
this.pdfViewer.currentScaleValue = value;
|
|
|
}
|
|
|
webViewerRotateCw() {
|
|
|
+ if (!this.pdfDocument) return
|
|
|
this.rotatePages(90);
|
|
|
}
|
|
|
webViewerRotateCcw() {
|
|
|
+ if (!this.pdfDocument) return
|
|
|
this.rotatePages(-90);
|
|
|
}
|
|
|
webViewerSwitchScrollMode(mode) {
|
|
|
+ if (!this.pdfDocument) return
|
|
|
this.pdfViewer.scrollMode = mode;
|
|
|
}
|
|
|
webViewerSwitchSpreadMode(mode) {
|
|
|
+ if (!this.pdfDocument) return
|
|
|
this.pdfViewer.spreadMode = mode;
|
|
|
}
|
|
|
|
|
@@ -2662,6 +2699,7 @@ class ComPDFKitViewer {
|
|
|
}
|
|
|
|
|
|
async setToolMode(mode) {
|
|
|
+ if (!this.pdfDocument) return
|
|
|
const oldMode = this.toolMode;
|
|
|
this.toolMode = mode;
|
|
|
if (mode === 'editor' && !this.fontFileInited) {
|
|
@@ -2728,14 +2766,17 @@ class ComPDFKitViewer {
|
|
|
}
|
|
|
|
|
|
setPropertyPanel(props) {
|
|
|
+ if (!this.pdfDocument) return
|
|
|
this.eventBus.dispatch("propertyPanelChanged", props);
|
|
|
}
|
|
|
|
|
|
setFreetextProperty(data) {
|
|
|
+ if (!this.pdfDocument) return
|
|
|
this.eventBus.dispatch('setFreetextProperty', data)
|
|
|
}
|
|
|
|
|
|
setContentEditorProperty(type, props) {
|
|
|
+ if (!this.pdfDocument) return
|
|
|
if (type === 'text') {
|
|
|
this.eventBus.dispatch("textPropertyChanged", props);
|
|
|
} else if (type === 'image') {
|
|
@@ -2744,6 +2785,7 @@ class ComPDFKitViewer {
|
|
|
}
|
|
|
|
|
|
setInitProperty() {
|
|
|
+ if (!this.pdfDocument) return
|
|
|
if (this.activeTool === 'textfield') {
|
|
|
this.eventBus.dispatch("initTextfieldName");
|
|
|
} else if (this.activeTool === 'checkbox') {
|
|
@@ -2756,6 +2798,7 @@ class ComPDFKitViewer {
|
|
|
}
|
|
|
|
|
|
handleField(prop) {
|
|
|
+ if (!this.pdfDocument) return
|
|
|
const id = prop.id
|
|
|
const key = prop.key
|
|
|
const value = prop.value
|
|
@@ -2777,6 +2820,7 @@ class ComPDFKitViewer {
|
|
|
}
|
|
|
|
|
|
handleSign(flag, param) {
|
|
|
+ if (!this.pdfDocument) return
|
|
|
if (flag === 'create' && !this.InkSign) {
|
|
|
this.InkSign = new InkSign({
|
|
|
pdfViewer: this.pdfViewer,
|
|
@@ -2800,6 +2844,7 @@ class ComPDFKitViewer {
|
|
|
}
|
|
|
|
|
|
handleStamp(data) {
|
|
|
+ if (!this.pdfDocument) return
|
|
|
if (!data) return
|
|
|
let imgUi = data.stampContainer
|
|
|
let pageNum = data.page
|
|
@@ -3022,6 +3067,7 @@ class ComPDFKitViewer {
|
|
|
// 页面编辑 - 获取每个页面图片
|
|
|
// 获取范围内的页面图片 index起始索引 num页面数量
|
|
|
async getDocEditorPages(pageSize, index = 0, num = null) {
|
|
|
+ if (!this.pdfDocument) return
|
|
|
if (!this.docEditorCopy) {
|
|
|
const doc = await this.messageHandler.sendWithPromise('createEditorDoc', {
|
|
|
doc: this.doc,
|
|
@@ -3392,6 +3438,7 @@ class ComPDFKitViewer {
|
|
|
// 页面编辑 - 保存
|
|
|
async saveDocumentEdit(op) {
|
|
|
const doc = this.docEditorCopy.doc
|
|
|
+ if (!doc) return
|
|
|
const clearDocument = async () => {
|
|
|
await this.messageHandler.sendWithPromise('ClearDocument', doc)
|
|
|
this.docEditorCopy = null
|
|
@@ -3417,7 +3464,6 @@ class ComPDFKitViewer {
|
|
|
|
|
|
const newUrl = URL.createObjectURL(blobData)
|
|
|
if (this.saveAction === 'set' && this.#oldPwd !== this.#pwd) this.#oldPwd = this.#pwd
|
|
|
- await this.loadDocument(newUrl, { filename, notUpdatePwd: true })
|
|
|
await clearDocument()
|
|
|
return newUrl
|
|
|
}
|