Flow Text Example
This example shows how to split flow text objects on several pages. The text contains some formatting in HTML.
Result
Page 1 with title:
Page 2 entire page filled with text:
Page 3 last page:
Remarks
The FlowTextExampleReport
class defines a custom report that inherits from PdfReport.
Create
The Create
method overrides the base Create method to generate the report's specific content.
The text is split over multiple pages.
Code
using Root.ReportNet.Pdf;
using System;
using System.IO;
namespace Root.ReportExamples.TextExamples;
public class FlowTextExampleReport : PdfReport {
private const double _marginLeft = 30;
private const double _contentWidth = 150;
private const double _contentY = 245;
private const double _footerY = 25;
protected override void Create() {
StandardFontDef boldFontDef = new(StandardFont.Helvetica, FontDefOptions.Bold);
FontProp titleFont = new(boldFontDef, 24);
StandardFontDef textFontDef = new(StandardFont.Helvetica);
FontProp textFont = new(textFontDef, 12);
PenProp pen = new(0, Color.Red);
Page _page = new();
AddPage(_page);
double y = _contentY;
_page.AddCenteredMM(y, new RepString(titleFont, "Flow Text"));
y -= 25;
Stream stream = GetType().Assembly.GetManifestResourceStream("Root.ReportExamples.TextExamples.ExampleText.txt") ?? throw new InvalidOperationException("cannot find ressource");
StreamReader streamReader = new(stream);
string exampleText = streamReader.ReadToEnd();
RepTextMM text = new(textFont, $"{exampleText}\n\n\n{exampleText}", _contentWidth, TextOptions.Html);
VerticalObjectSplitter splitter = new(text);
do { // split text on pages
_page.AddMM(_marginLeft, y, new RepLine(pen, 8, 0)); // set marker line
RepObject obj = splitter.GetObjectMM(heightMM: y - _footerY - 5);
_page.AddMM(_marginLeft, y, obj);
if (splitter.Done) {
break;
}
_page = new();
AddPage(_page);
y = _contentY;
} while (true);
foreach (Page page in Pages) { // print page numbers
page.AddMM(_marginLeft + _contentWidth, AlignHor.Right, _footerY, AlignVert.Top, new RepString(textFont, $"{page.PageNumber} / {NumberOfPages}"));
}
}
}