Skip to content
Guides

CPDFReaderView - Props

Open Document

CPDFReaderView is a React component designed to display PDF documents. Below are details about the required document prop.

document

Specifies the path or URI of the PDF document to be displayed.

  • Type: string
  • Required: Yes

**Usage Examples:

  • (Android) For local storage file path:
tsx
<CPDFReaderView
	document={'/storage/emulated/0/Download/PDF_document.pdf'}/>
  • (Android) For content Uri:
tsx
<CPDFReaderView
	document={'content://...'}/>
  • (Android) For assets path:
tsx
<CPDFReaderView
	document={'file:///android_asset/...'}/>
  • (iOS) For app bundle file path:
tsx
<CPDFReaderView	
	document={'pdf_document.pdf'}/>

password

The password to open the document is an optional parameter.

  • Type: string

Usage Examples:

tsx
<CPDFReaderView	
	document={'pdf_document.pdf'}
  password={'password'}/>

configuration

Used to pass configuration parameters when rendering a PDF file to customize UI features and PDF view properties. ComPDFKit provides default configuration parameters through ComPDFKit.getDefaultConfig.

  • Type:string
  • Required: Yes

Usage Examples:

tsx
<CPDFReaderView
  document={samplePDF}
  configuration={ComPDFKit.getDefaultConfig({

  })}
  style={{flex:1}}
  />

Document

open

Reopens a specified document in the current CPDFReaderView component.

Parameters:

NameTypeDescription
documentstringThe file path of the PDF document.
passwordstringThe password for the document, which can be null or empty.

Returns a Promise.

NameTypeDescription
resultboolA promise that resolves to true if the document is successfully opened, otherwise false.
tsx
await pdfReaderRef.current?._pdfDocument.open(document, 'password');

hasChange

Checks whether the document has been modified.

Returns a Promise.

Promise Parameters:

NameTypeDescription
hasChangebooleantrue: The document has been modified,
false: The document has not been modified.
tsx
const hasChange = await pdfReaderRef.current?.hasChange();

save

Save the current document changes.

Returns a Promise.

Promise Parameters:

NameTypeDescription
resultbooleantrue: Save successful,
false: Save failed.
js
const saveResult = await pdfReaderRef.current.save();

onSaveDocument

function, optional

This function will be called when the document is saved.

Parameters:

NameTypeDescription
pageNumberintthe current page number
tsx
<CPDFReaderView
	onSaveDocument={()=>{}}
	/>

getFileName

Gets the file name of the PDF document.

Returns a Promise.

NameTypeDescription
resultstringGets the file name of the PDF document.
tsx
const fileName = await pdfReaderRef.current?._pdfDocument.getFileName();

isImageDoc

Checks if the PDF document is an image document. This is a time-consuming operation that depends on the document size.

Returns a Promise.

NameTypeDescription
resultbooleanReturn true if the document is a scanned image document, otherwise return false.
tsx
const isImageDoc = await pdfReaderRef.current?._pdfDocument.isImageDoc();

getDocumentPath

Retrieves the path of the current document.

Returns a Promise.

NameTypeDescription
pathstringRetrieves the path of the current document.
tsx
const documentPath = await pdfReaderRef.current?._pdfDocument.getDocumentPath();

Viewer

setMargins

Set the current PDF view margin.

Parameters:

NameTypeDescription
leftintmargin left
topintmargin top
rightintmargin right
bottomintmargin bottom
tsx
await pdfReaderRef.current?.setMargins(10,10,10,10);

setPageSpacing

Sets the spacing between pages. This method is supported only on the Android platform.

  • For the iOS platform, use the setMarginsmethod instead.The spacing between pages is equal to the value of CPDFEdgeInsets.top.

Parameters:

NameTypeDescription
pageSpacingintThe space between pages, in pixels.
tsx
await pdfReaderRef.current?.setPageSpacing(10);

Note: This method only supports the Android platform.

setScale

Set the page scale, Value Range: 1.0~5.0

