Guides
插入页面
插入空白页面
以下是插入空白页面的示例代码:
tsx
const pdfReaderRef = useRef<CPDFReaderView>(null);
<CPDFReaderView
ref={pdfReaderRef}
document={samplePDF}
configuration={ComPDFKit.getDefaultConfig({
})}/>
const insertResult = await pdfReaderRef.current?._pdfDocument.insertBlankPage(1, CPDFPageSize.a4);从其他 PDF 插入页面
以下示例演示如何从其他 PDF 插入页面:
tsx
const pdfReaderRef = useRef<CPDFReaderView>(null);
<CPDFReaderView
ref={pdfReaderRef}
document={samplePDF}
configuration={ComPDFKit.getDefaultConfig({})}
/>
// 定义要导入的文档的文件路径
// 对于本地文件(例如来自应用缓存):
const filePath = '/data/user/0/com.compdfkit.flutter.example/cache/temp/PDF_Document.pdf';
// 对于 Android 内容 URI(例如来自媒体存储):
const filePath = 'content://media/external/file/1000045118';
// 指定要导入的页面。空数组 [] 表示导入所有页面。
// 在此示例中,仅导入第一页(索引 0)。
const pages = [0];
// 定义插入位置。
// 0 表示将页面插入到文档的开头。
const insertPosition = 0;
// 如果文档被加密,则提供文档密码。如果不需要密码,请留空。
const password = '';
// 将文档导入到 PDF 阅读器中。
const importResult = await pdfReaderRef.current?._pdfDocument.importDocument(
filePath,
pages,
insertPosition,
password
);插入图片页
SDK支持将图片作为一页PDF页面插入, 以下是以不同的方式插入图片页:
- Android 文件路径
tsx
const imagePath = 'file:///storage/emulated/0/Download/photo.jpg';
const success = await pdfReaderRef.current?._pdfDocument.insertImagePage(2, imagePath, CPDFPageSize.custom(imageWidth, imageHeight));
if (success) {
await pdfReaderRef.current?.reloadPages2();
}- Android content URI
tsx
const imagePath = 'content://media/external/images/media/12345';
const success = await pdfReaderRef.current?._pdfDocument.insertImagePage(2, imagePath, CPDFPageSize.custom(imageWidth, imageHeight));
if (success) {
await pdfReaderRef.current?.reloadPages2();
}- Android asset path:
tsx
const imagePath = 'file:///assets/photo.jpg';
const success = await pdfReaderRef.current?._pdfDocument.insertImagePage(2, imagePath, CPDFPageSize.custom(imageWidth, imageHeight));
if (success) {
await pdfReaderRef.current?.reloadPages2();
}- iOS 文件路径
tsx
const imagePath = 'var/mobile/Containers/Data/Application/.../Documents/photo.jpg';
const success = await pdfReaderRef.current?._pdfDocument.insertImagePage(2, imagePath, CPDFPageSize.custom(imageWidth, imageHeight));
if (success) {
await pdfReaderRef.current?.reloadPages2();
}