I have used iText5 in a Software for generating ID-Cards (Name, Barcode etc.). It Produces a PDF-File with every ID-Card as a Page.
Now is iText5 not running anymore and I switched to 7. But the old Logic in new Syntax is producing not the same.
internal static string erzeugeAusweisdruck(List<int> schlüsselSammlung, String connectionString, string druck_id)
{
string rückgabe = "";
try
{
// einmaligen Dateinamen erzeugen
string dateiendungPdf = ".pdf";
string stempel = DateTime.Now.Year + "_" + DateTime.Now.Month + "_" + DateTime.Now.Day + "_" + DateTime.Now.Hour + "_" + DateTime.Now.Minute + "_" + DateTime.Now.Second;
string pdfDateinameKomplett = "AUSGABE/Ausweisdruck_" + stempel + dateiendungPdf;
rückgabe = pdfDateinameKomplett;
// Ausweisgrößen von Millimeter in Pixel umrechnen
float ausweishöhe = 153;
float ausweisbreite = 251;
// Viereck für die Ausweisgröße
iText.Kernel.Geom.Rectangle ausweisgröße = new iText.Kernel.Geom.Rectangle(0, 0, ausweisbreite, ausweishöhe);
using (PdfWriter writer = new PdfWriter(pdfDateinameKomplett))
{
using (PdfDocument pdfDoc = new PdfDocument(writer))
{
using (Document doc = new Document(pdfDoc, new iText.Kernel.Geom.PageSize(ausweisgröße)))
{
PdfDocumentInfo info = pdfDoc.GetDocumentInfo();
info.SetTitle("Ausweisdruck");
// Ausweise generieren (schlüsselSammlung durchlaufen)
foreach (int schlüssel in schlüsselSammlung)
{
// Antrag generieren und prüfen ob Ausweis-Nummer existiert
ausweisantrag tempAntrag = new ausweisantrag(schlüssel.ToString(), connectionString);
string ausweisnummer = "";
// Person erzeugen
string personid = tempAntrag.personID;
// kein so komplexes Objekt nötig !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// person personObjekt = new person(tempAntrag.personID);
person personObjekt = person.erzeugePersonObjektNurNameUndGeburtsdatum(tempAntrag.personID, connectionString);
// Ausweisnummer = Verbandsnummer
ausweisnummer = person.holeVerbandsnummer(tempAntrag.personID, connectionString);
// AB HIER EINZELNE AUSWEISE bzw. SEITEN
// itext Barcode (Code 39)
// BARCODE ERZEUGEN
Barcode39 barcode39 = new Barcode39(pdfDoc);
barcode39.SetCode(ausweisnummer);
barcode39.SetBarHeight(25);
// GDI+
iText.Layout.Element.Image bcImage = new iText.Layout.Element.Image(barcode39.CreateFormXObject(pdfDoc));
barcode39 = null;
// SCHRIFTEN
iText.Kernel.Colors.Color schwarz = ColorConstants.BLACK;
string schriftart = iText.IO.Font.Constants.StandardFonts.HELVETICA;
PdfFont schriftGrossFett = PdfFontFactory.CreateFont(schriftart);
Style grossFett = new Style().SetFontSize(10).SetBold().SetFontColor(schwarz).SetFont(schriftGrossFett);
PdfFont schriftGrossDünn = PdfFontFactory.CreateFont(schriftart);
Style grossDünn = new Style().SetFontSize(8).SetFontColor(schwarz).SetFont(schriftGrossDünn);
PdfFont schriftKlein = PdfFontFactory.CreateFont(schriftart);
Style klein = new Style().SetFontSize(6).SetFont(schriftKlein).SetFontColor(schwarz);
// Name und Geburtsdatum in einer Zeile (Tabelle)
// Paragraph hat im gegensatz zu Paragraph die Option ALIGNMENT
Paragraph labelGeburtsdatum = new Paragraph(personObjekt.geburtsdatum.Value.ToShortDateString()).AddStyle(grossDünn);
labelGeburtsdatum.SetTextAlignment(TextAlignment.RIGHT);
Paragraph labelMitgliedName = new Paragraph(personObjekt.vorname + " " + personObjekt.nachname).AddStyle(grossFett);
// LEADING ist Zeilenhöhe
labelMitgliedName.SetFixedLeading(8);
// Tabelle mit 2 Spalten (75% und 25% Breite)
Table tabelleNameGeburtsdatum = new Table(new float[] { 3, 1 });
tabelleNameGeburtsdatum.SetWidth(ausweisbreite);
//tabelleNameGeburtsdatum.LockedWidth = true;
tabelleNameGeburtsdatum.SetMargin(0);
Cell zelleName = new Cell();
zelleName.SetPaddingLeft(8);
zelleName.SetPaddingTop(65);
// JEDE ZELLE MUSS BORDER ENTFERNT WERDEN
Border keinRahmen = Border.NO_BORDER;
zelleName.SetBorder(keinRahmen);
zelleName.SetHorizontalAlignment(HorizontalAlignment.LEFT);
zelleName.Add(labelMitgliedName);
tabelleNameGeburtsdatum.AddCell(zelleName);
Cell zelleGeburtsdatum = new Cell();
zelleGeburtsdatum.SetPaddingTop(65);
zelleGeburtsdatum.SetPaddingRight(8);
zelleGeburtsdatum.SetBorder(keinRahmen);
zelleGeburtsdatum.SetHorizontalAlignment(HorizontalAlignment.RIGHT);
zelleGeburtsdatum.Add(labelGeburtsdatum);
tabelleNameGeburtsdatum.AddCell(zelleGeburtsdatum);
doc.Add(tabelleNameGeburtsdatum);
// STAMMVEREIN - Name und Nummer in einer Zeile
verein stammverein = person.holeStammverein(personid, connectionString);
string nameStammverein = institution.gebeNameVonInstitutionAngepasstAnDtype(stammverein.verein_id, connectionString);
string nummerStammverein = verein.holeVereinsnummerMitVereinID(stammverein.verein_id, connectionString);
Paragraph labelStammvereinName = new Paragraph(nameStammverein).AddStyle(grossDünn);
Paragraph labelStammvereinNummer = new Paragraph(nummerStammverein).AddStyle(grossDünn);
labelStammvereinNummer.SetTextAlignment(TextAlignment.RIGHT);
Table tabelleStammverein = new Table(new float[] { 3, 1 });
tabelleStammverein.SetWidth(ausweisbreite);
//tabelleStammverein.LockedWidth = true;
tabelleStammverein.SetMargin(0);
Cell zelleNameSV = new Cell();
// zelleNameSV.SetPaddingTop = Utilities.MillimetersToPoints(1);
zelleNameSV.SetPaddingLeft(8);
zelleNameSV.SetBorder(keinRahmen);
zelleNameSV.SetHorizontalAlignment(HorizontalAlignment.RIGHT);
zelleNameSV.Add(labelStammvereinName);
tabelleStammverein.AddCell(zelleNameSV);
Cell zelleNummerSV = new Cell();
zelleNummerSV.SetBorder(keinRahmen);
zelleNummerSV.SetHorizontalAlignment(HorizontalAlignment.RIGHT);
// zelleNummerSV.SetPaddingTop = Utilities.MillimetersToPoints(1);
zelleNummerSV.SetPaddingRight(8);
zelleNummerSV.Add(labelStammvereinNummer);
tabelleStammverein.AddCell(zelleNummerSV);
doc.Add(tabelleStammverein);
// Tabelle für Mitgliedsnummer, Barcode und Nested-Table für Disziplinen
Table tabelleBarcodeDisziplinen = new Table(new float[] { 1, 1 });
tabelleBarcodeDisziplinen.SetWidth(ausweisbreite);
//tabelleBarcodeDisziplinen.LockedWidth = true;
tabelleBarcodeDisziplinen.SetMargin(0);
// linke Spalte (DISZIPLINEN)
Cell zelleDisziplinen = new Cell();
zelleDisziplinen.SetHorizontalAlignment(HorizontalAlignment.LEFT);
zelleDisziplinen.SetBorder(keinRahmen);
zelleDisziplinen.SetPaddingLeft(8);
// zelleDisziplinen.SetHorizontalAlignment(HorizontalAlignment.LEFT);
// NESTED-Tabelle Disziplinen
// 3 Spalten
Table tableDisziplinen = new Table(new float[] { 1, 1, 1 });
tableDisziplinen.SetWidth(UnitValue.CreatePercentValue(100)).UseAllAvailableWidth();
// GÜLTIGE DISZIPLINEN HOLEN UND DRUCHLAUFEN
List<string> gültigeDisziplinen = disziplin.holeGueltigeDisziplinenFürAusweisZuEinerPerson(personid, connectionString);
// Zähler für anzahl verarbeiteter Spalten in der Tabelle um ggf. mit leeren Zellen auffüllen zu können
// NEU: Nur wenn auch Disziplinen vorhanden, SONST leer einfügen
if (gültigeDisziplinen.Count > 0)
{
int zählerSpalte = 1;
foreach (string disziplinVerein in gültigeDisziplinen)
{
Cell zelle = new Cell();
zelle.SetBorder(keinRahmen);
zelle.SetHorizontalAlignment(HorizontalAlignment.LEFT);
Paragraph inhalt = new Paragraph(disziplinVerein).AddStyle(klein); ;
inhalt.SetTextAlignment(TextAlignment.CENTER);
zelle.Add(inhalt);
tableDisziplinen.AddCell(zelle);
zählerSpalte++;
// Wenn Spalte 3 erreicht den Spaltenzähler wieder auf 1 setzen
if (zählerSpalte == 3)
{
zählerSpalte = 1;
}
}
// leere Zelle zum Auffüllen wenn Anzahl != 3 der Spalten in Tabelle
Cell zelleLeer = new Cell();
zelleLeer.SetBorder(keinRahmen);
zelleLeer.SetHorizontalAlignment(HorizontalAlignment.CENTER);
Paragraph inhaltLeer = new Paragraph(" ").AddStyle(klein);
zelleLeer.Add(inhaltLeer);
// prüfen bei welcher Spalte die Befüllung gestoppt hat
if (zählerSpalte == 1)
{
// zwei leere Zellen auffüllen
tableDisziplinen.AddCell(zelleLeer);
tableDisziplinen.AddCell(zelleLeer);
}
if (zählerSpalte == 2)
{
// eine leere Zelle auffüllen
tableDisziplinen.AddCell(zelleLeer);
}
zelleDisziplinen.Add(tableDisziplinen);
tabelleBarcodeDisziplinen.AddCell(zelleDisziplinen);
}
else
{
int zählerSpalte = 1;
Cell zelle = new Cell();
zelle.SetBorder(keinRahmen);
zelle.SetHorizontalAlignment(HorizontalAlignment.LEFT);
Paragraph inhalt = new Paragraph(" ").AddStyle(klein);
inhalt.SetTextAlignment(TextAlignment.CENTER);
zelle.Add(inhalt);
tableDisziplinen.AddCell(zelle);
// leere Zelle zum Auffüllen wenn Anzahl != 3 der Spalten in Tabelle
Cell zelleLeer = new Cell();
zelleLeer.SetBorder(keinRahmen);
zelleLeer.SetHorizontalAlignment(HorizontalAlignment.CENTER);
Paragraph inhaltLeer = new Paragraph(" ").AddStyle(klein);
zelleLeer.Add(inhaltLeer);
// prüfen bei welcher Spalte die Befüllung gestoppt hat
if (zählerSpalte == 1)
{
// zwei leere Zellen auffüllen
tableDisziplinen.AddCell(zelleLeer);
tableDisziplinen.AddCell(zelleLeer);
}
if (zählerSpalte == 2)
{
// eine leere Zelle auffüllen
tableDisziplinen.AddCell(zelleLeer);
}
zelleDisziplinen.Add(tableDisziplinen);
tabelleBarcodeDisziplinen.AddCell(zelleDisziplinen);
}
// rechte Spalte (AUSWEISNUMMER, BARCODE)
Paragraph labelAusweisnummer = new Paragraph(ausweisnummer).AddStyle(grossFett);
labelAusweisnummer.SetTextAlignment(TextAlignment.RIGHT);
Cell zelleBarcodeNummer = new Cell();
zelleBarcodeNummer.SetPaddingRight(8);
//zelleBarcodeNummer.SetPaddingTop = Utilities.MillimetersToPoints(5);
zelleBarcodeNummer.SetBorder(keinRahmen);
zelleBarcodeNummer.SetHorizontalAlignment(HorizontalAlignment.RIGHT);
zelleBarcodeNummer.Add(labelAusweisnummer);
// ÄNDERUNGEN GDI+ Fehler HIER:
// VORHER: iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(barcodeDateinameKomplett);
// iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(new MemoryStream(File.ReadAllBytes(barcodeDateinameKomplett)));
iText.Layout.Element.Image img = bcImage;
if (bcImage != null)
{
bcImage = null;
}
// Barcode-Image etwas verkleinern
img.ScaleAbsolute(Convert.ToSingle(double.Parse(img.GetImageWidth().ToString()) * 0.85), Convert.ToSingle(double.Parse(img.GetImageHeight().ToString()) * 0.85));
img.SetHorizontalAlignment(HorizontalAlignment.RIGHT);
img.SetPaddingTop(3);
zelleBarcodeNummer.Add(img);
// GDI+
if (img != null)
{
img = null;
}
// Druckdatum
Paragraph labelDruckdatum = new Paragraph(DateTime.Now.ToShortDateString()).AddStyle(klein);
labelDruckdatum.SetHorizontalAlignment(HorizontalAlignment.RIGHT);
zelleBarcodeNummer.Add(labelDruckdatum);
tabelleBarcodeDisziplinen.AddCell(zelleBarcodeNummer);
doc.Add(tabelleBarcodeDisziplinen);
}
}
}
}
}
catch (Exception ex)
{
rückgabe = "FEHLER: " + ex.Message;
mySQL.speichereFehlermeldung(druck_id, ex.Message, connectionString);
}
//mySQL.erzeugeSofortDownloadFürEineDatei("Ausweisdruck", "ausweise/" + pdfDateiname + ".pdf", mySession.Current.benutzer.benutzerID.ToString(), "Ausweisdruck");
return rückgabe;
}
In Itext5 I used
Document.NewPage();
for every ID-Card inside the Loop.
How can I realise that in iText7? PDF-File with multiple Pages, each with a Size of 5 * 8 centimetres.
Hope someone can give some Impulses. Thanks