Text Flow is Not Happening to to Next Page
This code should let the Quill content flow through each page.
But, the text is just overflowing in page 1.
Thank you.
// Process segments on the current page until there is no vertical space.
while (processedDeltas.isNotEmpty) {
// Always take the first segment.
final Map<String, dynamic> segment = processedDeltas.first;
String textContent = segment['insert']?.toString() ?? '';
if (textContent.trim().isEmpty) {
processedDeltas.removeAt(0);
continue;
}
// Choose font style.
sf.PdfFont textFont =
sf.PdfStandardFont(sf.PdfFontFamily.helvetica, 12);
if (segment['bold'] == true) {
textFont = sf.PdfStandardFont(sf.PdfFontFamily.helvetica, 12,
style: sf.PdfFontStyle.bold);
} else if (segment['italic'] == true) {
textFont = sf.PdfStandardFont(sf.PdfFontFamily.helvetica, 12,
style: sf.PdfFontStyle.italic);
} else if (segment['underline'] == true) {
textFont = sf.PdfStandardFont(sf.PdfFontFamily.helvetica, 12,
style: sf.PdfFontStyle.underline);
}
// Create a text element and measure its full height using an infinite height.
final sf.PdfTextElement fullTextElement = sf.PdfTextElement(
text: textContent,
font: textFont,
format: sf.PdfStringFormat(
alignment: sf.PdfTextAlignment.left,
lineAlignment: sf.PdfVerticalAlignment.top,
wordWrap: sf.PdfWordWrapType.word,
),
);
final sf.PdfLayoutResult? fullLayout = fullTextElement.draw(
page: currentPage,
bounds: Rect.fromLTWH(
leftMargin, currentY, availableWidth, double.infinity),
);
// If nothing was drawn, remove this segment.
if (fullLayout == null) {
processedDeltas.removeAt(0);
continue;
}
final double fullTextHeight = fullLayout.bounds.height;
final double availableHeight = pageBottom - currentY;
// First check if the text would fit in the available space
if (fullTextHeight <= availableHeight) {
// Create a new text element for the actual drawing
//START
final sf.PdfTextElement drawingElement = sf.PdfTextElement(
text: textContent,
font: textFont,
format: sf.PdfStringFormat(
alignment: sf.PdfTextAlignment.left,
lineAlignment: sf.PdfVerticalAlignment.top,
wordWrap: sf.PdfWordWrapType.word,
),
);
final sf.PdfLayoutResult? drawResult = drawingElement.draw(
page: currentPage,
bounds: Rect.fromLTWH(
leftMargin, currentY, availableWidth, availableHeight),
);
if (drawResult != null) {
textDrawnOnPage = true;
currentY = drawResult.bounds.bottom + 5;
processedDeltas.removeAt(0);
}
} else {
//END
// The text does not fully fit on this page.
// Determine an approximate split point.
final double ratio = availableHeight / fullTextHeight;
int approxChars = (textContent.length * ratio).floor();
if (approxChars < 1) {
approxChars = 1; // Ensure progress.
}
int splitIndex = _findBestSplitPoint(textContent, approxChars);
if (splitIndex < 1) {
splitIndex = 1;
}
final String fittingText =
textContent.substring(0, splitIndex).trim();
final String remainingText =
textContent.substring(splitIndex).trim();
// Create a new text element for the fitting part.
final sf.PdfTextElement fittingElement = sf.PdfTextElement(
text: fittingText,
font: textFont,
format: sf.PdfStringFormat(
alignment: sf.PdfTextAlignment.left,
lineAlignment: sf.PdfVerticalAlignment.top,
wordWrap: sf.PdfWordWrapType.word,
),
);
final sf.PdfLayoutResult? fitResult = fittingElement.draw(
page: currentPage,
bounds: Rect.fromLTWH(
leftMargin, currentY, availableWidth, availableHeight),
);
if (fitResult != null) {
textDrawnOnPage = true;
}
// Update the segment: if any text remains, replace the first segment with it;
// otherwise, remove the segment.
if (remainingText.isNotEmpty) {
processedDeltas[0] = {...segment, 'insert': remainingText};
} else {
processedDeltas.removeAt(0);
}
// We have used all available vertical space for this page.
currentY = pageBottom;
break; // Force a break so the next page is used.
}
// If we have reached (or nearly reached) the bottom, stop processing on this page.
if (currentY >= pageBottom - 5) {
break;
}
} // End inner while for segments on this page.
if (textDrawnOnPage) {
lastUsedPageIndex = currentPageIndex;
} else {
// If nothing was drawn on this page, stop processing further pages.
break;
}
// Advance to the next page.
currentPageIndex++;
}
SIGN IN To post a reply.
1 Reply
IJ
Irfana Jaffer Sadhik
Syncfusion Team
February 4, 2025 02:49 PM UTC
Hi Ali,
We attempted to reproduce the reported issue using our test inputs, but everything worked as expected on our end. We have attached a sample for your reference. please try it on your end and let us know the outcome.
We kindly ask that you share the modified sample with us so we can analyze it further and assist you accordingly.
Regards,
Irfana J.
SIGN IN To post a reply.
- 1 Reply
- 2 Participants
-
AU Ali Uthuman
- Feb 3, 2025 08:47 AM UTC
- Feb 4, 2025 02:49 PM UTC