Parameters:

NameTypeDescription
scalenumberscale value
tsx
await pdfReaderRef.current?.setScale(2.0);

getScale

Get the current page scale

Returns a Promise.

NameTypeDescription
resultnumberReturns the zoom ratio of the current page.
tsx
const scale = await pdfReaderRef.current?.getScale();

setCanScale

Whether allow to scale.

Parameters:

NameTypeDescription
canScalebooleanscale value
tsx
await pdfReaderRef.current?.setCanScale(false);

Note: This method only supports the Android platform.

setReadBackgroundColor

Sets background color of reader.

Parameters:

NameTypeDescription
themeCPDFThemes
tsx
await pdfReaderRef.current?.setReadBackgroundColor(CPDFThemes.LIGHT);

Explanation of Themes

ModeDescriptionOption Values
LIGHTUses a white background and black text, suitable for reading in well-lit environments.CPDFThemes.LIGHT
DARKUses a dark background and light text, suitable for reading in low-light environments.CPDFThemes.DARK
SEPIAUse a beige background for users who are used to reading on paper.CPDFThemes.SEPIA
RESEDASoft light green background reduces discomfort from high brightness and strong contrast when reading, effectively relieving visual fatigue.CPDFThemes.RESEDA

getReadBackgroundColor

Get background color of reader.

Returns a Promise.

NameTypeDescription
resultCPDFThemes
tsx
CPDFThemes theme = await pdfReaderRef.current?.getReadBackgroundColor();

setFormFieldHighlight

Sets whether to display highlight Form Field.

Parameters:

NameTypeDescription
isFormFieldHighlightbooleantrue to display highlight Form Field.
tsx
await pdfReaderRef.current?.setFormFieldHighlight(true);

isFormFieldHighlight

Whether to display highlight Form Field.

Returns a Promise.

NameTypeDescription
resultbooleanWhether the form field is currently highlighted
tsx
const isFormFieldHighlight = await pdfReaderRef.current?.isFormFieldHighlight();

setLinkHighlight

Sets whether to display highlight Link.

Parameters:

NameTypeDescription
isLinkHighlightbooleanWhether to highlight Link.
tsx
await pdfReaderRef.current?.setLinkHighlight(true);

isLinkHighlight

Whether to display highlight Link.

Returns a Promise.

NameTypeDescription
resultboolean
tsx
const isLinkHighlight = await pdfReaderRef.current?.isLinkHighlight();

setVerticalMode

Sets whether it is vertical scroll mode.

Parameters:

NameTypeDescription
isVerticalModebooleanWhether it is vertical scroll mode.
tsx
await pdfReaderRef.current?.setVerticalMode(true);

isVerticalMode

Whether it is vertical scroll mode.

Returns a Promise.

NameTypeDescription
resultbooleanReturns true for vertical scrolling and false for horizontal scrolling
tsx
await pdfReaderRef.current?.isVerticalMode();

setContinueMode

Sets whether it is continuous scroll mode.

Parameters:

NameTypeDescription
isContinueModebooleanWhether it is continuous scroll mode.
tsx
await pdfReaderRef.current?.setContinueMode(true);

isContinueMode

Whether it is continuous scroll mode.

Returns a Promise.

NameTypeDescription
resultbooleanReturns true if the page is scrolled continuously, otherwise returns false
tsx
await pdfReaderRef.current?.isContinueMode();

setDoublePageMode

Sets whether it is double page mode.

Parameters:

NameTypeDescription
isDoublePageModebooleanWhether it is double page mode.
tsx
await pdfReaderRef.current?.setDoublePageMode(true);

isDoublePageMode

Whether it is double page mode.

Returns a Promise.

NameTypeDescription
resultbooleanReturns true if double page display is enabled, otherwise returns false
tsx
await pdfReaderRef.current?.isDoublePageMode();
tsx
await pdfReaderRef.current?.isContinueMode();

setCoverPageMode

Sets whether it is cover page mode.

Parameters:

