Skip to content

保存文档

手动保存

在使用 CPDFReaderView 组件时,文档会在共享、扁平化或添加水印等操作期间自动保存。此外,您也可以随时手动保存文档。例如:

tsx
const CPDFReaderViewExampleScreen = () => {

  const pdfReaderRef = useRef<CPDFReaderView>(null);

  return (
    <View style={{flex : 1}}>
      <Button title='Save As' onPress={async ()=>{
          const success = await pdfReaderRef.current?.save();
        }}></Button>
      <CPDFReaderView
        ref={pdfReaderRef}
        document={samplePDF}
        configuration={ComPDFKit.getDefaultConfig({})}
        />
    </View>
  );
};

另存为

CPDFReaderView 组件提供了 saveAs 方法,允许用户将当前打开的 PDF 文档保存到指定目录。

示例用法

tsx
// 目标文件路径(Android / iOS)
const savePath = '/data/user/0/com.compdfkit.flutter.example/cache/temp/PDF_Document.pdf';

// Android 支持的 URI 格式路径,例如:
const savePath = 'content://media/external/file/1000045118';

// 配置选项
const removeSecurity = false; // 是否移除 PDF 文档密码
const fontSubset = true; // 是否嵌入 PDF 文件中使用的字体

// 执行保存操作
const result = await pdfReaderRef.current?._pdfDocument.saveAs(savePath, removeSecurity, fontSubset);

保存回调

在创建 CPDFReaderView 时,您可以设置保存监听器来处理保存事件。例如:

tsx
const pdfReaderRef = useRef<CPDFReaderView>(null);

const saveDocument = () => {
    console.log('ComPDFKitRN saveDocument');
}

<CPDFReaderView
    ref={pdfReaderRef}
    document={samplePDF}
    saveDocument={saveDocument}
    configuration={ComPDFKit.getDefaultConfig({
    })}/>