Resource Base
Table of contents

PerspectiveViewer Class

Perspective Viewer is used to do page boundaries manual adjustment & perspective transformation. The page data displayed on this viewer is its original data.

API Index

Create and Destroy Instances

API Name Description
PerspectiveViewer() Default constructor of a PerspectiveViewer instance.
destroy() Destroy the PerspectiveViewer instance.

Viewer Control

API Name Description
bindContainer() Bind the viewer to the specified container.
unbindContainer() Unbind the viewer from the specified container.
isBoundContainer Return whether the viewer is bound to a container.
getStyle() Get the style object of PerspectiveViewer.
updateStyle() Update the style object of PerspectiveViewer.
getUiConfig() Get current UiConfig object.
updateUiConfig() Update UiConfig object.
show() Show the viewer.
hide() Hide the viewer.
isVisible Return whether the viewer is shown or hidden.

Document and Page Control

API Name Description
openDocument() Open the specified document by document uid.
closeDocument() Close current document.
currentDocument Return the object of the current document.
getPageCount() Get the page count in the viewer.
goToPage() Navigate to the specified page by index.
getCurrentPageIndex() Get the index of current page.
getCurrentPageUid() Get the uid of the current page.
indexToUid() Get the uid of the specified page by its index.
uidToIndex() Get the index of the specified page by its uid.

Perspective Transformation

API Name Description
setQuadSelection() Set a quadrilateral selection on the current page.
getQuadSelection() Get the quadrilateral selection.
resetQuadSelection() Reset the quadrilateral selection to the original one.
applyPerspective() Performs a perspective transformation in current page based on the specified quadrangle.

Edit Operations

API Name Description
rotate() Rotate the specified pages.
saveOperations() Save the edit operations in pages to document.

Events

API Name Description
on() Bind a listener to the specified event.
off() Unbind event listener(s) from the specified event.

Integrated Events

Event Name Description
resized Triggered when the viewer is resized.
pageRendered Triggered when a page has been completely rendered.
currentIndexChanged Triggered when currentIndex is changed.
currentPageChanged Triggered when current page is changed.
quadModifiedModified Triggered when the quadrilateral selection is modified.
click Triggered when click in the viewer’s viewing area.
dblclick Triggered when double click in the viewer’s viewing area.
rightclick Triggered when right click in the viewer’s viewing area.

Create and Destroy Instances

PerspectiveViewer()

Default constructor of a PerspectiveViewer instance.

Syntax

new Dynamsoft.DDV.PerspectiveViewer(options?: PerspectiveViewerConstructorOptions);

Parameters

options: The constructor options for a PerspectiveViewer instance. Please refer to PerspectiveViewerConstructorOptions.

Code Snippet

const perspectiveViewer = new Dynamsoft.DDV.PerspectiveViewer({
    container: document.getElementById("viewer"),
});

Exception

Error Code Error Message
-80100 XXX(API): XXX(ParameterName) is invalid.
-80001 License string is invalid.
-80002 XXX(LicenseModuleName) module license has expired.
-80003 XXX(LicenseModuleName) module license is missing.
-80004 XXX(LicenseModuleName) module license version does not match.
-80005 Domain does not match the domain bound to the XXX(LicenseModuleName) module license.
-80050 DDV.Core.init() has not been set up yet.
-80051 DDV.Core.init() has not been completed.

destroy()

Destroy the PerspectiveViewer instance.

Syntax

destroy(): void;

Code Snippet

perspectiveViewer.destroy();

Remark

  • The editing operations (rotating) in pages will be saved to document automatically when destroy the viewer instance.

See Also

saveOperations

Viewer Control

bindContainer()

Bind the viewer to the specified container.

Syntax

bindContainer(container: string | HTMLElement): void;

Parameters

container: The container which is used to show the viewer. Its id or HTMLElement is acceppted.

Code Snippet

// Assume there is a container with id "viewercontainer" on the page.
perspectiveViewer.bindContainer("viewercontainer");

Exception

