Open a Document
ComPDFKit PDF SDK for Flutter offers multiple ways to open PDF documents depending on your use case:
- Use
CPDFReaderWidget
to display an embedded reader UI. - Use
ComPDFKit.openDocument()
to quickly launch a full-screen document viewer. - Use
CPDFDocument
for background processing or non-UI operations.
Using CPDFReaderWidget (Recommended for embedded reader scenarios)
dart
const String _documentPath = 'pdfs/PDF_Document.pdf';
File document = await CPDFFileUtil.extractAsset(_documentPath, shouldOverwrite: false);
Scaffold(
resizeToAvoidBottomInset: false,
appBar: AppBar(),
body: CPDFReaderWidget(
document: document.path,
configuration: CPDFConfiguration(),
onCreated: (controller) {
},))
Using ComPDFKit.openDocument() (Quick full-screen viewer)
This is ideal for quickly launching a standalone document viewer.
dart
const String _documentPath = 'pdfs/PDF_Document.pdf';
File document = await CPDFFileUtil.extractAsset(_documentPath);
ComPDFKit.openDocument(document.path, password: '', configuration: CPDFConfiguration());
Using CPDFDocument (For background or logic-only use cases)
This is useful for non-UI operations such as annotation management, import/export, etc.
dart
const String _documentPath = 'pdfs/PDF_Document.pdf';
File document = await CPDFFileUtil.extractAsset(context, _documentPath);
// Create and open the document
CPDFDocument document = await CPDFDocument.createInstance();
CPDFDocumentError error = await document.open(document.path);
if (error == CPDFDocumentError.success) {
// Perform operations with the CPDFDocument instance...
}