Skip to content

Close a Document ​

Foxit PDF SDK for Web closes the currently open document through PDFViewer.close. PDFUI wraps PDFViewer, so with UIExtension you can also close documents through pdfui.close().

Closing a document releases its render objects, clears active state, and triggers document-close workflows. When opening a new document, the SDK also attempts to close the current document first.

Basic Usage ​

javascript
await pdfui.close();

If you use PDFViewCtrl:

javascript
await pdfViewer.close();

When no document is open, calling close() completes without throwing an error.

Confirm Before Close ​

close() accepts a pre-close callback. If the callback returns false, the SDK prevents the document from closing.

javascript
await pdfui.close(async function(pdfDoc) {
    const confirmed = window.confirm('Close the current document?');
    return confirmed;
});

This suits scenarios such as:

  • The document has unsaved changes and the user must confirm.
  • Business state must be checked before closing.
  • Data must be saved asynchronously before closing.

After-Close Callback ​

The second parameter to close() is an after-close callback for cleaning up business state or updating the UI.

javascript
await pdfui.close(
    async function(pdfDoc) {
        return window.confirm('Close the current document?');
    },
    function() {
        console.log('Document closed');
    }
);

Set Default Callbacks in Initialization Options ​

If your application needs unified before/after close handling, set default callbacks through customs.closeDocBefore and customs.closeDocAfter when creating PDFUI or PDFViewer.

javascript
const pdfui = new UIExtension.PDFUI({
    viewerOptions: {
        libPath: './lib',
        jr: {
            licenseSN: licenseSN,
            licenseKey: licenseKey
        },
        customs: {
            closeDocBefore: async function(pdfDoc) {
                return window.confirm('Close the current document?');
            },
            closeDocAfter: function() {
                console.log('Document close completed');
            }
        }
    },
    renderTo: '#pdf-ui'
});

When you call close() directly without parameters, the SDK uses the default callbacks from initialization options.

javascript
await pdfui.close();

Close Flow When Opening a New Document ​

Before openPDFByFile or openPDFByHttpRangeRequest opens a new document, the SDK closes the current document. If you configured closeDocBefore, that callback also affects the open-new-document flow.

javascript
const pdfui = new UIExtension.PDFUI({
    viewerOptions: {
        libPath: './lib',
        jr: {
            licenseSN: licenseSN,
            licenseKey: licenseKey
        },
        customs: {
            closeDocBefore: async function() {
                // When this returns false, the current document is not closed and the new document does not open.
                return window.confirm('Close the current document and open a new one?');
            }
        }
    },
    renderTo: '#pdf-ui'
});

Error Handling ​

If the before-close callback returns false, the Promise returned by close() is rejected. Catch it in application code as needed.

javascript
try {
    await pdfui.close(function() {
        return false;
    });
} catch (error) {
    console.log('Document close was cancelled', error);
}

Notes ​

  • Do not continue using old PDFDocRender or PDFPageRender objects during or after the close flow.
  • If annotations, forms, or document edits must be saved before closing, complete that work in closeDocBefore or block the close.
  • Opening a new document triggers the current document close flow; confirmation logic must cover both explicit close and document switch scenarios.