using System.Collections.Generic; using System.IO; using iTextSharp.text; using iTextSharp.text.pdf; namespace FahrzeugVerwaltung { public class PdfService { // Erstellt ein PDF mit den Daten eines Fahrzeugs public bool ErstelleFahrzeugPdf(Fahrzeug fahrzeug, string dateiPfad) { try { 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}")); document.Close(); return true; } catch { return false; } } // Erstellt ein PDF mit allen Fahrzeugen public bool ErstelleFahrzeuglistePdf(List fahrzeuge, string dateiPfad) { try { 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 }); 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); } document.Add(table); document.Close(); return true; } catch { return false; } } } }