Skip to content

PDF Generation Template Editor

Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.

View on GitHub

Core Bridge ​

instance.Core gives integrators direct access to the Core object model. It is the @compdfkit/core namespace plus three members that belong to this viewer:

javascript
const {
  documentViewer,      // the main DocumentViewer
  annotationManager,   // documentViewer.getAnnotationManager()
  getDocumentViewers,  // () => all DocumentViewer objects of this WebViewer
  Annotations, Tools, Actions, SaveOptions, PageRotation, createDocument,
} = instance.Core;

Role and rules ​

  • instance.Core is a frozen object. The namespace members (Annotations, Tools, Document, …) are the same classes the UI uses.
  • Inside the UI, components and APIs do not call @compdfkit/core directly. They go through the thin wrappers in src/core/, which take the documentViewerKey as the last parameter and hold no business state.
  • Integrators use documentViewer, annotationManager and the document returned by documentViewer.getDocument().

Document viewers ​

documentViewer is the main viewer (key 1). The document comparison feature creates more viewers; getDocumentViewers() returns them all, and instance.UI.getDocumentViewer(key) returns one by key.

javascript
const viewers = instance.Core.getDocumentViewers();
console.log(viewers.length, viewers[0] === instance.Core.documentViewer);

Capability map ​

Document loading and saving ​

instance.UI.loadDocument(src, options) loads a document into the viewer. documentViewer.getDocument() returns the Document; Document.getFileData({ flags }) returns the PDF data. instance.Core.createDocument(src, options) creates a Document without displaying it.

documentViewer.getPageCount(), getCurrentPage(), setCurrentPage(pageNumber), getZoomLevel(), zoomTo(zoom), getViewerElement(), getScrollViewElement(), and getDisplayModeManager().getDisplayMode() for coordinate conversion.

Search and text ​

documentViewer.search(text, mode, options) and clearSearchResults(); getSelectedText() returns the selected text.

Document editing ​

Document.insertBlankPages, insertPages, removePages, rotatePages, movePages, copyPages, extractPages, setPassword.

Annotations ​

annotationManager.addAnnotation(s), deleteAnnotation(s), getAnnotationsList(), selectAnnotation, redrawAnnotation, exportAnnotations(), importAnnotations(xfdf), setCurrentUser(name), enableReadOnlyMode(), setPermissionCheckCallback(fn). Annotation classes are in instance.Core.Annotations.

Tools ​

documentViewer.getTool(toolName), setToolMode(tool), getToolMode(). Tool classes and ToolNames are in instance.Core.Tools.

Forms ​

annotationManager.getFieldManager() returns the FieldManager (fields, widget highlighting).

Content editing ​

documentViewer.getContentEditManager() returns the ContentEditManager (startContentEditMode, endContentEditMode, getContentBoxes, …).

Events ​

documentViewer.addEventListener(name, handler) and annotationManager.addEventListener(name, handler). See Events.

Examples ​

Read the page count and go to the last page ​

javascript
const { documentViewer } = instance.Core;
documentViewer.setCurrentPage(documentViewer.getPageCount());

Export annotations ​

javascript
const xfdfString = await instance.Core.annotationManager.exportAnnotations();
console.log(xfdfString);

Save the document ​

javascript
const { documentViewer, SaveOptions } = instance.Core;
const data = await documentViewer.getDocument().getFileData({ flags: SaveOptions.INCREMENTAL });
console.log(data.byteLength);