gpt4 book ai didi

pdf - 我想从PDF文件中获取总页数

转载 作者:行者123 更新时间:2023-12-03 04:55:23 26 4
gpt4 key购买 nike

我正在尝试在Flutter应用中实现功能。我想接收PDF文件中的页面总数。到目前为止,我检查过的所有查看器仅提供 View ,没有提供任何接收总页数的方法。
我现在正在使用 flutter_full_pdf_viewer 库进行查看。
请指导。

最佳答案

您可以使用https://pub.dev/packages/pdf_render软件包
该包提供pageCount属性
您可以将filePathassetName用于PDF文件位置

程式码片段

PdfDocumentLoader(
//filePath: "yourFilePath",
assetName: 'assets/hello.pdf',
documentBuilder: (context, pdfDocument, pageCount) => LayoutBuilder(
builder: (context, constraints) => ListView.builder(
controller: controller,
itemCount: pageCount,
itemBuilder: (context, index) => Container(
margin: EdgeInsets.all(margin),
padding: EdgeInsets.all(padding),
color: Colors.black12,
child: PdfPageView(
pageNumber: index + 1,

工作演示

enter image description here

完整的示例代码
import 'package:flutter/material.dart';
import 'package:pdf_render/pdf_render_widgets.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
/// render at 100 dpi
static const scale = 100.0 / 72.0;
static const margin = 4.0;
static const padding = 1.0;
static const wmargin = (margin + padding) * 2;
static final controller = ScrollController();

@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: const Text('Pdf_render example app'),
),
backgroundColor: Colors.grey,
body: Center(
child: PdfDocumentLoader(
//filePath: "yourFilePath",
assetName: 'assets/hello.pdf',
documentBuilder: (context, pdfDocument, pageCount) => LayoutBuilder(
builder: (context, constraints) => ListView.builder(
controller: controller,
itemCount: pageCount,
itemBuilder: (context, index) => Container(
margin: EdgeInsets.all(margin),
padding: EdgeInsets.all(padding),
color: Colors.black12,
child: PdfPageView(
pageNumber: index + 1,
calculateSize: (pageWidth, pageHeight, aspectRatio) => Size(constraints.maxWidth - wmargin, (constraints.maxWidth - wmargin) / aspectRatio),

customizer: (context, page, size) => Stack(
alignment: Alignment.bottomCenter,
children: <Widget>[
if (page != null) page,
Text('${index + 1}', style: TextStyle(fontSize: 50)) // adding page number on the bottom of rendered page
],)
)
)
)
),
)
)
),
);
}
}

关于pdf - 我想从PDF文件中获取总页数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60702252/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com