David PHAM-VAN

Improve Table Of Content

... ... @@ -8,6 +8,7 @@
- Typo: rename "litteral" with "literal"
- Fix tabs and other spaces placeholder
- Prevent modifying the document once saved
- Improve Table Of Content
## 3.7.4
... ...
... ... @@ -58,7 +58,7 @@ class PdfOutline extends PdfObjectDict {
this.color,
this.destMode = PdfOutlineMode.fitPage,
this.style = PdfOutlineStyle.normal,
String? page,
PdfPage? page,
}) : assert(anchor == null || (dest == null && rect == null)),
_page = page,
super(pdfDocument);
... ... @@ -76,12 +76,20 @@ class PdfOutline extends PdfObjectDict {
PdfPage? dest;
/// Page number
String? get page =>
_page ??
(dest != null
? (pdfDocument.pdfPageList.pages.indexOf(dest!) + 1).toString()
: null);
final String? _page;
String? get page {
final int? num;
if (_page != null) {
num = pdfDocument.pdfPageList.pages.indexOf(_page!);
} else if (dest != null) {
num = pdfDocument.pdfPageList.pages.indexOf(dest!);
} else {
num = null;
}
return num == null ? null : (num + 1).toString();
}
final PdfPage? _page;
/// The region on the destination page
final PdfRect? rect;
... ...
... ... @@ -59,8 +59,8 @@ class Anchor extends SingleChildWidget {
if (description != null) {
final rb = mat.transform3(Vector3(box!.right, box!.top, 0));
final ibox = PdfRect.fromLTRB(lt.x, lt.y, rb.x, rb.y);
PdfAnnot(context.page, PdfAnnotText(rect: ibox, content: description!));
final iBox = PdfRect.fromLTRB(lt.x, lt.y, rb.x, rb.y);
PdfAnnot(context.page, PdfAnnotText(rect: iBox, content: description!));
}
}
}
... ... @@ -622,7 +622,7 @@ class Outline extends Anchor {
anchor: name,
color: color,
style: style,
page: context.pageLabel,
page: context.page,
)..effectiveLevel = level;
final root = context.document.outline;
... ...
... ... @@ -151,7 +151,7 @@ class TableOfContent extends StatelessWidget {
thickness: 0.2,
)),
SizedBox(width: 8),
Text('${c.page}'),
DelayedWidget(build: (_) => Text('${c.page}')),
],
),
),
... ...
... ... @@ -380,3 +380,37 @@ class InheritedWidget extends SingleChildWidget {
paintChild(_context!);
}
}
class DelayedWidget extends SingleChildWidget {
DelayedWidget({required this.build}) : super();
final BuildCallback build;
@override
Widget? get child => _child;
Widget? _child;
@override
void layout(Context context, BoxConstraints constraints,
{bool parentUsesSize = false}) {
_child = build(context);
super.layout(context, constraints);
}
void delayedPaint(Context context) {
_child = build(context);
child!.layout(
context,
BoxConstraints.tight(box!.size),
parentUsesSize: false,
);
paintChild(context);
}
@override
void paint(Context context) {
delayedPaint(context);
super.paint(context);
}
}
... ...
... ... @@ -48,6 +48,7 @@ import 'widget_table_test.dart' as widget_table;
import 'widget_test.dart' as widget;
import 'widget_text_test.dart' as widget_text;
import 'widget_theme_test.dart' as widget_theme;
import 'widget_toc_test.dart' as widget_toc;
import 'widget_watermark_test.dart' as widget_watermark;
import 'widget_wrap_test.dart' as widget_wrap;
... ... @@ -83,6 +84,7 @@ void main() {
widget_table.main();
widget_text.main();
widget_theme.main();
widget_toc.main();
widget_watermark.main();
widget_wrap.main();
widget.main();
... ...
/*
* Copyright (C) 2017, David PHAM-VAN <dev.nfet.net@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import 'dart:io';
import 'dart:math';
import 'package:pdf/widgets.dart';
import 'package:test/test.dart';
late Document pdf;
void main() {
setUpAll(() {
Document.debug = true;
pdf = Document();
});
test('Pdf Widgets TableOfContent', () {
pdf.addPage(
Page(
build: (context) =>
Center(child: Text('Document', style: Theme.of(context).header0)),
),
);
pdf.addPage(
MultiPage(
footer: (c) => Padding(
padding: const EdgeInsets.only(top: 20),
child: Align(
alignment: Alignment.centerRight, child: Text(c.pageLabel))),
build: (context) => [
...Iterable<Widget>.generate(40, (index) {
final level = (sin(index / 5) * 6).abs().toInt();
return Column(
children: [
Header(
child: Text('Hello $index level $level'),
text: 'Hello $index',
level: level,
),
Lorem(length: 60),
],
);
}),
],
),
);
pdf.addPage(
MultiPage(
footer: (c) => Padding(
padding: const EdgeInsets.only(top: 20),
child: Align(
alignment: Alignment.centerRight, child: Text(c.pageLabel))),
build: (context) => [
Center(
child:
Text('Table of content', style: Theme.of(context).header0)),
SizedBox(height: 20),
TableOfContent(),
],
),
index: 1,
);
});
tearDownAll(() async {
final file = File('widgets-toc.pdf');
await file.writeAsBytes(await pdf.save());
});
}
... ...
No preview for this file type