|
| 1 | +using Syncfusion.Pdf; |
| 2 | +using Syncfusion.Pdf.Graphics; |
| 3 | +using Syncfusion.Pdf.Grid; |
| 4 | +using Syncfusion.Drawing; |
| 5 | + |
| 6 | +namespace BlazorPDF.Data |
| 7 | +{ |
| 8 | + public class ExportService |
| 9 | + { |
| 10 | + private readonly IWebHostEnvironment _hostingEnvironment; |
| 11 | + public ExportService(IWebHostEnvironment hostingEnvironment) |
| 12 | + { |
| 13 | + _hostingEnvironment = hostingEnvironment; |
| 14 | + } |
| 15 | + public MemoryStream CreatePDF() |
| 16 | + { |
| 17 | + PdfDocument document = new PdfDocument(); |
| 18 | + PdfPage currentPage = document.Pages.Add(); |
| 19 | + SizeF clientSize = currentPage.GetClientSize(); |
| 20 | + FileStream imageStream = new FileStream(_hostingEnvironment.WebRootPath + "//images//pdf//icon.png", FileMode.Open, FileAccess.Read); |
| 21 | + PdfImage icon = new PdfBitmap(imageStream); |
| 22 | + SizeF iconSize = new SizeF(40, 40); |
| 23 | + PointF iconLocation = new PointF(14, 13); |
| 24 | + PdfGraphics graphics = currentPage.Graphics; |
| 25 | + graphics.DrawImage(icon, iconLocation, iconSize); |
| 26 | + PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20, PdfFontStyle.Bold); |
| 27 | + var headerText = new PdfTextElement("INVOICE", font, new PdfSolidBrush(Color.FromArgb(1, 53, 67, 168))); |
| 28 | + headerText.StringFormat = new PdfStringFormat(PdfTextAlignment.Right); |
| 29 | + PdfLayoutResult result = headerText.Draw(currentPage, new PointF(clientSize.Width - 25, iconLocation.Y + 10)); |
| 30 | + |
| 31 | + font = new PdfStandardFont(PdfFontFamily.Helvetica, 10); |
| 32 | + headerText = new PdfTextElement("To ", font); |
| 33 | + result = headerText.Draw(currentPage, new PointF(14, result.Bounds.Bottom + 30)); |
| 34 | + font = new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold); |
| 35 | + headerText = new PdfTextElement("John Smith,", font); |
| 36 | + result = headerText.Draw(currentPage, new PointF(14, result.Bounds.Bottom + 3)); |
| 37 | + font = new PdfStandardFont(PdfFontFamily.Helvetica, 10); |
| 38 | + headerText = new PdfTextElement(string.Format("{0}, {1}", "398 W Broadway, Evanston Ave Fargo,", "\nNorth Dakota, 10012"), font); |
| 39 | + result = headerText.Draw(currentPage, new PointF(14, result.Bounds.Bottom + 3)); |
| 40 | + |
| 41 | + font = new PdfStandardFont(PdfFontFamily.Helvetica, 10, PdfFontStyle.Bold); |
| 42 | + headerText = new PdfTextElement("Invoice No.#23698720 ", font); |
| 43 | + headerText.StringFormat = new PdfStringFormat(PdfTextAlignment.Right); |
| 44 | + headerText.Draw(currentPage, new PointF(clientSize.Width - 25, result.Bounds.Y - 20)); |
| 45 | + |
| 46 | + PdfGrid grid = new PdfGrid(); |
| 47 | + font = new PdfStandardFont(PdfFontFamily.Helvetica, 10, PdfFontStyle.Regular); |
| 48 | + grid.Style.Font = font; |
| 49 | + grid.Columns.Add(4); |
| 50 | + grid.Columns[1].Width = 70; |
| 51 | + grid.Columns[2].Width = 70; |
| 52 | + grid.Columns[3].Width = 70; |
| 53 | + |
| 54 | + grid.Headers.Add(1); |
| 55 | + PdfStringFormat stringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle); |
| 56 | + PdfGridRow header = grid.Headers[0]; |
| 57 | + |
| 58 | + header.Cells[0].Value = "Item & description"; |
| 59 | + header.Cells[0].StringFormat.LineAlignment = PdfVerticalAlignment.Middle; |
| 60 | + header.Cells[1].Value = "Hrs/Qty"; |
| 61 | + header.Cells[1].StringFormat = stringFormat; |
| 62 | + header.Cells[2].Value = "Rate($)"; |
| 63 | + header.Cells[2].StringFormat = stringFormat; |
| 64 | + header.Cells[3].Value = "Amount($)"; |
| 65 | + header.Cells[3].StringFormat = stringFormat; |
| 66 | + |
| 67 | + PdfGridCellStyle cellStyle = new PdfGridCellStyle(); |
| 68 | + cellStyle.Borders.All = PdfPens.Transparent; |
| 69 | + cellStyle.TextBrush = PdfBrushes.White; |
| 70 | + cellStyle.BackgroundBrush = new PdfSolidBrush(Color.FromArgb(1, 53, 67, 168)); |
| 71 | + |
| 72 | + for (int j = 0; j < header.Cells.Count; j++) |
| 73 | + { |
| 74 | + PdfGridCell cell = header.Cells[j]; |
| 75 | + cell.Style = cellStyle; |
| 76 | + } |
| 77 | + |
| 78 | + PdfGridRow row = grid.Rows.Add(); |
| 79 | + row.Cells[0].Value = "API Development"; |
| 80 | + row.Cells[0].StringFormat.LineAlignment = PdfVerticalAlignment.Middle; |
| 81 | + |
| 82 | + row.Cells[1].Value = $"{25}"; |
| 83 | + row.Cells[1].StringFormat = stringFormat; |
| 84 | + |
| 85 | + row.Cells[2].Value = $"{24.46}"; |
| 86 | + row.Cells[2].StringFormat = stringFormat; |
| 87 | + |
| 88 | + decimal amount = (decimal)(25 * 24.46); |
| 89 | + row.Cells[3].Value = String.Format("{0:0.##}", amount); |
| 90 | + row.Cells[3].StringFormat = stringFormat; |
| 91 | + |
| 92 | + decimal sum = 0; |
| 93 | + sum += amount; |
| 94 | + |
| 95 | + row = grid.Rows.Add(); |
| 96 | + row.Cells[0].Value = "Desktop Software Development"; |
| 97 | + row.Cells[0].StringFormat.LineAlignment = PdfVerticalAlignment.Middle; |
| 98 | + row.Cells[1].Value = $"{25}"; |
| 99 | + row.Cells[1].StringFormat = stringFormat; |
| 100 | + row.Cells[2].Value = $"{47.83}"; |
| 101 | + row.Cells[2].StringFormat = stringFormat; |
| 102 | + amount = (decimal)(25 * 47.83); |
| 103 | + row.Cells[3].Value = String.Format("{0:0.##}", amount); |
| 104 | + row.Cells[3].StringFormat = stringFormat; |
| 105 | + |
| 106 | + sum += amount; |
| 107 | + |
| 108 | + row = grid.Rows.Add(); |
| 109 | + row.Cells[0].Value = "Site admin development"; |
| 110 | + row.Cells[0].StringFormat.LineAlignment = PdfVerticalAlignment.Middle; |
| 111 | + row.Cells[1].Value = $"{33}"; |
| 112 | + row.Cells[1].StringFormat = stringFormat; |
| 113 | + row.Cells[2].Value = $"{85.1}"; |
| 114 | + row.Cells[2].StringFormat = stringFormat; |
| 115 | + amount = (decimal)(33 * 85.1); |
| 116 | + row.Cells[3].Value = String.Format("{0:0.##}", amount); |
| 117 | + row.Cells[3].StringFormat = stringFormat; |
| 118 | + |
| 119 | + sum += amount; |
| 120 | + |
| 121 | + grid.ApplyBuiltinStyle(PdfGridBuiltinStyle.PlainTable3); |
| 122 | + PdfGridStyle gridStyle = new PdfGridStyle(); |
| 123 | + gridStyle.CellPadding = new PdfPaddings(5, 5, 5, 5); |
| 124 | + grid.Style = gridStyle; |
| 125 | + |
| 126 | + PdfGridLayoutFormat layoutFormat = new PdfGridLayoutFormat(); |
| 127 | + layoutFormat.Layout = PdfLayoutType.Paginate; |
| 128 | + result = grid.Draw(currentPage, 14, result.Bounds.Bottom + 30, clientSize.Width - 35, layoutFormat); |
| 129 | + |
| 130 | + currentPage.Graphics.DrawRectangle(new PdfSolidBrush(Color.FromArgb(255, 239, 242, 255)), |
| 131 | + new RectangleF(result.Bounds.Right - 100, result.Bounds.Bottom + 20, 100, 20)); |
| 132 | + |
| 133 | + PdfTextElement element = new PdfTextElement("Total", font); |
| 134 | + element.Draw(currentPage, new RectangleF(result.Bounds.Right - 100, result.Bounds.Bottom + 22, result.Bounds.Width, result.Bounds.Height)); |
| 135 | + |
| 136 | + var totalPrice = $"$ {Math.Round(sum, 2)}"; |
| 137 | + element = new PdfTextElement(totalPrice, font); |
| 138 | + element.StringFormat = new PdfStringFormat(PdfTextAlignment.Right); |
| 139 | + element.Draw(currentPage, new RectangleF(15, result.Bounds.Bottom + 22, result.Bounds.Width, result.Bounds.Height)); |
| 140 | + |
| 141 | + |
| 142 | + MemoryStream stream = new MemoryStream(); |
| 143 | + document.Save(stream); |
| 144 | + document.Close(true); |
| 145 | + stream.Position = 0; |
| 146 | + return stream; |
| 147 | + |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments