Home > Software design >  How to create multi page PDF using syncfusion pdf creator?
How to create multi page PDF using syncfusion pdf creator?

Time:04-06

I'm trying to create a pdf document using the syncfusion pdf package. Currently I'm able to make a single page pdf but the data I'm getting will go to multiple pages. How to identify the length of data being provided and create multiple pages to add the data to PDF

My code

Future<void> _createPDFAndDownload(String? dataToAdd, int? fileName) async {
final PdfDocument document = PdfDocument();
PdfPage page = document.pages.add();
document.pageSettings.size = PdfPageSize.a4;

final PdfPageTemplateElement headerTemplate =
    PdfPageTemplateElement(const Rect.fromLTWH(0, 0, 515, 50));

headerTemplate.graphics.drawString(
    'Management', PdfStandardFont(PdfFontFamily.helvetica, 12),
    bounds: const Rect.fromLTWH(200, 15, 200, 20));

document.template.top = headerTemplate;

PdfFont font = PdfStandardFont(PdfFontFamily.helvetica, 12);

page.graphics.drawString(
  dataToAdd!,
  font,
  bounds: Rect.fromLTWH(
      0, 0, page.getClientSize().width, page.getClientSize().height),
);

PdfSecurity security = document.security;

//Specifies encryption algorithm and key size
security.algorithm = PdfEncryptionAlgorithm.rc4x128Bit;

security.userPassword = 'password';

Size pageSize = page.getClientSize();
Size waterMarkSize = font.measureString('Management');
PdfGraphics graphics = page.graphics;
double x = pageSize.width / 2;
double y = pageSize.height / 2;
graphics.save();
graphics.translateTransform(x, y);
graphics.setTransparency(0.25);
graphics.rotateTransform(-40);
graphics.drawString('Management', font,
    pen: PdfPen(PdfColor(255, 0, 0)),
    brush: PdfBrushes.blue,
    bounds: Rect.fromLTWH(
        -waterMarkSize.width / 2,
        -waterMarkSize.height / 2,
        waterMarkSize.width,
        waterMarkSize.height));
graphics.restore();

List<int> bytes = document.save();

final folderName = "Management";
final subdirectory = "Docs";
final _fileName = fileName.toString();
final path = Directory("storage/emulated/0/$folderName/");
final path2 = Directory("storage/emulated/0/$folderName/$subdirectory/");
File fileDef =
    File("storage/emulated/0/$folderName//$subdirectory/$_fileName.pdf");
var status = await Permission.storage.status;
if (!status.isGranted) {
  await Permission.storage.request();
}
if ((await path.exists())) {
  await fileDef.writeAsBytes(bytes, flush: true);
  showToast("File Location "   path2.path);
} else {
  await path.create();
  await path2.create();
  fileDef.writeAsBytes(bytes, flush: true);
  showToast("File Location "   path2.path);
}
document.dispose();
}

The dataToAdd will have data which crosses the first page I want to create multiple page add to it

CodePudding user response:

I'm able to get the multiple page pdf using the following code

Future<void> _createPDFAndDownload(String? dataToAdd, int? fileName) async {
final PdfDocument document = PdfDocument();
final PdfPage page = document.pages.add();

final PdfLayoutResult? layoutResult = PdfTextElement(
        text: dataToAdd!,
        font: PdfStandardFont(PdfFontFamily.helvetica, 12),
        brush: PdfSolidBrush(PdfColor(0, 0, 0)))
    .draw(
        page: page,
        bounds: Rect.fromLTWH(
            0, 0, page.getClientSize().width, page.getClientSize().height),
        format: PdfLayoutFormat(
          layoutType: PdfLayoutType.paginate,
        ));
}
  • Related