Error Code Error Message
-80100 XXX(API): XXX(ParameterName) is invalid.
-80102 XXX(API): XXX(ParameterName) is missing.
-80301 The specified container does not exist.

Remark

  • A viewer can only be bound to one container at once. If you bind the viewer to another container when it has been bound to a container, the viewer will be bound to the new container and unbound from the old container automatically.

unbindContainer()

Unbind the viewer from the specified container.

Syntax

unbindContainer(): void;

Code Snippet

perspectiveViewer.unbindContainer();

isBoundContainer

Return whether the viewer is bound to a container.

Syntax

readonly isBoundContainer: boolean;

getStyle()

Get the style object of PerspectiveViewer.

Syntax

getStyle(perspectiveViewerStyleName: PerspectiveViewerStyleName): PerspectiveViewerStyle | null;

Parameters

perspectiveViewerStyleName: A PerspectiveViewerStyleName can be one of three types.

type PerspectiveViewerStyleName = "canvasStyle" | "pageStyle" | "quadSelectionStyle";

Return values

The style object. Please refer to Style Interfaces..

Code Snippet

// Get pageStyle object;
const pageStyle = perspectiveViewer.getStyle("pageStyle");

Warning

Error Code Error Message API Return Value
-80100 XXX(API): XXX(ParameterName) is invalid. null
-80102 XXX(API): XXX(ParameterName) is missing. null
-80103 XXX(API): The value for XXX(ParameterName) is not supported. null

updateStyle()

Update the style object of PerspectiveViewer.

Syntax

updateStyle(perspectiveViewerStyleName: PerspectiveViewerStyleName, perspectiveViewerStyle: PerspectiveViewerStyle): boolean;

Parameters

perspectiveViewerStyleName: A PerspectiveViewerStyleName can be one of three types.

type PerspectiveViewerStyleName = "canvasStyle" | "pageStyle" | "quadSelectionStyle";

perspectiveViewerStyle: The style object. Please refer to Style Interfaces.

Return Value

true: Successfully.

false: Failed.

Code Snippet

  • First method

      // Get style object
      const pageStyle = perspectiveViewer.getStyle("pageStyle");
    
      // Modify the style object
      pageStyle.background = "red";
      pageStyle.border = "1px solid green";
    
      // Update page style
      perspectiveViewer.updateStyle("pageStyle", pageStyle);
    
  • Second method

      // Update the style object directly
      perspectiveViewer.updateStyle("pageStyle", {
          background: "red",
          border: "1px solid green",
      });
    

Warning

Error Code Error Message API Return Value
-80100 XXX(API): XXX(ParameterName) is invalid. false
-80102 XXX(API): XXX(ParameterName) is missing. false
-80103 XXX(API): The value for XXX(ParameterName) is not supported. false

Remark

  • The updates are independent of whether the viewer is displayed and are updated in real time.

getUiConfig()

Get current UiConfig object.

Syntax

getUiConfig(): UiConfig;

Return Value

The UiConfig object.

Code Snippet

const viewerUi = perspectiveViewer.getUiConfig();

updateUiConfig()

Update UiConfig object.

Syntax

updateUiConfig(uiConfig: UiConfig): boolean;

Parameters

uiConfig: The UiConfig to update.

Return Value

true: Successfully.

false: Failed.

Code Snippet

const viewerUi = Dynamsoft.DDV.getDefaultUiConfig("perspectiveViewer");
const header = viewerUi.children[0];
header.children.splice(0,0,Dynamsoft.DDV.Elements.Load); //Add Load element in header.
perspectiveViewer.updateUiConfig(viewerUi);

Warning

Error Code Error Message API Return Value
-80100 XXX(API): XXX(ParameterName) is invalid. false
-80102 XXX(API): XXX(ParameterName) is missing. false
-80313 The element XXX(ElementName) is not supported in XXX(ClassName) class. false

Remark

  • The updates are independent of whether the viewer is displayed and are updated in real time.

show()

Show the viewer.

Syntax

