DavBfr / dart_pdf

Pdf creation module for dart/flutter
https://pub.dev/packages/pdf
Apache License 2.0
1.4k stars 626 forks source link

Force addPage() to start on odd or even page number #1763

Open xErik opened 1 week ago

xErik commented 1 week ago

Is your feature request related to a problem? Please describe.

Consider a book where:

  1. A chapter title page is on an odd page (right hand side)
  2. Followed by a blank page (left hand side)
  3. Followed by text on an odd page (right hand side)

addPage() does not allow to force the start of the chapter page to the next odd page number.

Describe the solution you'd like

enum PageNumber { odd, even }

addPage(  startNext: PageNumber.odd )

Here, addPage injects blank pages as needed to achieve the desired effect.

Describe alternatives you've considered

I tried to get the current page count before inserting a new page. But the document hasn't been rendered yet. Thus, the actual page count is not accessible. And whether an additionally injected blank page is needed cannot be determined at coding time.

I may have overlooked an obvious solution to this problem, or an already existing API parameter.

xErik commented 6 days ago

I found out how to get the current page count.

I'll leave this feature request open, as I am not sure if this new feature is still a good idea or not.

This code will inject an empty page and push the next page with content to an odd page (right hand):

final pageCount = doc.document.pdfPageList.pages.length;
final isNextEven = (pageCount + 1) % 2 == 0;

if (isNextEven) {
  doc.addPage(
    pw.Page(
      pageFormat: format.copyWith(marginBottom: 1.5 * PdfPageFormat.cm),
      orientation: pw.PageOrientation.portrait,
      build: (context) => pw.NewPage(),
    ),
  );