David PHAM-VAN

Draw page content only if not empty

... ... @@ -9,6 +9,7 @@
- Improve verbose output
- Import already defined form
- Add support for deleted objects
- Draw page content only if not empty
## 3.9.0
... ...
... ... @@ -123,6 +123,9 @@ class PdfGraphics {
/// Default font if none selected
PdfFont? get defaultFont => _page.getDefaultFont();
bool _altered = false;
bool get altered => _altered;
/// Draw a surface on the previously defined shape
/// set evenOdd to false to use the nonzero winding number rule to determine the region to fill and to true to use the even-odd rule to determine the region to fill
void fillPath({bool evenOdd = false}) {
... ... @@ -136,6 +139,7 @@ class PdfGraphics {
}());
_buf.putString('f${evenOdd ? '*' : ''} ');
_altered = true;
assert(() {
if (_page.pdfDocument.verbose) {
... ... @@ -158,6 +162,7 @@ class PdfGraphics {
}());
_buf.putString('${close ? 's' : 'S'} ');
_altered = true;
assert(() {
if (_page.pdfDocument.verbose) {
... ... @@ -178,6 +183,7 @@ class PdfGraphics {
}());
_buf.putString('h ');
_altered = true;
assert(() {
if (_page.pdfDocument.verbose) {
... ... @@ -224,6 +230,7 @@ class PdfGraphics {
}());
_buf.putString('${close ? 'b' : 'B'}${evenOdd ? '*' : ''} ');
_altered = true;
assert(() {
if (_page.pdfDocument.verbose) {
... ... @@ -248,6 +255,7 @@ class PdfGraphics {
// The shader needs to be registered in the page resources
_page.addShader(shader);
_buf.putString('${shader.name} sh ');
_altered = true;
assert(() {
if (_page.pdfDocument.verbose) {
... ... @@ -354,7 +362,7 @@ class PdfGraphics {
}
_buf.putString(' cm ${img.name} Do Q ');
}
_altered = true;
assert(() {
if (_page.pdfDocument.verbose) {
... ... @@ -571,6 +579,7 @@ class PdfGraphics {
return true;
}());
_altered = true;
}
void reset() {
... ...
... ... @@ -20,6 +20,7 @@ import '../graphics.dart';
import '../page_format.dart';
import 'annotation.dart';
import 'graphic_stream.dart';
import 'object.dart';
import 'object_dict.dart';
import 'object_stream.dart';
... ... @@ -64,11 +65,13 @@ class PdfPage extends PdfObjectDict with PdfGraphicStream {
PdfPageRotation rotate;
/// This holds the contents of the page.
final contents = <PdfObjectStream>[];
final contents = <PdfObject>[];
/// This holds any Annotations contained within this page.
final annotations = <PdfAnnot>[];
final _contentGraphics = <PdfObject, PdfGraphics>{};
/// This returns a [PdfGraphics] object, which can then be used to render
/// on to this page. If a previous [PdfGraphics] object was used, this object
/// is appended to the page, and will be drawn over the top of any previous
... ... @@ -76,6 +79,7 @@ class PdfPage extends PdfObjectDict with PdfGraphicStream {
PdfGraphics getGraphics() {
final stream = PdfObjectStream(pdfDocument);
final g = PdfGraphics(this, stream.buf);
_contentGraphics[stream] = g;
contents.add(stream);
return g;
}
... ... @@ -100,8 +104,15 @@ class PdfPage extends PdfObjectDict with PdfGraphicStream {
params['/MediaBox'] =
PdfArray.fromNum(<double>[0, 0, pageFormat.width, pageFormat.height]);
for (final content in contents) {
if (!_contentGraphics[content]!.altered) {
content.inUse = false;
}
}
// The graphic operations to draw the page
final contentList = PdfArray.fromObjects(contents);
final contentList =
PdfArray.fromObjects(contents.where((e) => e.inUse).toList());
if (params.containsKey('/Contents')) {
final prevContent = params['/Contents']!;
... ... @@ -124,9 +135,10 @@ class PdfPage extends PdfObjectDict with PdfGraphicStream {
// The /Annots object
if (annotations.isNotEmpty) {
if (params.containsKey('/Annots')) {
final annotsList = params['/Annots'];
if (annotsList is PdfArray) {
annotsList.values.addAll(PdfArray.fromObjects(annotations).values);
final annotationList = params['/Annots'];
if (annotationList is PdfArray) {
annotationList.values
.addAll(PdfArray.fromObjects(annotations).values);
}
} else {
params['/Annots'] = PdfArray.fromObjects(annotations);
... ...