show(): void;

Code Snippet

perspectiveViewer.show();

Remark

  • The viewer is shown automatically when it is created.

hide()

Hide the viewer.

Syntax

hide(): void;

Code Snippet

perspectiveViewer.hide();

isVisible

Return whether the viewer is shown or hidden.

Syntax

readonly isVisible: boolean;

Remark

  • The viewer is shown automatically when it is created which means the default value of isVisible is true.

Document and Page Control

openDocument()

Open the specified document.

Syntax

openDocument(docUid: string | doc: IDocument): void;

Parameters

docUid: The uid of the specified document.

doc: The object of the document to open. Please refer to IDocument.

Code Snippet

// Assume there is a document whose id is "lnn0ll9o124".
perspectiveViewer.openDocument("lnn0ll9o124");

// OR
// Assume there is a document object firstDoc.
const docUid = firstDoc.uid;
perspectiveViewer.openDocument(docUid);
perspectiveViewer.openDocument(firstDoc);

Exception

Error Code Error Message
-80100 XXX(API): docUid or doc is invalid.
-80102 XXX(API): docUid or doc is missing.
-80104 XXX(API): The specified document(s) do not exist.

Remark

  • If another ducument is opened when there is a document already opened, the opened document will be closed automatically.

closeDocument()

Close current document.

Syntax

closeDocument(): boolean; 

Return Value

true: Successfully.

false: Failed.

Code Snippet

perspectiveViewer.closeDocument();

Warning

Error Code Error Message API Return Value
-80304 No document opened. false

currentDocument

Return the object of the current document.

Syntax

readonly currentDocument: IDocument | null;

Code Snippet

const currentDoc = perspectiveViewer.currentDocument;

See Also

IDocument

getPageCount()

Get the page count in the viewer.

Syntax

getPageCount(): number;

Return Value

The page count.

Code Snippet

const pageCount = perspectiveViewer.getPageCount();

Warning

Error Code Error Message API Return Value
-80304 No document opened. -1

goToPage()

Navigate to the specified page by index.

Syntax

goToPage(index: number): number;

Parameters

index: The index of the page which need to navigate to.

Return Value

The index of the page which navigate to.

Code Snippet

// Navigate to page 4.
perspectiveViewer.goToPage(3);

Warning

Error Code Error Message API Return Value
-80100 XXX(API): XXX(ParameterName) is invalid. -1
-80101 XXX(API): XXX(ParameterName) is out of range. -1
-80102 XXX(API): XXX(ParameterName) is missing. -1
-80304 No document opened. -1
-80305 There is no image in the current document. -1

getCurrentPageIndex()

Get the index of the current page.

Syntax

getCurrentPageIndex(): number; 

Return Value

The index of the current page.

Code Snippet

const currentIndex = perspectiveViewer.getCurrentPageIndex();

Warning

Error Code Error Message API Return Value
-80304 No document opened. -1
-80305 There is no image in the current document. -1

getCurrentPageUid()

Get the uid of the current page.

Syntax

getCurrentPageUid(): string;

Return Value

The uid of the current page.

Code Snippet

const curPageUid = perspectiveViewer.getCurrentPageUid();

Warning

Error Code Error Message API Return Value
-80304 No document opened. ''
-80305 There is no image in the current document. ''

indexToUid()

Get the uid of the specified page by its index.

Syntax

indexToUid(index: number): string;

Parameters

index: The index of the specified page.

Return Value

The uid of the page.

Code Snippet

// Get the first page's uid
const firstPageUid = perspectiveViewer.indexToUid(0);

Warning

Error Code Error Message API Return Value
-80100 XXX(API): XXX(ParameterName) is invalid. ''
-80101 XXX(API): XXX(ParameterName) is out of range. ''
-80102 XXX(API): XXX(ParameterName) is missing. ''
-80304 No document opened. ''
-80305 There is no image in the current document. ''

uidToIndex()

Get the index of the specified page by its uid.

Syntax

uidToIndex(pageUid: string): number;

Parameters

pageUid: The uid of the specified page.

Return Value

The index of the page.

Code Snippet

const curPageUid = perspectiveViewer.getCurrentPageUid();
perspectiveViewer.uidToIndex(curPageUid);

Warning

Error Code Error Message API Return Value
-80100 XXX(API): XXX(ParameterName) is invalid. -1
-80102 XXX(API): XXX(ParameterName) is missing. -1
-80105 XXX(API): The specified page(s) do not exist. -1
-80304 No document opened. -1
-80305 There is no image in the current document. -1

Perspective Transformation

setQuadSelection()

Set a quadrilateral selection on the current page.

Syntax

setQuadSelection(quad: Quad): boolean;

Parameters

quad: A quadrangle. Please refer to Quad.

Example

const quad = [
	[80,200],
	[180,120],
	[115,340],
	[70,330]
];

perspectiveViewer.setQuadSelection(quad);

Warning

Error Code Error Message API Return Value
-80100 XXX(API): XXX(ParameterName) is invalid. false
-80102 XXX(API): XXX(ParameterName) is missing. false
-80304 No document opened. false
-80305 There is no image in the current document. false
-80312 The specified quad exceeds the bounds of the current page. false

Remark

  • In the viewer, only one quadrilateral selection can exist on the page at a time, which means if there is a quadrilateral selection existed when a new selection is drawn, the old one will be clear automatically.

getQuadSelection()

Get the quadrilateral selection.

Syntax

getQuadSelection(): Quad | null;

Return value

A quadrangle. Please refer to Quad.

Example

const quad = perspectiveViewer.getQuadSelection();

Warning

Error Code Error Message API Return Value
-80304 No document opened. null
-80305 There is no image in the current document. null

resetQuadSelection()

Reset the quadrilateral selection to the original one. The original selection means the last saved one.

Syntax

resetQuadSelection(indices?: number[]): boolean;

Parameters

indices: The array of the pages indices which will be reset. If not set, the current page will be reset.

Return Value

true: Successfully.

false: Failed.

Example

perspectiveViewer.resetQuadSelection();

Warning

Error Code Error Message API Return Value
-80100 XXX(API): XXX(ParameterName) is invalid. false
-80304 No document opened. false
-80305 There is no image in the current document. false

applyPerspective()

Performs a perspective transformation in current page based on the specified quadrangle.

Syntax

applyPerspective(quad: Quad): Promise<Blob>;

Parameters

quad: The quadrangle to be transferred. Please refer to Quad.

Return value

The Blob of the result image after perspective transformation.

Example

const quad = perspectiveViewer.getQuadSelection();

await perspectiveViewer.applyPerspective(quad);

Promise Exception

Error Code Error Message
-80100 XXX(API): XXX(ParameterName) is invalid.
-80102 XXX(API): XXX(ParameterName) is missing.
-80304 No document opened.
-80305 There is no image in the current document.
-80312 The specified quad exceeds the bounds of the current page.

Edit Operations

rotate()

Rotate the specified pages.

Syntax

rotate(
    angle: number,
    indices?: number[] 
): boolean; 

Parameters

angle: Specify the angle. Only multiples of 90 degrees are supported. Clockwise rotation is positive, counterclockwise rotation is negative.

indices: The array of the pages indices which will be rotated. If not set, the current page will be rotated.

Return Value

true: Successfully.

false: Failed.

Code Snippet

// Rotate the first and second pages 90 degrees clockwise.
perspectiveViewer.rotate(90, [0,1]);

// Rotate current page 90 degrees counterclockwise.
perspectiveViewer.rotate(-90);

Warning

Error Code Error Message API Return Value
-80100 XXX(API): XXX(ParameterName) is invalid. false
-80102 XXX(API): XXX(ParameterName) is missing. false
-80103 XXX(API): The value for XXX(ParameterName) is not supported. false
-80304 No document opened. false
-80305 There is no image in the current document. false

saveOperations()

This method takes effect only for rotate operation.

