PDF-Layout verbessert
This commit is contained in:
parent
943c9df593
commit
cfb405a002
@ -7,7 +7,7 @@ namespace FahrzeugVerwaltung
|
|||||||
{
|
{
|
||||||
public class PdfService
|
public class PdfService
|
||||||
{
|
{
|
||||||
// Erstellt PDF für ein Fahrzeug
|
// Erstellt ein PDF mit formatierten Daten eines Fahrzeugs
|
||||||
public bool ErstelleFahrzeugPdf(Fahrzeug fahrzeug, string dateiPfad)
|
public bool ErstelleFahrzeugPdf(Fahrzeug fahrzeug, string dateiPfad)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@ -15,13 +15,33 @@ namespace FahrzeugVerwaltung
|
|||||||
using var document = new Document(PageSize.A4);
|
using var document = new Document(PageSize.A4);
|
||||||
PdfWriter.GetInstance(document, new FileStream(dateiPfad, FileMode.Create));
|
PdfWriter.GetInstance(document, new FileStream(dateiPfad, FileMode.Create));
|
||||||
document.Open();
|
document.Open();
|
||||||
document.Add(new Paragraph($"Fahrzeug: {fahrzeug.Marke} {fahrzeug.Modell}"));
|
|
||||||
document.Add(new Paragraph($"Baujahr: {fahrzeug.Baujahr}"));
|
var titelSchrift = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 16);
|
||||||
document.Add(new Paragraph($"Leistung: {fahrzeug.Leistung} PS"));
|
document.Add(new Paragraph("Fahrzeugdaten", titelSchrift)
|
||||||
document.Add(new Paragraph($"Kilometerstand: {fahrzeug.KilometerstandFormatiert}"));
|
{
|
||||||
document.Add(new Paragraph($"Farbe: {fahrzeug.Farbe}"));
|
Alignment = Element.ALIGN_CENTER,
|
||||||
document.Add(new Paragraph($"Kaufpreis: {fahrzeug.KaufpreisFormatiert}"));
|
SpacingAfter = 20f
|
||||||
document.Add(new Paragraph($"Aktueller Wert: {fahrzeug.AktuellerWertFormatiert}"));
|
});
|
||||||
|
|
||||||
|
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();
|
document.Close();
|
||||||
return true;
|
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<Fahrzeug> fahrzeuge, string dateiPfad)
|
public bool ErstelleFahrzeuglistePdf(List<Fahrzeug> fahrzeuge, string dateiPfad)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@ -39,22 +59,35 @@ namespace FahrzeugVerwaltung
|
|||||||
using var document = new Document(PageSize.A4.Rotate());
|
using var document = new Document(PageSize.A4.Rotate());
|
||||||
PdfWriter.GetInstance(document, new FileStream(dateiPfad, FileMode.Create));
|
PdfWriter.GetInstance(document, new FileStream(dateiPfad, FileMode.Create));
|
||||||
document.Open();
|
document.Open();
|
||||||
var table = new PdfPTable(8) { WidthPercentage = 100 };
|
|
||||||
string[] headers = { "Marke", "Modell", "Baujahr", "Leistung", "Kilometer", "Kaufpreis", "Farbe", "Aktueller Wert" };
|
var titelSchrift = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 16);
|
||||||
foreach (var h in headers)
|
document.Add(new Paragraph("Fahrzeugliste", titelSchrift)
|
||||||
table.AddCell(new PdfPCell(new Phrase(h)) { BackgroundColor = BaseColor.LIGHT_GRAY });
|
{
|
||||||
|
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)
|
foreach (var f in fahrzeuge)
|
||||||
{
|
{
|
||||||
table.AddCell(f.Marke);
|
tabelle.AddCell(f.Marke);
|
||||||
table.AddCell(f.Modell);
|
tabelle.AddCell(f.Modell);
|
||||||
table.AddCell(f.Baujahr.ToString());
|
tabelle.AddCell(f.Baujahr.ToString());
|
||||||
table.AddCell($"{f.Leistung} PS");
|
tabelle.AddCell($"{f.Leistung} PS");
|
||||||
table.AddCell(f.KilometerstandFormatiert);
|
tabelle.AddCell(f.KilometerstandFormatiert);
|
||||||
table.AddCell(f.KaufpreisFormatiert);
|
tabelle.AddCell(f.KaufpreisFormatiert);
|
||||||
table.AddCell(f.Farbe);
|
tabelle.AddCell(f.Farbe);
|
||||||
table.AddCell(f.AktuellerWertFormatiert);
|
tabelle.AddCell(f.AktuellerWertFormatiert);
|
||||||
}
|
}
|
||||||
document.Add(table);
|
|
||||||
|
document.Add(tabelle);
|
||||||
document.Close();
|
document.Close();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user