pdfDecodePageText function

PdfPageText? pdfDecodePageText(
  1. Uint8List bytes
)

Reverses pdfEncodePageText, or returns null when bytes are absent, truncated, corrupt, or from an incompatible format — every such case is a cache miss the caller recomputes from.

Implementation

PdfPageText? pdfDecodePageText(Uint8List bytes) {
  try {
    final r = _Reader(bytes);
    if (r.u32() != _textBlobMagic) return null;
    final pageIndex = r.i32();
    final text = r.str();
    final runCount = r.u32();
    final runs = <PdfExtractedRun>[];
    for (var i = 0; i < runCount; i++) {
      final runText = r.str();
      final startIndex = r.i32();
      final transform =
          PdfMatrix(r.f64(), r.f64(), r.f64(), r.f64(), r.f64(), r.f64());
      final width = r.f64();
      final bounds = PdfRect(r.f64(), r.f64(), r.f64(), r.f64());
      runs.add(PdfExtractedRun(
        text: runText,
        startIndex: startIndex,
        transform: transform,
        width: width,
        bounds: bounds,
      ));
    }
    return PdfPageText(pageIndex: pageIndex, text: text, runs: runs);
  } catch (_) {
    return null;
  }
}