Save the edit operations in pages to document.

Syntax

saveOperations(): boolean;

Return Value

true: Successfully.

false: Failed.

Code Snippet

perspectiveViewer.saveOperations();

Warning

Error Code Error Message API Return Value
-80304 No document opened. false

Events

on()

Bind a listener to the specified event.

Syntax

on(eventName: EventName, listener:(event:EventObject)=>void): void;

Parameters

eventName: Specify the event name. It can be an integrated event name or a custom event name configured through UiConfig-events.

listener: Specify the listener.

Code Snippet

// Bind a listener to the integrated event resized.
const eventFunc = (e)=>{
    console.log(e);
    console.log(e.oldWidth);
    console.log(e.newWidth);
};

perspectiveViewer.on("resized", eventFunc);

Warning

Error Code Error Message
-80100 XXX(API): XXX(ParameterName) is invalid.
-80102 XXX(API): XXX(ParameterName) is missing.

off()

Unbind event listener(s) from the specified event.

Syntax

off(eventName: EventName, listener?:(event:EventObject)=>void): void;

Parameters

eventName: Specify the event name. It can be an integrated event name or a custom event name configured through UiConfig-events.

listener: Specify the listener. If no listener is specified, unbind all event listeners from the specified event.

Code Snippet

const eventFunc = (e)=>{
    console.log(e);
    console.log(e.oldWidth);
    console.log(e.newWidth);
};

perspectiveViewer.on("resized", eventFunc);

// Unbind the specified event listener.
perspectiveViewer.off("resized", eventFunc);

Warning

Error Code Error Message
-80100 XXX(API): XXX(ParameterName) is invalid.
-80102 XXX(API): XXX(ParameterName) is missing.

Integrated Events

resized

Triggered when the viewer is resized.

Callback

ResizedEvent: An EventObject.

Attributes

oldWidth: The old width of the viewer.

oldHeight: The old height of the viewer.

newWidth: The new width of the viewer.

newHeight: The new height of the viewer.

pageRendered

Triggered when a page has been completely rendered. We only render the pages that are visible on the screen, so this event won’t get fired for every page in the document at once. This event will get called when the user scrolls up and down the document, or when a page is rotated, or anything else that makes it rerender.

Callback

PageRenderedEvent: An EventObject.

Attributes

index: The index of the rendered page.

pageUid: The pageUid of the rendered page.

currentIndexChanged

Triggered when currentIndex is changed.

Callback

CurrentindexChangedEvent: An EventObject.

Attributes

oldIndex: The old current index.

newIndex: The new current index. If there is no index in the viewer, return -1.

currentPageChanged

Triggered when current page is changed.

Callback

CurrentPageChangedEvent: An EventObject.

Attributes

oldPageUid: The uid of the page which is old current index. If the old page is removed, return ''.

newPageUid: The uid of the page which is new current index. If there is no index in the viewer, return ''.

quadModified

Triggered when the quadrilateral selection is modified.

Callback

QuadModifiedEvent: An EventObject.

Attributes

oldQuad: The old quadrangle.

newQuad: The new quadrangle.

Mouse Events

click

Triggered when click in the viewer’s viewing area. On mobile device, triggered when tap in the viewer’s viewing area.

dblclick

Triggered when double click in the viewer’s viewing area.

rightclick

Triggered when right click in the viewer’s viewing area. On mobile device, triggered when long-tap in the viewer’s viewing area.

Callback for mouse events

VPointerEvent: An EventObject.

Attributes

index: The page index.

pageUid: The page uid.

imageX: The relative x-coordinate of the click pointer on the image.

imageY: The relative y-coordinate of the click pointer on the image.

canvasX: The relative x-coordinate of the click pointer on the canvas.

canvasY: The relative x-coordinate of the click pointer on the canvas.

nativeEvent: PointerEvent

This page is compatible for:

Is this page helpful?

YesYes NoNo

In this article:

latest version

  • Latest version(2.0)
  • Version 1.x
    • Version 1.1
Change +