NameTypeDescription
isCoverPageModebooleanWhether to display the document in cover form
tsx
await pdfReaderRef.current?.setCoverPageMode(true);

isCoverPageMode

Whether it is cover page mode.

Returns a Promise.

NameTypeDescription
resultbooleanReturns true if the document cover is displayed, otherwise returns false
tsx
await pdfReaderRef.current?.isCoverPageMode();

setCropMode

Sets whether it is crop mode.

Parameters:

NameTypeDescription
isCropModebooleanWhether it is crop mode.
tsx
await pdfReaderRef.current?.setCropMode(true);

isCropMode

Whether it is cover page mode.

Returns a Promise.

NameTypeDescription
resultbooleanReturns true if the current mode is clipping mode, otherwise returns false
tsx
await pdfReaderRef.current?.isCropMode();

setPageSameWidth

In the single page mode, set whether all pages keep the same width and the original page keeps the same width as readerView.

Parameters:

NameTypeDescription
isPageSameWidthbooleantrue: All pages keep the same width, the original state keeps the same width as readerView; false: Show in the actual width of page
tsx
await pdfReaderRef.current?.setPageSameWidth(true);

Note: This method only supports the Android platform.

isPageInScreen

Gets whether the specified pageIndex is displayed on the screen

Parameters:

NameTypeDescription
pageIndexintpage index
tsx
const isPageInScreen = await pdfReaderRef.current?.isPageInScreen(1);

Note: This method only supports the Android platform.

setFixedScroll

Sets whether to fix the position of the non-swipe direction when zooming in for reading.

Parameters:

NameTypeDescription
isFixedScrollbooleanWhether to fix scrolling
tsx
await pdfReaderRef.current?.setFixedScroll(true);

Note: This method only supports the Android platform.

setPreviewMode

Switch the mode displayed by the current CPDFReaderWidget.

Parameters:

NameTypeDescription
viewModeCPDFViewModeThe view mode to display
tsx
await pdfReaderRef.current?.setPreviewMode(CPDFViewMode.VIEWER);

getPreviewMode

Get the currently displayed mode.

Returns a Promise.

NameTypeDescription
viewModeCPDFViewModeReturns the currently displayed view mode
tsx
const mode = await pdfReaderRef.current?.getPreviewMode();

showThumbnailView

Displays the thumbnail view. When [editMode] is true, the page enters edit mode, allowing operations such as insert, delete, extract, etc.

Returns a Promise.

NameTypeDescription
editModebooleanWhether to enable edit mode
tsx
await pdfReaderRef.current?.showThumbnailView(true);

showBotaView

Displays the BOTA view, which includes the document outline, bookmarks, and annotation list.

tsx
await pdfReaderRef.current?.showBotaView();

showAddWatermarkView

Displays the BOTA view, which includes the document outline, bookmarks, and annotation list.

tsx
await pdfReaderRef.current?.showAddWatermarkView();

showSecurityView

Displays the document security settings view, allowing users to configure document security options.

tsx
await pdfReaderRef.current?.showSecurityView();

showDisplaySettingView

Displays the display settings view, where users can configure options such as scroll direction, scroll mode, and themes.

tsx
await pdfReaderRef.current?.showDisplaySettingView();

enterSnipMode

Enters snip mode, allowing users to capture screenshots.

tsx
await pdfReaderRef.current?.enterSnipMode();

exitSnipMode

Enters snip mode, allowing users to capture screenshots.

tsx
await pdfReaderRef.current?.exitSnipMode();

printDocument

Invokes the system's print service to print the current document.

tsx
await pdfReaderRef.current?._pdfDocument.printDocument();

Page

setDisplayPageIndex

Jump to the index page.

Parameters:

NameTypeDescription
pageIndexintJump to page number
tsx
await pdfReaderRef.current?.setDisplayPageIndex(1);

getCurrentPageIndex

get current page index.

Returns a Promise.

Promise Parameters:

