在flutter中使用syncfusion包的PDF内部导航



我一直在flutter中开发一个应用程序,以生成一个包含索引列表及其相应内容部分的PDF文件。我想要实现的是在单击索引元素的同时导航到特定内容。我遵循了此处的文档:https://help.syncfusion.com/flutter/pdf/working-with-hyperlinks,但这并没有解决我的问题。

import 'dart:ui';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';   
import 'dart:io';
import 'package:share/share.dart';
import 'package:lorem_ipsum/lorem_ipsum.dart';
import 'package:syncfusion_flutter_pdf/pdf.dart';
reportView(context) async {
PdfDocument document = PdfDocument();
PdfPage firstPage = document.pages.add();
PdfPage secondPage = document.pages.add();

Rect firstRectangle = Rect.fromLTWH(0, 0, 150, 100);
Rect secondRectangle = Rect.fromLTWH(170, 100, 0, 0);
//Contents for first page
firstPage.graphics.drawString(
'Hello World!!!', PdfStandardFont(PdfFontFamily.helvetica, 27),
brush: PdfBrushes.darkBlue, bounds: firstRectangle);
//Contents for second page
secondPage.graphics.drawString(
loremIpsum(words: 60, paragraphs: 3, initWithLorem: true), PdfStandardFont(PdfFontFamily.helvetica, 27),
brush: PdfBrushes.darkBlue,bounds: secondRectangle);

//Creating the first annotation
PdfDocumentLinkAnnotation firstAnnotation = new PdfDocumentLinkAnnotation(firstRectangle,PdfDestination(secondPage));
firstPage.annotations.add(firstAnnotation);
//Creating the second annotation
PdfDocumentLinkAnnotation secondAnnotation = new PdfDocumentLinkAnnotation(secondRectangle,PdfDestination(firstPage));
firstPage.annotations.add(secondAnnotation);
//Saving the document
final String dir = (await getApplicationDocumentsDirectory()).path;
final String path = '$dir/App_' + '.pdf';
await File(path).writeAsBytes(document.save());
print(path);
Share.shareFiles([path]);
document.dispose();
}

在我最终生成的PDF中,我似乎找不到任何可点击的点。我在这里做错了什么?为了内部导航的正常工作,我还有其他事情要做吗?

目前我们还不支持为文档链接注释绘制外观。因此,我们建议您使用页面图形绘制可点击点,如下所示。

//Create PDF document
PdfDocument document = PdfDocument();
//Add pages
PdfPage firstPage = document.pages.add();
PdfPage secondPage = document.pages.add();
Rect firstRectangle = Rect.fromLTWH(0, 0, 150, 100);
Rect secondRectangle = Rect.fromLTWH(170, 100, 150, 100);
//Contents for first page
firstPage.graphics.drawString(
'Hello World!!!', PdfStandardFont(PdfFontFamily.helvetica, 27),
brush: PdfBrushes.darkBlue, bounds: firstRectangle);
//Contents for second page
secondPage.graphics.drawString(
'second page text', PdfStandardFont(PdfFontFamily.helvetica, 27),
brush: PdfBrushes.darkBlue, bounds: secondRectangle);
//Creating the first annotation
PdfDocumentLinkAnnotation firstAnnotation = new PdfDocumentLinkAnnotation(
firstRectangle, PdfDestination(secondPage));
firstPage.annotations.add(firstAnnotation);
//Draw appearance.
firstPage.graphics
.drawRectangle(bounds: firstRectangle, pen: PdfPens.black);
//Creating the second annotation
PdfDocumentLinkAnnotation secondAnnotation = new PdfDocumentLinkAnnotation(
secondRectangle, PdfDestination(firstPage));
firstPage.annotations.add(secondAnnotation);
//Draw appearance.
firstPage.graphics
.drawRectangle(bounds: secondRectangle, pen: PdfPens.black);
//Saving the document
final String dir = (await getApplicationDocumentsDirectory()).path;
final String path = '$dir/App_' + '.pdf';
await File(path).writeAsBytes(document.save());
print(path);
Share.shareFiles([path]);
document.dispose();

此外,第二个文档链接注释添加了宽度和高度0,0,因为注释配置不正确。我们也更改了上面代码的边界。

最新更新