Skip to content
ComPDF
Guides

旋转页面

旋转指定页面的显示方向,支持 90 度顺时针和逆时针旋转。

使用 CPDFReaderWidget

dart
CPDFReaderWidgetController? _controller;
// 初始化 CPDFReaderWidget,并在 onCreated 回调中获取 controller
CPDFReaderWidget(
  document: documentPath,
  configuration: CPDFConfiguration(),
  onCreated: (controller) {
    setState(() {
      this._controller = controller;
    });
  },
);

// 旋转第一页页面
    const pageIndex = 0;
    final page = controller.document.pageAtIndex(pageIndex);
    final currentRotation = await page.getRotation();
    final newAngle = currentRotation + 90;

    final result = await page.setRotation(newAngle);
    // 成功后刷新页面
    if (result) {
      controller.reloadPages();
    }

使用 CPDFDocument

dart
// 创建并打开文档
CPDFDocument document = await CPDFDocument.createInstance();
var error = await document.open(pdfFilePath);
if (error == CPDFDocumentError.success) {
  // 旋转第一页页面
      const pageIndex = 0;
      final page = document.pageAtIndex(pageIndex);
      final currentRotation = await page.getRotation();
      final newAngle = currentRotation + 90; 
      final result = await page.setRotation(newAngle);
}