NameTypeDescription
pageIndexintReturns the page index of the current document display
tsx
const pageIndex = await pdfReaderRef.current?.getCurrentPageIndex();

onPageChanged

function, optional

This function is called when the page number has been changed.

Parameters:

NameTypeDescription
pageNumberintthe current page number
tsx
<CPDFReaderView
	onPageChanged={(pageIndex:number)=>{
	}}
  />

getPageCount

Get the total number of pages in the current document

Returns a Promise.

Promise Parameters:

NameTypeDescription
countintGet the total number of pages in the current document
tsx
const pageCount = await pdfReaderRef.current?._pdfDocument.getPageCount();

importDocument

Imports another PDF document and inserts it at a specified position in the current document.

Parameters:

NameTypeDescription
filePathstringThe path of the PDF document to import. Must be a valid, accessible path on the device.
pagesArray[number]The collection of pages to import, represented as an array of integers. If null or an empty array is passed, the entire document will be imported.
insertPositionnumberThe position to insert the external document into the current document. This value must be provided. If not specified, the document will be inserted at the end of the current document.
passwordstringThe password for the document, if it is encrypted. If the document is not encrypted, an empty string '' can be passed.

Returns a Promise.

NameTypeDescription
resultboolReturns a Promise<boolean> indicating whether the document import was successful.
- true indicates success
- false or an error indicates failure
tsx
// Define the file path of the document to import
// For local files (e.g., from app cache):
const filePath = '/data/user/0/com.compdfkit.flutter.example/cache/temp/PDF_Document.pdf';
// For Android content URIs (e.g., from media storage):
const filePath = 'content://media/external/file/1000045118';

// Specify the pages to import. An empty array [] imports all pages.
// In this example, only the first page (index 0) is imported.
const pages = [0]; 

// Define the position to insert the imported pages.
// 0 means inserting at the beginning of the document.
const insertPosition = 0; 

// Provide the document password if encrypted. Leave empty if not required.
const password = '';

// Import the document into the PDF reader.
const importResult = await pdfReaderRef.current?._pdfDocument.importDocument(
  filePath, 
  pages, 
  insertPosition, 
  password
);

splitDocumentPages

Splits the specified pages from the current document and saves them as a new document.

This function extracts the given pages from the current PDF document and saves them as a new document at the provided save path.

Parameters:

NameTypeDescription
savePathstringThe path where the new document will be saved.
pagesArray[number]Pages to extract from the current document.

Returns a Promise.

NameTypeDescription
resultboolA Promise that resolves to true if the operation is successful, or false if it fails.
tsx
const savePath = '/data/user/0/com.compdfkit.flutter.example/cache/temp/PDF_Document.pdf';
// Pages to extract from the current document
const pages = [0, 1, 2]; 
const result = await pdfReaderRef.current?.splitDocumentPages(savePath, pages);

Annotations

import Annotations

Imports annotations from the specified XFDF file into the current PDF document.

Parameters:

NameTypeDescription
xfdfFilestringPath of the XFDF file to be imported.
The Android platform supports the following paths:
- assets file:'file:///android_assets/test.xfdf'
- file path: '/data/xxx.xfdf'
- Uri: 'content://xxxx'

Returns a Promise.

Promise Parameters:

NameTypeDescription
resultbooleantrue: import successful,
false: import failed.
tsx
const result = await pdfReaderRef.current.importAnnotations('xxx.xfdf');

export Annotations

Exports annotations from the current PDF document to an XFDF file.

Returns a Promise.

Promise Parameters:

NameTypeDescription
xfdfPathstringThe path of the XFDF file if export is successful; an empty string if the export fails.
tsx
const exportXfdfFilePath = await pdfReaderRef.current?.exportAnnotations();

removeAllAnnotations

Delete all comments in the current document.

Returns a Promise.

Promise Parameters:

NameTypeDescription
resultbooleantrue、false
tsx
const removeResult = await pdfReaderRef.current?.removeAllAnnotations();

getAnnotations

Retrieves all annotations on the current page.

