/// <summary> /// Used for extract information about the header. /// </summary> /// <param name="tableHeader">Lines containing the header</param> /// <param name="separators">Lines following the header, delimitating the end of the header.</param> /// <returns>Meta-information about the input file</returns> /// <remarks> /// The software uses the line following the header in order to understand how long the field can be. /// Nor the line containing the header, neither the single field are valid indicators about the maximum lenght /// of the fields. /// </remarks> private List <ShipmentElement> HandleTableInfo(string tableHeader, string separators) { List <ShipmentElement> tableInfo = new List <ShipmentElement>(); int index = 0; while (index < separators.Length) { int start, end, lenght; //Try to find the lenght of one header element start = index; while (index + 1 < separators.Length && separators[++index] == SEPARATOR_CHAR) { ; } end = index; //Ignore white characters while (index + 1 < separators.Length && separators[++index] == ' ') { ; } lenght = Math.Min(end, tableHeader.Length) - start; if (lenght <= 0) { break; } ShipmentElement current = new ShipmentElement() { Name = tableHeader.Substring(start, lenght).Trim(), CharacterEnd = end, CharacterStart = start, Position = tableInfo.Count }; tableInfo.Add(current); } return(tableInfo); }
/// <summary> /// Used for creating the PDF output file /// </summary> /// <returns>Byte array containing the output PDF file</returns> public byte[] MakeFinalReport(ShipmentsData shipments) { Rectangle page = new Rectangle(PageSize.A4); PDFMakerSection section = ConfigurationManager.GetSection("PDFMaker") as PDFMakerSection; Dictionary <ShipmentElement, FinalReportSettingsElement> elementConfigurations = new Dictionary <ShipmentElement, FinalReportSettingsElement>(); // Merge the configuration with the info retrieved foreach (FinalReportSettingsElement field in section.FinalReportSettings) { ShipmentElement element = shipments.MetaInformation.Single(x => x.Name == field.SourceFieldName); elementConfigurations[element] = field; } using (var stream = new MemoryStream()) using (var document = new Document(page)) using (var writer = PdfWriter.GetInstance(document, stream)) { // Initialization of PDF file document.Open(); document.AddAuthor("Marco"); document.AddCreationDate(); document.AddLanguage("Italian"); document.AddTitle(string.Format("Report di spedizioni per il {0}", DateTime.Now.ToString("dd/M/yyyy"))); //Every iteration print information for a single courier foreach (var currentCourier in shipments.Shipments) { Chunk idPhrase = new Chunk(currentCourier.ID.ToString() + " ") { Font = FontFactory.GetFont(section.TitleFontName, section.IDFontSize, Font.BOLD, BaseColor.BLACK) }; Chunk namePhrase = new Chunk(currentCourier.Name) { Font = FontFactory.GetFont(section.TitleFontName, section.NameFontSize, Font.BOLD, BaseColor.BLACK) }; Chunk dateChunk = new Chunk(shipments.Date) { Font = FontFactory.GetFont(section.TitleFontName, section.DateFontSize, Font.BOLD, BaseColor.BLACK) }; Paragraph title = new Paragraph() { Alignment = 1 }; title.Add(idPhrase); title.Add(namePhrase); title.Add(Chunk.NEWLINE); title.Add(dateChunk); title.Add(Chunk.NEWLINE); title.Add(Chunk.NEWLINE); PdfPTable table = new PdfPTable(elementConfigurations.Count) { TotalWidth = 523, LockedWidth = true }; table.SetWidths(elementConfigurations.Select(x => x.Key.CharacterEnd - x.Key.CharacterStart).ToArray()); // Set the header foreach (var item in elementConfigurations) { table.AddCell(new PdfPCell(new Phrase(new Chunk(item.Value.DestinationFieldName) { Font = new Font(Font.FontFamily.COURIER, 9, Font.BOLD) }))); } // Every iteration add a line to the table foreach (var currentShipment in currentCourier.Shipments) { // Handling shipments with empty "mitt" field if (currentShipment.Single(x => x.Key.Name == "Mitt").Value.Length == 0) { continue; } // Every iteration add a cell to a line foreach (var currentShipmentElement in currentShipment) { if (elementConfigurations.ContainsKey(currentShipmentElement.Key)) { table.AddCell(new PdfPCell(new Phrase(new Chunk(currentShipmentElement.Value) { Font = new Font(Font.FontFamily.COURIER, 8) }))); } } // Handle records that doesn't contain all the columns table.CompleteRow(); } // Used to effectively create the page(s) associated to a given courier document.NewPage(); document.Add(title); if (table.Rows.Count > 1) { document.Add(table); } else { Paragraph noShipment = new Paragraph() { Alignment = 1 }; Chunk noShipmentMessage = new Chunk("Nessun ritiro.") { Font = new Font(Font.FontFamily.COURIER, 12, Font.BOLD) }; noShipment.Add(noShipmentMessage); document.Add(noShipment); } } return(stream.ToArray()); } }