private async void GeneratePDF_Click(object sender, RoutedEventArgs e) { #region Field Definitions IEnumerable <Adventure> products = Provider.GetProducts(); Stream fontStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("SampleBrowser.Pdf.Assets.verdana.ttf"); PdfStandardFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 8f); #endregion styleName = this.gridBuiltinstyleComboBox.SelectedItem.ToString(); PdfDocument document = new PdfDocument(); PdfPage page = document.Pages.Add(); #region PdfGrid grid = new PdfGrid(); grid.DataSource = products; PdfGridBuiltinStyle style = ConvertToPdfGridBuiltinStyle(styleName); PdfGridBuiltinStyleSettings setting = new PdfGridBuiltinStyleSettings(); setting.ApplyStyleForHeaderRow = true; setting.ApplyStyleForBandedRows = true; grid.Style.CellPadding.All = 2; grid.ApplyBuiltinStyle(style, setting); PdfGridLayoutFormat gridLayoutFormat = new PdfGridLayoutFormat(); gridLayoutFormat.Layout = PdfLayoutType.Paginate; gridLayoutFormat.Break = PdfLayoutBreakType.FitElement; grid.Draw(page, PointF.Empty, gridLayoutFormat); #endregion MemoryStream stream = new MemoryStream(); await document.SaveAsync(stream); document.Close(true); SaveFile(stream, "GridBuiltinStyle.pdf"); }
public ActionResult AddPrescription(PrescriptionViewModel model) { if (ModelState.IsValid && model.days != null) { var prescription = _db.Prescriptions.Where(p => p.PrescriptionId == model.Id).FirstOrDefault(); PdfDocument doc = new PdfDocument(); //Add a page. PdfPage page = doc.Pages.Add(); //Create PDF graphics for the page PdfGraphics graphics = page.Graphics; //Set the standard font PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 10); graphics.DrawString("Patient Name : " + prescription.appointment.patient.User.Name, font, PdfBrushes.Black, new PointF(0, 0)); graphics.DrawString("Appoiment Time " + prescription.appointment.AppointmentTime.ToString("dd-MMMM-yyyy hh:mm tt"), font, PdfBrushes.Black, new PointF(0, 12)); graphics.DrawString("Doctor Name : " + prescription.appointment.doctor.User.Name, font, PdfBrushes.Black, new PointF(0, 24)); graphics.DrawString("Doctor Contact No : " + prescription.appointment.doctor.User.PhoneNumber, font, PdfBrushes.Black, new PointF(0, 36)); //Create a PdfGrid. PdfGrid pdfGrid = new PdfGrid(); //Create a DataTable. DataTable dataTable = new DataTable(); //Add columns to the DataTable dataTable.Columns.Add("Medicine Name"); dataTable.Columns.Add("Quantity"); dataTable.Columns.Add("Days"); dataTable.Columns.Add("Times"); for (int i = 0; i < model.times.Count; i++) { dataTable.Rows.Add(new object[] { model.medicines[i], model.quantity[i], model.days[i], model.times[i] }); } //Assign data source. pdfGrid.DataSource = dataTable; PdfGridBuiltinStyleSettings tableStyleOption = new PdfGridBuiltinStyleSettings(); tableStyleOption.ApplyStyleForBandedRows = true; tableStyleOption.ApplyStyleForHeaderRow = true; pdfGrid.ApplyBuiltinStyle(PdfGridBuiltinStyle.GridTable4Accent4, tableStyleOption); //Draw grid to the page of PDF document. pdfGrid.Draw(page, new PointF(0, 50)); string filePathString = "~/Images/" + model.Id + ".pdf"; // Open the document in browser after saving it doc.Save(Server.MapPath(filePathString)); //close the document doc.Close(true); _db.Entry(prescription).State = EntityState.Modified; prescription.FileURL = filePathString; _db.SaveChanges(); TempData["Success"] = "Prescription added for appointment no. " + model.Id; return(RedirectToAction("Index", "DoctorAccount")); } return(View(model)); }
/// <summary> /// Gets PDF file for invoice using Syncfusion.Pdf /// </summary> /// <param name="invoiceId">invoice Id for which file should be generated</param> /// <returns>PDF file for invoice</returns> public FileStreamResult GetInvoicePdf(string invoiceId) { //Declaring colors var black = Color.FromArgb(255, 0, 0, 0); var white = Color.FromArgb(255, 255, 255, 255); var lightGray = Color.FromArgb(255, 220, 220, 220); var model = InvoiceManager.GetInvoiceViewModelById(invoiceId); model.InvoiceLines = InvoiceManager.GetInvoiceLineViewModels(invoiceId); var invoiceNo = model.InvoiceNumber; var invoiceHdrText = $"Faktura VAT: {invoiceNo}"; var customer = model.Customer; var invoiceLines = model.InvoiceLines; var lightGrayBrush = new PdfSolidBrush(lightGray); //Creating new PDF document instance PdfDocument document = new PdfDocument(); //Setting margin document.PageSettings.Margins.All = 20; //Adding a new page PdfPage page = document.Pages.Add(); PdfGraphics g = page.Graphics; //Load the custom font as stream Stream fontStream = System.IO.File.OpenRead("wwwroot/fonts/Roboto-Regular.ttf"); //Create a new PDF true type font. PdfTrueTypeFont customFont8 = new PdfTrueTypeFont(fontStream, 8, PdfFontStyle.Regular); PdfTrueTypeFont customFont8Bold = new PdfTrueTypeFont(fontStream, 8, PdfFontStyle.Bold); PdfTrueTypeFont customFont10 = new PdfTrueTypeFont(fontStream, 10, PdfFontStyle.Regular); PdfTrueTypeFont customFont12 = new PdfTrueTypeFont(fontStream, 12, PdfFontStyle.Regular); PdfTrueTypeFont customFont12Bold = new PdfTrueTypeFont(fontStream, 12, PdfFontStyle.Bold); PdfTrueTypeFont customFont30 = new PdfTrueTypeFont(fontStream, 30, PdfFontStyle.Regular); //Creating font instances PdfFont headerFont = customFont30; //PdfFont standardText12 = new PdfStandardFont(PdfFontFamily.Helvetica, 12); PdfFont standardText12 = customFont12; PdfFont standardText12Bold = customFont12Bold; PdfFont standardText10 = customFont10; PdfFont standardText8 = customFont8; PdfFont standardText8Bold = customFont8Bold; //Set page size document.PageSettings.Size = PdfPageSize.A4; //Set page orientation document.PageSettings.Orientation = PdfPageOrientation.Landscape; //pen var pen = new PdfPen(Color.Black) { Width = 0.5F }; //Invoice number printing var invoiceNoRectangle = new Syncfusion.Drawing.RectangleF(180, 30, 200, 100); g.DrawRectangle(lightGrayBrush, invoiceNoRectangle); var result = BodyContent(invoiceHdrText, headerFont, black, PdfTextAlignment.Center, PdfVerticalAlignment.Middle, page, invoiceNoRectangle); //Customer data printing var customerDataHeaderRectangle = new Syncfusion.Drawing.RectangleF(10, 200, 200, 15); var customerDataContentRectangle = new Syncfusion.Drawing.RectangleF(10, 215, 200, 100); g.DrawRectangle(lightGrayBrush, customerDataHeaderRectangle); result = BodyContent("Klient", standardText12Bold, black, PdfTextAlignment.Center, PdfVerticalAlignment.Middle, page, customerDataHeaderRectangle); g.DrawRectangle(pen, new PdfSolidBrush(white), customerDataContentRectangle); var rect = new RectangleF(12, 215, 200, 25); result = BodyContent(customer.Name, standardText10, black, PdfTextAlignment.Left, PdfVerticalAlignment.Top, page, rect); rect = new RectangleF(12, result.Bounds.Bottom + 5, 200, 25); result = BodyContent($"{customer.Street}/{customer.BuildingNumber}", standardText10, black, PdfTextAlignment.Left, PdfVerticalAlignment.Top, page, rect); rect = new RectangleF(12, result.Bounds.Bottom, 200, 25); result = BodyContent($"{customer.PostalCode} {customer.City}", standardText10, black, PdfTextAlignment.Left, PdfVerticalAlignment.Top, page, rect); //Dates data printing var datesX = page.Graphics.ClientSize.Width - customerDataHeaderRectangle.Width - customerDataHeaderRectangle.X; var datesY = customerDataHeaderRectangle.Y; var datesWidth = customerDataHeaderRectangle.Width; var datesHeight = customerDataHeaderRectangle.Height; //issue date var issueDateHeaderRectangle = new Syncfusion.Drawing.RectangleF(datesX, datesY, datesWidth, datesHeight); var issueDateDataContentRectangle = new Syncfusion.Drawing.RectangleF(datesX, datesY + datesHeight, datesWidth, 20); g.DrawRectangle(lightGrayBrush, issueDateHeaderRectangle); BodyContent("Data wydania", standardText12Bold, black, PdfTextAlignment.Center, PdfVerticalAlignment.Middle, page, issueDateHeaderRectangle); g.DrawRectangle(pen, new PdfSolidBrush(white), issueDateDataContentRectangle); result = BodyContent("2018-10-04", standardText10, black, PdfTextAlignment.Center, PdfVerticalAlignment.Middle, page, issueDateDataContentRectangle); //receipt date var receiptDateHeaderRectangle = new Syncfusion.Drawing.RectangleF(datesX, result.Bounds.Bottom + 9, datesWidth, datesHeight); var receiptDateDataContentRectangle = new Syncfusion.Drawing.RectangleF(datesX, result.Bounds.Bottom + 9 + datesHeight, datesWidth, 20); g.DrawRectangle(lightGrayBrush, receiptDateHeaderRectangle); BodyContent("Data przyjęcia", standardText12Bold, black, PdfTextAlignment.Center, PdfVerticalAlignment.Middle, page, receiptDateHeaderRectangle); g.DrawRectangle(pen, new PdfSolidBrush(white), receiptDateDataContentRectangle); result = BodyContent("2018-10-04", standardText10, black, PdfTextAlignment.Center, PdfVerticalAlignment.Middle, page, receiptDateDataContentRectangle); //receipt date var paymentDateHeaderRectangle = new Syncfusion.Drawing.RectangleF(datesX, result.Bounds.Bottom + 9, datesWidth, datesHeight); var paymentDateDataContentRectangle = new Syncfusion.Drawing.RectangleF(datesX, result.Bounds.Bottom + 9 + datesHeight, datesWidth, 20); g.DrawRectangle(lightGrayBrush, paymentDateHeaderRectangle); BodyContent("Data płatności", standardText12Bold, black, PdfTextAlignment.Center, PdfVerticalAlignment.Middle, page, paymentDateHeaderRectangle); g.DrawRectangle(pen, new PdfSolidBrush(white), paymentDateDataContentRectangle); result = BodyContent("2018-10-04", standardText10, black, PdfTextAlignment.Center, PdfVerticalAlignment.Middle, page, paymentDateDataContentRectangle); //adding grid for invoice lines PdfGrid grid = new PdfGrid(); grid.Style.AllowHorizontalOverflow = false; //Set Data source for invoice lines if (invoiceLines.Count() == 0) { invoiceLines = new List <InvoiceLineViewModel>() { new InvoiceLineViewModel() }; } grid.DataSource = invoiceLines.Select(x => new { ItemName = x.ItemName, Description = x.Description, Quantity = x.Quantity, Price = x.Price.ToString("N"), TaxRate = x.TaxRate.ToString("##"), NetValue = x.BaseNetto.ToString("N"), TaxValue = x.BaseTax.ToString("N"), GrossValue = x.BaseGross.ToString("N") }); //grid styling var styleName = "GridTable4"; PdfGridBuiltinStyleSettings setting = new PdfGridBuiltinStyleSettings(); setting.ApplyStyleForHeaderRow = false; setting.ApplyStyleForBandedRows = false; setting.ApplyStyleForBandedColumns = false; setting.ApplyStyleForFirstColumn = false; setting.ApplyStyleForLastColumn = false; setting.ApplyStyleForLastRow = false; var gridHeaderStyle = new PdfGridRowStyle() { BackgroundBrush = new PdfSolidBrush(lightGray), Font = standardText8Bold }; //Set layout properties PdfLayoutFormat format = new PdfLayoutFormat(); format.Break = PdfLayoutBreakType.FitElement; format.Layout = PdfLayoutType.Paginate; PdfGridBuiltinStyle style = (PdfGridBuiltinStyle)Enum.Parse(typeof(PdfGridBuiltinStyle), styleName); grid.ApplyBuiltinStyle(style, setting); grid.Style.Font = standardText8; grid.Style.CellPadding.All = 2; grid.Style.AllowHorizontalOverflow = false; //set header texts PdfGridRow pdfGridHeader = grid.Headers[0]; pdfGridHeader.Style = gridHeaderStyle; pdfGridHeader.Cells[0].Value = "Kod pozycji"; pdfGridHeader.Cells[1].Value = "Opis"; pdfGridHeader.Cells[2].Value = "Ilość"; pdfGridHeader.Cells[3].Value = "Cena"; pdfGridHeader.Cells[4].Value = "VAT(%)"; pdfGridHeader.Cells[5].Value = "Netto"; pdfGridHeader.Cells[6].Value = "VAT"; pdfGridHeader.Cells[7].Value = "Brutto"; //grid location var gridStartLocation = new PointF(0, result.Bounds.Bottom + 50); //Draw table result = grid.Draw(page, gridStartLocation, format); var summaryByTaxRate = invoiceLines .GroupBy(x => x.TaxRate) .Select(x => new { TaxRate = x.Key.ToString("##"), NetValue = x.Sum(y => y.BaseNetto).ToString("N"), TaxValue = x.Sum(y => y.BaseTax).ToString("N"), GrossValue = x.Sum(y => y.BaseGross).ToString("N") }); //adding summary grid PdfGrid summaryGrid = new PdfGrid(); summaryGrid.Style.AllowHorizontalOverflow = false; //Set Data source summaryGrid.DataSource = summaryByTaxRate; summaryGrid.ApplyBuiltinStyle(style, setting); summaryGrid.Style.Font = standardText8; summaryGrid.Style.CellPadding.All = 2; summaryGrid.Style.AllowHorizontalOverflow = false; //set header texts PdfGridRow pdfSummaryGridHeader = summaryGrid.Headers[0]; pdfSummaryGridHeader.Style = gridHeaderStyle; pdfSummaryGridHeader.Cells[0].Value = "według stawki VAT"; pdfSummaryGridHeader.Cells[1].Value = "wartość netto"; pdfSummaryGridHeader.Cells[2].Value = "wartość VAT"; pdfSummaryGridHeader.Cells[3].Value = "wartość brutto"; summaryGrid.Columns[0].Width = 80; summaryGrid.Columns[1].Width = 60; summaryGrid.Columns[2].Width = 60; summaryGrid.Columns[3].Width = 60; //create summary total GridRow var summaryTotalNetto = invoiceLines.Sum(x => x.BaseNetto); var summaryTotalTax = invoiceLines.Sum(x => x.BaseTax); var summaryTotalGross = invoiceLines.Sum(x => x.BaseGross); var summaryTotalRow = new PdfGridRow(summaryGrid); summaryGrid.Rows.Add(summaryTotalRow); summaryTotalRow.Cells[0].Value = "Razem:"; summaryTotalRow.Cells[0].Style.Font = customFont8Bold; summaryTotalRow.Cells[1].Value = summaryTotalNetto.ToString("N"); summaryTotalRow.Cells[2].Value = summaryTotalTax.ToString("N"); summaryTotalRow.Cells[3].Value = summaryTotalGross.ToString("N"); float columnsWidth = 0; foreach (PdfGridColumn column in summaryGrid.Columns) { columnsWidth += column.Width; } //Summary grid location var summaryGridStartLocation = new PointF(page.GetClientSize().Width - columnsWidth, result.Bounds.Bottom + 20); //Draw table result = summaryGrid.Draw(page, summaryGridStartLocation, format); //Saving the PDF to the MemoryStreamcolumnsWidth MemoryStream ms = new MemoryStream(); document.Save(ms); //If the position is not set to '0' then the PDF will be empty. ms.Position = 0; var contentType = "application/pdf"; var fileName = $"{invoiceNo.Replace("/", "_")}_{DateHelper.GetCurrentDatetime():yyyyMMdd}.pdf"; //Download the PDF document in the browser. FileStreamResult fileStreamResult = new FileStreamResult(ms, contentType); fileStreamResult.FileDownloadName = fileName; return(fileStreamResult); }
public ActionResult AdventureCycle(string styleName, string Header, string Bandedrow, string Bandedcolumn, string Firstcolumn, string Lastcolumn, string Lastrow, string InsideBrowser) { if (styleName == "" || styleName == null) { styleName = "GridTable4"; } //Create PDF document PdfDocument doc = new PdfDocument(); //Set font PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 7); //Create Pdf ben for drawing broder PdfPen borderPen = new PdfPen(PdfBrushes.DarkBlue); borderPen.Width = 0; //Create DataTable for source PdfPage page = doc.Pages.Add(); //Use DataTable as source PdfGrid grid = new PdfGrid(); //Create dataset with the "Customers" table from Norwind database IEnumerable <Orders> orders = Provider.GetOrdersData(_hostingEnvironment.WebRootPath); grid.Style.AllowHorizontalOverflow = true; //Set Data source grid.DataSource = orders; #region PdfGridBuiltinStyleSettings PdfGridBuiltinStyleSettings setting = new PdfGridBuiltinStyleSettings(); setting.ApplyStyleForHeaderRow = Header != null ? true : false; setting.ApplyStyleForBandedRows = Bandedrow != null ? true : false; setting.ApplyStyleForBandedColumns = Bandedcolumn != null ? true : false; setting.ApplyStyleForFirstColumn = Firstcolumn != null ? true : false; setting.ApplyStyleForLastColumn = Lastcolumn != null ? true : false; setting.ApplyStyleForLastRow = Lastrow != null ? true : false; #endregion //Set layout properties PdfLayoutFormat format = new PdfLayoutFormat(); format.Break = PdfLayoutBreakType.FitElement; format.Layout = PdfLayoutType.Paginate; PdfGridBuiltinStyle style = ConvertToPdfGridBuiltStyle(styleName); grid.ApplyBuiltinStyle(style, setting); grid.Style.Font = font; grid.Style.CellPadding.All = 2; grid.Style.AllowHorizontalOverflow = false; //Draw table grid.Draw(page, PointF.Empty, format); MemoryStream stream = new MemoryStream(); //Save the PDF document doc.Save(stream); //Close the PDF document doc.Close(true); stream.Position = 0; //Download the PDF document in the browser. FileStreamResult fileStreamResult = new FileStreamResult(stream, "application/pdf"); fileStreamResult.FileDownloadName = "AdventureCycle.pdf"; return(fileStreamResult); }
private void DrawDataTable(PdfPage page, PdfGraphics graphics) { //Create a new PdfGrid. PdfGrid pdfGrid = new PdfGrid(); //Pagination PdfGridLayoutFormat layoutFormat = new PdfGridLayoutFormat(); layoutFormat.Break = PdfLayoutBreakType.FitColumnsToPage; layoutFormat.Layout = PdfLayoutType.Paginate; //Cell Padding pdfGrid.Style.CellPadding.All = 4; //Cell align text PdfStringFormat centredText = new PdfStringFormat() { LineAlignment = PdfVerticalAlignment.Middle, Alignment = PdfTextAlignment.Center }; PdfStringFormat leftText = new PdfStringFormat() { LineAlignment = PdfVerticalAlignment.Middle, Alignment = PdfTextAlignment.Left }; PdfStringFormat rightText = new PdfStringFormat() { LineAlignment = PdfVerticalAlignment.Middle, Alignment = PdfTextAlignment.Right }; //Add 7 column pdfGrid.Columns.Add(5); pdfGrid.Columns[0].Width = 270; pdfGrid.Columns[1].Width = 50; pdfGrid.Columns[2].Width = 70; pdfGrid.Columns[3].Width = 80; pdfGrid.Columns[4].Width = 100; //Add header. pdfGrid.Headers.Add(1); PdfGridRow pdfGridHeader = pdfGrid.Headers[0]; pdfGridHeader.Style.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 14); pdfGridHeader.Cells[0].Value = AppResources.Description; pdfGridHeader.Cells[1].Value = AppResources.Taxes; pdfGridHeader.Cells[2].Value = AppResources.Quantity; pdfGridHeader.Cells[3].Value = AppResources.UnitPrice; pdfGridHeader.Cells[4].Value = AppResources.Price; //centre text dans cellule for (var i = 0; i < 5; i++) { pdfGridHeader.Cells[i].StringFormat = centredText; } //Add Rows string logo = model.CurrencyLogo; Stream fontstream = typeof(ContactsPage).GetTypeInfo().Assembly.GetManifestResourceStream("voltaire.Resources.Raleway-Regular.ttf"); for (int i = 0; i < OrderItems.Count; i++) { PdfGridRow pdfGridRow = pdfGrid.Rows.Add(); pdfGridRow.Style.Font = new PdfTrueTypeFont(fontstream, 12); pdfGridRow.Cells[0].Value = string.IsNullOrEmpty(OrderItems[i].Description) ? "" : OrderItems[i].Description; pdfGridRow.Cells[0].StringFormat = leftText; pdfGridRow.Cells[1].Value = OrderItems[i].TaxPercent.ToString() + "%"; pdfGridRow.Cells[1].StringFormat = centredText; pdfGridRow.Cells[2].Value = OrderItems[i].Quantity.ToString(); pdfGridRow.Cells[2].StringFormat = centredText; pdfGridRow.Cells[3].Value = OrderItems[i].Product.PriceUnit + logo; pdfGridRow.Cells[3].StringFormat = centredText; pdfGridRow.Cells[4].Value = (OrderItems[i].Product.PriceUnit * OrderItems[i].Product.ProductQty) + logo; pdfGridRow.Cells[4].StringFormat = rightText; } //ajout sub total PdfGridRow pdfGridRowTotal = pdfGrid.Rows.Add(); pdfGridRowTotal.Style.Font = new PdfTrueTypeFont(fontstream, 12); pdfGridRowTotal.Cells[0].ColumnSpan = 4; pdfGridRowTotal.Cells[0].Value = AppResources.TotalHT; pdfGridRowTotal.Cells[0].StringFormat = rightText; pdfGridRowTotal.Cells[4].Value = SaleOrder.AmountUntaxed + logo; pdfGridRowTotal.Cells[4].StringFormat = rightText; //ajout taxes if (true) { PdfGridRow pdfGridRowDeposit = pdfGrid.Rows.Add(); pdfGridRowDeposit.Style.Font = new PdfTrueTypeFont(fontstream, 12); pdfGridRowDeposit.Cells[0].ColumnSpan = 4; pdfGridRowDeposit.Cells[0].Value = AppResources.Taxes; pdfGridRowDeposit.Cells[0].StringFormat = rightText; pdfGridRowDeposit.Cells[4].Value = SaleOrder.AmountTax + logo; pdfGridRowDeposit.Cells[4].StringFormat = rightText; } //Total PdfGridRow pdfGridRowtotal = pdfGrid.Rows.Add(); pdfGridRowtotal.Style.Font = new PdfTrueTypeFont(fontstream, 12); pdfGridRowtotal.Cells[0].ColumnSpan = 4; pdfGridRowtotal.Cells[0].Value = AppResources.Total; pdfGridRowtotal.Cells[0].StringFormat = rightText; pdfGridRowtotal.Cells[4].Value = $"{SaleOrder.AmountTotal}{logo}"; pdfGridRowtotal.Cells[4].StringFormat = rightText; //signature if (Signature != null) { PdfGridRow pdfGridRowsign = pdfGrid.Rows.Add(); pdfGridRowsign.Height = 70; PdfBitmap pBmp = new PdfBitmap(new MemoryStream(Signature)); pdfGridRowsign.Cells[0].Style.BackgroundImage = pBmp; pdfGridRowsign.Cells[0].Value = ""; } //Style du PdfGrid PdfGridBuiltinStyleSettings tableStyleOption = new PdfGridBuiltinStyleSettings(); tableStyleOption.ApplyStyleForBandedRows = true; tableStyleOption.ApplyStyleForHeaderRow = true; pdfGrid.ApplyBuiltinStyle(PdfGridBuiltinStyle.GridTable4Accent1); //ecriture du pdfGrid resultDataTable = pdfGrid.Draw(page, new Syncfusion.Drawing.PointF(0, 300), layoutFormat); }