flutter-examples icon indicating copy to clipboard operation
flutter-examples copied to clipboard

[Sf_Pdf] How to draw an Elements outside the Pages margin?

Open Wizzel1 opened this issue 3 years ago • 0 comments

I am trying to give my pages a heading, since my Content varies in height, I can't follow the documentation because it adds the pages beforehand.

//Add the pages to the document. (not possible for me, I dont know the final pagecount)
for (int i = 1; i <= 5; i++) {. 
  document.pages.add().graphics.drawString(
      'page$i', PdfStandardFont(PdfFontFamily.timesRoman, 11),
      bounds: Rect.fromLTWH(250, 0, 615, 100));
}

because of that, I am getting the pagecount after the document has been layed out:

    for (var i = 0; i < document.pages.count; i++) {
       final page = document.pages[i];

     //TODO: Add a heading here.
    }

However, since the document has been created with the default margins, I am unable to draw the heading to the page.

I tried to add the heading with a negative offset like so :

    for (var i = 0; i < document.pages.count; i++) {
      PdfTextElement(
              text: 'Heading 1',
              font: PdfStandardFont(PdfFontFamily.helvetica, 16,
                  style: PdfFontStyle.bold),
              brush: PdfSolidBrush(PdfColor(0, 0, 0)))
          .draw(
        page: document.pages[i],
        bounds: Rect.fromLTWH(0, -10, pageWidth, pageHeight), //Trying to place the heading 10 pixels above my content.
      );
    }

But the heading then gets cut off by the margin.

How can I draw the PdfTextElement outside the margin bounds?

Wizzel1 avatar Apr 14 '22 13:04 Wizzel1