KpiEvaluationReports.cs 6.03 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using iText.IO.Image;
using iText.Kernel.Pdf.Action;
using iText.Layout;
using iText.Layout.Borders;
using iText.Layout.Element;
using PDFGenerator.Models;
using PDFGenerator.Utilities;
using System.Collections.Generic;
using Reports = PDFGenerator.Models.KpiEvaluationReports;

namespace PDFGenerator.EvaluationReport
{
    public class KpiEvaluationReports : BaseSection
    {
        private Reports Reports { get; }
        private Table Table { get; set; }
        private Dictionary<string, byte[]> CustomUploads { get; set; }

        public KpiEvaluationReports(Reports reports, Dictionary<string, byte[]> customUploads, Document document,
            Bookmarks bookmarks, TableOfContents tableOfContents) : base(document, bookmarks, tableOfContents)
        {
            Reports = reports;
            CustomUploads = customUploads;
        }

        /// <summary>
        /// Renders the report section for the current kpi.
        /// </summary>
        public override void Render()
        {
            if (Reports.UploadedFilesForKpi != null)
            {
                AddCustomUploads(Reports.UploadedFilesForKpi, CustomUploads);
            }

            if (Reports.Paragraphs == null)
            {
                return;
            }

            Table = new Table(1, false).UseAllAvailableWidth().SetMarginTop(-10f);

            AddParagraphWithLeadingToTable(Table, Reports.Paragraphs[0], true);

            AddLinks();

            AddReports();

            Document.Add(Table);
        }

        /// <summary>
        /// Adds links for the report section.
        /// </summary>
        private void AddLinks()
        {
            if (Reports.ExternalLinks != null)
            {
                Link link;
                Paragraph hyperlink;
                foreach (KeyValuePair<string, string> item in Reports.ExternalLinks)
                {
                    link = new Link(item.Key, PdfAction.CreateURI(item.Value));
                    link.AddStyle(Styles.Link);

                    hyperlink = new Paragraph(link).SetMarginTop(-1f).SetMultipliedLeading(1f);

                    Table.AddCell(new Cell().Add(hyperlink).SetBorder(Border.NO_BORDER).SetPaddingBottom(9f));
                }
            }

            if (Reports.InternalLinksOnSamePhrase != null)
            {
                Paragraph links;
                Link link;
                foreach (List<string> linkSet in Reports.InternalLinksOnSamePhrase)
                {
                    string expression = "Report(s) siehe Anhang. (";
                    links = ParagraphFactory.GetCustomParagraph(expression, Styles.Paragraph, 0, 9, 1f);

                    for (int i = 0; i < linkSet.Count; i++)
                    {
                        link = new Link(linkSet[i], PdfAction.CreateGoTo(linkSet[i]));
                        link.AddStyle(Styles.Paragraph);
                        links.Add(link);
                        if (i < linkSet.Count - 1)
                        {
                            links.Add(", ");
                        }
                    }

                    links.Add(")");
                    Table.AddCell(new Cell().Add(links).SetBorder(Border.NO_BORDER).SetPaddingBottom(9f));
                }
            }

            if (Reports.InternalLinks != null)
            {
                foreach (KeyValuePair<string, string> item in Reports.InternalLinks)
                {
                    Paragraph link = ParagraphFactory.GetStandardParagraph(item.Key)
                    .SetMarginTop(-2f)
                    .SetAction(PdfAction.CreateGoTo(item.Value));

                    Table.AddCell(new Cell().Add(link).SetBorder(Border.NO_BORDER).SetPaddingBottom(0f));
                }
            }
        }

        /// <summary>
        /// Adds reports and/or paragraphs for the report section.
        /// </summary>
        private void AddReports()
        {
            if (Reports.Reports == null)
            {
                // index 0 is reserved for the headline
                for (int i = 1; i < Reports.Paragraphs.Count; i++)
                {
                    Paragraph p = ParagraphFactory.GetStandardParagraph(Reports.Paragraphs[i]);
                    Table.AddCell(new Cell().SetPaddingTop(0f).SetBorder(Border.NO_BORDER).Add(p));
                }

                return;
            }

            AddPageBreak();

Sebastian Drenckberg's avatar
Sebastian Drenckberg committed
129
            // if order between images and text is important
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
            if (Reports.ReportElementOrder != null)
            {
                int countImages = 0;
                int countParagraphs = 1;
                Reports.ReportElementOrder.ForEach(type =>
                {
                    if (type == LayoutElement.Image)
                    {
                        AddImageToTable(Reports.Reports[countImages++]);
                    }

                    if (type == LayoutElement.Paragraph)
                    {
                        Paragraph p = ParagraphFactory.GetStandardParagraph(Reports.Paragraphs[countParagraphs++]);
                        Table.AddCell(new Cell().SetPaddingTop(0f).SetBorder(Border.NO_BORDER).Add(p));
                    }
                });

                return;
            }

            Reports.Reports.ForEach(item =>
            {
                if (item != null)
                {
                    AddImageToTable(item);
                }
            });
        }

        /// <summary>
        /// Adds a single report.
        /// </summary>
Sebastian Drenckberg's avatar
Sebastian Drenckberg committed
163
        /// <param name="images">List of images representing the report.</param>
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
        private void AddImageToTable(List<byte[]> images)
        {
            ImageData imageData;
            Image image;
            foreach (byte[] imageB in images)
            {
                imageData = ImageDataFactory.Create(imageB);
                image = new Image(imageData);

                Cell cell = new Cell()
                .Add(GetScaledImage(image))
                .SetBorder(Border.NO_BORDER)
                .SetPadding(0)
                .SetPaddingBottom(12f);

                Table.AddCell(cell);
            }
        }
    }
}