This method fetches all annotations present on the current page of the PDF document and returns a list of corresponding CPDFAnnotation instances.

Promise Parameters:

NameTypeDescription
annotationsCPDFAnnotation[]A promise that resolves with all annotations on the current page, or an empty array if retrieval fails.
tsx
// Page index, where 0 represents the first page
const pageIndex = 0;

// Retrieve the page object from the document
const page = pdfReaderRef?.current?._pdfDocument.pageAtIndex(pageIndex);

// Fetch all annotations on the specified page
const annotations = await page?.getAnnotations();

flattenAllPages

Flatten all pages of the current document.

Parameters:

NameTypeDescription
savePathstringThe path to save the flattened document. On Android, you can pass a Uri.
fontSubsetbooleanWhether to include the font subset when saving.

Returns a Promise.

NameTypeDescription
resultbooleanReturns 'true' if the flattened document is saved successfully, otherwise 'false'.
tsx
const savePath = 'file:///storage/emulated/0/Download/flatten.pdf';
// or use Uri on the Android Platform.
const savePath = await ComPDFKit.createUri('flatten_test.pdf', 'compdfkit', 'application/pdf');
const fontSubset = true;
const result = await pdfReaderRef.current?._pdfDocument.flattenAllPages(savePath, fontSubset);

Forms

importWidgets

Imports the form data from the specified XFDF file into the current PDF document.

The API only imports form data and modifies the form content through the corresponding form name.

Parameters:

NameTypeDescription
xfdfFilestringPath of the XFDF file to be imported.The Android platform supports the following paths:
- assets file:'file:///android_assets/test.xfdf'
- file path: '/data/xxx.xfdf'
- Uri: 'content://xxxx'

Returns a Promise.

Promise Parameters:

NameTypeDescription
resultbooleantrue: import successful,``false: import failed.
tsx
const result = await pdfReaderRef.current.importWidgets('xxx.xfdf');

exportWidgets

exports the form data from the current PDF document to an XFDF file.

Returns a Promise.

Promise Parameters:

NameTypeDescription
xfdfPathstringThe path of the XFDF file if export is successful; an empty string if the export fails.
tsx
const exportXfdfFilePath = await pdfReaderRef.current?.exportWidgets();

getWidgets

Retrieves all form widgets on the current page.

This method fetches all form widgets present on the current page of the PDF document and returns a list of corresponding CPDFWidget instances.

Returns a Promise.

Promise Parameters:

NameTypeDescription
widgetsCPDFWidget[]true: import successful,``false: import failed.
tsx
const pageIndex = 0;
const page = pdfReaderRef?.current?._pdfDocument.pageAtIndex(pageIndex);
const widgets = await page?.getWidgets();

Related Widgets

ClassDescription
CPDFWidgetBase class for all form widgets
CPDFTextWidgetText input field widget
CPDFSignatureWidgetSignature widget
CPDFRadiobuttonWidgetRadio button widget
CPDFPushbuttonWidgetButton widget
CPDFListboxWidgetList box widget
CPDFCheckboxWidgetCheckbox widget
CPDFComboboxWidgetCombo box widget

Fill Form Fields

ComPDFKit supports programmatically filling form fields in a PDF document.

The steps to fill in form fields using code are as follows:

  1. Get the page object of the form to be filled in from CPDFDocument.

  2. Retrieve all forms from the page object.

  3. Traverse all forms to find the one to be filled in.

  4. Modify the form field contents as needed.

This example shows how to fill in form fields:

tsx
const pageIndex = 0;
// Retrieve the page object of the first page
const cpdfPage: CPDFPage = pdfReaderRef?.current?._pdfDocument.pageAtIndex(pageIndex);

// Retrieve all form widgets on the current page
const widgets = await page?.getWidgets();

// Fill in the text field content
// Assume that there is a text field form on the current page and retrieve the CPDFTextWidget object
const textWidget = widgets[0] as CPDFTextWidget;
// Set the text field content to "Hello World"
await textWidget.setText('Hello World');
// Refresh the appearance of the form to apply changes, this step is necessary
await textWidget.updateAp();

