Commit 2e674851 authored by Alina Heinze's avatar Alina Heinze
Browse files

Erste Version des esb Gesamtberichtes

parent bd4e927b
Pipeline #434574 passed with stage
in 12 seconds
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using PDFGenerator.Models.EsbGeneralReport;
using PDFGenerator.Renderers;
namespace PDFGenerator.Controllers
{
[ApiController]
[Route("[controller]")]
public class EsbGeneralReportController : ControllerBase
{
private ILogger<EsbGeneralReportController> Logger { get; }
public EsbGeneralReportController(ILogger<EsbGeneralReportController> logger)
{
Logger = logger;
}
/// <summary>
/// Generates the PDF for an evaluation report.
/// </summary>
/// <param name="esbReport">Evaluation report for which the PDF should be rendered.</param>
/// <returns>PDF document as byte array.</returns>
[HttpPost("RenderEsbReport")]
[RequestSizeLimit(100000000)]
public byte[] RenderReport([FromBody] EsbGeneralReport esbReport)
{
Logger.Log(LogLevel.Debug, "Start rendering pdf report.");
IRenderer Renderer = new EsbGeneralReportRenderer(esbReport);
byte[] file = Renderer.Render();
Logger.Log(LogLevel.Debug, "Finished rendering pdf report.");
return file;
}
}
}
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using PDFGenerator.Utilities;
namespace PDFGenerator.DocumentStructures
{
public abstract class BaseSection
{
protected PdfOutline RootOutline { get; }
protected Document Document { get; set; }
protected Bookmarks Bookmarks { get; }
public BaseSection(Document document)
{
Document = document;
}
public BaseSection(Document document, Bookmarks bookmarks)
{
// get root outline from the PdfDocument: false indicates iText does not need to update the outlines.
RootOutline = document.GetPdfDocument().GetOutlines(false);
Bookmarks = bookmarks;
Document = document;
}
......@@ -29,6 +40,15 @@ namespace PDFGenerator.DocumentStructures
protected PageSize GetPageSize()
{
return Document.GetPdfDocument().GetDefaultPageSize();
}
/// <summary>
/// Gets the current page.
/// </summary>
/// <returns>The current page.</returns>
protected int GetCurrentPage()
{
return Document.GetPdfDocument().GetNumberOfPages();
}
}
}
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using System.Collections.Generic;
using System.IO;
namespace PDFGenerator.DocumentStructures
{
public struct TocItem
{
public string Title { get; set; }
public int Indent { get; set; }
public int Page { get; set; }
}
public abstract class BaseTableOfContents
{
protected List<TocItem> TocItems { get; set; }
public int TocPages { get; protected set; }
protected Document Document { get; }
protected static Text NewLine { get; } = new Text("\n");
public BaseTableOfContents(Document document)
{
TocItems = new List<TocItem>();
Document = document;
}
/// <summary>
/// Renders the content for the table of contents.
/// </summary>
/// <param name="appendixStart">The page number for the beginning of appendix.</param>
public void Render(int appendixStart)
{
TocPages = NumerOfPagesForTOC(appendixStart);
AddTOC(Document, appendixStart);
}
/// <summary>
/// Determines the number of pages for rendering the table of contents.
/// </summary>
/// <param name="appendixStart">The page number for the beginning of appendix.</param>
/// <returns></returns>
private int NumerOfPagesForTOC(int appendixStart)
{
int pages = 0;
using (MemoryStream memoryStream = new MemoryStream())
{
PdfWriter writer = new PdfWriter(memoryStream);
PdfDocument pdfDocument = new PdfDocument(writer);
PageSize pageSize = Document.GetPdfDocument().GetDefaultPageSize();
Document document = new Document(pdfDocument, pageSize);
document.SetMargins(
Document.GetTopMargin(),
Document.GetRightMargin(),
Document.GetBottomMargin(),
Document.GetLeftMargin()
);
AddTOC(document, appendixStart);
pages = document.GetPdfDocument().GetNumberOfPages();
document.Close();
}
return pages;
}
protected abstract void AddTOC(Document document, int appendixStart);
/// <summary>
/// Adds a new item to the table of contents item list.
/// </summary>
/// <param name="item">Item to add the table of contents item list.</param>
public void AddToTOC(TocItem item)
{
TocItems.Add(item);
}
}
}
using iText.Layout;
using iText.Layout.Borders;
using iText.Layout.Element;
using iText.Layout.Properties;
using PDFGenerator.Models.EsbGeneralReport;
using PDFGenerator.Utilities;
using System.Linq;
using Styles = PDFGenerator.Utilities.Globals.Constants.EsbGeneralReport.Styles;
namespace PDFGenerator.DocumentStructures.EsbGeneralReport
{
public class Chapter : EsbGeneralReportBaseSection
{
public PDFChapter PDFChapter { get; set; }
public int Number { get; set; }
public readonly ReportTypeName[] ReportTypeNames = new ReportTypeName[] { new ReportTypeName(ReportType.Abbildung, "Abbildung"), new ReportTypeName(ReportType.Tabelle, "Tabelle") };
public Chapter(PDFChapter chapter, Document document, Bookmarks bookmarks, TableOfContents tableOfContents, PDFHeader header) : base(document, bookmarks, tableOfContents, header)
{
PDFChapter = chapter;
}
public Chapter(Document document, Bookmarks bookmarks, TableOfContents tableOfContents, PDFHeader header) : base(document, bookmarks, tableOfContents, header)
{
}
public override void Render()
{
if (PDFChapter != null)
{
Bookmarks.AddRootOutline(MakeTitle(PDFChapter.Title, Number), RootOutline);
TocItem tocItem = new TocItem
{
Title = PDFChapter.Title,
Indent = 0,
Page = GetCurrentPage()
};
TableOfContents.AddToTOC(tocItem);
Paragraph p = new Paragraph(new Text(Number < 10 ? "0" + Number : "" + Number).AddStyle(Styles.ChapterCoverNumber)).SetTextAlignment(TextAlignment.LEFT).SetMarginTop(300f).SetMultipliedLeading(1f).SetDestination(MakeTitle(PDFChapter.Title, Number));
p.Add(NewLine);
p.Add(new Text(PDFChapter.Title).AddStyle(Styles.ChapterCoverTitle));
Document.Add(p);
AddPageBreak();
RenderHeader(Document, PDFHeader, 10);
int startPageCount = GetCurrentPage();
int endPageCount = GetCurrentPage();
p = new Paragraph().SetTextAlignment(TextAlignment.LEFT).SetMultipliedLeading(1.15f).SetMarginTop(30f);
Text titleNumber = new Text(Number + ".").AddStyle(Styles.Title);
Text titleText = new Text(PDFChapter.Title).AddStyle(Styles.Title);
p.Add(titleNumber).Add(" ").Add(titleText);
p.Add(NewLine);
Document.Add(p);
int sectionCounterTable = 1;
int sectionCounterImage = 1;
foreach (EsbReportQuestion section in PDFChapter.ReportSections)
{
p = new Paragraph().SetTextAlignment(TextAlignment.LEFT).SetMultipliedLeading(1f).SetMarginTop(15f);
Table t = new Table(new UnitValue[] { new UnitValue(UnitValue.PERCENT, 15f), new UnitValue(UnitValue.PERCENT, 85f) }, true);
t.SetBorder(Border.NO_BORDER);
Cell cLeft = new Cell();
Cell cRight = new Cell();
cLeft.SetBorder(Border.NO_BORDER);
cRight.SetBorder(Border.NO_BORDER);
Paragraph celParagraph = new Paragraph().AddStyle(Styles.Normal).SetMultipliedLeading(1f);
Text cellText = new Text(ReportTypeNames.Where(typeNames => typeNames.Type == section.Type).Single().Name + " ");
switch (section.Type)
{
case ReportType.Tabelle:
cellText.SetText(cellText.GetText() + Number + "." + sectionCounterTable + ":");
sectionCounterTable++;
break;
case ReportType.Abbildung:
cellText.SetText(cellText.GetText() + sectionCounterImage + ":");
sectionCounterImage++;
break;
}
celParagraph.Add(cellText);
cLeft.Add(celParagraph);
cRight.Add(new Paragraph(new Text(section.Text).AddStyle(Styles.Normal)).SetMultipliedLeading(1f));
t.AddCell(cLeft);
t.AddCell(cRight);
p.Add(t);
Document.Add(p);
endPageCount = GetCurrentPage();
if (endPageCount > startPageCount)
{
startPageCount = endPageCount;
RenderHeader(Document, PDFHeader, 10);
}
}
AddPageBreak();
}
}
}
public readonly struct ReportTypeName
{
public readonly ReportType Type { get; }
public readonly string Name { get; }
public ReportTypeName(ReportType type, string name)
{
Type = type;
Name = name;
}
}
}
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using PDFGenerator.Models.EsbGeneralReport;
using PDFGenerator.Utilities;
using Styles = PDFGenerator.Utilities.Globals.Constants.EsbGeneralReport.Styles;
namespace PDFGenerator.DocumentStructures.EsbGeneralReport
{
public class Cover : EsbGeneralReportBaseSection
{
public PDFCover PDFCover { get; set; }
public Cover(PDFCover cover, Document document, Bookmarks bookmarks, TableOfContents tableOfContents, PDFHeader header) : base(document, bookmarks, tableOfContents, header)
{
PDFCover = cover;
}
public override void Render()
{
int count = 0;
RenderHeader(Document, PDFHeader, 5);
Paragraph p = new Paragraph().SetMarginTop(100f).SetTextAlignment(TextAlignment.LEFT);
while (count <= 4)
{
p.Add(new Text(PDFCover.Titles[count++]).AddStyle(Styles.CoverSubsubtitle));
p.Add(NewLine);
}
Document.Add(p);
Document.Add(new Paragraph(new Text(PDFCover.Titles[count++]).AddStyle(Styles.CoverSubtitle)).SetMarginTop(40f).SetMultipliedLeading(1.1f).SetTextAlignment(TextAlignment.LEFT));
Document.Add(new Paragraph(new Text(PDFCover.Titles[count++]).AddStyle(Styles.CoverTitle)).SetMarginTop(90f).SetMultipliedLeading(1.1f).SetTextAlignment(TextAlignment.LEFT));
AddPageBreak();
}
}
}
using iText.IO.Image;
using iText.Kernel.Geom;
using iText.Layout;
using iText.Layout.Element;
using PDFGenerator.Models.EsbGeneralReport;
using PDFGenerator.Utilities;
using static PDFGenerator.Utilities.Conversion;
namespace PDFGenerator.DocumentStructures.EsbGeneralReport
{
public abstract class EsbGeneralReportBaseSection : BaseSection
{
protected static Text NewLine { get; } = new Text("\n");
protected TableOfContents TableOfContents { get; set; }
protected PDFHeader PDFHeader { get; set; }
public EsbGeneralReportBaseSection(Document document, Bookmarks bookmarks, TableOfContents tableOfContents, PDFHeader header) : base(document, bookmarks)
{
TableOfContents = tableOfContents;
PDFHeader = header;
}
public EsbGeneralReportBaseSection(Document document, Bookmarks bookmarks, TableOfContents tableOfContents) : base(document, bookmarks)
{
TableOfContents = tableOfContents;
}
/// <summary>
/// Renders the content in each section.
/// </summary>
public abstract void Render();
public static void RenderHeader(Document document, PDFHeader header, float paddingRight)
{
ImageData imageData = ImageDataFactory.Create(header.Logo);
Image image = new Image(imageData);
PageSize pageSize = document.GetPdfDocument().GetDefaultPageSize(); ;
image.ScaleAbsolute(MillimetersToPoints(60), MillimetersToPoints(16));
image.SetFixedPosition(
pageSize.GetRight() - image.GetImageScaledWidth() - MillimetersToPoints(paddingRight),
pageSize.GetTop() - image.GetImageScaledHeight() - MillimetersToPoints(8)
);
document.Add(image);
}
public static string MakeTitle(string title, int number)
{
return number + ". " + title;
}
}
}
using iText.Kernel.Colors;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Borders;
using iText.Layout.Element;
using iText.Layout.Properties;
using PDFGenerator.Models.EsbGeneralReport;
using System.IO;
using Margins = PDFGenerator.Utilities.Globals.Constants.EsbGeneralReport.Margins;
using Styles = PDFGenerator.Utilities.Globals.Constants.EsbGeneralReport.Styles;
namespace PDFGenerator.DocumentStructures.EsbGeneralReport
{
public class Footer
{
public PDFFooter PDFFooter { get; set; }
public MemoryStream ResultStream { get; set; }
public MemoryStream IncomingStream { get; set; }
public Footer(PDFFooter footer, MemoryStream incomingStream)
{
PDFFooter = footer;
IncomingStream = incomingStream;
}
public void Render()
{
ResultStream = new MemoryStream();
PdfDocument pdfDocument = new PdfDocument(new PdfReader(new MemoryStream(IncomingStream.ToArray())), new PdfWriter(ResultStream));
pdfDocument.InitializeOutlines();
using (Document doc = new Document(pdfDocument))
{
for (int i = 4; i <= pdfDocument.GetNumberOfPages(); i++)
{
Rectangle pageSize = pdfDocument.GetPage(i).GetPageSize();
Paragraph footer = new Paragraph(new Text(PDFFooter.Text + " | " + (i - 1)).AddStyle(Styles.Footer)).SetPaddingTop(5).SetTextAlignment(TextAlignment.RIGHT);
footer.SetWidth(pageSize.GetWidth() - (Margins.LeftMargin + Margins.RightMargin));
footer.SetBorderTop(new SolidBorder(new DeviceRgb(128, 128, 128), 0.5f));
float y = pageSize.GetBottom() + 40;
doc.ShowTextAligned(footer, Margins.LeftMargin, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0);
}
}
}
}
}
using iText.Layout;
using iText.Layout.Borders;
using iText.Layout.Element;
using iText.Layout.Properties;
using PDFGenerator.Models.EsbGeneralReport;
using PDFGenerator.Utilities;
using Styles = PDFGenerator.Utilities.Globals.Constants.EsbGeneralReport.Styles;
namespace PDFGenerator.DocumentStructures.EsbGeneralReport
{
public class Imprint : EsbGeneralReportBaseSection
{
public PDFImprint PDFImprint { get; set; }
public Imprint(PDFImprint imprint, Document document, Bookmarks bookmarks, TableOfContents tableOfContents) : base(document, bookmarks, tableOfContents)
{
PDFImprint = imprint;
}
public override void Render()
{
Paragraph p = new Paragraph().SetMarginTop(480f).SetMarginLeft(220f).SetTextAlignment(TextAlignment.LEFT).SetMultipliedLeading(0.8f);
foreach (string text in PDFImprint.Title)
{
p.Add(new Text(text).AddStyle(Styles.Small));
p.Add(NewLine);
}
p.Add(NewLine);
foreach (string text in PDFImprint.Address)
{
p.Add(new Text(text).AddStyle(Styles.Small));
p.Add(NewLine);
}
p.Add(NewLine);
Table t = new Table(new float[] { 100f, 100f }, false);
t.SetMarginLeft(220f);
t.SetBorder(Border.NO_BORDER);
foreach (System.Collections.Generic.KeyValuePair<string, string> line in PDFImprint.ContactTable)
{
Cell cLeft = new Cell();
Cell cRight = new Cell();
cLeft.SetBorder(Border.NO_BORDER);
cRight.SetBorder(Border.NO_BORDER);
cLeft.Add(new Paragraph(new Text(line.Key).AddStyle(Styles.Small)).SetMultipliedLeading(0.8f));
cRight.Add(new Paragraph(new Text(line.Value).AddStyle(Styles.Small)).SetMultipliedLeading(0.8f));
t.AddCell(cLeft);
t.AddCell(cRight);
}
Document.Add(p);
Document.Add(t);
AddPageBreak();
}
}
}
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using PDFGenerator.Models.EsbGeneralReport;
using PDFGenerator.Utilities;
using Styles = PDFGenerator.Utilities.Globals.Constants.EsbGeneralReport.Styles;
namespace PDFGenerator.DocumentStructures.EsbGeneralReport
{
public class PreliminaryNote : EsbGeneralReportBaseSection
{
public PDFPreliminaryNote PDFPreliminaryNote { get; set; }
public PreliminaryNote(PDFPreliminaryNote preliminaryNote, Document document, Bookmarks bookmarks, TableOfContents tableOfContents) : base(document, bookmarks, tableOfContents)
{
PDFPreliminaryNote = preliminaryNote;
}
public override void Render()
{
Bookmarks.AddRootOutline("Vorbemerkung", RootOutline);
TocItem tocItem = new TocItem
{
Title = "Vorbemerkung",
Indent = 0,
Page = GetCurrentPage()
};
TableOfContents.PreliminaryItem = tocItem;
Paragraph p = new Paragraph().SetTextAlignment(TextAlignment.LEFT).SetMultipliedLeading(1.15f).SetMarginTop(30f).SetDestination("Vorbemerkung");
p.Add(new Text("Vorbemerkung").AddStyle(Styles.Title));
p.Add(NewLine);
p.Add(NewLine);
foreach (string para in PDFPreliminaryNote.Paragraphs)
{
p.Add(new Text(para).AddStyle(Styles.Paragraph));
p.Add(NewLine);
p.Add(NewLine);
}
Document.Add(p);
AddPageBreak();
}
}
}
using iText.Kernel.Pdf.Action;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using PDFGenerator.Models.EsbGeneralReport;
using Styles = PDFGenerator.Utilities.Globals.Constants.EsbGeneralReport.Styles;
namespace PDFGenerator.DocumentStructures.EsbGeneralReport
{
public class TableOfContents : BaseTableOfContents
{
public TocItem PreliminaryItem { get; set; }
public PDFHeader PDFHeader { get; set; }
public TableOfContents(Document document, PDFHeader header) : base(document)
{
PDFHeader = header;
}
protected override void AddTOC(Document document, int appendixStart)
{
EsbGeneralReportBaseSection.RenderHeader(document, PDFHeader, 10);
Paragraph paragraph = new Paragraph(new Text("Inhaltsverzeichnis").AddStyle(Styles.Title)).SetMarginTop(20f);
document.Add(paragraph);
float width = Document.GetPdfDocument().GetDefaultPageSize().GetWidth();
TabStop tabStop = new TabStop(width, TabAlignment.RIGHT);
TabStop tabStopNumber = new TabStop(25, TabAlignment.LEFT);
Text title = new Text(PreliminaryItem.Title).AddStyle(Styles.TOCContent);
Text page = new Text(PreliminaryItem.Page + TocPages + "").AddStyle(Styles.TOCContent);
paragraph = new Paragraph()
.AddTabStops(tabStopNumber)
.AddTabStops(tabStop)
.Add(title)
.SetAction(PdfAction.CreateGoTo(PreliminaryItem.Title))
.Add(new Tab())
.Add(page).SetMarginTop(50f);
paragraph.Add(NewLine);
paragraph.Add(NewLine);
document.Add(paragraph);
int count = 1;
foreach (TocItem item in TocItems)
{
Text titleCounter = new Text(count + ".");
title = new Text(item.Title);
page = new Text(item.Page + TocPages + "");