addPageNumbers method

Future<void> addPageNumbers({
  1. double fontSize = 10,
  2. Color color = const Color(0xFF000000),
  3. double margin = 24,
  4. int startAt = 1,
  5. bool rightAligned = true,
})

Implementation

Future<void> addPageNumbers({double fontSize = 10, Color color = const Color(0xFF000000), double margin = 24, int startAt = 1, bool rightAligned = true}) async {
  for (int index = 0; index < document.pageCount; index++) {
    final UPdfPage? page = await document.page(index);
    if (page == null) continue;
    final String label = "${startAt + index}";
    final double x = rightAligned ? page.box.right - margin - fontSize * label.length * 0.6 : page.box.left + margin;
    final double y = page.box.top + margin;
    final String escaped = String.fromCharCodes(UPdfSerializer.literalString(Uint8List.fromList(label.codeUnits)));
    await appendContent(
      index,
      "BT ${_colorOperator(color, stroke: false)} /Helv ${UPdfSerializer.number(fontSize)} Tf 1 0 0 1 ${UPdfSerializer.number(x)} ${UPdfSerializer.number(y)} Tm $escaped Tj ET",
      extraResources: <String, Object?>{
        "Font": UPdfDict(<String, Object?>{
          "Helv": UPdfDict(<String, Object?>{
            "Type": const UPdfName("Font"),
            "Subtype": const UPdfName("Type1"),
            "BaseFont": const UPdfName("Helvetica"),
            "Encoding": const UPdfName("WinAnsiEncoding"),
          }),
        }),
      },
    );
  }
}