private Cell createCell(iText.Layout.Element.Image content, HorizontalAlignment alignment, int colSpan) { content.SetHorizontalAlignment(alignment); Cell cell = new Cell(1, colSpan).Add(content); cell.SetBorder(iText.Layout.Borders.Border.NO_BORDER); return(cell); }
private void CreateDecklistPDF() { AddToLog("Creating PDF..."); List <Card> tempList = DeckList.ToList <Card>(); string qrURL = "https://www.underworldsdb.com/shared.php?deck=0,"; int objectiveCount = 0; int gambitCount = 0; int upgradeCount = 0; foreach (Card currentCard in tempList) { switch (currentCard.Type) { case "Objective": objectiveCount++; break; case "Ploy": gambitCount++; break; case "Spell": gambitCount++; break; case "Upgrade": upgradeCount++; break; } } int maxRows = Math.Max(objectiveCount, Math.Max(gambitCount, upgradeCount)); maxRows = maxRows + 3; if (!Directory.Exists(_decklistsFolderFullPath)) { Directory.CreateDirectory(_decklistsFolderFullPath); } string datePrefix = DateTime.Now.ToString("yyyyMMdd_HHmmss_"); string decklistFileName; if (DeckName.Length > 0) { decklistFileName = datePrefix + DeckName + _decklistFileExtension; } else { decklistFileName = datePrefix + _decklistFile + _decklistFileExtension; } string decklistFileFullPath = _decklistsFolderFullPath + decklistFileName; AddToLog("PDF file: " + decklistFileFullPath); PdfWriter pdfWriter = new PdfWriter(decklistFileFullPath); PdfDocument pdfDocument = new PdfDocument(pdfWriter); Document document = new Document(pdfDocument, PageSize.A4); document.SetMargins(20, 20, 20, 20); Table table = new Table(3); table.SetWidth(UnitValue.CreatePercentValue(100)); PdfFont regular = PdfFontFactory.CreateFont(iText.IO.Font.Constants.StandardFonts.HELVETICA); PdfFont bold = PdfFontFactory.CreateFont(iText.IO.Font.Constants.StandardFonts.HELVETICA_BOLD); for (int row = 1; row <= maxRows; row++) { for (int col = 1; col <= 3; col++) { if (row == 1 && col == 2) { if (DeckName.Length > 0) { table.AddCell(createCell(DeckName, TextAlignment.CENTER, bold, false, false, false, false)); } else { table.AddCell(createCell("Decklist", TextAlignment.CENTER, bold, false, false, false, false)); } continue; } if (row == 2 && col == 1) { table.AddCell(createCell("Objectives (" + objectiveCount.ToString() + ")", TextAlignment.CENTER, bold, false, false, false, false)); continue; } if (row == 2 && col == 2) { table.AddCell(createCell("Gambits (" + gambitCount.ToString() + ")", TextAlignment.CENTER, bold, false, false, false, false)); continue; } if (row == 2 && col == 3) { table.AddCell(createCell("Upgrades (" + upgradeCount.ToString() + ")", TextAlignment.CENTER, bold, false, false, false, false)); continue; } if (row > 2 && row < maxRows) { if (col == 1) { bool bFound = false; for (int i = 0; i < tempList.Count; i++) { if (tempList[i].Type == "Objective") { bool championship = false; if (tempList[i].OrganisedPlay.Substring(0, 1) == "V") { championship = true; } bool restricted = false; if (tempList[i].Restricted == "Restricted") { restricted = true; } bool surge = false; if (tempList[i].ObjType == "Surge") { surge = true; } table.AddCell(createCell(tempList[i].Name, TextAlignment.LEFT, regular, true, championship, restricted, surge)); qrURL += tempList[i].Number + ","; tempList.RemoveAt(i); bFound = true; break; } } if (bFound) { continue; } } if (col == 2) { bool bFound = false; for (int i = 0; i < tempList.Count; i++) { if (tempList[i].Type == "Ploy" || tempList[i].Type == "Spell") { bool championship = false; if (tempList[i].OrganisedPlay.Substring(0, 1) == "V") { championship = true; } bool restricted = false; if (tempList[i].Restricted == "Restricted") { restricted = true; } table.AddCell(createCell(tempList[i].Name, TextAlignment.LEFT, regular, true, championship, restricted, false)); qrURL += tempList[i].Number + ","; tempList.RemoveAt(i); bFound = true; break; } } if (bFound) { continue; } } if (col == 3) { bool bFound = false; for (int i = 0; i < tempList.Count; i++) { if (tempList[i].Type == "Upgrade") { bool championship = false; if (tempList[i].OrganisedPlay.Substring(0, 1) == "V") { championship = true; } bool restricted = false; if (tempList[i].Restricted == "Restricted") { restricted = true; } table.AddCell(createCell(tempList[i].Name, TextAlignment.LEFT, regular, true, championship, restricted, false)); qrURL += tempList[i].Number + ","; tempList.RemoveAt(i); bFound = true; break; } } if (bFound) { continue; } } } if (row == maxRows && col == 1) { QrCodeEncodingOptions options = new QrCodeEncodingOptions(); options = new QrCodeEncodingOptions { DisableECI = true, CharacterSet = "UTF-8", Width = 150, Height = 150, }; var writer = new BarcodeWriter(); writer.Format = BarcodeFormat.QR_CODE; writer.Options = options; qrURL = qrURL.Remove(qrURL.Length - 1, 1); if (DeckName.Length > 0) { qrURL += "&deckname=" + DeckName.Replace(" ", "%20"); } if (qrURL.Length <= 2953) { Image img = writer.Write(qrURL); MemoryStream ms = new MemoryStream(); img.Save(ms, ImageFormat.Bmp); ImageData rawImage = ImageDataFactory.Create(ms.ToArray()); iText.Layout.Element.Image image = new iText.Layout.Element.Image(rawImage); table.AddCell(createCell(image, HorizontalAlignment.CENTER, 3)); } break; } table.AddCell(createCell("", TextAlignment.CENTER, regular, false, false, false, false)); } } document.Add(table); AddToLog("Finalizing..."); document.Close(); pdfWriter.Close(); AddToLog("Finished"); }
private void CreateWarbandPDF() { int warbandsToCreate = 1; int warbandProgress = 0; AddToLog("Step 2/2 - Creating PDF..."); if (SelectedWarbandIndex == 1) { warbandsToCreate = WarbandList.Count() - 2; } if (!Directory.Exists(_warbandsFolderFullPath)) { Directory.CreateDirectory(_warbandsFolderFullPath); } for (int i = 2; i < warbandsToCreate + 2; i++) { warbandProgress++; string warbandName; int fighterSuccess = 0; bool isInspired = false; if (SelectedWarbandIndex == 1) { warbandName = WarbandList[i]; AddToLog("Warband " + warbandProgress.ToString() + "/" + warbandsToCreate.ToString() + " - " + warbandName); } else { warbandName = WarbandList[SelectedWarbandIndex]; AddToLog("Warband - " + warbandName); } string warbandFileName = warbandName + _warbandFileExtension; string warbandFileFullPath = _warbandsFolderFullPath + warbandFileName; AddToLog("PDF file: " + warbandFileFullPath); AddToLog("Creating file..."); PdfWriter pdfWriter = new PdfWriter(warbandFileFullPath); PdfDocument pdfDocument = new PdfDocument(pdfWriter); Document document = new Document(pdfDocument, PageSize.A4); document.SetMargins(20, 20, 20, 20); AddToLog("Creating layout..."); Table table = new Table(3, false); table.SetHorizontalAlignment(HorizontalAlignment.CENTER); AddToLog("Inserting fighters..."); for (int currentFighter = 1; currentFighter <= _maxFighters; currentFighter++) { if (!isInspired) { AddToLog("Fighter " + currentFighter.ToString() + " - Normal"); } else { AddToLog("Fighter " + currentFighter.ToString() + " - Inspired"); } string fighterFolderFullPath = _fightersFolderFullPath + warbandName + "\\"; string fighterFileName = GetPurifiedWarbandName(warbandName) + "-" + currentFighter.ToString(); if (isInspired) { fighterFileName += "-inspired"; } fighterFileName += _fighterFileExtension; string fighterFileFullPath = fighterFolderFullPath + fighterFileName; try { iText.Layout.Element.Image image = new iText.Layout.Element.Image(ImageDataFactory.Create(fighterFileFullPath)); image.ScaleAbsolute(_widthInPoints, _heightInPoints); Cell cell = new Cell(); cell.Add(image); cell.SetBorder(iText.Layout.Borders.Border.NO_BORDER); cell.SetPaddings(0, 1, 1, 0); table.AddCell(cell); fighterSuccess++; AddToLog("...Success!"); } catch { AddToLog("...doesn't exist. Aborting!"); break; } if (!isInspired) { currentFighter--; } isInspired = !isInspired; } document.Add(table); AddToLog(fighterSuccess + " inserted"); AddToLog("Finalizing..."); document.Close(); AddToLog("Step 2/2 - Finished"); pdfWriter.Close(); } }
private Cell createCell(string content, TextAlignment alignment, PdfFont font, bool isCard, bool championship, bool restricted, bool surge) { Paragraph p = new Paragraph(); if (isCard) { Image img; iText.Layout.Element.Image image; ImageData rawImage; if (championship) { img = (Image)Resources.ResourceManager.GetObject("res_trophy_green"); } else { img = (Image)Resources.ResourceManager.GetObject("res_trophy_red"); } using (MemoryStream ms = new MemoryStream()) { img.Save(ms, ImageFormat.Png); rawImage = ImageDataFactory.Create(ms.ToArray()); } image = new iText.Layout.Element.Image(rawImage); p.Add(image); p.Add(" "); p.Add(new Text(content)); if (surge) { img = (Image)Resources.ResourceManager.GetObject("res_lightning"); using (MemoryStream ms = new MemoryStream()) { img.Save(ms, ImageFormat.Png); rawImage = ImageDataFactory.Create(ms.ToArray()); } image = new iText.Layout.Element.Image(rawImage); p.Add(" "); p.Add(image); } if (restricted) { img = (Image)Resources.ResourceManager.GetObject("res_lock"); using (MemoryStream ms = new MemoryStream()) { img.Save(ms, ImageFormat.Png); rawImage = ImageDataFactory.Create(ms.ToArray()); } image = new iText.Layout.Element.Image(rawImage); p.Add(" "); p.Add(image); } } else { p.Add(new Text(content)); } Cell cell = new Cell(1, 1).Add(p); cell.SetTextAlignment(alignment); cell.SetBorder(iText.Layout.Borders.Border.NO_BORDER); cell.SetFont(font); return(cell); }
private void CreateDeckPDF() { int cardSuccess = 0; int cardError = 0; int cardProgress = 0; AddToLog("Step 2/2 - Creating PDF..."); if (!Directory.Exists(_decksFolderFullPath)) { Directory.CreateDirectory(_decksFolderFullPath); } string datePrefix = DateTime.Now.ToString("yyyyMMdd_HHmmss_"); string deckFileName; if (DeckName.Length > 0) { deckFileName = datePrefix + DeckName + _deckFileExtension; } else { deckFileName = datePrefix + _deckFile + _deckFileExtension; } string deckFileFullPath = _decksFolderFullPath + deckFileName; AddToLog("PDF file: " + deckFileFullPath); AddToLog("Creating file..."); PdfWriter pdfWriter = new PdfWriter(deckFileFullPath); PdfDocument pdfDocument = new PdfDocument(pdfWriter); Document document = new Document(pdfDocument, PageSize.A4); document.SetMargins(20, 20, 20, 20); AddToLog("Creating layout..."); Table table = new Table(3, false); table.SetHorizontalAlignment(iText.Layout.Properties.HorizontalAlignment.CENTER); AddToLog("Inserting cards..."); foreach (Card currentCard in DeckList) { cardProgress++; AddToLog("Card " + cardProgress.ToString() + "/" + DeckList.Count.ToString() + " - (" + currentCard.Number + ") " + currentCard.Name); try { string cardFolderFullPath = _cardsFolderFullPath + currentCard.Release + "\\"; string cardFileName = GetPurifiedCardName(currentCard) + _cardFileExtension; string cardFileFullPath = cardFolderFullPath + cardFileName; iText.Layout.Element.Image image = new iText.Layout.Element.Image(ImageDataFactory.Create(cardFileFullPath)); image.ScaleAbsolute(_widthInPoints, _heightInPoints); Cell cell = new Cell(); cell.Add(image); cell.SetBorder(iText.Layout.Borders.Border.NO_BORDER); cell.SetPaddings(0, 1, 1, 0); table.AddCell(cell); cardSuccess++; AddToLog("...Success!"); } catch { cardError++; AddToLog("...Error!"); } } document.Add(table); AddToLog(cardSuccess + " inserted"); AddToLog(cardError + " failed"); AddToLog("Finalizing..."); document.Close(); AddToLog("Step 2/2 - Finished"); pdfWriter.Close(); }