diff --git a/WpfApp4/PdfService.cs b/WpfApp4/PdfService.cs index 49c3596..0e69dd4 100644 --- a/WpfApp4/PdfService.cs +++ b/WpfApp4/PdfService.cs @@ -7,7 +7,7 @@ namespace FahrzeugVerwaltung { public class PdfService { - // Erstellt PDF für ein Fahrzeug + // Erstellt ein PDF mit formatierten Daten eines Fahrzeugs public bool ErstelleFahrzeugPdf(Fahrzeug fahrzeug, string dateiPfad) { try @@ -15,13 +15,33 @@ namespace FahrzeugVerwaltung using var document = new Document(PageSize.A4); PdfWriter.GetInstance(document, new FileStream(dateiPfad, FileMode.Create)); document.Open(); - document.Add(new Paragraph($"Fahrzeug: {fahrzeug.Marke} {fahrzeug.Modell}")); - document.Add(new Paragraph($"Baujahr: {fahrzeug.Baujahr}")); - document.Add(new Paragraph($"Leistung: {fahrzeug.Leistung} PS")); - document.Add(new Paragraph($"Kilometerstand: {fahrzeug.KilometerstandFormatiert}")); - document.Add(new Paragraph($"Farbe: {fahrzeug.Farbe}")); - document.Add(new Paragraph($"Kaufpreis: {fahrzeug.KaufpreisFormatiert}")); - document.Add(new Paragraph($"Aktueller Wert: {fahrzeug.AktuellerWertFormatiert}")); + + var titelSchrift = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 16); + document.Add(new Paragraph("Fahrzeugdaten", titelSchrift) + { + Alignment = Element.ALIGN_CENTER, + SpacingAfter = 20f + }); + + var tabelle = new PdfPTable(2) { WidthPercentage = 80 }; + tabelle.SetWidths(new float[] { 1f, 2f }); + + void Zeile(string text, string wert) + { + tabelle.AddCell(new PdfPCell(new Phrase(text)) { BackgroundColor = BaseColor.LIGHT_GRAY }); + tabelle.AddCell(new Phrase(wert)); + } + + Zeile("Marke", fahrzeug.Marke); + Zeile("Modell", fahrzeug.Modell); + Zeile("Baujahr", fahrzeug.Baujahr.ToString()); + Zeile("Leistung", $"{fahrzeug.Leistung} PS"); + Zeile("Kilometer", fahrzeug.KilometerstandFormatiert); + Zeile("Farbe", fahrzeug.Farbe); + Zeile("Kaufpreis", fahrzeug.KaufpreisFormatiert); + Zeile("Aktueller Wert", fahrzeug.AktuellerWertFormatiert); + + document.Add(tabelle); document.Close(); return true; } @@ -31,7 +51,7 @@ namespace FahrzeugVerwaltung } } - // Erstellt PDF mit allen Fahrzeugen + // Erstellt eine übersichtliche Liste aller Fahrzeuge als PDF public bool ErstelleFahrzeuglistePdf(List fahrzeuge, string dateiPfad) { try @@ -39,22 +59,35 @@ namespace FahrzeugVerwaltung using var document = new Document(PageSize.A4.Rotate()); PdfWriter.GetInstance(document, new FileStream(dateiPfad, FileMode.Create)); document.Open(); - var table = new PdfPTable(8) { WidthPercentage = 100 }; - string[] headers = { "Marke", "Modell", "Baujahr", "Leistung", "Kilometer", "Kaufpreis", "Farbe", "Aktueller Wert" }; - foreach (var h in headers) - table.AddCell(new PdfPCell(new Phrase(h)) { BackgroundColor = BaseColor.LIGHT_GRAY }); + + var titelSchrift = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 16); + document.Add(new Paragraph("Fahrzeugliste", titelSchrift) + { + Alignment = Element.ALIGN_CENTER, + SpacingAfter = 20f + }); + + var tabelle = new PdfPTable(8) { WidthPercentage = 100 }; + string[] ueberschriften = { "Marke", "Modell", "Baujahr", "Leistung", "Kilometer", "Kaufpreis", "Farbe", "Aktueller Wert" }; + + foreach (var u in ueberschriften) + { + tabelle.AddCell(new PdfPCell(new Phrase(u)) { BackgroundColor = BaseColor.LIGHT_GRAY }); + } + foreach (var f in fahrzeuge) { - table.AddCell(f.Marke); - table.AddCell(f.Modell); - table.AddCell(f.Baujahr.ToString()); - table.AddCell($"{f.Leistung} PS"); - table.AddCell(f.KilometerstandFormatiert); - table.AddCell(f.KaufpreisFormatiert); - table.AddCell(f.Farbe); - table.AddCell(f.AktuellerWertFormatiert); + tabelle.AddCell(f.Marke); + tabelle.AddCell(f.Modell); + tabelle.AddCell(f.Baujahr.ToString()); + tabelle.AddCell($"{f.Leistung} PS"); + tabelle.AddCell(f.KilometerstandFormatiert); + tabelle.AddCell(f.KaufpreisFormatiert); + tabelle.AddCell(f.Farbe); + tabelle.AddCell(f.AktuellerWertFormatiert); } - document.Add(table); + + document.Add(tabelle); document.Close(); return true; }