How to create multiple page PDF?

I'm trying to create a multiple page PDF. I'm getting a variable which has data that crosses the first page. I'm creating a page using document.pages.add() but this is creating only one page and I'm adding data to this page using page.graphics.drawstring(). The code I have written is


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();
}





1 Reply

SV Surya Venkatesan Syncfusion Team March 31, 2022 01:26 PM UTC

Hi Sujeeth,


We recommend you draw the long text through the PdfTextElement or PdfLayoutResult API class to get a better result. These APIs help to move to the next page automatically if the given text is overflowing on the current page. So, if you want to create multiple pages with large date content, Kindly choose these APIs. We have updated the provided code snippet to create multiple pages using PdfTextElement, if given text overflow with the current page, that will automatically forward to the next page,

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);

 

    PdfTextElement(text: dataToAdd!, font: font).draw(

        page: page,

        bounds: Rect.fromLTWH(

            0, 0, page.getClientSize().width, page.getClientSize().height));

 

    // 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('ScribeRyte', 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();

 

    document.dispose();

Refer to the following documentation to get more details, https://help.syncfusion.com/flutter/pdf/working-with-text#creating-a-multicolumn-pdf-document


Kindly try with the provided details and let us know the result.


Regards,

Surya V


Loader.
Up arrow icon