private OrderlineUniter UniteOrderlines(List <OrderLine> unSortedOrderlines) { OrderlineUniter uniter = new OrderlineUniter(); foreach (OrderLine orderLine in unSortedOrderlines) { OrderWrapper key = uniter.QuantityPerProduct.FirstOrDefault(x => x.ProdV.Id == orderLine.ProductVersion.Id); if (key != null && key.OrderedUnit == orderLine.Unit) { //The orderline already exists in the list, so we simply add the quantity of the current orderline. key.Quantity += orderLine.Quantity; } else { //The orderlines does not exsist, so we add a new one to the list. uniter.QuantityPerProduct.Add(new OrderWrapper(orderLine.Quantity, orderLine.Unit, orderLine.ProductVersion)); } } //Sorts the orderlines by name. Orderlines with the same name but different units will be two different entries, but grouped together. uniter.Sort(); return(uniter); }
private void CreatePDF(OrderlineUniter orders) { string danishAlphabet = "abcdefghijklmnopqrstuvwxyzæøåABCDEFGHIJKLMNOPQRSTUVWXYZÆØÅ"; //Used to get the height ratio. //PDF Sharp initializations. PdfDocument pdf = new PdfDocument(); PdfPage pdfPage = pdf.AddPage(); XGraphics graph = XGraphics.FromPdfPage(pdfPage); XFont fontHeader = new XFont("Verdana", 13, XFontStyle.Bold); XFont fontParagraph = new XFont("Verdana", 13, XFontStyle.Regular); XSize size = PageSizeConverter.ToSize(PdfSharp.PageSize.A4); //Margins int marginTop = 20; int marginLeft = 50; int marginRight = 50; //Line height double lineHeight = graph.MeasureString(danishAlphabet, fontParagraph).Height + 5; //Calculates the headline positions. string nameString = "Produkt"; double nameLength = graph.MeasureString(nameString, fontHeader).Width; double nameX = marginLeft; string unitString = "Enhed"; double unitLength = graph.MeasureString(unitString, fontHeader).Width; double unitX = size.Width - unitLength - marginRight; string quantityString = "Antal"; double quantityLength = graph.MeasureString(quantityString, fontHeader).Width; double quantityX = size.Width - nameLength - unitLength - marginRight; //Draws the product name headline. graph.DrawString( nameString, fontHeader, XBrushes.Black, new XRect(nameX, marginTop, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft); //Draws the quantity headline. graph.DrawString( quantityString, fontHeader, XBrushes.Black, new XRect(quantityX, marginTop, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft); //Draws the unit headline. graph.DrawString( unitString, fontHeader, XBrushes.Black, new XRect(unitX, marginTop, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft); //Draw entries for (int i = 0; i < orders.QuantityPerProduct.Count; i++) { //Calculates the position of the current line. double lineY = lineHeight * (i + 1); //Gives every second line a light gray background color. if (i % 2 == 1) { XSolidBrush brush = new XSolidBrush(XColors.LightGray); graph.DrawRectangle(brush, marginLeft, lineY + marginTop, size.Width - marginLeft - marginRight, lineHeight - 2); } //Draws the product name. graph.DrawString( orders.QuantityPerProduct.ElementAt(i).ProdV.Product.Name, fontParagraph, XBrushes.Black, new XRect(nameX, marginTop + lineY, pdfPage.Width, pdfPage.Height), XStringFormats.TopLeft); //Draws the quantity. graph.DrawString( orders.QuantityPerProduct.ElementAt(i).Quantity.ToString(), fontParagraph, XBrushes.Black, new XRect(quantityX, marginTop + lineY, pdfPage.Width, pdfPage.Height), XStringFormats.TopLeft); //Draws the unit. graph.DrawString( orders.QuantityPerProduct.ElementAt(i).OrderedUnit.Name, fontParagraph, XBrushes.Black, new XRect(unitX, marginTop + lineY, pdfPage.Width, pdfPage.Height), XStringFormats.TopLeft); } //Code used to setup the save file dialog. SaveFileDialog saveFileDialog = new SaveFileDialog(); //Asks the user for the position of the code. saveFileDialog.Title = "Save PDF"; saveFileDialog.CheckPathExists = true; saveFileDialog.DefaultExt = "pdf"; saveFileDialog.Filter = "PDF (*.pdf)|*.pdf|All files (*.*)|*.*"; saveFileDialog.FilterIndex = 2; saveFileDialog.RestoreDirectory = true; saveFileDialog.FileName = orders.QuantityPerProduct.ElementAt(0).ProdV.Supplier; //File name corresponds to name of the supplier by default. //Shows the save file dialog. if (saveFileDialog.ShowDialog() == DialogResult.OK) { pdf.Save(saveFileDialog.FileName); } //Opens the newly generated pdf. Process.Start(saveFileDialog.FileName); }