Table of Contents

Overview of Report Objects

The example creates a simple report with a header, footer and content.

Result

Page 1 - RepString:
Result of RepObjectOverview, page 1

Page 2 - RepText:
Result of RepObjectOverview, page 2

Page 3 - RepDateTime:
Result of RepObjectOverview, page 3

Page 4 - RepDouble:
Result of RepObjectOverview, page 4

Page 5 - RepInt32:
Result of RepObjectOverview, page 5

Remarks

The class MyFirstReport defines a custom report inheriting from HeaderFooterReport.

Constructor

The code in the constructor is setting up a specific header with a company name, report title and subtitle. It also sets the margin of the content, i.e. the distance between the header and the content.

Create

The Create method overrides the base Create method to add specific report content.

Code

using System;
using System.Diagnostics;
using System.Globalization;
using static System.Net.Mime.MediaTypeNames;

namespace Root.ReportExamples.RepObjectOverview;

  private readonly StandardFontDef _fontDef = new(StandardFont.Helvetica);

  private readonly StandardFontDef _boldFontDef = new(StandardFont.Helvetica, FontDefOptions.Bold);

  private readonly FontProp _font;

  private readonly FontProp _warningFont;

  private readonly PenProp _pen = new(0, Color.Magenta);

  private Page _page = new();

  private const double _x1 = 15;

  private const double _x2 = 190;

  private double _y = 270;


  public RepObjectOverviewReport() {
    CreationDate = new DateTime(2024, 3, 27, 13, 42, 34);
    _font = new(_fontDef, 10);
    _warningFont = new(_fontDef, 8, Color.Red);
  }


  protected override void Create() {
    RepStringExamples();
    RepTextExamples();
    RepDateTimeExamples();
    RepDoubleExamples();
    RepInt32Examples();
    RepArcExamples();
  }


  private void CreatePage(string subtitle) {
    _page = new(PageSize.A4, PageOrientation.Landscape);
    AddPage(_page);
    _y = 190;

    RepString rs = new(new(_boldFontDef, 14), $"Overview of Report Objects: {subtitle}");
    _page.AddCenteredMM(_y, rs);
    _y -= 15;
  }


  private void CreateSubtitle(string subtitle) {
    _y -= _font.LineFeedMM * 0.5;
    RepString rs = new(new(_boldFontDef, 10), subtitle);
    _page.AddMM(_x1, _y, rs);
    _y -= _font.LineFeedMM * 2;
  }


  private void CreateLine(string label, RepObject obj) {
    RepTextMM rt = new(_font, label, _x2 - _x1);
    _page.AddMM(_x1, _y, rt);

    _page.AddMM(_x2, _y, obj);
    double pos = _y + obj.AlignedBBox.BottomMM - 2;

    _y -= rt.HeightMM + 2;
    foreach (WarningItem w in obj.GetWarnings() ?? []) {
      _page.AddMM(_x1 + 2, _y, new RepString(_warningFont, "-"));
      rt = new(_warningFont, w.Message, _x2 - _x1 - 10);
      _page.AddMM(_x1 + 5, _y, rt);
      _y -= rt.HeightMM + 2;
    }
    if (_y > pos) _y = pos;
    _y -= 2;
  }


  private void RepStringExamples() {
    CreatePage("RepString");

    FontProp blueFont = new(_fontDef, 10, Color.Blue, FontPropOptions.Underlined);
    FontProp greenFont = new(_fontDef, 10, Color.DarkGreen, FontPropOptions.Underlined);

    RepString rs = new(_font, "abc");
    CreateLine("new RepString(font, \"abc\")", rs);

    rs = new(_font, "abcdefghijklmn", 50, TextOptions.EllipsisIfTooLong);
    CreateLine("new RepString(font, \"abcdefghijklmn\", 50, TextOptions.EllipsisIfTooLong)", rs);

    rs = new(_font, "abcdefghijklmn", 50, TextOptions.SmallerFontIfToolLong);
    CreateLine("new RepString(font, \"abcdefghijklmn\", 50, TextOptions.SmallerFontIfToolLong)", rs);

    rs = new RepStringMM(blueFont, "This is an underlined blue text with ellipsis.", 66, TextOptions.EllipsisIfTooLong);
    CreateLine("new RepStringMM(font, \"This is an underlined blue text with ellipsis.\", 66, TextOptions.EllipsisIfTooLong)", rs);

    rs = new RepStringMM(blueFont, "This is an underlined blue text without ellipsis.", 100, TextOptions.EllipsisIfTooLong);
    CreateLine("new RepStringMM(font, \"This is an underlined blue text without ellipsis.\", 100, TextOptions.EllipsisIfTooLong)", rs);

    rs = new RepStringMM(greenFont, "This is an underlined green text with reduced font size.", 70);
    CreateLine("new RepStringMM(font, \"This is an underlined green text with reduced font size.\", 70)", rs);

    FlowContainer cont = new(_page.Width);
    rs = new RepStringMM(blueFont, "This is an underlined blue text with ellipsis.", 66, TextOptions.EllipsisIfTooLong);
    rs.RotateDeg(-10);
    cont.Add(0, 0, rs);
    cont.Add(0, 0, new RepRectMM(_pen, 66, rs.HeightMM).RotateDeg(-10));
    CreateLine("new RepStringMM(font, \"This is an underlined blue text with ellipsis.\", 66, TextOptions.EllipsisIfTooLong)", cont);

    rs = new RepStringMM(blueFont, "This is an underlined blue text without ellipsis.", 100, TextOptions.EllipsisIfTooLong);
    rs.RotateDeg(-10);
    CreateLine("new RepStringMM(font, \"This is an underlined blue text without ellipsis.\", 100, TextOptions.EllipsisIfTooLong)", rs);

    cont = new(_page.Width);
    rs = new RepStringMM(greenFont, "This is an underlined green text with reduced font size.", 70);
    rs.RotateDeg(-10);
    cont.Add(0, 0, rs);
    cont.Add(0, 0, new RepRectMM(_pen, 70, rs.HeightMM).RotateDeg(-10));
    CreateLine("new RepStringMM(font, \"This is an underlined green text with reduced font size.\", 70)", cont);
  }


  private void RepTextExamples() {
    CreatePage("RepText");

    RepText rt = new(_font, "abc");
    CreateLine("new RepText(font, \"abc\")", rt);

    rt = new(_font, "abcdefghijklmn", 50);
    CreateBox(50);
    CreateLine("new RepText(font, \"abcdefghijklmn\", 50)", rt);

    rt = new(_font, $"This is a {RepText.Bold("bold")} text: {RepText.BoldOn}abc");
    CreateLine(@"new RepText(font, ""This is a {RepText.Bold(""bold"")} text: {RepText.BoldOn}abc"")", rt);

    rt = new(_font, $"This is an {RepText.Italic("italic")} text: {RepText.ItalicOn}abc");
    CreateLine(@"new RepText(font, ""This is an {RepText.Italic(""italic"")} text: {RepText.ItalicOn}abc"")", rt);

    rt = new(_font, $"This is an {RepText.Underline("underlinded")} text: {RepText.UnderlineOn}abc");
    CreateLine(@"new RepText(font, ""This is an {RepText.Underline(""underlinded"")} text: {RepText.UnderlineOn}abc"")", rt);

    rt = new(_font, $"Signature: {RepText.UnderlineOn}" + new string(' ', 30), 119);
    CreateBox(119);
    CreateLine(@"new RepText(font, ""Signature: {RepText.UnderlineOn}                         "")", rt);

    rt = new(_font, $"This is a {RepText.Strikethrough("strikethrough")} text: {RepText.StrikethroughOn}abc");
    CreateLine(@"new RepText(font, ""This is a {RepText.Strikethrough(""strikethrough"")} text: a{RepText.StrikethroughOn}abc"")", rt);

    rt = new(_font, $"This is a {RepText.Subscript("subcript")} text: a{RepText.SubscriptOn}n");
    CreateLine(@"new RepText(font, ""This is a {RepText.Subscript(""subscript"")} text: a{RepText.SubscriptOn}n"")", rt);

    rt = new(_font, $"This is a {RepText.Superscript("superscript")} text: a{RepText.SuperscriptOn}2");
    CreateLine(@"new RepText(font, ""This is a {RepText.Superscript(""superscript"")} text: a{RepText.SuperscriptOn}2"")", rt);

    rt = new(_font, $"This text is shown in font size 15: {RepText.FontSize(14)}abc");
    CreateLine(@"new RepText(font, ""This text is shown in font size 15: {RepText.FontSize(14)}abc"")", rt);

    rt = new(_font, $"This text is shown in red: {RepText.FontColor(Color.Red)}abc");
    CreateLine(@"new RepText(font, ""This text is shown in red: {RepText.FontColor(Color.Red)}abc"")", rt);

    rt = new(_font, $"This is a {RepText.BackgroundOn}highlighted{RepText.BackgroundOff} text.");
    CreateLine(@"new RepText(font, ""This is a {RepText.BackgroundOn}highlighted{RepText.BackgroundOff} text."")", rt);

    rt = new(_font, $"This is a red highlighted text: {RepText.Background(Color.Red)}abc");
    CreateLine(@"new RepText(font, ""This is a red highlighted text: {RepText.Background(Color.Red)}abc"")", rt);

    rt = new(_font, "This is a multiline text with text option 'None'.", 150, TextOptions.None);
    CreateBox(150);
    CreateLine(@"new RepText(font, ""Multiline text with text option 'None'."", 150, TextOption.None)", rt);

    rt = new(_font, "This is a multiline text with text option 'AdjustWidth'.", 150, TextOptions.AdjustWidth);
    CreateBox(150);
    CreateLine(@"new RepText(font, ""This is a multiline text with text option 'AdjustWidth'."", 150, TextOption.AdjustWidth)", rt);

    rt = new(_font, "This is a multiline text with text option 'Center'.", 150, TextOptions.Center);
    CreateBox(150);
    CreateLine(@"new RepText(font, ""Multiline text with text option 'Center'."", 150, TextOption.Center)", rt);

    rt = new(_font, "Multiline text with option 'Center' and 'AdjustWidth'.", 150, TextOptions.Center | TextOptions.AdjustWidth);
    CreateBox(150);
    CreateLine(@"new RepText(font, ""Multiline text with option 'Center' and 'AdjustWidth'."", 150, TextOptions.Center | TextOption.AdjustWidth)", rt);

    rt = new(_font, "This is a multiline text with text option 'Right'.", 150, TextOptions.Right);
    CreateBox(150);
    CreateLine(@"new RepText(font, ""Multiline text with text option 'Right'."", 150, TextOption.Right)", rt);

    rt = new(_font, "Multiline text with option 'Right' and 'AdjustWidth'.", 150, TextOptions.Right | TextOptions.AdjustWidth);
    CreateBox(150);
    CreateLine(@"new RepText(font, ""Multiline text with option 'Right' and 'AdjustWidth'."", 150, TextOptions.Right | TextOption.AdjustWidth)", rt);

    rt = new(_font, $"Text: {RepText.Underline("underlined")}, {RepText.Strikethrough("strikethrough")}, {RepText.BackgroundOn}highlighted");
    rt.RotateDeg(-10);
    CreateLine(@"new RepText(font, ""Text: {RepText.Underline(""underlined"")}, {RepText.Strikethrough(""strikethrough"")}, {RepText.BackgroundOn}highlighted"").RotateDeg(-10)", rt);


    void CreateBox(double width) {
      _page.AddMM(_x2 + PdfTools.GetMM(width), _y + rt.Box.BottomMM, new RepLine(_pen, 0, rt.Height));
      _page.AddMM(_x2, _y + rt.Box.BottomMM, new RepRect(_pen, rt.Width, rt.Height));
    }
  }


  private void RepDateTimeExamples() {
    CreatePage("RepDateTime");

    CreateSubtitle("Invariant Culture");
    CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
    DateTime dt = new(2024, 8, 7, 6, 54, 32);
    Test(dt, 0, "08/07/2024 06:54:32", "G");
    Test(dt, 0, "08/07/2024 06:54", "g");
    Test(dt, 0, "08/07/2024", "d");
    Test(dt, 0, "06:54:32", "T");
    Test(dt, 0, "06:54", "t");

    CreateSubtitle("Culture de-DE");
    CultureInfo.CurrentCulture = new CultureInfo("de-DE");
    Test(dt, 0, "07.08.2024 06:54:32", "G");

    CreateSubtitle("Culture de-CH");
    CultureInfo.CurrentCulture = new CultureInfo("de-CH");
    Test(dt, 0, "07.08.2024 06:54:32", "G");
    Test(dt, 25, "07.08.2024 06:54:32", "G");


    void Test(DateTime dateTime, double widthMax, string expected, string format) {
      RepDateTime rdt;
      string statement;
      if (widthMax == 0) {
        rdt = new RepDateTime(_font, dateTime, format);
        statement = $"new RepDateTime(font, new(2024, 8, 7, 6, 54, 32), \"{format}\")";
      }
      else {
        rdt = new RepDateTimeMM(_font, dateTime, format, widthMax);
        statement = $"new RepDateTimeMM(font, new(2024, 8, 7, 6, 54, 32), \"{format}\", {widthMax})";
      }
      Debug.Assert(expected == rdt.Text);
      CreateLine(statement, rdt);
    }
  }


  private void RepDoubleExamples() {
    CreatePage("RepDouble");

    CreateSubtitle("Invariant Culture");
    CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
    Test(1234.567, "N0", 0, "1,235");
    Test(-1234.567, "N0", 0, "-1,235");
    Test(1234.567, "N2", 0, "1,234.57");
    Test(-1234.567, "N2", 0, "-1,234.57");
    Test(1234567.891, "N2", 20, "1,234,567.89");
    Test(-1234567.891, "N2", 20, "-1,234,567.89");

    Test(1234.567, "F0", 0, "1235");
    Test(-1234.567, "F0", 0, "-1235");
    Test(1234.567, "F2", 0, "1234.57");
    Test(-1234.567, "F2", 0, "-1234.57");
    Test(1234567.891, "F2", 19, "1234567.89");
    Test(-1234567.891, "F2", 19, "-1234567.89");

    CreateSubtitle("Culture de-DE");
    CultureInfo.CurrentCulture = new CultureInfo("de-DE");
    Test(1234.56, "N2", 0, "1.234,56");
    Test(-1234.56, "N2", 0, "-1.234,56");

    CreateSubtitle("Culture de-CH");
    CultureInfo.CurrentCulture = new CultureInfo("de-CH");
    Test(1234.56, "N2", 0, "1’234.56");
    Test(-1234.56, "N2", 0, "-1’234.56");


    void Test(double number, string format, double widthMax, string expected) {
      RepDouble rd;
      string statement;
      if (widthMax == 0) {
        rd = new RepDouble(_font, number, format);
        statement = $"new RepDouble(font, {number}, \"{format}\")";
      }
      else {
        rd = new RepDoubleMM(_font, number, format, widthMax);
        statement = $"new RepDoubleMM(font, {number}, \"{format}\", {widthMax})";
      }
      Debug.Assert(expected == rd.Text);
      CreateLine(statement, rd);
    }
  }


  private void RepInt32Examples() {
    CreatePage("RepInt32");

    CreateSubtitle("Invariant Culture");
    CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
    Test(1234, "N0", 0, "1,234");
    Test(-1234, "N0", 0, "-1,234");
    Test(1234567, "N0", 15, "1,234,567");
    Test(-1234567, "N0", 15, "-1,234,567");
    Test(1234, "F0", 0, "1234");
    Test(-1234, "F0", 0, "-1234");
    Test(1234567, "F0", 14, "1234567");
    Test(-1234567, "F0", 14, "-1234567");
    Test(254, "x", 17, "fe");
    Test(-254, "x", 17, "ffffff02");

    CreateSubtitle("Culture de-DE");
    CultureInfo.CurrentCulture = new CultureInfo("de-DE");
    Test(1234, "N0", 0, "1.234");
    Test(-1234, "N0", 0, "-1.234");
    Test(1234567, "N0", 0, "1.234.567");
    Test(-1234567, "N0", 0, "-1.234.567");

    CreateSubtitle("Culture de-CH");
    CultureInfo.CurrentCulture = new CultureInfo("de-CH");
    Test(1234, "N0", 0, "1’234");
    Test(-1234, "N0", 0, "-1’234");
    Test(1234567, "N0", 0, "1’234’567");
    Test(-1234567, "N0", 0, "-1’234’567");


    void Test(int number, string format, double widthMax, string expected) {
      RepInt32 ri;
      string statement;
      if (widthMax == 0) {
        ri = new RepInt32(_font, number, format);
        statement = $"new RepInt32(font, {number}, \"{format}\")";
      }
      else {
        ri = new RepInt32MM(_font, number, format, widthMax);
        statement = $"new RepInt32MM(font, {number}, \"{format}\", {widthMax})";
      }
      Debug.Assert(expected == ri.Text);
      CreateLine(statement, ri);
    }
  }


  private void RepArcExamples() {
    CreatePage("RepArc");
    PenProp pen = new(1);

    RepObject ro = new RepArcMM(pen, 10, 5, 0, 90);
    _page.AddMM(_x2 + ro.Box.LeftMM, _y + ro.Box.BottomMM, new RepRect(_pen, ro.Box.Width, ro.Box.Height));
    CreateLine("new RepArcMM(pen, 10, 5, 0, 90)", ro);
    _y -= 5;

    ro = new RepArcMM(pen, 10, 5, -120, 270);
    _page.AddMM(_x2 + ro.Box.LeftMM, _y + ro.Box.BottomMM, new RepRect(_pen, ro.Box.Width, ro.Box.Height));
    CreateLine("new RepArcMM(pen, 10, 5, -120, 270)", ro);
  }
}