// Modify the radio button's checked state
const radioButtonWidget = widgets[0] as CPDFRadiobuttonWidget;
// Set the radio button to checked
await radioButtonWidget.setChecked(true);
// Refresh the appearance of the radio button
await radioButtonWidget.updateAp();

// Modify the checkbox's checked state
const checkboxWidget = widgets[0] as CPDFCheckboxWidget;
// Set the checkbox to checked
await checkboxWidget.setChecked(true);
// Refresh the appearance of the checkbox
await checkboxWidget.updateAp();

// Add an electronic signature to the signature form
const signatureWidget = widgets[0] as CPDFSignatureWidget;
// Android-supported URI format:
await signatureWidget.addImageSignature('content://media/external/images/media/123');
// Or file path:
await signatureWidget.addImageSignature('/path/to/image');
// Refresh the appearance of the signature form
await signatureWidget.updateAp();

Security

isEncrypted

Checks if the PDF document is encrypted.

Returns a Promise.

NameTypeDescription
resultboolean
tsx
const isEncrypted = await pdfReaderRef.current?._pdfDocument.isEncrypted();

getPermissions

Gets the current document's permissions.

There are three types of permissions:

No restrictions: CPDFDocumentPermissions.NONEIf the document has an open password and an owner password,using the open password will grant CPDFDocumentPermissions.USER permissions, and using the owner password will grant CPDFDocumentPermissions.OWNER permissions.

Returns a Promise.

NameTypeDescription
resultCPDFDocumentPermissionsGets the current document's permissions.
tsx
const permissions = await pdfReaderRef.current?._pdfDocument.getPermissions();

checkOwnerUnlocked

Check if owner permissions are unlocked

Returns a Promise.

NameTypeDescription
resultboolean
tsx
const unlocked = await pdfReaderRef.current?._pdfDocument.checkOwnerUnlocked();

checkOwnerPassword

Whether the owner password is correct. If the password is correct, the document will be unlocked with full owner permissions.

Parameters:

NameTypeDescription
passwordstringpassword The owner password to be verified.

Returns a Promise.

NameTypeDescription
resultbooleanA promise that resolves to true if the owner password is correct, otherwise false.
tsx
const check = await pdfReaderRef.current?._pdfDocument.checkOwnerPassword('ownerPassword');

setPassword

This method sets the document password, including the user password for access restrictions and the owner password for granting permissions.

  • To enable permissions like printing or copying, the owner password must be set; otherwise, the settings will not take effect.

Parameters:

NameTypeDescription
userPasswordstringThe user password for document access restrictions.
ownerPasswordstringThe owner password to grant permissions (e.g., printing, copying).
allowsPrintingbooleanWhether printing is allowed (true or false).
allowsCopyingbooleanWhether copying is allowed (true or false).
encryptAlgoCPDFDocumentEncryptAlgoThe encryption algorithm to use (e.g., CPDFDocumentEncryptAlgo.rc4).

Returns a Promise.

NameTypeDescription
resultbooleanA promise that resolves to true if the password is successfully set, otherwise false.
tsx
const success = await pdfReaderRef.current?._pdfDocument.setPassword(
  'user_password',
  'owner_password',
  false,
  false,
  CPDFDocumentEncryptAlgo.rc4
);

removePassword

Remove the user password and owner permission password. set in the document, and perform an incremental save.

Returns a Promise.

NameTypeDescription
resultbooleanReturns true if password removal is successful, otherwise false.
tsx
const check = await pdfReaderRef.current?._pdfDocument.checkOwnerPassword('ownerPassword');

getEncryptAlgo

Get the encryption algorithm of the current document

Returns a Promise.

NameTypeDescription
resultCPDFDocumentEncryptAlgoEncryption Type
tsx
const encryptAlgo = await pdfReaderRef.current?._pdfDocument.getEncryptAlgo();