rewriteDocument method
Future<PdfRewriteResult>
rewriteDocument(
- PdfDocumentParser source, {
- int? bytesBefore,
- bool useIsolate = false,
Rewrites a document already opened by a parser.
Useful when the file came from a reader and is not entirely in memory; in
that case pass bytesBefore so the report knows the size of the source.
Implementation
Future<PdfRewriteResult> rewriteDocument(
PdfDocumentParser source, {
int? bytesBefore,
bool useIsolate = false,
}) async {
if (source.isEncrypted) {
throw PdfMergeException(
'O documento está criptografado. A leitura de PDFs criptografados '
'ainda não é suportada, e regravar sem descriptografar produziria '
'conteúdo corrompido.',
);
}
final signatureFields = _countSignatureFields(source);
if (signatureFields > 0 && options.rejectSignedSources) {
throw PdfMergeException(
'O documento tem $signatureFields assinatura(s) digital(is). '
'Regravar invalidaria todas elas e rejectSignedSources está ligado.',
);
}
final destination = PdfDocument(
compress: compress,
verbose: verbose,
version: version,
deflate: deflate,
);
final merger = PdfDocumentMerger(
destination,
options: options.toMergeOptions(),
);
merger.append(source, label: 'documento');
merger.finish();
final output = await destination.save(useIsolate: useIsolate);
return PdfRewriteResult(
bytes: output,
report: PdfRewriteReport(
objectsBefore: _liveObjectCount(source),
objectsAfter: destination.objects.where((e) => e.inUse).length,
// `reader.length` instead of `source.bytes.length`: the latter would
// materialize the whole file in memory just to measure it.
bytesBefore: bytesBefore ?? source.reader.length,
bytesAfter: output.length,
pagesBefore: source.pageCount,
pagesAfter: destination.pdfPageList.pages.length,
signatureFieldsBefore: signatureFields,
warnings: merger.warnings,
),
);
}