CourseInformationTables.cs 4.81 KB
Newer Older
1
2
3
4
using iText.Kernel.Colors;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
5
using PDFGenerator.Models.EvaluationReport;
6
7
8
9
using PDFGenerator.Utilities;
using System.Collections.Generic;
using System.Linq;
using static PDFGenerator.Utilities.Globals;
10
using Styles = PDFGenerator.Utilities.Globals.Constants.EvaluationReport.Styles;
11

12
namespace PDFGenerator.DocumentStructures.EvaluationReport
13
{
14
    public class CourseInformationTables : EvaluationReportBaseSection
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
    {
        private PDFCourseInformationTables Tables { get; }
        public CourseInformationTables(PDFCourseInformationTables tables, Document document, Bookmarks bookmarks, TableOfContents tableOfContents)
        : base(document, bookmarks, tableOfContents)
        {
            Tables = tables;
        }

        /// <summary>
        /// Renders the tables for general course information.
        /// </summary>
        public override void Render()
        {
            // negative values indicate that a specific expression should not be considered neither in the bookmark tree nor in table of contents
            string[] sections = Tables.Sections.Where(section => section.Value >= 0).Select(section => section.Key).ToArray();
            string[] tableTitles = Tables.Sections.Where(section => section.Value < 0).Select(section => section.Key).ToArray();
            int sectionCount = 0;

            Bookmarks.AddRootOutline(sections[sectionCount], RootOutline);

            TocItem tocItem = new TocItem
            {
                Title = sections[sectionCount],
                Indent = Tables.Sections[sections[sectionCount]],
                Page = GetCurrentPage()
            };
            TableOfContents.AddToTOC(tocItem);

            Paragraph p = new Paragraph(new Text(sections[sectionCount]).AddStyle(Styles.H1)).SetDestination(sections[sectionCount++]);
            Document.Add(p);


            Table table;
            foreach (List<Dictionary<string, string>> TablesForSubject in Tables.Tables)
            {
                Bookmarks.AddOutline(sections[sectionCount], OutlineLevel.Level1);

                tocItem = new TocItem
                {
                    Title = sections[sectionCount],
                    Indent = Tables.Sections[sections[sectionCount]],
                    Page = GetCurrentPage()
                };
                TableOfContents.AddToTOC(tocItem);

                p = new Paragraph(new Text(sections[sectionCount]).AddStyle(Styles.H3)).SetDestination(sections[sectionCount++]);
                Document.Add(p);

                AddLineBreak();

                int dictCount = 0;
                foreach (Dictionary<string, string> dict in TablesForSubject)
                {
                    // specific for po-version tables if available
                    if (dictCount > 0 && dictCount < TablesForSubject.Count - 1)
                    {
                        string header = dictCount == 1 ? tableTitles[0] : tableTitles[1];

                        p = new Paragraph(new Text(header).AddStyle(Styles.H3));
                        Document.Add(p);
                        AddLineBreak();
                    }
                    // add more distance between tables
                    else if (dictCount > 0)
                    {
                        Document.Add(new Paragraph(string.Empty).SetMarginBottom(8f));
                    }


                    table = new Table(UnitValue.CreatePercentArray(new float[] { 1f, 1f }))
                    .SetWidth(UnitValue.CreatePercentValue(100))
86
                    .SetFixedLayout()
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
                    .SetMarginTop(-2f)
                    .SetMarginBottom(10f);

                    foreach (KeyValuePair<string, string> item in dict)
                    {
                        AddCell(table, item.Key, true);
                        AddCell(table, item.Value != null ? item.Value : "", false);
                    }

                    dictCount++;
                    Document.Add(table);
                }
            }

            AddPageBreak();
        }

        /// <summary>
        /// Adds a new cell to table.
        /// </summary>
Sebastian Drenckberg's avatar
Sebastian Drenckberg committed
107
108
109
        /// <param name="table">The table on which a new cell is added.</param>
        /// <param name="text">The cell content.</param>
        /// <param name="leftColumn">Flag to differentiate between left and right column.</param>
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
        private void AddCell(Table table, string text, bool leftColumn)
        {
            Cell cell = new Cell().Add(
                new Paragraph(
                    new Text(text).AddStyle(Styles.Small).SetFontColor(ColorConstants.BLACK)
                ).SetMultipliedLeading(0.9f)
            ).SetPadding(4.7f);

            if (leftColumn)
            {
                cell.SetBackgroundColor(new DeviceRgb(142, 186, 229));
            }

            table.AddCell(cell);
        }
    }
}