void Pdf_Clicked(object sender, EventArgs args) { PdfDocument document = new PdfDocument(); document.PageSettings.Orientation = PdfPageOrientation.Landscape; document.PageSettings.Margins.All = 50; PdfPage page = document.Pages.Add(); PdfGraphics g = page.Graphics; PdfTextElement element = new PdfTextElement(@"Syncfusion Software 2501 Aerial Center Parkway Suite 200 Morrisville, NC 27560 USA Tel +1 888.936.8638 Fax +1 919.573.0306"); element.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 12); element.Brush = new PdfSolidBrush(new PdfColor(89, 89, 93)); PdfLayoutResult result = element.Draw(page, new RectangleF(0, 0, page.Graphics.ClientSize.Width / 2, 200)); Stream imgStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("XamarinIOInvoice.SyncfusionLogo.jpg"); PdfImage img = PdfImage.FromStream(imgStream); page.Graphics.DrawImage(img, new RectangleF(g.ClientSize.Width - 200, result.Bounds.Y, 190, 45)); PdfFont subHeadingFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 14); g.DrawRectangle(new PdfSolidBrush(new PdfColor(126, 151, 173)), new RectangleF(0, result.Bounds.Bottom + 40, g.ClientSize.Width, 20)); PdfGrid grid = new PdfGrid(); grid.DataSource = GetDataSource(); PdfGridCellStyle cellStyle = new PdfGridCellStyle(); cellStyle.Borders.All = PdfPens.White; PdfGridRow header = grid.Headers[0]; PdfGridCellStyle headerStyle = new PdfGridCellStyle(); headerStyle.Borders.All = new PdfPen(new PdfColor(126, 151, 173)); headerStyle.BackgroundBrush = new PdfSolidBrush(new PdfColor(126, 151, 173)); headerStyle.TextBrush = PdfBrushes.White; headerStyle.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 14f, PdfFontStyle.Regular); for (int i = 0; i < header.Cells.Count; i++) { if (i == 0) header.Cells[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); else header.Cells[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle); } header.ApplyStyle(headerStyle); cellStyle.Borders.Bottom = new PdfPen(new PdfColor(217, 217, 217), 0.70f); cellStyle.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 12f); cellStyle.TextBrush = new PdfSolidBrush(new PdfColor(131, 130, 136)); foreach (PdfGridRow row in grid.Rows) { row.ApplyStyle(cellStyle); for (int i = 0; i < row.Cells.Count; i++) { PdfGridCell cell = row.Cells[i]; if (i == 0) cell.StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); else cell.StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle); if (i > 1) { float val = float.MinValue; float.TryParse(cell.Value.ToString(), out val); cell.Value = "$" + val.ToString(); } } } PdfGridLayoutFormat layoutFormat = new PdfGridLayoutFormat(); layoutFormat.Layout = PdfLayoutType.Paginate; PdfGridLayoutResult gridResult = grid.Draw(page, new RectangleF(new Syncfusion.Drawing.PointF(0, result.Bounds.Bottom + 40), new Syncfusion.Drawing.SizeF(g.ClientSize.Width, g.ClientSize.Height - 100)), layoutFormat); float pos = 0.0f; for (int i = 0; i < grid.Columns.Count - 1; i++) pos += grid.Columns[i].Width; PdfFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 14f); gridResult.Page.Graphics.DrawString("Total Due", font, new PdfSolidBrush(new PdfColor(126, 151, 173)), new RectangleF(new Syncfusion.Drawing.PointF(pos, gridResult.Bounds.Bottom + 20), new Syncfusion.Drawing.SizeF(grid.Columns[3].Width - pos, 20)), new PdfStringFormat(PdfTextAlignment.Right)); gridResult.Page.Graphics.DrawString("Thank you for your business!", new PdfStandardFont(PdfFontFamily.TimesRoman, 12), new PdfSolidBrush(new PdfColor(89, 89, 93)), new Syncfusion.Drawing.PointF(pos - 55, gridResult.Bounds.Bottom + 60)); pos += grid.Columns[4].Width; gridResult.Page.Graphics.DrawString("$13600", font, new PdfSolidBrush(new PdfColor(131, 130, 136)), new RectangleF(new Syncfusion.Drawing.PointF(pos, gridResult.Bounds.Bottom + 20), new Syncfusion.Drawing.SizeF(grid.Columns[4].Width - pos, 20)), new PdfStringFormat(PdfTextAlignment.Right)); MemoryStream data = new MemoryStream(); document.Save(data); document.Close(); DependencyService.Get<ISave>().SaveTextAsync("Invoice.pdf", "application/pdf", data); }
private void options_CellExporting(object sender, DataGridCellPdfExportingEventArgs e) { if (e.CellType == ExportCellType.StackedHeaderCell) { if (e.CellValue.ToString() == "Order Details") { var cellStyle = new PdfGridCellStyle(); cellStyle.BackgroundBrush = PdfBrushes.DarkCyan; cellStyle.TextBrush = PdfBrushes.White; e.PdfGridCell.Style = cellStyle; } else if (e.CellValue.ToString() == "Customer Details") { var cellStyle = new PdfGridCellStyle(); cellStyle.BackgroundBrush = PdfBrushes.LightCyan; e.PdfGridCell.Style = cellStyle; } else if (e.CellValue.ToString() == "City Details") { var cellStyle = new PdfGridCellStyle(); cellStyle.BackgroundBrush = PdfBrushes.DarkGray; cellStyle.TextBrush = PdfBrushes.White; e.PdfGridCell.Style = cellStyle; } } }
/// <summary> /// Method for export the selected records in SfDataGrid to PDF /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void OnPdfExportSelectedItems(object sender, RoutedEventArgs e) { cellstyle = new PdfGridCellStyle(); cellstyle.StringFormat = new PdfStringFormat() { Alignment = PdfTextAlignment.Right }; cellstyle.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 9f, PdfFontStyle.Regular); var options = new PdfExportingOptions() { AutoColumnWidth = (bool)this.ColumnWidth.IsChecked, AutoRowHeight = (bool)this.RowHeight.IsChecked, ExportGroups = (bool)this.ExportGroup.IsChecked, ExportGroupSummary = (bool)this.ExportGroupSummary.IsChecked, ExportTableSummary = (bool)this.ExportTableSummary.IsChecked, RepeatHeaders = (bool)this.RepeatHeader.IsChecked, FitAllColumnsInOnePage = (bool)this.FitAllColumns.IsChecked, ExportingEventHandler = GridPdfExportingEventhandler, CellsExportingEventHandler = GridCellPdfExportingEventhandler, PageHeaderFooterEventHandler = PdfHeaderFooterEventHandler }; var pdfDocument = this.sfGrid.ExportToPdf(this.sfGrid.SelectedItems, options); pdfDocument.PageSettings.Margins.Top = -34f; pdfDocument.PageSettings.Margins.Bottom = -30f; var savePicker = new FileSavePicker { SuggestedStartLocation = PickerLocationId.Desktop, SuggestedFileName = "Sample" }; savePicker.FileTypeChoices.Add("Pdf Files(.pdf)", new List <string>() { ".pdf" }); var storageFile = await savePicker.PickSaveFileAsync(); if (storageFile != null) { await pdfDocument.SaveAsync(storageFile); var msgDialog = new MessageDialog("Do you want to view the Document?", "File has been created successfully."); var yesCmd = new UICommand("Yes"); var noCmd = new UICommand("No"); msgDialog.Commands.Add(yesCmd); msgDialog.Commands.Add(noCmd); var cmd = await msgDialog.ShowAsync(); if (cmd == yesCmd) { // Launch the saved file bool success = await Windows.System.Launcher.LaunchFileAsync(storageFile); } } }
static void CellsExportingEventHandler(object sender, TreeGridCellPdfExportingEventArgs e) { if (e.CellType == TreeGridCellType.RecordCell && e.ColumnName == "Title") { var cellstyle = new PdfGridCellStyle(); cellstyle.BackgroundBrush = PdfBrushes.LightGreen; e.PdfGridCell.Style = cellstyle; } }
private static async Task CreatePDFAsync() { PdfDocument document = new PdfDocument(); //Add a page to the document. PdfPage page = document.Pages.Add(); //Create PDF graphics for the page. PdfGraphics graphics = page.Graphics; //Set the standard font. PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20); //Draw the text. graphics.DrawString("This is a grid", font, PdfBrushes.Black, new PointF(10, 10)); var pdfBitmap1 = await GetPdfBitmap("watch"); graphics.DrawImage(pdfBitmap1, 10, 60, 120, 120); //Creates the PdfGrid PdfGrid pdfGrid = new PdfGrid(); //Adds the columns pdfGrid.Columns.Add(); pdfGrid.Columns.Add(); pdfGrid.Columns[0].Width = 50; //Adding grid cell style PdfGridCellStyle rowStyle = new PdfGridCellStyle(); //Creating Border PdfBorders border = new PdfBorders(); border.All = PdfPens.Blue; //setting border to the style rowStyle.Borders = border; //Adds the row and value to the cell for (int i = 0; i < 10; i++) { PdfGridRow pdfGridRow = pdfGrid.Rows.Add(); pdfGrid.Rows[i].Height = 80; pdfGridRow.Cells[1].Value = "asdf asdf"; //pdfGridRow.Cells[0].Style = rowStyle; pdfGridRow.Cells[1].Style = rowStyle; //Applies the image pdfGrid.Rows[i].Cells[0].Style.BackgroundImage = pdfBitmap1; pdfGrid.Rows[i].Cells[0].Value = ""; } //Draws the Grid pdfGrid.Draw(page, new PointF(10, 200)); await SaveFile(document); }
public IActionResult generatePDF() { PdfDocument doc = new PdfDocument(); //Add a page. PdfPage page = doc.Pages.Add(); //Create a PdfGrid. PdfGrid pdfGrid = new PdfGrid(); PdfGraphics graphics = page.Graphics; //Load the image as stream. FileStream imageStream = new FileStream("wwwroot/images/logo2.png", FileMode.Open, FileAccess.Read); PdfBitmap image = new PdfBitmap(imageStream); graphics.DrawImage(image, 0, 0); PdfGridCellStyle headerStyle = new PdfGridCellStyle(); headerStyle.Borders.All = new PdfPen(new PdfColor(126, 151, 173)); headerStyle.BackgroundBrush = new PdfSolidBrush(new PdfColor(126, 151, 173)); headerStyle.TextBrush = PdfBrushes.White; headerStyle.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 14f, PdfFontStyle.Regular); //Add values to list //List<object> data = new List<object>(); var list_all = _context.Shop_items.ToList(); //Object row1 = new { ID = "E01", Name = "Clay" }; //Object row2 = new { ID = "E02", Name = "Thomas" }; //Object row3 = new { ID = "E03", Name = "Andrew" }; //Object row4 = new { ID = "E04", Name = "Paul" }; //Object row5 = new { ID = "E05", Name = "Gray" }; //data.Add(row1); //data.Add(row2); //data.Add(row3); //data.Add(row4); //data.Add(row5); //Add list to IEnumerable IEnumerable <object> dataTable = list_all; //Assign data source. pdfGrid.DataSource = dataTable; //Draw grid to the page of PDF document. pdfGrid.Draw(page, new Syncfusion.Drawing.PointF(10, 10)); //Save the PDF document to stream MemoryStream stream = new MemoryStream(); doc.Save(stream); //If the position is not set to '0' then the PDF will be empty. stream.Position = 0; //Close the document. doc.Close(true); //Defining the ContentType for pdf file. string contentType = "application/pdf"; //Define the file name. string fileName = "Output.pdf"; //Creates a FileContentResult object by using the file contents, content type, and file name. return(File(stream, contentType, fileName)); }
private void PdfGrid_BeginCellLayout(object sender, PdfGridBeginCellLayoutEventArgs args) { // setting transparent color pen as table BorderPen. PdfPen transparentPen = new PdfPen(new PdfColor(System.Drawing.Color.FromArgb(0, 255, 255, 255)), .3f); PdfGridCellStyle pdfGridCellStyle = new PdfGridCellStyle(); pdfGridCellStyle.Borders.All = transparentPen; args.Style = pdfGridCellStyle; args.Style.StringFormat = new PdfStringFormat(PdfTextAlignment.Center); }
static Commands() { cellstyle = new PdfGridCellStyle(); cellstyle.StringFormat = new PdfStringFormat() { Alignment = PdfTextAlignment.Right }; var font = new Font("Segoe UI", 9f, System.Drawing.FontStyle.Regular); cellstyle.Font = new PdfTrueTypeFont(font, true); CommandManager.RegisterClassCommandBinding(typeof(SfDataGrid), new CommandBinding(ExportToPdf, OnExecuteExportToPdf, OnCanExecuteExportToExcel)); }
public ExportToPdfHelper() { cellstyle = new PdfGridCellStyle(); cellstyle.StringFormat = new PdfStringFormat() { Alignment = PdfTextAlignment.Right }; var font = new Font("Segoe UI", 9f, System.Drawing.FontStyle.Regular); cellstyle.Font = new PdfTrueTypeFont(font, true); cellstyle.Borders.All = new PdfPen(PdfBrushes.DarkGray, 0.2f); //CommandManager.RegisterClassCommandBinding(typeof(SfDataGrid), new CommandBinding(ExportToPdf, OnExecuteExportToPdf, OnCanExecuteExportToExcel)); }
private void PdfGrid_BeginCellLayout(object sender, PdfGridBeginCellLayoutEventArgs args) { // setting transparent color pen as table BorderPen. PdfPen transparentPen = new PdfPen(new PdfColor(System.Drawing.Color.FromArgb(Color.Transparent.A, Color.Transparent.R, Color.Transparent.G, Color.Transparent.B)), .3f); PdfGridCellStyle pdfGridCellStyle = new PdfGridCellStyle(); pdfGridCellStyle.Borders.All = transparentPen; args.Style = pdfGridCellStyle; args.Style.Font = new PdfTrueTypeFont(new Font("Arial", 8, FontStyle.Regular), true); args.Style.StringFormat = new PdfStringFormat(PdfTextAlignment.Center); }
private async void OnPdfExportDataGrid(object sender, RoutedEventArgs e) { cellstyle = new PdfGridCellStyle(); cellstyle.StringFormat = new PdfStringFormat() { Alignment = PdfTextAlignment.Right }; cellstyle.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 9f, PdfFontStyle.Regular); cellstyle.Borders.All = new PdfPen(PdfBrushes.DarkGray, 0.2f); var options = new PdfExportingOptions() { AutoColumnWidth = (bool)this.ColumnWidth.IsChecked, AutoRowHeight = (bool)this.RowHeight.IsChecked, ExportGroups = (bool)this.ExportGroup.IsChecked, ExportGroupSummary = (bool)this.ExportGroupSummary.IsChecked, ExportTableSummary = (bool)this.ExportTableSummary.IsChecked, RepeatHeaders = (bool)this.RepeatHeader.IsChecked, FitAllColumnsInOnePage = (bool)this.FitAllColumns.IsChecked, ExportingEventHandler = GridPdfExportingEventhandler, CellsExportingEventHandler = GridCellPdfExportingEventhandler, PageHeaderFooterEventHandler = PdfHeaderFooterEventHandler }; var pdfDocument = this.sfDataGrid.ExportToPdf(this.sfDataGrid.SelectedItems, options); pdfDocument.PageSettings.Margins.Top = -34f; pdfDocument.PageSettings.Margins.Bottom = -30f; var savePicker = new FileSavePicker { SuggestedStartLocation = PickerLocationId.Desktop, SuggestedFileName = "Sample" }; savePicker.FileTypeChoices.Add("Pdf Files(.pdf)", new List<string>() { ".pdf" }); var storageFile = await savePicker.PickSaveFileAsync(); if (storageFile != null) { await pdfDocument.SaveAsync(storageFile); var msgDialog = new MessageDialog("Do you want to view the Document?", "File has been created successfully."); var yesCmd = new UICommand("Yes"); var noCmd = new UICommand("No"); msgDialog.Commands.Add(yesCmd); msgDialog.Commands.Add(noCmd); var cmd = await msgDialog.ShowAsync(); if (cmd == yesCmd) { // Launch the saved file bool success = await Windows.System.Launcher.LaunchFileAsync(storageFile); } } }
public Form1() { InitializeComponent(); ProductInfoRepository pro = new ProductInfoRepository(); sfDataGrid1.DataSource = pro.ProductInfo; GridSettings(); cellstyle = new PdfGridCellStyle(); cellstyle.StringFormat = new PdfStringFormat() { Alignment = PdfTextAlignment.Right }; var font = new Font("Segoe UI", 9f, System.Drawing.FontStyle.Regular); cellstyle.Font = new PdfTrueTypeFont(font, true); this.sfButton2.Click += new System.EventHandler(this.PrintButton_Click); }
void Pdf_Clicked(object sender, EventArgs args) { PdfDocument document = new PdfDocument(); document.PageSettings.Orientation = PdfPageOrientation.Landscape; document.PageSettings.Margins.All = 50; PdfPage page = document.Pages.Add(); PdfGraphics g = page.Graphics; PdfTextElement element = new PdfTextElement(@"Syncfusion Software 2501 Aerial Center Parkway Suite 200 Morrisville, NC 27560 USA Tel +1 888.936.8638 Fax +1 919.573.0306"); element.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 12); element.Brush = new PdfSolidBrush(new PdfColor(89, 89, 93)); PdfLayoutResult result = element.Draw(page, new RectangleF(0, 0, page.Graphics.ClientSize.Width / 2, 200)); Stream imgStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("XamarinIOInvoice.SyncfusionLogo.jpg"); PdfImage img = PdfImage.FromStream(imgStream); page.Graphics.DrawImage(img, new RectangleF(g.ClientSize.Width - 200, result.Bounds.Y, 190, 45)); PdfFont subHeadingFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 14); g.DrawRectangle(new PdfSolidBrush(new PdfColor(126, 151, 173)), new RectangleF(0, result.Bounds.Bottom + 40, g.ClientSize.Width, 20)); PdfGrid grid = new PdfGrid(); grid.DataSource = GetDataSource(); PdfGridCellStyle cellStyle = new PdfGridCellStyle(); cellStyle.Borders.All = PdfPens.White; PdfGridRow header = grid.Headers[0]; PdfGridCellStyle headerStyle = new PdfGridCellStyle(); headerStyle.Borders.All = new PdfPen(new PdfColor(126, 151, 173)); headerStyle.BackgroundBrush = new PdfSolidBrush(new PdfColor(126, 151, 173)); headerStyle.TextBrush = PdfBrushes.White; headerStyle.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 14f, PdfFontStyle.Regular); for (int i = 0; i < header.Cells.Count; i++) { if (i == 0) { header.Cells[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); } else { header.Cells[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle); } } header.ApplyStyle(headerStyle); cellStyle.Borders.Bottom = new PdfPen(new PdfColor(217, 217, 217), 0.70f); cellStyle.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 12f); cellStyle.TextBrush = new PdfSolidBrush(new PdfColor(131, 130, 136)); foreach (PdfGridRow row in grid.Rows) { row.ApplyStyle(cellStyle); for (int i = 0; i < row.Cells.Count; i++) { PdfGridCell cell = row.Cells[i]; if (i == 0) { cell.StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); } else { cell.StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle); } if (i > 1) { float val = float.MinValue; float.TryParse(cell.Value.ToString(), out val); cell.Value = "$" + val.ToString(); } } } PdfGridLayoutFormat layoutFormat = new PdfGridLayoutFormat(); layoutFormat.Layout = PdfLayoutType.Paginate; PdfGridLayoutResult gridResult = grid.Draw(page, new RectangleF(new Syncfusion.Drawing.PointF(0, result.Bounds.Bottom + 40), new Syncfusion.Drawing.SizeF(g.ClientSize.Width, g.ClientSize.Height - 100)), layoutFormat); float pos = 0.0f; for (int i = 0; i < grid.Columns.Count - 1; i++) { pos += grid.Columns[i].Width; } PdfFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 14f); gridResult.Page.Graphics.DrawString("Total Due", font, new PdfSolidBrush(new PdfColor(126, 151, 173)), new RectangleF(new Syncfusion.Drawing.PointF(pos, gridResult.Bounds.Bottom + 20), new Syncfusion.Drawing.SizeF(grid.Columns[3].Width - pos, 20)), new PdfStringFormat(PdfTextAlignment.Right)); gridResult.Page.Graphics.DrawString("Thank you for your business!", new PdfStandardFont(PdfFontFamily.TimesRoman, 12), new PdfSolidBrush(new PdfColor(89, 89, 93)), new Syncfusion.Drawing.PointF(pos - 55, gridResult.Bounds.Bottom + 60)); pos += grid.Columns[4].Width; gridResult.Page.Graphics.DrawString("$13600", font, new PdfSolidBrush(new PdfColor(131, 130, 136)), new RectangleF(new Syncfusion.Drawing.PointF(pos, gridResult.Bounds.Bottom + 20), new Syncfusion.Drawing.SizeF(grid.Columns[4].Width - pos, 20)), new PdfStringFormat(PdfTextAlignment.Right)); MemoryStream data = new MemoryStream(); document.Save(data); document.Close(); DependencyService.Get <ISave>().SaveTextAsync("Invoice.pdf", "application/pdf", data); }
private async void GeneratePDF_Click(object sender, RoutedEventArgs e) { total = 0; string id = this.OrderIdList.SelectedValue.ToString(); IEnumerable <CustOrders> products = Orders.GetProducts(id); List <CustOrders> list = new List <CustOrders>(); foreach (CustOrders cust in products) { list.Add(cust); } var reducedList = list.Select(f => new { f.ProductID, f.ProductName, f.Quantity, f.UnitPrice, f.Discount, f.Price }).ToList(); IEnumerable <ShipDetails> shipDetails = Orders.GetShipDetails(); PdfDocument document = new PdfDocument(); document.PageSettings.Orientation = PdfPageOrientation.Landscape; document.PageSettings.Margins.All = 50; PdfPage page = document.Pages.Add(); PdfGraphics g = page.Graphics; PdfTextElement element = new PdfTextElement("Northwind Traders\n67, rue des Cinquante Otages,\nElgin,\nUnites States."); element.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 12); element.Brush = new PdfSolidBrush(new PdfColor(89, 89, 93)); PdfLayoutResult result = element.Draw(page, new RectangleF(0, 0, page.Graphics.ClientSize.Width / 2, 200)); Stream imgStream = typeof(Invoice).GetTypeInfo().Assembly.GetManifestResourceStream("Syncfusion.SampleBrowser.UWP.Pdf.Pdf.Assets.logo.jpg"); PdfImage img = new PdfBitmap(imgStream); page.Graphics.DrawImage(img, new RectangleF(g.ClientSize.Width - 200, result.Bounds.Y, 190, 45)); PdfFont subHeadingFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 14); g.DrawRectangle(new PdfSolidBrush(new PdfColor(126, 151, 173)), new RectangleF(0, result.Bounds.Bottom + 40, g.ClientSize.Width, 30)); element = new PdfTextElement("INVOICE " + id.ToString(), subHeadingFont); element.Brush = PdfBrushes.White; result = element.Draw(page, new PointF(10, result.Bounds.Bottom + 48)); string currentDate = "DATE " + DateTime.Now.ToString("MM/dd/yyyy"); SizeF textSize = subHeadingFont.MeasureString(currentDate); g.DrawString(currentDate, subHeadingFont, element.Brush, new PointF(g.ClientSize.Width - textSize.Width - 10, result.Bounds.Y)); element = new PdfTextElement("BILL TO ", new PdfStandardFont(PdfFontFamily.TimesRoman, 10)); element.Brush = new PdfSolidBrush(new PdfColor(126, 155, 203)); result = element.Draw(page, new PointF(10, result.Bounds.Bottom + 25)); g.DrawLine(new PdfPen(new PdfColor(126, 151, 173), 0.70f), new PointF(0, result.Bounds.Bottom + 3), new PointF(g.ClientSize.Width, result.Bounds.Bottom + 3)); GetShipDetails(shipDetails); element = new PdfTextElement(shipName, new PdfStandardFont(PdfFontFamily.TimesRoman, 10)); element.Brush = new PdfSolidBrush(new PdfColor(89, 89, 93)); result = element.Draw(page, new RectangleF(10, result.Bounds.Bottom + 5, g.ClientSize.Width / 2, 100)); element = new PdfTextElement(string.Format("{0}, {1}, {2}", address, shipCity, shipCountry), new PdfStandardFont(PdfFontFamily.TimesRoman, 10)); element.Brush = new PdfSolidBrush(new PdfColor(89, 89, 93)); result = element.Draw(page, new RectangleF(10, result.Bounds.Bottom + 3, g.ClientSize.Width / 2, 100)); PdfGrid grid = new PdfGrid(); grid.DataSource = reducedList; PdfGridCellStyle cellStyle = new PdfGridCellStyle(); cellStyle.Borders.All = PdfPens.White; PdfGridRow header = grid.Headers[0]; PdfGridCellStyle headerStyle = new PdfGridCellStyle(); headerStyle.Borders.All = new PdfPen(new PdfColor(126, 151, 173)); headerStyle.BackgroundBrush = new PdfSolidBrush(new PdfColor(126, 151, 173)); headerStyle.TextBrush = PdfBrushes.White; headerStyle.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 14f, PdfFontStyle.Regular); for (int i = 0; i < header.Cells.Count; i++) { if (i == 0 || i == 1) { header.Cells[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); } else { header.Cells[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle); } } header.ApplyStyle(headerStyle); cellStyle.Borders.Bottom = new PdfPen(new PdfColor(217, 217, 217), 0.70f); cellStyle.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 12f); cellStyle.TextBrush = new PdfSolidBrush(new PdfColor(131, 130, 136)); foreach (PdfGridRow row in grid.Rows) { row.ApplyStyle(cellStyle); for (int i = 0; i < row.Cells.Count; i++) { PdfGridCell cell = row.Cells[i]; if (i == 1) { cell.StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); } else if (i == 0) { cell.StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); } else { cell.StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle); } if (i > 2) { float val = float.MinValue; float.TryParse(cell.Value.ToString(), out val); cell.Value = '$' + val.ToString("0.00"); } } } grid.Columns[0].Width = 100; grid.Columns[1].Width = 200; PdfGridLayoutFormat layoutFormat = new PdfGridLayoutFormat(); layoutFormat.Layout = PdfLayoutType.Paginate; PdfGridLayoutResult gridResult = grid.Draw(page, new RectangleF(new PointF(0, result.Bounds.Bottom + 40), new SizeF(g.ClientSize.Width, g.ClientSize.Height - 100)), layoutFormat); float pos = 0.0f; for (int i = 0; i < grid.Columns.Count - 1; i++) { pos += grid.Columns[i].Width; } PdfFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 14f); GetTotalPrice(products); gridResult.Page.Graphics.DrawString("Total Due", font, new PdfSolidBrush(new PdfColor(126, 151, 173)), new RectangleF(new PointF(pos, gridResult.Bounds.Bottom + 20), new SizeF(grid.Columns[3].Width - pos, 20)), new PdfStringFormat(PdfTextAlignment.Right)); gridResult.Page.Graphics.DrawString("Thank you for your business!", new PdfStandardFont(PdfFontFamily.TimesRoman, 12), new PdfSolidBrush(new PdfColor(89, 89, 93)), new PointF(pos - 55, gridResult.Bounds.Bottom + 60)); pos += grid.Columns[4].Width; gridResult.Page.Graphics.DrawString('$' + string.Format("{0:N2}", total), font, new PdfSolidBrush(new PdfColor(131, 130, 136)), new RectangleF(new PointF(pos, gridResult.Bounds.Bottom + 20), new SizeF(grid.Columns[4].Width - pos, 20)), new PdfStringFormat(PdfTextAlignment.Right)); MemoryStream stream = new MemoryStream(); await document.SaveAsync(stream); document.Close(true); Save(stream, "Invoice.pdf"); }
public ActionResult DownloadPDF(int orderId) { var order = orderRepository.GetOrderDetailsById(orderId); int ShipingCharge = Convert.ToInt32(configuration["shippingCharge"]); PdfDocument document = new PdfDocument(); //Adds page settings document.PageSettings.Orientation = PdfPageOrientation.Landscape; document.PageSettings.Margins.All = 50; //Adds a page to the document PdfPage page = document.Pages.Add(); PdfGraphics graphics = page.Graphics; //Set the standard font PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 35, PdfFontStyle.Bold); //Draw the text graphics.DrawString("Prince Fashion!", font, PdfBrushes.Gray, new PointF(120, 5)); FileStream imageStream = new FileStream("wwwroot/images/logo.png", FileMode.Open, FileAccess.Read); RectangleF bounds = new RectangleF(10, 0, 80, 40); PdfImage image = PdfImage.FromStream(imageStream); //Draws the image to the PDF page page.Graphics.DrawImage(image, bounds); PdfBrush solidBrush = new PdfSolidBrush(new PdfColor(116, 152, 190)); bounds = new RectangleF(0, bounds.Bottom + 50, graphics.ClientSize.Width, 40); //Draws a rectangle to place the heading in that region. graphics.DrawRectangle(solidBrush, bounds); //Creates a font for adding the heading in the page PdfFont subHeadingFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 14); //Creates a text element to add the invoice number PdfTextElement element1; if (order.OrderType.Equals("DoneOnlinepayment")) { element1 = new PdfTextElement("Type :: " + order.OrderType, subHeadingFont); } else { element1 = new PdfTextElement("Type :: COD "); } element1.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 16, PdfFontStyle.Bold); element1.Brush = new PdfSolidBrush(new PdfColor(0, 0, 0)); element1.Draw(page, new PointF(graphics.ClientSize.Width - 250, bounds.Top - 40)); PdfTextElement element = new PdfTextElement("INVOICE " + orderId, subHeadingFont); element.Brush = PdfBrushes.White; //Draws the heading on the page PdfLayoutResult result = element.Draw(page, new PointF(10, bounds.Top + 8)); string currentDate = "DATE " + DateTime.Now.ToString("dd/MM/yyyy"); //Measures the width of the text to place it in the correct location SizeF textSize = subHeadingFont.MeasureString(currentDate); PointF textPosition = new PointF(graphics.ClientSize.Width - textSize.Width - 10, result.Bounds.Y); //Draws the date by using DrawString method graphics.DrawString(currentDate, subHeadingFont, element.Brush, textPosition); PdfFont timesRoman = new PdfStandardFont(PdfFontFamily.TimesRoman, 10); //Creates text elements to add the address and draw it to the page. element = new PdfTextElement("BILL TO ", timesRoman); element.Brush = new PdfSolidBrush(new PdfColor(126, 155, 203)); result = element.Draw(page, new PointF(10, result.Bounds.Bottom + 25)); element = new PdfTextElement("Name :: ", timesRoman); element.Brush = new PdfSolidBrush(new PdfColor(0, 0, 0)); result = element.Draw(page, new PointF(10, result.Bounds.Bottom + 5)); element = new PdfTextElement(order.FirstName.ToUpper() + " " + order.LastName.ToUpper(), timesRoman); element.Brush = new PdfSolidBrush(new PdfColor(0, 0, 0)); result = element.Draw(page, new PointF(result.Bounds.Left + 40, result.Bounds.Bottom - 10)); element = new PdfTextElement("Mob No :: ", timesRoman); element.Brush = new PdfSolidBrush(new PdfColor(0, 0, 0)); result = element.Draw(page, new PointF(result.Bounds.Left + 200, result.Bounds.Bottom - 10)); element = new PdfTextElement(order.PhoneNumber, timesRoman); element.Brush = new PdfSolidBrush(new PdfColor(0, 0, 0)); result = element.Draw(page, new PointF(result.Bounds.Left + 45, result.Bounds.Bottom - 10)); element = new PdfTextElement("Address :: ", timesRoman); element.Brush = new PdfSolidBrush(new PdfColor(0, 0, 0)); result = element.Draw(page, new PointF(10, result.Bounds.Bottom + 5)); element = new PdfTextElement(order.AddressLine1 + " , " + order.AddressLine2, timesRoman); element.Brush = new PdfSolidBrush(new PdfColor(0, 0, 0)); result = element.Draw(page, new PointF(result.Bounds.Left + 45, result.Bounds.Bottom - 10)); element = new PdfTextElement("Email Id:: ", timesRoman); element.Brush = new PdfSolidBrush(new PdfColor(0, 0, 0)); result = element.Draw(page, new PointF(result.Bounds.Left + 195, result.Bounds.Bottom - 10)); element = new PdfTextElement(order.Email, timesRoman); element.Brush = new PdfSolidBrush(new PdfColor(0, 0, 0)); result = element.Draw(page, new PointF(result.Bounds.Left + 45, result.Bounds.Bottom - 10)); element = new PdfTextElement(order.City + " , " + order.State + " -" + order.ZipCode, timesRoman); element.Brush = new PdfSolidBrush(new PdfColor(0, 0, 0)); result = element.Draw(page, new PointF(55, result.Bounds.Bottom + 5)); element = new PdfTextElement(order.Country, timesRoman); element.Brush = new PdfSolidBrush(new PdfColor(0, 0, 0)); result = element.Draw(page, new PointF(55, result.Bounds.Bottom + 5)); PdfPen linePen = new PdfPen(new PdfColor(126, 151, 173), 0.70f); PointF startPoint = new PointF(0, result.Bounds.Bottom + 3); PointF endPoint = new PointF(graphics.ClientSize.Width, result.Bounds.Bottom + 3); //Draws a line at the bottom of the address graphics.DrawLine(linePen, startPoint, endPoint); DataTable dt = new DataTable(); dt.Columns.Add("Product Code"); dt.Columns.Add("Product Disc"); dt.Columns.Add("Price"); dt.Columns.Add("Discount"); dt.Columns.Add("Discount Price"); foreach (var p in order.OrderLines) { float discountPrice = (float)Math.Round(p.Product.Price - p.Product.Price * p.Product.Discount / 100, 2); dt.Rows.Add(p.Product.ProductName, p.Product.ShortDescription, "Rs. " + p.Product.Price, p.Product.Discount + " %", "Rs. " + discountPrice); } if (order.OrderType.Equals("codpayment")) { dt.Rows.Add("Shipping Charge", "", "Rs. " + ShipingCharge, "", "Rs. " + ShipingCharge); } DataTable invoiceDetails = dt; //Creates a PDF grid PdfGrid grid = new PdfGrid(); //Adds the data source grid.DataSource = invoiceDetails; //Creates the grid cell styles PdfGridCellStyle cellStyle = new PdfGridCellStyle(); cellStyle.Borders.All = PdfPens.White; PdfGridRow header = grid.Headers[0]; //Creates the header style PdfGridCellStyle headerStyle = new PdfGridCellStyle(); headerStyle.Borders.All = new PdfPen(new PdfColor(126, 155, 203)); headerStyle.BackgroundBrush = new PdfSolidBrush(new PdfColor(126, 155, 203)); headerStyle.TextBrush = PdfBrushes.White; headerStyle.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 14f, PdfFontStyle.Regular); //Adds cell customizations for (int i = 0; i < header.Cells.Count; i++) { header.Cells[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); } //Applies the header style header.ApplyStyle(headerStyle); PdfGridStyle gridStyle = new PdfGridStyle(); gridStyle.CellPadding = new PdfPaddings(2, 2, 2, 2); gridStyle.CellSpacing = 1; grid.Style = gridStyle; PdfStringFormat stringFormat = new PdfStringFormat(); stringFormat.Alignment = PdfTextAlignment.Left + 5; stringFormat.LineAlignment = PdfVerticalAlignment.Middle; //Apply string formatting for whole table for (int i = 0; i < grid.Columns.Count; i++) { grid.Columns[i].Format = stringFormat; } grid.ApplyBuiltinStyle(PdfGridBuiltinStyle.GridTable4Accent1); cellStyle.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 12f); cellStyle.TextBrush = new PdfSolidBrush(new PdfColor(131, 130, 136)); //Creates the layout format for grid PdfGridLayoutFormat layoutFormat = new PdfGridLayoutFormat(); // Creates layout format settings to allow the table pagination layoutFormat.Layout = PdfLayoutType.Paginate; //Draws the grid to the PDF page. PdfGridLayoutResult gridResult = grid.Draw(page, new RectangleF(new PointF(0, result.Bounds.Bottom + 40), new SizeF(graphics.ClientSize.Width, graphics.ClientSize.Height - 100)), layoutFormat); element = new PdfTextElement("Total Price:: ", timesRoman); element.Brush = new PdfSolidBrush(new PdfColor(0, 0, 0)); result = element.Draw(page, new PointF(graphics.ClientSize.Width - 200, gridResult.Bounds.Bottom + 10)); element = new PdfTextElement("Rs. " + order.OrderTotal.ToString("0.00"), timesRoman); element.Brush = new PdfSolidBrush(new PdfColor(0, 0, 0)); result = element.Draw(page, new PointF(graphics.ClientSize.Width - 140, result.Bounds.Bottom - 10)); element = new PdfTextElement("Total Product:: ", timesRoman); element.Brush = new PdfSolidBrush(new PdfColor(0, 0, 0)); result = element.Draw(page, new PointF(graphics.ClientSize.Width - 500, gridResult.Bounds.Bottom + 10)); element = new PdfTextElement(order.TotalItem.ToString("0"), timesRoman); element.Brush = new PdfSolidBrush(new PdfColor(0, 0, 0)); result = element.Draw(page, new PointF(graphics.ClientSize.Width - 420, result.Bounds.Bottom - 10)); element = new PdfTextElement("OrderDate:: ", timesRoman); element.Brush = new PdfSolidBrush(new PdfColor(0, 0, 0)); result = element.Draw(page, new PointF(10, gridResult.Bounds.Bottom + 10)); element = new PdfTextElement(order.OrderPlacedDate.ToString("dd/MM/yyyy"), timesRoman); element.Brush = new PdfSolidBrush(new PdfColor(0, 0, 0)); result = element.Draw(page, new PointF(60, result.Bounds.Bottom - 10)); element1.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 20, PdfFontStyle.Bold); element1 = new PdfTextElement("Thanks,", subHeadingFont); element1.Brush = new PdfSolidBrush(new PdfColor(0, 0, 0)); result = element1.Draw(page, new PointF(graphics.ClientSize.Width - 130, result.Bounds.Bottom + 20)); element1 = new PdfTextElement("Prince Digital,Surat", subHeadingFont); element1.Brush = new PdfSolidBrush(new PdfColor(0, 0, 0)); result = element1.Draw(page, new PointF(graphics.ClientSize.Width - 130, result.Bounds.Bottom + 1)); MemoryStream stream = new MemoryStream(); document.Save(stream); //Set the position as '0'. stream.Position = 0; //Download the PDF document in the browser FileStreamResult fileStreamResult = new FileStreamResult(stream, "application/pdf"); string FileName = "order_" + order.OrderId + ".pdf"; fileStreamResult.FileDownloadName = FileName; return(fileStreamResult); }
/// <summary> /// Creates PDF /// </summary> public async void CreatePDF(IList <InvoiceItem> dataSource, BillingInformation billInfo, double totalDue) { PdfDocument document = new PdfDocument(); document.PageSettings.Orientation = PdfPageOrientation.Portrait; document.PageSettings.Margins.All = 50; PdfPage page = document.Pages.Add(); PdfGraphics g = page.Graphics; PdfTextElement element = new PdfTextElement(@"Syncfusion Software 2501 Aerial Center Parkway Suite 200 Morrisville, NC 27560 USA Tel +1 888.936.8638 Fax +1 919.573.0306"); element.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 12); element.Brush = new PdfSolidBrush(new PdfColor(0, 0, 0)); PdfLayoutResult result = element.Draw(page, new RectangleF(0, 0, page.Graphics.ClientSize.Width / 2, 200)); Stream imgStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Syncfusion.SampleBrowser.UWP.DocIO.DocIO.Invoice.Assets.SyncfusionLogo.jpg"); PdfImage img = PdfImage.FromStream(imgStream); page.Graphics.DrawImage(img, new RectangleF(g.ClientSize.Width - 200, result.Bounds.Y, 190, 45)); PdfFont subHeadingFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 14); g.DrawRectangle(new PdfSolidBrush(new PdfColor(34, 83, 142)), new RectangleF(0, result.Bounds.Bottom + 40, g.ClientSize.Width, 30)); element = new PdfTextElement("INVOICE " + billInfo.InvoiceNumber.ToString(), subHeadingFont); element.Brush = PdfBrushes.White; result = element.Draw(page, new PointF(10, result.Bounds.Bottom + 48)); string currentDate = "DATE " + billInfo.Date.ToString("d"); SizeF textSize = subHeadingFont.MeasureString(currentDate); g.DrawString(currentDate, subHeadingFont, element.Brush, new PointF(g.ClientSize.Width - textSize.Width - 10, result.Bounds.Y)); element = new PdfTextElement("BILL TO ", new PdfStandardFont(PdfFontFamily.TimesRoman, 12)); element.Brush = new PdfSolidBrush(new PdfColor(34, 83, 142)); result = element.Draw(page, new PointF(10, result.Bounds.Bottom + 25)); g.DrawLine(new PdfPen(new PdfColor(34, 83, 142), 0.70f), new PointF(0, result.Bounds.Bottom + 3), new PointF(g.ClientSize.Width, result.Bounds.Bottom + 3)); element = new PdfTextElement(billInfo.Name, new PdfStandardFont(PdfFontFamily.TimesRoman, 11)); element.Brush = new PdfSolidBrush(new PdfColor(0, 0, 0)); result = element.Draw(page, new RectangleF(10, result.Bounds.Bottom + 5, g.ClientSize.Width / 2, 100)); element = new PdfTextElement(billInfo.Address, new PdfStandardFont(PdfFontFamily.TimesRoman, 11)); element.Brush = new PdfSolidBrush(new PdfColor(0, 0, 0)); result = element.Draw(page, new RectangleF(10, result.Bounds.Bottom + 3, g.ClientSize.Width / 2, 100)); string[] headers = new string[] { "Item", "Quantity", "Rate", "Taxes", "Amount" }; PdfGrid grid = new PdfGrid(); grid.Columns.Add(headers.Length); //Adding headers in to the grid grid.Headers.Add(1); PdfGridRow headerRow = grid.Headers[0]; int count = 0; foreach (string columnName in headers) { headerRow.Cells[count].Value = columnName; count++; } //Adding rows into the grid foreach (var item in dataSource) { PdfGridRow row = grid.Rows.Add(); row.Cells[0].Value = item.ItemName; row.Cells[1].Value = item.Quantity.ToString(); row.Cells[2].Value = "$" + item.Rate.ToString("#,###.00", CultureInfo.InvariantCulture); row.Cells[3].Value = "$" + item.Taxes.ToString("#,###.00", CultureInfo.InvariantCulture); row.Cells[4].Value = "$" + item.TotalAmount.ToString("#,###.00", CultureInfo.InvariantCulture); } PdfGridCellStyle cellStyle = new PdfGridCellStyle(); cellStyle.Borders.All = PdfPens.White; PdfGridRow header = grid.Headers[0]; PdfGridCellStyle headerStyle = new PdfGridCellStyle(); headerStyle.Borders.All = new PdfPen(new PdfColor(34, 83, 142)); headerStyle.BackgroundBrush = new PdfSolidBrush(new PdfColor(34, 83, 142)); headerStyle.TextBrush = PdfBrushes.White; headerStyle.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 12f, PdfFontStyle.Regular); for (int i = 0; i < header.Cells.Count; i++) { if (i == 0) { header.Cells[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); } else { header.Cells[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle); } } header.Cells[0].Value = "ITEM"; header.Cells[1].Value = "QUANTITY"; header.Cells[2].Value = "RATE"; header.Cells[3].Value = "TAXES"; header.Cells[4].Value = "AMOUNT"; header.ApplyStyle(headerStyle); grid.Columns[0].Width = 180; cellStyle.Borders.Bottom = new PdfPen(new PdfColor(34, 83, 142), 0.70f); cellStyle.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 11f); cellStyle.TextBrush = new PdfSolidBrush(new PdfColor(0, 0, 0)); foreach (PdfGridRow row in grid.Rows) { row.ApplyStyle(cellStyle); for (int i = 0; i < row.Cells.Count; i++) { PdfGridCell cell = row.Cells[i]; if (i == 0) { cell.StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); } else { cell.StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle); } if (i > 1) { if (cell.Value.ToString().Contains("$")) { cell.Value = cell.Value.ToString(); } else { if (cell.Value is double) { cell.Value = "$" + ((double)cell.Value).ToString("#,###.00", CultureInfo.InvariantCulture); } else { cell.Value = "$" + cell.Value.ToString(); } } } } } PdfGridLayoutFormat layoutFormat = new PdfGridLayoutFormat(); layoutFormat.Layout = PdfLayoutType.Paginate; PdfGridLayoutResult gridResult = grid.Draw(page, new RectangleF(new PointF(0, result.Bounds.Bottom + 40), new SizeF(g.ClientSize.Width, g.ClientSize.Height - 100)), layoutFormat); float pos = 0.0f; for (int i = 0; i < grid.Columns.Count - 1; i++) { pos += grid.Columns[i].Width; } PdfFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 12f, PdfFontStyle.Bold); gridResult.Page.Graphics.DrawString("TOTAL DUE", font, new PdfSolidBrush(new PdfColor(34, 83, 142)), new RectangleF(new PointF(pos, gridResult.Bounds.Bottom + 10), new SizeF(grid.Columns[3].Width - pos, 20)), new PdfStringFormat(PdfTextAlignment.Right)); gridResult.Page.Graphics.DrawString("Thank you for your business!", new PdfStandardFont(PdfFontFamily.TimesRoman, 12), new PdfSolidBrush(new PdfColor(0, 0, 0)), new PointF(pos - 210, gridResult.Bounds.Bottom + 60)); pos += grid.Columns[4].Width; gridResult.Page.Graphics.DrawString("$" + totalDue.ToString("#,###.00", CultureInfo.InvariantCulture), font, new PdfSolidBrush(new PdfColor(0, 0, 0)), new RectangleF(pos, gridResult.Bounds.Bottom + 10, grid.Columns[4].Width - pos, 20), new PdfStringFormat(PdfTextAlignment.Right)); StorageFile stFile = null; if (!(Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons"))) { FileSavePicker savePicker = new FileSavePicker(); savePicker.DefaultFileExtension = ".pdf"; savePicker.SuggestedFileName = "Invoice"; savePicker.FileTypeChoices.Add("Adobe PDF Document", new List <string>() { ".pdf" }); stFile = await savePicker.PickSaveFileAsync(); } else { StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder; stFile = await local.CreateFileAsync("Invoice.pdf", CreationCollisionOption.ReplaceExisting); } if (stFile != null) { Stream stream = await stFile.OpenStreamForWriteAsync(); await document.SaveAsync(stream); stream.Flush(); stream.Dispose(); document.Close(true); MessageDialog msgDialog = new MessageDialog("Do you want to view the Document?", "File has been created successfully."); UICommand yesCmd = new UICommand("Yes"); msgDialog.Commands.Add(yesCmd); UICommand noCmd = new UICommand("No"); msgDialog.Commands.Add(noCmd); IUICommand cmd = await msgDialog.ShowAsync(); if (cmd == yesCmd) { // Launch the saved file bool success = await Windows.System.Launcher.LaunchFileAsync(stFile); } } }
public async Task <FileStreamResult> CompleteAsync(int?id) { var paper = await _context.papers .FirstOrDefaultAsync(m => m.id == id); List <StudentData> students = new List <StudentData>(); List <StudentData> print = new List <StudentData>(); students = await _context.studentDatas.ToListAsync(); StudentData student = null; for (int i = 0; i < students.Count(); i++) { if ((students[i].Studentid).Equals(paper.StudentID)) { student = new StudentData(); student.Faculty = students[i].Faculty; student.Course = students[i].Course; student.Note = students[i].Note; print.Add(student); } } PdfDocument document = new PdfDocument(); //Add a page to the document PdfPage page = document.Pages.Add(); //Create PDF graphics for the page PdfGraphics graphics = page.Graphics; //Set the standard font PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20); FileStream imageStream = new FileStream("ua.jpg", FileMode.Open, FileAccess.Read); PdfBitmap image = new PdfBitmap(imageStream); //Draw the image graphics.DrawImage(image, 30, 0); if (paper.type == "Notes") { DataTable table = new DataTable("INFO"); // Declare DataColumn and DataRow variables. DataColumn column; DataColumn column2; // Create column. column = new DataColumn(); column.DataType = Type.GetType("System.String"); column.ColumnName = "Course"; table.Columns.Add(column); column2 = new DataColumn(); column2.DataType = Type.GetType("System.Int32"); column2.ColumnName = "Note"; table.Columns.Add(column2); foreach (StudentData item in print) { table.Rows.Add(item.Course, item.Note); } PdfBrush solidBrush = new PdfSolidBrush(new PdfColor(126, 151, 173)); Rectangle bounds = new RectangleF(0, 200, graphics.ClientSize.Width, 30); //Draws a rectangle to place the heading in that region. graphics.DrawRectangle(solidBrush, bounds); //Creates a font for adding the heading in the page PdfFont subHeadingFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 14); //Creates a text element to add the invoice number PdfTextElement element = new PdfTextElement("Student " + paper.Firstname + " " + paper.lastname, subHeadingFont); element.Brush = PdfBrushes.White; //Draws the heading on the page PdfLayoutResult result = element.Draw(page, new PointF(10, bounds.Top + 8)); string currentDate = "DATE " + DateTime.Now.ToString("MM/dd/yyyy"); //Measures the width of the text to place it in the correct location SizeF textSize = subHeadingFont.MeasureString(currentDate); PointF textPosition = new PointF(graphics.ClientSize.Width - textSize.Width - 10, result.Bounds.Y); //Draws the date by using DrawString method graphics.DrawString(currentDate, subHeadingFont, element.Brush, textPosition); PdfFont timesRoman = new PdfStandardFont(PdfFontFamily.TimesRoman, 10); //Creates text elements to add the address and draw it to the page. element = new PdfTextElement("Student ID: " + paper.StudentID, timesRoman); element.Brush = new PdfSolidBrush(new PdfColor(126, 155, 203)); result = element.Draw(page, new PointF(10, result.Bounds.Bottom + 25)); PdfPen linePen = new PdfPen(new PdfColor(126, 151, 173), 0.70f); PointF startPoint = new PointF(0, result.Bounds.Bottom + 3); PointF endPoint = new PointF(graphics.ClientSize.Width, result.Bounds.Bottom + 3); //Draws a line at the bottom of the address graphics.DrawLine(linePen, startPoint, endPoint); //Creates the datasource for the table //Creates a PDF grid PdfGrid grid = new PdfGrid(); //Adds the data source grid.DataSource = table; //Creates the grid cell styles PdfGridCellStyle cellStyle = new PdfGridCellStyle(); cellStyle.Borders.All = PdfPens.White; PdfGridRow header = grid.Headers[0]; //Creates the header style PdfGridCellStyle headerStyle = new PdfGridCellStyle(); headerStyle.Borders.All = new PdfPen(new PdfColor(126, 151, 173)); headerStyle.BackgroundBrush = new PdfSolidBrush(new PdfColor(126, 151, 173)); headerStyle.TextBrush = PdfBrushes.White; headerStyle.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 14f, PdfFontStyle.Regular); //Adds cell customizations for (int i = 0; i < header.Cells.Count; i++) { if (i == 0 || i == 1) { header.Cells[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); } else { header.Cells[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle); } } //Applies the header style header.ApplyStyle(headerStyle); cellStyle.Borders.Bottom = new PdfPen(new PdfColor(217, 217, 217), 0.70f); cellStyle.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 12f); cellStyle.TextBrush = new PdfSolidBrush(new PdfColor(131, 130, 136)); //Creates the layout format for grid PdfGridLayoutFormat layoutFormat = new PdfGridLayoutFormat(); // Creates layout format settings to allow the table pagination layoutFormat.Layout = PdfLayoutType.Paginate; //Draws the grid to the PDF page. PdfGridLayoutResult gridResult = grid.Draw(page, new RectangleF(new PointF(0, result.Bounds.Bottom + 40), new SizeF(graphics.ClientSize.Width, graphics.ClientSize.Height - 100)), layoutFormat); } if (paper.type == "Continue Education") { int now = DateTime.Now.Year; int y = now - 1; String text = "The student " + paper.Firstname + " " + paper.lastname + " continu his/her education for the year: "; String text2 = y + "-" + now; graphics.DrawString(text, font, PdfBrushes.Black, new PointF(0, 180)); graphics.DrawString(text2, font, PdfBrushes.Red, new PointF(0, 210)); } //Saving the PDF to the MemoryStream MemoryStream stream = new MemoryStream(); document.Save(stream); //Set the position as '0'. stream.Position = 0; //Download the PDF document in the browser FileStreamResult fileStreamResult = new FileStreamResult(stream, "application/pdf"); fileStreamResult.FileDownloadName = paper.StudentID + "_" + paper.type + ".pdf"; paper.Status = "Complete"; _context.Update(paper); await _context.SaveChangesAsync(); RedirectToAction("Admin"); return(fileStreamResult); }
public void CreatePDF(List <object[]> items) { //Create a new PDF document. PdfDocument doc = new PdfDocument(); //Add a page. PdfPage page = doc.Pages.Add(); //Create a PdfGrid. PdfGrid pdfGrid = new PdfGrid(); //Loads the image from disk string CSPath = Directory.GetParent(Directory.GetParent(Directory.GetParent(Directory.GetCurrentDirectory()).FullName).FullName).FullName; string ImagePath = Path.Combine(CSPath, @"DAL\Images\LongLogo.jpeg"); PdfImage image = PdfImage.FromFile(ImagePath); RectangleF bounds = new RectangleF(50, 0, 400, 150); //Draws the image to the PDF page page.Graphics.DrawImage(image, bounds); Font font = new Font("Ariel", 14); PdfFont pdfFont = new PdfTrueTypeFont(font, true); PdfStringFormat format = new PdfStringFormat(); //Set right-to-left text direction for RTL text format.TextDirection = PdfTextDirection.RightToLeft; format.Alignment = PdfTextAlignment.Right; //Draw grid to the page of PDF document. PdfBrush solidBrush = new PdfSolidBrush(new PdfColor(126, 151, 173)); bounds = new RectangleF(0, bounds.Bottom + 90, page.Graphics.ClientSize.Width, 30); //Draws a rectangle to place the heading in that region. page.Graphics.DrawRectangle(solidBrush, bounds); //Creates a text element to add the invoice number PdfTextElement element = new PdfTextElement("", pdfFont); element.Brush = PdfBrushes.White; //Draws the heading on the page PdfLayoutResult result = element.Draw(page, new PointF(10, bounds.Top + 8)); string currentDate = DateTime.Now.ToString("MM/dd/yyyy"); //Measures the width of the text to place it in the correct location SizeF textSize = pdfFont.MeasureString(currentDate); PointF textPosition = new PointF(page.Graphics.ClientSize.Width - textSize.Width + 70, result.Bounds.Y); //Draws the date by using DrawString method page.Graphics.DrawString(currentDate, pdfFont, element.Brush, textPosition, format); //Creates text elements to add the address and draw it to the page. element = new PdfTextElement("רשימת הקניות שלך:", pdfFont); element.Brush = new PdfSolidBrush(new PdfColor(126, 155, 203)); element.StringFormat = format; result = element.Draw(page, new PointF(page.Graphics.ClientSize.Width - textSize.Width + 70, result.Bounds.Bottom + 25)); PdfPen linePen = new PdfPen(new PdfColor(126, 151, 173), 0.70f); PointF startPoint = new PointF(0, result.Bounds.Bottom + 3); PointF endPoint = new PointF(page.Graphics.ClientSize.Width, result.Bounds.Bottom + 3); //Draws a line at the bottom of the address page.Graphics.DrawLine(linePen, startPoint, endPoint); //Create a DataTable. DataTable dataTable = new DataTable(); //Add columns to the DataTable dataTable.Columns.Add("הכי זול אצל:"); dataTable.Columns.Add("כמות"); dataTable.Columns.Add("תיאור"); dataTable.Columns.Add("שם"); //Add rows to the DataTable. foreach (var item in items) { dataTable.Rows.Add(item); } //Creates the datasource for the table DataTable Details = dataTable; //Creates a PDF grid PdfGrid grid = new PdfGrid(); grid.Style.Font = pdfFont; //Adds the data source grid.DataSource = Details; //Creates the grid cell styles PdfGridCellStyle cellStyle = new PdfGridCellStyle(); cellStyle.Borders.All = PdfPens.White; //PdfGridRow header = grid.Headers[0]; //Creates the header style PdfGridCellStyle headerStyle = new PdfGridCellStyle(); headerStyle.Borders.All = new PdfPen(new PdfColor(126, 151, 173)); headerStyle.BackgroundBrush = new PdfSolidBrush(new PdfColor(126, 151, 173)); headerStyle.TextBrush = PdfBrushes.White; headerStyle.Font = pdfFont; headerStyle.StringFormat = format; //Applies the header style grid.Headers[0].ApplyStyle(headerStyle); foreach (PdfGridColumn Column in grid.Columns) { Column.Format = format; } cellStyle.Borders.Bottom = new PdfPen(new PdfColor(217, 217, 217), 0.70f); cellStyle.Font = pdfFont; cellStyle.TextBrush = new PdfSolidBrush(new PdfColor(131, 130, 136)); cellStyle.StringFormat = format; //Creates the layout format for grid PdfGridLayoutFormat layoutFormat = new PdfGridLayoutFormat(); // Creates layout format settings to allow the table pagination layoutFormat.Layout = PdfLayoutType.Paginate; //Draws the grid to the PDF page. PdfGridLayoutResult gridResult = grid.Draw(page, new RectangleF(new PointF(0, result.Bounds.Bottom + 40), new SizeF(page.Graphics.ClientSize.Width, page.Graphics.ClientSize.Height - 100)), layoutFormat); pdfGrid.Draw(page, new PointF(10, 10)); //Save the document. try { doc.Save("Output.pdf"); System.Diagnostics.Process.Start("Output.pdf"); } catch { } //close the document doc.Close(true); }
void PDFButton_Clicked(object sender, EventArgs e) { PdfDocument document = new PdfDocument(); document.PageSettings.Orientation = PdfPageOrientation.Portrait; document.PageSettings.Margins.All = 50; PdfPage page = document.Pages.Add(); PdfGraphics g = page.Graphics; PdfTextElement element = new PdfTextElement(@"Syncfusion Software 2501 Aerial Center Parkway Suite 200 Morrisville, NC 27560 USA Tel +1 888.936.8638 Fax +1 919.573.0306"); element.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 14); element.Brush = new PdfSolidBrush(new PdfColor(0, 0, 0)); PdfLayoutResult result = element.Draw(page, new RectangleF(0, 0, page.Graphics.ClientSize.Width / 2, 200)); Stream imgStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("XamarinIOInvoice.SyncfusionLogo.jpg"); PdfImage img = PdfImage.FromStream(imgStream); page.Graphics.DrawImage(img, new RectangleF(g.ClientSize.Width - 200, result.Bounds.Y, 190, 45)); PdfFont subHeadingFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 15); g.DrawRectangle(new PdfSolidBrush(new PdfColor(34, 83, 142)), new RectangleF(0, result.Bounds.Bottom + 40, g.ClientSize.Width, 30)); element = new PdfTextElement("INVOICE: " + billInfo.InvoiceNumber.ToString(), subHeadingFont); element.Brush = PdfBrushes.White; result = element.Draw(page, new PointF(10, result.Bounds.Bottom + 48)); string currentDate = "DATE: " + billInfo.Date.ToString(); SizeF textSize = subHeadingFont.MeasureString(currentDate); g.DrawString(currentDate, subHeadingFont, element.Brush, new PointF(g.ClientSize.Width - textSize.Width - 10, result.Bounds.Y)); element = new PdfTextElement("BILL TO ", new PdfStandardFont(PdfFontFamily.TimesRoman, 14)); element.Brush = new PdfSolidBrush(new PdfColor(34, 83, 142)); result = element.Draw(page, new PointF(10, result.Bounds.Bottom + 25)); g.DrawLine(new PdfPen(new PdfColor(34, 83, 142), 0.70f), new PointF(0, result.Bounds.Bottom + 3), new PointF(g.ClientSize.Width, result.Bounds.Bottom + 3)); element = new PdfTextElement(billInfo.Name, new PdfStandardFont(PdfFontFamily.TimesRoman, 14)); element.Brush = new PdfSolidBrush(new PdfColor(0, 0, 0)); result = element.Draw(page, new RectangleF(10, result.Bounds.Bottom + 5, g.ClientSize.Width / 2, 100)); element = new PdfTextElement(billInfo.Address, new PdfStandardFont(PdfFontFamily.TimesRoman, 14)); element.Brush = new PdfSolidBrush(new PdfColor(0, 0, 0)); result = element.Draw(page, new RectangleF(10, result.Bounds.Bottom + 3, g.ClientSize.Width / 2, 100)); PdfGrid grid = new PdfGrid(); grid.DataSource = this.ListSource; PdfGridCellStyle cellStyle = new PdfGridCellStyle(); cellStyle.Borders.All = PdfPens.White; PdfGridRow header = grid.Headers[0]; PdfGridCellStyle headerStyle = new PdfGridCellStyle(); headerStyle.Borders.All = new PdfPen(new PdfColor(34, 83, 142)); headerStyle.BackgroundBrush = new PdfSolidBrush(new PdfColor(34, 83, 142)); headerStyle.TextBrush = PdfBrushes.White; headerStyle.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 15f, PdfFontStyle.Regular); for (int i = 0; i < header.Cells.Count; i++) { if (i == 0) header.Cells[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); else header.Cells[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle); } header.Cells[0].Value = "ITEM"; header.Cells[1].Value = "QUANTITY"; header.Cells[2].Value = "RATE"; header.Cells[3].Value = "TAXES (%)"; header.Cells[4].Value = "AMOUNT"; header.ApplyStyle(headerStyle); cellStyle.Borders.Bottom = new PdfPen(new PdfColor(217, 217, 217), 0.70f); cellStyle.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 14f); cellStyle.TextBrush = new PdfSolidBrush(new PdfColor(0, 0, 0)); foreach (PdfGridRow row in grid.Rows) { row.ApplyStyle(cellStyle); for (int i = 0; i < row.Cells.Count; i++) { PdfGridCell cell = row.Cells[i]; if (i == 0) cell.StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); else cell.StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle); if (i > 1 && i != 3) { //float val = float.MinValue; //float.TryParse(cell.Value.ToString(), out val); if (cell.Value.ToString().Contains("$")) { cell.Value = cell.Value.ToString(); } else { cell.Value = "$" + cell.Value.ToString(); } } } } PdfGridLayoutFormat layoutFormat = new PdfGridLayoutFormat(); layoutFormat.Layout = PdfLayoutType.Paginate; PdfGridLayoutResult gridResult = grid.Draw(page, new RectangleF(new PointF(0, result.Bounds.Bottom + 40), new SizeF(g.ClientSize.Width, g.ClientSize.Height - 100)), layoutFormat); float pos = 0.0f; for (int i = 0; i < grid.Columns.Count - 1; i++) pos += grid.Columns[i].Width; PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 14f,PdfFontStyle.Bold); gridResult.Page.Graphics.DrawString("TOTAL DUE", font, new PdfSolidBrush(new PdfColor(34, 83, 142)), new RectangleF(new PointF(pos, gridResult.Bounds.Bottom + 10), new SizeF(grid.Columns[3].Width - pos, 20)), new PdfStringFormat(PdfTextAlignment.Right)); gridResult.Page.Graphics.DrawString("Thank you for your business!", new PdfStandardFont(PdfFontFamily.TimesRoman, 14), new PdfSolidBrush(new PdfColor(0, 0, 0)), new PointF(pos -210, gridResult.Bounds.Bottom + 60)); pos += grid.Columns[4].Width; gridResult.Page.Graphics.DrawString("$" + GetNetAmount().ToString(), font, new PdfSolidBrush(new PdfColor(0, 0, 0)), new RectangleF(new Syncfusion.Drawing.PointF(pos, gridResult.Bounds.Bottom + 10), new Syncfusion.Drawing.SizeF(grid.Columns[4].Width - pos, 20)), new PdfStringFormat(PdfTextAlignment.Right)); MemoryStream data = new MemoryStream(); document.Save(data); document.Close(); DependencyService.Get<ISave>().SaveTextAsync("Invoice.pdf", "application/pdf", data); }
/// <summary> /// Invoked when this page is about to be displayed in a Frame. /// </summary> /// <param name="e">Event data that describes how this page was reached. The Parameter /// property is typically used to configure the page.</param> //protected override void OnNavigatedTo(NavigationEventArgs e) //{ //} private async void GeneratePDF_Click(object sender, RoutedEventArgs e) { Stream image1 = typeof(ImageInsertion).GetTypeInfo().Assembly.GetManifestResourceStream("Syncfusion.SampleBrowser.UWP.Pdf.Pdf.Assets.CustomTag.jpg"); PdfColor blackColor = new PdfColor(System.Drawing.Color.FromArgb(255, 0, 0, 0)); PdfFont fontnormal = new PdfStandardFont(PdfFontFamily.TimesRoman, 10); PdfFont fontTitle = new PdfStandardFont(PdfFontFamily.TimesRoman, 22, PdfFontStyle.Bold); PdfFont fontHead = new PdfStandardFont(PdfFontFamily.TimesRoman, 10, PdfFontStyle.Bold); PdfFont fontHead2 = new PdfStandardFont(PdfFontFamily.TimesRoman, 16, PdfFontStyle.Bold); #region content string string pdfChapter = "We�ll begin with a conceptual overview of a simple PDF document. This chapter is designed to be a brief orientation before diving in and creating a real document from scratch \r\n \r\n A PDF file can be divided into four parts: a header, body, cross-reference table, and trailer. The header marks the file as a PDF, the body defines the visible document, the cross-reference table lists the location of everything in the file, and the trailer provides instructions for how to start reading the file."; string header = "The header is simply a PDF version number and an arbitrary sequence of binary data.The binary data prevents na�ve applications from processing the PDF as a text file. This would result in a corrupted file, since a PDF typically consists of both plain text and binary data (e.g., a binary font file can be directly embedded in a PDF)."; string body = "The page tree serves as the root of the document. In the simplest case, it is just a list of the pages in the document. Each page is defined as an independent entity with metadata (e.g., page dimensions) and a reference to its resources and content, which are defined separately. Together, the page tree and page objects create the �paper� that composes the document.\r\n \r\n Resources are objects that are required to render a page. For example, a single font is typically used across several pages, so storing the font information in an external resource is much more efficient. A content object defines the text and graphics that actually show up on the page. Together, content objects and resources define theappearance of an individual page. \r\n \r\n Finally, the document�s catalog tells applications where to start reading the document. Often, this is just a pointer to the root page tree."; string resource = "The third object is a resource defining a font configuration. \r\n \r\n The /Font key contains a whole dictionary, opposed to the name/value pairs we�ve seen previously (e.g., /Type /Page). The font we configured is called /F0, and the font face we selected is /Times-Roman. The /Subtype is the format of the font file, and /Type1 refers to the PostScript type 1 file format. The specification defines 14 �standard� fonts that all PDF applications should support."; string resource2 = "Any of these values can be used for the /BaseFont in a /Font dictionary. Nonstandard fonts can be embedded in a PDF document, but it is not easy to do manually. We will put off custom fonts until we can use iTextSharp�s high-level framework."; string crossRef = "After the header and the body comes the cross-reference table. It records the byte location of each object in the body of the file. This enables random-access of the document, so when rendering a page, only the objects required for that page are read from the file. This makes PDFs much faster than their PostScript predecessors, which had to read in the entire file before processing it."; string trailer = "Finally, we come to the last component of a PDF document. The trailer tells applications how to start reading the file. At minimum, it contains three things: \r\n 1. A reference to the catalog which links to the root of the document. \r\n 2. The location of the cross-reference table. \r\n 3. The size of the cross-reference table. \r\n \r\n Since a trailer is all you need to begin processing a document, PDFs are typically read back-to-front: first, the end of the file is found, and then you read backwards until you arrive at the beginning of the trailer. After that, you should have all the information you need to load any page in the PDF."; #endregion //Create a new PDF document. PdfDocument document = new PdfDocument(); document.DocumentInformation.Title = "CustomTag"; #region page1 //Add a page to the document. PdfPage page1 = document.Pages.Add(); //Load the image from the disk. PdfBitmap image = new PdfBitmap(image1); PdfStructureElement imageElement = new PdfStructureElement(PdfTagType.Figure); imageElement.AlternateText = "PDF Succintly image"; //adding tag to the PDF image image.PdfTag = imageElement; //Draw the image page1.Graphics.DrawImage(image, 0, 0, page1.GetClientSize().Width, page1.GetClientSize().Height - 20); #endregion #region page2 PdfPage page2 = document.Pages.Add(); PdfStructureElement hTextElement1 = new PdfStructureElement(PdfTagType.Heading); PdfStructureElement headingFirstLevel = new PdfStructureElement(PdfTagType.HeadingLevel1); headingFirstLevel.Parent = hTextElement1; PdfTextElement headerElement1 = new PdfTextElement("Chapter 1 Conceptual Overview", fontTitle, PdfBrushes.Black); headerElement1.PdfTag = headingFirstLevel; headerElement1.Draw(page2, new PointF(100, 0)); //Initialize the structure element with tag type paragraph. PdfStructureElement textElement1 = new PdfStructureElement(PdfTagType.Paragraph); textElement1.Parent = headingFirstLevel; textElement1.ActualText = pdfChapter; PdfTextElement element1 = new PdfTextElement(pdfChapter, fontnormal); element1.PdfTag = textElement1; element1.Brush = new PdfSolidBrush(blackColor); element1.Draw(page2, new RectangleF(0, 40, page2.GetClientSize().Width, 70)); PdfStructureElement hTextElement2 = new PdfStructureElement(PdfTagType.HeadingLevel2); hTextElement2.Parent = hTextElement1; hTextElement2.ActualText = "Header"; PdfTextElement headerElement2 = new PdfTextElement("Header", fontHead2, PdfBrushes.Black); headerElement2.PdfTag = hTextElement1; headerElement2.Draw(page2, new PointF(0, 140)); PdfStructureElement textElement2 = new PdfStructureElement(PdfTagType.Paragraph); textElement2.Parent = hTextElement2; textElement2.ActualText = header; PdfTextElement element2 = new PdfTextElement(header, fontnormal); element2.PdfTag = textElement2; element2.Brush = new PdfSolidBrush(blackColor); element2.Draw(page2, new RectangleF(0, 170, page2.GetClientSize().Width, 40)); PdfStructureElement hTextElement3 = new PdfStructureElement(PdfTagType.HeadingLevel2); hTextElement3.Parent = hTextElement1; hTextElement3.ActualText = "Body"; PdfTextElement headerElement3 = new PdfTextElement("Body", fontHead2, PdfBrushes.Black); headerElement3.PdfTag = hTextElement1; headerElement3.Draw(page2, new PointF(0, 210)); PdfStructureElement textElement3 = new PdfStructureElement(PdfTagType.Paragraph); textElement3.Parent = hTextElement3; textElement3.ActualText = body; PdfTextElement element3 = new PdfTextElement(body, fontnormal); element3.PdfTag = textElement3; element3.Brush = new PdfSolidBrush(blackColor); element3.Draw(page2, new RectangleF(0, 240, page2.GetClientSize().Width, 120)); PdfStructureElement hTextElement6 = new PdfStructureElement(PdfTagType.HeadingLevel2); hTextElement6.Parent = hTextElement1; hTextElement6.ActualText = "Cross-Reference Table"; PdfTextElement headerElement5 = new PdfTextElement("Cross-Reference Table", fontHead2, PdfBrushes.Black); headerElement5.PdfTag = hTextElement6; headerElement5.Draw(page2, new PointF(0, 380)); PdfStructureElement textElement6 = new PdfStructureElement(PdfTagType.Paragraph); textElement6.Parent = hTextElement6; textElement6.ActualText = crossRef; PdfTextElement element6 = new PdfTextElement(crossRef, fontnormal); element6.PdfTag = textElement6; element6.Brush = new PdfSolidBrush(blackColor); element6.Draw(page2, new RectangleF(0, 410, page2.GetClientSize().Width, 50)); PdfStructureElement hTextElement7 = new PdfStructureElement(PdfTagType.HeadingLevel2); hTextElement7.Parent = hTextElement1; hTextElement7.ActualText = "Trailer"; PdfTextElement headerElement6 = new PdfTextElement("Trailer", fontHead2, PdfBrushes.Black); headerElement6.PdfTag = hTextElement7; headerElement6.Draw(page2, new PointF(0, 470)); PdfStructureElement textElement7 = new PdfStructureElement(PdfTagType.Paragraph); textElement7.Parent = hTextElement7; textElement7.ActualText = trailer; PdfTextElement element7 = new PdfTextElement(trailer, fontnormal); element7.PdfTag = textElement7; element7.Brush = new PdfSolidBrush(blackColor); element7.Draw(page2, new RectangleF(0, 500, page2.GetClientSize().Width, 110)); #endregion #region page3 PdfPage page3 = document.Pages.Add(); PdfStructureElement hTextElement4 = new PdfStructureElement(PdfTagType.HeadingLevel2); hTextElement4.Parent = hTextElement1; hTextElement4.ActualText = "Resource"; PdfTextElement headerElement4 = new PdfTextElement("Resource", fontHead2, PdfBrushes.Black); headerElement4.PdfTag = hTextElement1; headerElement4.Draw(page3, new PointF(0, 0)); PdfStructureElement textElement4 = new PdfStructureElement(PdfTagType.Paragraph); textElement4.Parent = hTextElement4; textElement4.ActualText = resource; PdfTextElement element4 = new PdfTextElement(resource, fontnormal); element4.PdfTag = textElement4; element4.Brush = new PdfSolidBrush(blackColor); element4.Draw(page3, new RectangleF(0, 40, page2.GetClientSize().Width, 70)); //Create a new PdfGrid. PdfGrid pdfGrid = new PdfGrid(); PdfStructureElement element = new PdfStructureElement(PdfTagType.Table); //Adding tag to PDF grid. pdfGrid.PdfTag = element; //Add three columns. pdfGrid.Columns.Add(3); PdfGridRow[] headerRow = pdfGrid.Headers.Add(3); PdfGridRow pdfGridHeader = pdfGrid.Headers[0]; pdfGridHeader.PdfTag = new PdfStructureElement(PdfTagType.TableRow); PdfGridCellStyle headerStyle = new PdfGridCellStyle(); headerStyle.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 13); pdfGridHeader.ApplyStyle(headerStyle); pdfGridHeader.Cells[0].Value = "Times Roman Family"; pdfGridHeader.Cells[0].PdfTag = new PdfStructureElement(PdfTagType.TableHeader); pdfGridHeader.Cells[1].Value = "Helvetica Family"; pdfGridHeader.Cells[1].PdfTag = new PdfStructureElement(PdfTagType.TableHeader); pdfGridHeader.Cells[2].Value = "Courier Family"; pdfGridHeader.Cells[2].PdfTag = new PdfStructureElement(PdfTagType.TableHeader); PdfGridRow pdfGridRow1 = pdfGrid.Rows.Add(); pdfGridRow1.PdfTag = new PdfStructureElement(PdfTagType.TableRow); pdfGridRow1.Cells[0].Value = "Times roman"; pdfGridRow1.Cells[1].Value = "Helvetica"; pdfGridRow1.Cells[2].Value = "Courier"; pdfGridRow1.Cells[0].PdfTag = new PdfStructureElement(PdfTagType.TableDataCell); pdfGridRow1.Cells[1].PdfTag = new PdfStructureElement(PdfTagType.TableDataCell); pdfGridRow1.Cells[2].PdfTag = new PdfStructureElement(PdfTagType.TableDataCell); PdfGridRow pdfGridRow2 = pdfGrid.Rows.Add(); pdfGridRow2.PdfTag = new PdfStructureElement(PdfTagType.TableRow); pdfGridRow2.Cells[0].Value = "Times-Bold"; pdfGridRow2.Cells[1].Value = "Helvetica-Bold"; pdfGridRow2.Cells[2].Value = "Courier-Bold"; pdfGridRow2.Cells[0].PdfTag = new PdfStructureElement(PdfTagType.TableDataCell); pdfGridRow2.Cells[1].PdfTag = new PdfStructureElement(PdfTagType.TableDataCell); pdfGridRow2.Cells[2].PdfTag = new PdfStructureElement(PdfTagType.TableDataCell); PdfGridRow pdfGridRow3 = pdfGrid.Rows.Add(); pdfGridRow3.PdfTag = new PdfStructureElement(PdfTagType.TableRow); pdfGridRow3.Cells[0].Value = "Times-Italic"; pdfGridRow3.Cells[1].Value = "Helvetica-Oblique"; pdfGridRow3.Cells[2].Value = "Courier-Oblique"; pdfGridRow3.Cells[0].PdfTag = new PdfStructureElement(PdfTagType.TableDataCell); pdfGridRow3.Cells[1].PdfTag = new PdfStructureElement(PdfTagType.TableDataCell); pdfGridRow3.Cells[2].PdfTag = new PdfStructureElement(PdfTagType.TableDataCell); PdfGridRow pdfGridRow4 = pdfGrid.Rows.Add(); pdfGridRow4.PdfTag = new PdfStructureElement(PdfTagType.TableRow); pdfGridRow4.Cells[0].Value = "Times-BoldItalic"; pdfGridRow4.Cells[1].Value = "Helvetica-BoldOblique"; pdfGridRow4.Cells[2].Value = "Courier-BoldOblique"; pdfGridRow4.Cells[0].PdfTag = new PdfStructureElement(PdfTagType.TableDataCell); pdfGridRow4.Cells[1].PdfTag = new PdfStructureElement(PdfTagType.TableDataCell); pdfGridRow4.Cells[2].PdfTag = new PdfStructureElement(PdfTagType.TableDataCell); pdfGrid.BeginCellLayout += PdfGrid_BeginCellLayout; pdfGrid.Draw(page3, new PointF(20, 130)); page3.Graphics.DrawRectangle(PdfPens.Black, new RectangleF(20, 120, 490, 90)); #endregion //Save the document and dispose it. MemoryStream stream = new MemoryStream(); await document.SaveAsync(stream); document.Close(true); Save(stream, "CustomtagSample.pdf"); }
public ActionResult CreateDocument() { //Creates a new PDF document PdfDocument document = new PdfDocument(); //Adds page settings document.PageSettings.Orientation = PdfPageOrientation.Portrait; document.PageSettings.Margins.All = 50; //Adds a page to the document PdfPage page = document.Pages.Add(); PdfGraphics graphics = page.Graphics; RectangleF bounds = new RectangleF(); if (person.photo != null) { //Loads the image as stream System.Drawing.Image img = System.Drawing.Image.FromFile(@person.photo.ImagePath); double perc; perc = 200.0 / img.Width; float newHeight = (float)(img.Height * perc); FileStream imageStream = new FileStream(person.photo.ImagePath, FileMode.Open, FileAccess.Read); bounds = new RectangleF(250, 0, 200, newHeight); PdfImage image = PdfImage.FromStream(imageStream); //Draws the image to the PDF page page.Graphics.DrawImage(image, bounds); } PdfBrush solidBrush = new PdfSolidBrush(new PdfColor(126, 151, 173)); bounds = new RectangleF(0, bounds.Bottom + 90, graphics.ClientSize.Width, 30); //Draws a rectangle to place the heading in that region. graphics.DrawRectangle(solidBrush, bounds); //Creates a font for adding the heading in the page PdfFont subHeadingFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 14); //Creates a text element to add the invoice number PdfTextElement element = new PdfTextElement("Personal Information", subHeadingFont); element.Brush = PdfBrushes.White; //Draws the heading on the page PdfLayoutResult result = element.Draw(page, new PointF(10, bounds.Top + 8)); string name = person.personalInfo.FirstName + " " + person.personalInfo.LastName; SizeF textSize = subHeadingFont.MeasureString(name); PointF textPosition = new PointF(graphics.ClientSize.Width - textSize.Width - 10, result.Bounds.Y); graphics.DrawString(name, subHeadingFont, element.Brush, textPosition); PdfFont timesRoman = new PdfStandardFont(PdfFontFamily.TimesRoman, 12); //Creates text elements to add the address and draw it to the page. element = new PdfTextElement(System.Environment.NewLine + "Phonenumber: " + person.personalInfo.Phonenumber + System.Environment.NewLine, timesRoman); element.Brush = new PdfSolidBrush(new PdfColor(1, 0, 0)); result = element.Draw(page, new PointF(10, result.Bounds.Bottom + 25)); PdfPen linePen = new PdfPen(new PdfColor(126, 151, 173), 0.70f); PointF startPoint = new PointF(0, result.Bounds.Bottom + 3); PointF endPoint = new PointF(graphics.ClientSize.Width, result.Bounds.Bottom + 3); //Draws a line at the bottom of the address graphics.DrawLine(linePen, startPoint, endPoint); element = new PdfTextElement(System.Environment.NewLine + "Email: " + person.personalInfo.Email + System.Environment.NewLine, timesRoman); element.Brush = new PdfSolidBrush(new PdfColor(1, 0, 0)); result = element.Draw(page, new PointF(10, result.Bounds.Bottom + 25)); linePen = new PdfPen(new PdfColor(126, 151, 173), 0.70f); startPoint = new PointF(0, result.Bounds.Bottom + 3); endPoint = new PointF(graphics.ClientSize.Width, result.Bounds.Bottom + 3); //Draws a line at the bottom of the address graphics.DrawLine(linePen, startPoint, endPoint); DateTime strDate = new DateTime(person.personalInfo.BirthDay.Year, person.personalInfo.BirthDay.Month, person.personalInfo.BirthDay.Day); element = new PdfTextElement(System.Environment.NewLine + "BirthDay: " + person.personalInfo.BirthDay.ToString("d") + System.Environment.NewLine, timesRoman); element.Brush = new PdfSolidBrush(new PdfColor(1, 0, 0)); result = element.Draw(page, new PointF(10, result.Bounds.Bottom + 25)); linePen = new PdfPen(new PdfColor(126, 151, 173), 0.70f); startPoint = new PointF(0, result.Bounds.Bottom + 3); endPoint = new PointF(graphics.ClientSize.Width, result.Bounds.Bottom + 3); //Draws a line at the bottom of the address graphics.DrawLine(linePen, startPoint, endPoint); element = new PdfTextElement(System.Environment.NewLine + "Location: " + person.personalInfo.City + System.Environment.NewLine, timesRoman); element.Brush = new PdfSolidBrush(new PdfColor(1, 0, 0)); result = element.Draw(page, new PointF(10, result.Bounds.Bottom + 25)); linePen = new PdfPen(new PdfColor(126, 151, 173), 0.70f); startPoint = new PointF(0, result.Bounds.Bottom + 3); endPoint = new PointF(graphics.ClientSize.Width, result.Bounds.Bottom + 3); //Draws a line at the bottom of the address graphics.DrawLine(linePen, startPoint, endPoint); if (!person.personalInfo.Skills.Contains("Enter text here...")) { element = new PdfTextElement(System.Environment.NewLine + "Skills: " + person.personalInfo.Skills + System.Environment.NewLine, timesRoman); element.Brush = new PdfSolidBrush(new PdfColor(1, 0, 0)); result = element.Draw(page, new PointF(10, result.Bounds.Bottom + 25)); linePen = new PdfPen(new PdfColor(126, 151, 173), 0.70f); startPoint = new PointF(0, result.Bounds.Bottom + 3); endPoint = new PointF(graphics.ClientSize.Width, result.Bounds.Bottom + 3); //Draws a line at the bottom of the address graphics.DrawLine(linePen, startPoint, endPoint); } DataTable Details; PdfGrid grid; PdfGridCellStyle cellStyle; PdfGridRow header; PdfGridCellStyle headerStyle = new PdfGridCellStyle(); PdfGridLayoutFormat layoutFormat = new PdfGridLayoutFormat(); PdfGridLayoutResult gridResult; if (person.gainedEducation != null && person.gainedEducation.Count > 0) { //Creates the datasource for the table Details = new DataTable(); Details = CreateDataTable(person.gainedEducation); //Creates a PDF grid grid = new PdfGrid(); //Adds the data source grid.DataSource = Details; //Creates the grid cell styles cellStyle = new PdfGridCellStyle(); cellStyle.Borders.All = PdfPens.White; header = grid.Headers[0]; //Creates the header style headerStyle = new PdfGridCellStyle(); headerStyle.Borders.All = new PdfPen(new PdfColor(126, 151, 173)); headerStyle.BackgroundBrush = new PdfSolidBrush(new PdfColor(126, 151, 173)); headerStyle.TextBrush = PdfBrushes.White; headerStyle.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 14f, PdfFontStyle.Regular); //Adds cell customizations for (int i = 0; i < header.Cells.Count; i++) { if (i == 0 || i == 1) { header.Cells[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); } else { header.Cells[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle); } } //Applies the header style header.ApplyStyle(headerStyle); cellStyle.Borders.Bottom = new PdfPen(new PdfColor(217, 217, 217), 0.70f); cellStyle.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 12f); cellStyle.TextBrush = new PdfSolidBrush(new PdfColor(131, 130, 136)); //Creates the layout format for grid layoutFormat = new PdfGridLayoutFormat(); // Creates layout format settings to allow the table pagination layoutFormat.Layout = PdfLayoutType.Paginate; //Draws the grid to the PDF page. gridResult = grid.Draw(page, new RectangleF (new PointF(0, result.Bounds.Bottom + 40), new SizeF(graphics.ClientSize.Width, graphics.ClientSize.Height - 50)), layoutFormat); page = document.Pages.Add(); element = new PdfTextElement(" ", subHeadingFont); element.Brush = PdfBrushes.White; graphics = page.Graphics; result = element.Draw(page, new PointF(10, 10)); } if (person.Languages != null && person.Languages.Count > 0) { Details = new DataTable(); //Creates the datasource for the table Details = CreateDataTable(person.Languages); //Creates a PDF grid grid = new PdfGrid(); //Adds the data source grid.DataSource = Details; //Creates the grid cell styles cellStyle = new PdfGridCellStyle(); cellStyle.Borders.All = PdfPens.White; header = grid.Headers[0]; //Creates the header style headerStyle = new PdfGridCellStyle(); headerStyle.Borders.All = new PdfPen(new PdfColor(126, 151, 173)); headerStyle.BackgroundBrush = new PdfSolidBrush(new PdfColor(126, 151, 173)); headerStyle.TextBrush = PdfBrushes.White; headerStyle.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 14f, PdfFontStyle.Regular); //Adds cell customizations for (int i = 0; i < header.Cells.Count; i++) { if (i == 0 || i == 1) { header.Cells[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); } else { header.Cells[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle); } } //Applies the header style header.ApplyStyle(headerStyle); cellStyle.Borders.Bottom = new PdfPen(new PdfColor(217, 217, 217), 0.70f); cellStyle.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 12f); cellStyle.TextBrush = new PdfSolidBrush(new PdfColor(131, 130, 136)); //Creates the layout format for grid layoutFormat = new PdfGridLayoutFormat(); // Creates layout format settings to allow the table pagination layoutFormat.Layout = PdfLayoutType.Paginate; //Draws the grid to the PDF page. gridResult = grid.Draw(page, new RectangleF (new PointF(0, result.Bounds.Bottom + 40), new SizeF(graphics.ClientSize.Width, graphics.ClientSize.Height - 50)), layoutFormat); page = document.Pages.Add(); element = new PdfTextElement(" ", subHeadingFont); element.Brush = PdfBrushes.White; graphics = page.Graphics; result = element.Draw(page, new PointF(10, 10)); } if (person.gainedExperience != null && person.gainedExperience.Count > 0) { Details = new DataTable(); //Creates the datasource for the table Details = CreateDataTable(person.gainedExperience); //Creates a PDF grid grid = new PdfGrid(); //Adds the data source grid.DataSource = Details; //Creates the grid cell styles cellStyle = new PdfGridCellStyle(); cellStyle.Borders.All = PdfPens.White; header = grid.Headers[0]; //Creates the header style headerStyle = new PdfGridCellStyle(); headerStyle.Borders.All = new PdfPen(new PdfColor(126, 151, 173)); headerStyle.BackgroundBrush = new PdfSolidBrush(new PdfColor(126, 151, 173)); headerStyle.TextBrush = PdfBrushes.White; headerStyle.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 14f, PdfFontStyle.Regular); //Adds cell customizations for (int i = 0; i < header.Cells.Count; i++) { if (i == 0 || i == 1) { header.Cells[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); } else { header.Cells[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle); } } //Applies the header style header.ApplyStyle(headerStyle); cellStyle.Borders.Bottom = new PdfPen(new PdfColor(217, 217, 217), 0.70f); cellStyle.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 12f); cellStyle.TextBrush = new PdfSolidBrush(new PdfColor(131, 130, 136)); //Creates the layout format for grid layoutFormat = new PdfGridLayoutFormat(); // Creates layout format settings to allow the table pagination layoutFormat.Layout = PdfLayoutType.Paginate; //Draws the grid to the PDF page. gridResult = grid.Draw(page, new RectangleF (new PointF(0, result.Bounds.Bottom + 40), new SizeF(graphics.ClientSize.Width, graphics.ClientSize.Height - 50)), layoutFormat); } //FileStream fileStream = new FileStream("Sample.pdf", FileMode.CreateNew, FileAccess.ReadWrite); ////Save and close the PDF document //document.Save(fileStream); ////document.Close(true); MemoryStream stream = new MemoryStream(); document.Save(stream); //If the position is not set to '0' then the PDF will be empty. stream.Position = 0; //Download the PDF document in the browser. FileStreamResult fileStreamResult = new FileStreamResult(stream, "application/pdf"); fileStreamResult.FileDownloadName = "YourCV.pdf"; return(fileStreamResult); ////Create a new PDF document //PdfDocument document = new PdfDocument(); ////Add a page to the document //PdfPage page = document.Pages.Add(); ////Create PDF graphics for the page //PdfGraphics graphics = page.Graphics; ////Set the standard font //PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20); ////Draw the text //graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new PointF(0, 0)); ////Saving the PDF to the MemoryStream //MemoryStream stream = new MemoryStream(); //document.Save(stream); ////If the position is not set to '0' then the PDF will be empty. //stream.Position = 0; ////Download the PDF document in the browser. //FileStreamResult fileStreamResult = new FileStreamResult(stream, "application/pdf"); //fileStreamResult.FileDownloadName = "YourCV.pdf"; //return fileStreamResult; }
public IActionResult Index() { //Creates a new PDF document PdfDocument document = new PdfDocument(); //Adds page settings document.PageSettings.Orientation = PdfPageOrientation.Landscape; document.PageSettings.Margins.All = 50; //Adds a page to the document PdfPage page = document.Pages.Add(); PdfGraphics graphics = page.Graphics; //Loads the image as stream FileStream imageStream = new FileStream("logo.png", FileMode.Open, FileAccess.Read); RectangleF bounds = new RectangleF(176, 0, 390, 130); PdfImage image = PdfImage.FromStream(imageStream); //Draws the image to the PDF page page.Graphics.DrawImage(image, bounds); PdfBrush solidBrush = new PdfSolidBrush(new PdfColor(126, 151, 173)); bounds = new RectangleF(0, bounds.Bottom + 90, graphics.ClientSize.Width, 30); //Draws a rectangle to place the heading in that region. graphics.DrawRectangle(solidBrush, bounds); //Creates a font for adding the heading in the page PdfFont subHeadingFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 14); //Creates a text element to add the invoice number PdfTextElement element = new PdfTextElement("Purchase Order", subHeadingFont); element.Brush = PdfBrushes.White; //Draws the heading on the page PdfLayoutResult result = element.Draw(page, new PointF(10, bounds.Top + 8)); string currentDate = "DATE " + DateTime.Now.ToString("MM/dd/yyyy"); //Measures the width of the text to place it in the correct location SizeF textSize = subHeadingFont.MeasureString(currentDate); PointF textPosition = new PointF(graphics.ClientSize.Width - textSize.Width - 10, result.Bounds.Y); //Draws the date by using DrawString method graphics.DrawString(currentDate, subHeadingFont, element.Brush, textPosition); PdfFont timesRoman = new PdfStandardFont(PdfFontFamily.TimesRoman, 10); //Creates text elements to add the address and draw it to the page. element = new PdfTextElement("BILL TO ", timesRoman); element.Brush = new PdfSolidBrush(new PdfColor(126, 155, 203)); result = element.Draw(page, new PointF(10, result.Bounds.Bottom + 25)); PdfPen linePen = new PdfPen(new PdfColor(126, 151, 173), 0.70f); PointF startPoint = new PointF(0, result.Bounds.Bottom + 3); PointF endPoint = new PointF(graphics.ClientSize.Width, result.Bounds.Bottom + 3); //Draws a line at the bottom of the address graphics.DrawLine(linePen, startPoint, endPoint); //Creates the datasource for the table var invoiceDetails = GetProductDetailsAsDataTable(); //Creates a PDF grid PdfGrid grid = new PdfGrid(); //Adds the data source grid.DataSource = invoiceDetails; //Creates the grid cell styles PdfGridCellStyle cellStyle = new PdfGridCellStyle(); cellStyle.Borders.All = PdfPens.White; PdfGridRow header = grid.Headers[0]; //Creates the header style PdfGridCellStyle headerStyle = new PdfGridCellStyle(); headerStyle.Borders.All = new PdfPen(new PdfColor(126, 151, 173)); headerStyle.BackgroundBrush = new PdfSolidBrush(new PdfColor(126, 151, 173)); headerStyle.TextBrush = PdfBrushes.White; headerStyle.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 14f, PdfFontStyle.Regular); //Adds cell customizations for (int i = 0; i < header.Cells.Count; i++) { if (i == 0 || i == 1) { header.Cells[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); } else { header.Cells[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle); } } //Applies the header style header.ApplyStyle(headerStyle); cellStyle.Borders.Bottom = new PdfPen(new PdfColor(217, 217, 217), 0.70f); cellStyle.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 12f); cellStyle.TextBrush = new PdfSolidBrush(new PdfColor(131, 130, 136)); //Creates the layout format for grid PdfGridLayoutFormat layoutFormat = new PdfGridLayoutFormat(); // Creates layout format settings to allow the table pagination layoutFormat.Layout = PdfLayoutType.Paginate; //Draws the grid to the PDF page. PdfGridLayoutResult gridResult = grid.Draw(page, new RectangleF(new PointF(0, result.Bounds.Bottom + 40), new SizeF(graphics.ClientSize.Width, graphics.ClientSize.Height - 100)), layoutFormat); //Save the PDF document to stream MemoryStream ms = new MemoryStream(); document.Save(ms); document.Close(true); ms.Position = 0; FileStreamResult fileStreamResult = new FileStreamResult(ms, "application/pdf"); fileStreamResult.FileDownloadName = "Sample.pdf"; return(fileStreamResult); }
private async void PDF_btn_Clicked(object sender, EventArgs e) { if (Global.Buildings[building_index].zero_status == "0") { await DisplayAlert("", "決済して管理していない物件です。", "はい"); } else { //// Create a new PDF document PdfDocument document = new PdfDocument(); document.PageSettings.Orientation = PdfPageOrientation.Landscape; PdfSection section1 = document.Sections.Add(); section1.PageSettings.Size = PdfPageSize.A4; PdfPage page = section1.Pages.Add(); //Add a page to the document //PdfPage page = document.Pages.Add(); //page.Size = PdfPageSize.A4; //Create PDF graphics for the page PdfGraphics graphics = page.Graphics; Stream fontStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("owner.arialuni.TTF"); //Stream fontStream = this.GetType().Assembly.GetManifestResourceStream("owner.Resources.arialuni.ttf"); string[] resources = Assembly.GetExecutingAssembly().GetManifestResourceNames(); //Set the standard font //PdfFont font = new PdfStandardFont(PdfFontFamily.ZapfDingbats, 20); PdfTrueTypeFont title_font = new PdfTrueTypeFont(fontStream, 25); PdfTrueTypeFont normal_font = new PdfTrueTypeFont(fontStream, 16); PdfTrueTypeFont big_font = new PdfTrueTypeFont(fontStream, 20); PdfTrueTypeFont small_font = new PdfTrueTypeFont(fontStream, 10); //Draw the title_text graphics.DrawString("■ 確 定 申 告 資 料( 収 支 明 細 書 )■", title_font, PdfBrushes.Black, new PointF(150, 10)); //Draw the owner_info_text graphics.DrawString("家主名", normal_font, PdfBrushes.Black, new PointF(10, 40)); string owner_name = "金井"; graphics.DrawString(owner_name, normal_font, PdfBrushes.Black, new PointF(70, 40)); graphics.DrawString("物件名", normal_font, PdfBrushes.Black, new PointF(10, 60)); string building_name = "レジデンシャル六本木"; graphics.DrawString(building_name, normal_font, PdfBrushes.Black, new PointF(70, 60)); string position = "北海道札幌市中央区南1条西1丁目"; graphics.DrawString("所在地", normal_font, PdfBrushes.Black, new PointF(10, 80)); graphics.DrawString(position, normal_font, PdfBrushes.Black, new PointF(70, 80)); //Draw the income_info_text graphics.DrawString("【収入】", big_font, PdfBrushes.Blue, new PointF(8, 100)); //Create a PdfGrid. PdfGrid income_Grid = new PdfGrid(); //Add the columns income_Grid.Columns.Add(17); //Specify the style for the PdfGridCell. PdfGridCellStyle pdfGridCellStyle = new PdfGridCellStyle(); pdfGridCellStyle.TextPen = PdfPens.White; pdfGridCellStyle.TextBrush = PdfBrushes.White; pdfGridCellStyle.BackgroundBrush = PdfBrushes.Blue; pdfGridCellStyle.Borders.All = PdfPens.Black; pdfGridCellStyle.Font = small_font; for (int i = 0; i < 8; i++) { PdfGridRow row = income_Grid.Rows.Add(); row.Height = 15; row.Cells[0].ColumnSpan = 2; row.Cells[2].ColumnSpan = 2; if (i == 0) { for (int j = 4; j < 17; j++) { if (j == 4) { row.Cells[j].Value = String.Format("{0}年{1}月", current_year - 2000, j - 3); } else if (j == 16) { row.Cells[j].Value = "合計"; } else { row.Cells[j].Value = String.Format("{0}月", j - 3); } row.Cells[j].Style = pdfGridCellStyle; } } } income_Grid.Rows[1].Cells[0].RowSpan = 6; //Set the value to the specific cell. income_Grid.Rows[7].Cells[0].Value = "収入計"; income_Grid.Rows[7].Cells[0].Style = pdfGridCellStyle; income_Grid.Rows[1].Cells[2].Value = "家賃"; income_Grid.Rows[1].Cells[2].Style = pdfGridCellStyle; income_Grid.Rows[2].Cells[2].Value = "管理費"; income_Grid.Rows[2].Cells[2].Style = pdfGridCellStyle; income_Grid.Rows[3].Cells[2].Value = "礼金"; income_Grid.Rows[3].Cells[2].Style = pdfGridCellStyle; income_Grid.Rows[4].Cells[2].Value = "更新料"; income_Grid.Rows[4].Cells[2].Style = pdfGridCellStyle; income_Grid.Rows[5].Cells[2].Value = "その他"; income_Grid.Rows[5].Cells[2].Style = pdfGridCellStyle; income_Grid.Rows[6].Cells[2].Value = "小計"; income_Grid.Rows[6].Cells[2].Style = pdfGridCellStyle; income_Grid.Draw(page, new PointF(10, 130)); //Draw the outcome_info_text graphics.DrawString("【支出】", big_font, PdfBrushes.Blue, new PointF(8, 255)); //Create a PdfGrid. PdfGrid outcome_Grid = new PdfGrid(); //Add the columns outcome_Grid.Columns.Add(17); for (int i = 0; i < 13; i++) { PdfGridRow row = outcome_Grid.Rows.Add(); row.Height = 15; row.Cells[0].ColumnSpan = 2; row.Cells[2].ColumnSpan = 2; if (i == 0) { for (int j = 4; j < 17; j++) { if (j == 4) { row.Cells[j].Value = String.Format("{0}年{1}{2}", current_year - 2000, j - 3, "月"); } else if (j == 16) { row.Cells[j].Value = "合計"; } else { row.Cells[j].Value = String.Format("{0}月", j - 3); } row.Cells[j].Style = pdfGridCellStyle; } } } outcome_Grid.Rows[1].Cells[0].RowSpan = 11; //Set the value to the specific cell. outcome_Grid.Rows[12].Cells[0].Value = "支出計"; outcome_Grid.Rows[12].Cells[0].Style = pdfGridCellStyle; outcome_Grid.Rows[1].Cells[2].Value = "建物管理費"; outcome_Grid.Rows[1].Cells[2].Style = pdfGridCellStyle; outcome_Grid.Rows[2].Cells[2].Value = "管理料"; outcome_Grid.Rows[2].Cells[2].Style = pdfGridCellStyle; outcome_Grid.Rows[3].Cells[2].Value = "送金料"; outcome_Grid.Rows[3].Cells[2].Style = pdfGridCellStyle; outcome_Grid.Rows[4].Cells[2].Value = "新 管理料"; outcome_Grid.Rows[4].Cells[2].Style = pdfGridCellStyle; outcome_Grid.Rows[5].Cells[2].Value = "印 代"; outcome_Grid.Rows[5].Cells[2].Style = pdfGridCellStyle; outcome_Grid.Rows[6].Cells[2].Value = "更新手数料"; outcome_Grid.Rows[6].Cells[2].Style = pdfGridCellStyle; outcome_Grid.Rows[7].Cells[2].Value = "健交損代"; outcome_Grid.Rows[7].Cells[2].Style = pdfGridCellStyle; outcome_Grid.Rows[8].Cells[2].Value = "内装代"; outcome_Grid.Rows[8].Cells[2].Style = pdfGridCellStyle; outcome_Grid.Rows[9].Cells[2].Value = "立替代"; outcome_Grid.Rows[9].Cells[2].Style = pdfGridCellStyle; outcome_Grid.Rows[10].Cells[2].Value = "都理費"; outcome_Grid.Rows[10].Cells[2].Style = pdfGridCellStyle; outcome_Grid.Rows[11].Cells[2].Value = "その他"; outcome_Grid.Rows[11].Cells[2].Style = pdfGridCellStyle; outcome_Grid.Rows[12].Cells[2].Value = "小計"; outcome_Grid.Rows[12].Cells[2].Style = pdfGridCellStyle; outcome_Grid.Draw(page, new PointF(10, 285)); //Draw the balance_info_text graphics.DrawString("【収支】", big_font, PdfBrushes.Blue, new PointF(8, 485)); //Create a PdfGrid. PdfGrid balance_Grid = new PdfGrid(); //Add the columns balance_Grid.Columns.Add(17); for (int i = 0; i < 2; i++) { PdfGridRow row = balance_Grid.Rows.Add(); row.Height = 15; row.Cells[0].ColumnSpan = 2; row.Cells[2].ColumnSpan = 2; if (i == 0) { for (int j = 4; j < 17; j++) { if (j == 4) { row.Cells[j].Value = String.Format("{0}年{1}月", current_year - 2000, j - 3); } else if (j == 16) { row.Cells[j].Value = "合計"; } else { row.Cells[j].Value = String.Format("{0}月", j - 3); } row.Cells[j].Style = pdfGridCellStyle; } } } //Set the value to the specific cell. balance_Grid.Rows[1].Cells[0].Value = "収支計"; balance_Grid.Rows[1].Cells[0].Style = pdfGridCellStyle; balance_Grid.Draw(page, new PointF(10, 505)); //Save the document to the stream MemoryStream stream = new MemoryStream(); document.Save(stream); //Close the document document.Close(true); //Save the stream as a file in the device and invoke it for viewing DependencyService.Get <ISave>().SaveAndView("Output.pdf", "application/pdf", stream); //Process.Start("Output.pdf"); } }
public bool CreatePDF(Movimientos movimientos, DataTable tablacarrito, Model.Usuario User) { try { PdfDocument document = new PdfDocument(); //Adds page settings document.PageSettings.Orientation = PdfPageOrientation.Portrait; document.PageSettings.Margins.All = 50; //Adds a page to the document PdfPage page = document.Pages.Add(); PdfGraphics graphics = page.Graphics; //Loads the image from disk //PdfImage image = PdfImage.FromFile("Logo.png"); Stream imageStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Inventario2.Assets.NewLogo.jpeg"); //Load the image from the disk. PdfBitmap image = new PdfBitmap(imageStream); //Draw the image RectangleF bounds = new RectangleF(0, 0, 110, 110); //Draws the image to the PDF page page.Graphics.DrawImage(image, bounds); //DRAW THE MAIN TITLE PdfFont Headfont = new PdfStandardFont(PdfFontFamily.Helvetica, 20); //Creates a text element to add the invoice number PdfTextElement headelement = new PdfTextElement("AUDIO VIDEO STUDIOS ", Headfont); headelement.Brush = PdfBrushes.Red; PdfLayoutResult result = headelement.Draw(page, new PointF(graphics.ClientSize.Width - 350, graphics.ClientSize.Height - 740)); PdfFont Subtitle = new PdfStandardFont(PdfFontFamily.Helvetica, 14); //Creates a text element to add the invoice number PdfTextElement subtitelement = new PdfTextElement("ORDEN DE SALIDA ", Subtitle); subtitelement.Brush = PdfBrushes.Red; PdfLayoutResult Subresult = subtitelement.Draw(page, new PointF(graphics.ClientSize.Width - 300, graphics.ClientSize.Height - 710)); PdfBrush solidBrush = new PdfSolidBrush(new PdfColor(222, 237, 242)); bounds = new RectangleF(bounds.Right, Subresult.Bounds.Bottom, graphics.ClientSize.Width - 300, 50); //Draws a rectangle to place the heading in that region. graphics.DrawRectangle(solidBrush, bounds); //creating fields, folio, fecha, lugar PdfFont campofont = new PdfStandardFont(PdfFontFamily.TimesRoman, 12); PdfTextElement lblugar = new PdfTextElement("EVENTO: ", campofont); lblugar.Brush = PdfBrushes.Black; PdfLayoutResult reslblugar = lblugar.Draw(page, new PointF(bounds.Left + 40, bounds.Top)); PdfTextElement lbfecha = new PdfTextElement("FECHA: ", campofont); lbfecha.Brush = PdfBrushes.Black; PdfLayoutResult reslbfecha = lbfecha.Draw(page, new PointF(bounds.Left + 40, bounds.Top + 16)); PdfTextElement lbfolio = new PdfTextElement("FOLIO: ", campofont); lbfolio.Brush = PdfBrushes.Black; PdfLayoutResult reslbfolio = lbfolio.Draw(page, new PointF(bounds.Left + 40, bounds.Top + 32)); PdfBrush solidBrush2 = new PdfSolidBrush(new PdfColor(190, 220, 228)); bounds = new RectangleF(bounds.Right, Subresult.Bounds.Bottom, graphics.ClientSize.Width - 300, 50); //Draws a rectangle to place the heading in that region. graphics.DrawRectangle(solidBrush2, bounds); //variables de campos PdfTextElement lugar = new PdfTextElement(movimientos.lugar, campofont); lugar.Brush = PdfBrushes.Black; PdfLayoutResult reslugar = lugar.Draw(page, new PointF(bounds.Left + 40, bounds.Top)); PdfTextElement fecha = new PdfTextElement(DateTime.Now.ToString(), campofont); fecha.Brush = PdfBrushes.Black; PdfLayoutResult resfecha = fecha.Draw(page, new PointF(bounds.Left + 40, bounds.Top + 16)); PdfTextElement folio = new PdfTextElement(movimientos.ID, campofont); folio.Brush = PdfBrushes.Black; PdfLayoutResult resfolio = folio.Draw(page, new PointF(bounds.Left + 40, bounds.Top + 32)); //create table //Creates the datasource for the table DataTable invoiceDetails = tablacarrito; //Creates a PDF grid PdfGrid grid = new PdfGrid(); //Adds the data source grid.DataSource = invoiceDetails; //Creates the grid cell styles PdfGridCellStyle cellStyle = new PdfGridCellStyle(); cellStyle.Borders.All = PdfPens.White; PdfGridRow header = grid.Headers[0]; //Creates the header style PdfGridCellStyle headerStyle = new PdfGridCellStyle(); headerStyle.Borders.All = new PdfPen(new PdfColor(126, 151, 173)); headerStyle.BackgroundBrush = new PdfSolidBrush(new PdfColor(126, 151, 173)); headerStyle.TextBrush = PdfBrushes.White; headerStyle.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 16f, PdfFontStyle.Regular); //Adds cell customizations for (int i = 0; i < header.Cells.Count; i++) { if (i == 0 || i == 1) { header.Cells[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); } else { header.Cells[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle); } } //Applies the header style header.ApplyStyle(headerStyle); cellStyle.Borders.Bottom = new PdfPen(new PdfColor(217, 217, 217), 0.70f); cellStyle.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 10f); cellStyle.TextBrush = new PdfSolidBrush(new PdfColor(131, 130, 136)); //Creates the layout format for grid PdfGridLayoutFormat layoutFormat = new PdfGridLayoutFormat(); // Creates layout format settings to allow the table pagination layoutFormat.Layout = PdfLayoutType.Paginate; //Draws the grid to the PDF page. PdfGridLayoutResult gridResult = grid.Draw(page, new RectangleF(new PointF(0, result.Bounds.Bottom + 150), new SizeF(graphics.ClientSize.Width, graphics.ClientSize.Height - 100)), layoutFormat); PdfGraphics graphicsSecond = gridResult.Page.Graphics; PdfPen linePen = new PdfPen(new PdfColor(126, 151, 173), 1.0f); PointF startPoint = new PointF(0, gridResult.Bounds.Bottom + 60); PointF endPoint = new PointF(150, gridResult.Bounds.Bottom + 60); //Draws a line at the bottom of the address graphicsSecond.DrawLine(linePen, startPoint, endPoint); PdfFont entregafont = new PdfStandardFont(PdfFontFamily.TimesRoman, 12); PdfTextElement lbentrega = new PdfTextElement("ENTREGA: ", entregafont); lbentrega.Brush = PdfBrushes.Black; PdfLayoutResult reslbentrega = lbentrega.Draw(gridResult.Page, new PointF(linePen.Width / 2.0f, startPoint.Y + 5)); //texto de quien entrega PdfFont usuarioentregafont = new PdfStandardFont(PdfFontFamily.TimesRoman, 12); PdfTextElement lbusuarioentrega = new PdfTextElement(Model.User.nombre + " " + Model.User.apellido_paterno, usuarioentregafont); lbusuarioentrega.Brush = PdfBrushes.Black; PdfLayoutResult reslbusuarioentrega = lbusuarioentrega.Draw(gridResult.Page, new PointF(linePen.Width / 2.0f, startPoint.Y - 20)); PdfPen linePenfinal = new PdfPen(new PdfColor(126, 151, 173), 1.0f); PointF startPointfinal = new PointF(350, gridResult.Bounds.Bottom + 60); PointF endPointfinal = new PointF(graphics.ClientSize.Width, gridResult.Bounds.Bottom + 60); //Draws a line at the bottom of the address graphicsSecond.DrawLine(linePenfinal, startPointfinal, endPointfinal); PdfFont recibefont = new PdfStandardFont(PdfFontFamily.TimesRoman, 12); PdfTextElement lbrecibe = new PdfTextElement("RECIBE: ", recibefont); lbrecibe.Brush = PdfBrushes.Black; PdfLayoutResult reslbrecibe = lbrecibe.Draw(gridResult.Page, new PointF(350.0f + (linePenfinal.Width / 2.0f), startPoint.Y + 5)); //texto de quien recibe PdfFont usuariofont = new PdfStandardFont(PdfFontFamily.TimesRoman, 12); PdfTextElement lbusuario = new PdfTextElement(User.nombre + " " + User.apellido_paterno, usuariofont); lbusuario.Brush = PdfBrushes.Black; PdfLayoutResult reslbusuario = lbusuario.Draw(gridResult.Page, new PointF(350.0f + (linePenfinal.Width / 2.0f), startPoint.Y - 20)); MemoryStream stream = new MemoryStream(); //Save the document. document.Save(stream); streamPDF = stream; //Close the document. document.Close(true); byte[] bytes = stream.ToArray(); bool res = SendSTMPT(bytes, correo); string save = "OrdenDeSalida-" + movimientos.ID; //Save the stream as a file in the device and invoke it for viewing // Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView(save + ".pdf", "application/pdf", stream); //The operation in Save under Xamarin varies between Windows Phone, Android and iOS platforms. Please refer PDF/Xamarin section for respective code samples. if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows) { // Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("Output.pdf", "application/pdf", stream); Xamarin.Forms.DependencyService.Get <ISave>().SaveAndView(save + ".pdf", "application/pdf", stream); } else { Xamarin.Forms.DependencyService.Get <ISave>().SaveAndView(save + ".pdf", "application/pdf", stream); } return(true); } catch { return(false); } }
/// <summary> /// Creates PDF /// </summary> public void CreatePDF(IList <InvoiceItem> dataSource, BillingInformation billInfo, double totalDue) { PdfDocument document = new PdfDocument(); document.PageSettings.Orientation = PdfPageOrientation.Portrait; document.PageSettings.Margins.All = 50; PdfPage page = document.Pages.Add(); PdfGraphics g = page.Graphics; PdfTextElement element = new PdfTextElement(@"Syncfusion Software 2501 Aerial Center Parkway Suite 200 Morrisville, NC 27560 USA Tel +1 888.936.8638 Fax +1 919.573.0306"); element.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 12); element.Brush = new PdfSolidBrush(new PdfColor(0, 0, 0)); PdfLayoutResult result = element.Draw(page, new RectangleF(0, 0, page.Graphics.ClientSize.Width / 2, 200)); Assembly assembly = typeof(MainPage).Assembly; Stream imgStream = assembly.GetManifestResourceStream("Invoice.Assets.SyncfusionLogo.jpg"); PdfImage img = PdfImage.FromStream(imgStream); page.Graphics.DrawImage(img, new RectangleF(g.ClientSize.Width - 200, result.Bounds.Y, 190, 45)); PdfFont subHeadingFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 14); g.DrawRectangle(new PdfSolidBrush(new PdfColor(34, 83, 142)), new RectangleF(0, result.Bounds.Bottom + 40, g.ClientSize.Width, 30)); element = new PdfTextElement("INVOICE " + billInfo.InvoiceNumber.ToString(), subHeadingFont); element.Brush = PdfBrushes.White; result = element.Draw(page, new PointF(10, result.Bounds.Bottom + 48)); string currentDate = "DATE " + billInfo.Date.ToString("d"); SizeF textSize = subHeadingFont.MeasureString(currentDate); g.DrawString(currentDate, subHeadingFont, element.Brush, new PointF(g.ClientSize.Width - textSize.Width - 10, result.Bounds.Y)); element = new PdfTextElement("BILL TO ", new PdfStandardFont(PdfFontFamily.TimesRoman, 12)); element.Brush = new PdfSolidBrush(new PdfColor(34, 83, 142)); result = element.Draw(page, new PointF(10, result.Bounds.Bottom + 25)); g.DrawLine(new PdfPen(new PdfColor(34, 83, 142), 0.70f), new PointF(0, result.Bounds.Bottom + 3), new PointF(g.ClientSize.Width, result.Bounds.Bottom + 3)); element = new PdfTextElement(billInfo.Name, new PdfStandardFont(PdfFontFamily.TimesRoman, 11)); element.Brush = new PdfSolidBrush(new PdfColor(0, 0, 0)); result = element.Draw(page, new RectangleF(10, result.Bounds.Bottom + 5, g.ClientSize.Width / 2, 100)); element = new PdfTextElement(billInfo.Address, new PdfStandardFont(PdfFontFamily.TimesRoman, 11)); element.Brush = new PdfSolidBrush(new PdfColor(0, 0, 0)); result = element.Draw(page, new RectangleF(10, result.Bounds.Bottom + 3, g.ClientSize.Width / 2, 100)); PdfGrid grid = new PdfGrid(); grid.DataSource = ConvertToDataTable(dataSource); PdfGridCellStyle cellStyle = new PdfGridCellStyle(); cellStyle.Borders.All = PdfPens.White; PdfGridRow header = grid.Headers[0]; PdfGridCellStyle headerStyle = new PdfGridCellStyle(); headerStyle.Borders.All = new PdfPen(new PdfColor(34, 83, 142)); headerStyle.BackgroundBrush = new PdfSolidBrush(new PdfColor(34, 83, 142)); headerStyle.TextBrush = PdfBrushes.White; headerStyle.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 12f, PdfFontStyle.Regular); for (int i = 0; i < header.Cells.Count; i++) { if (i == 0) { header.Cells[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); } else { header.Cells[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle); } } header.Cells[0].Value = "ITEM"; header.Cells[1].Value = "QUANTITY"; header.Cells[2].Value = "RATE"; header.Cells[3].Value = "TAXES"; header.Cells[4].Value = "AMOUNT"; header.ApplyStyle(headerStyle); grid.Columns[0].Width = 180; cellStyle.Borders.Bottom = new PdfPen(new PdfColor(34, 83, 142), 0.70f); cellStyle.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 11f); cellStyle.TextBrush = new PdfSolidBrush(new PdfColor(0, 0, 0)); foreach (PdfGridRow row in grid.Rows) { row.ApplyStyle(cellStyle); for (int i = 0; i < row.Cells.Count; i++) { PdfGridCell cell = row.Cells[i]; if (i == 0) { cell.StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); } else { cell.StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle); } if (i > 1) { if (cell.Value.ToString().Contains("$")) { cell.Value = cell.Value.ToString(); } else { if (cell.Value is double) { cell.Value = "$" + ((double)cell.Value).ToString("#,###.00", CultureInfo.InvariantCulture); } else { cell.Value = "$" + cell.Value.ToString(); } } } } } PdfGridLayoutFormat layoutFormat = new PdfGridLayoutFormat(); layoutFormat.Layout = PdfLayoutType.Paginate; PdfGridLayoutResult gridResult = grid.Draw(page, new RectangleF(new PointF(0, result.Bounds.Bottom + 40), new SizeF(g.ClientSize.Width, g.ClientSize.Height - 100)), layoutFormat); float pos = 0.0f; for (int i = 0; i < grid.Columns.Count - 1; i++) { pos += grid.Columns[i].Width; } PdfFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 12f, PdfFontStyle.Bold); gridResult.Page.Graphics.DrawString("TOTAL DUE", font, new PdfSolidBrush(new PdfColor(34, 83, 142)), new RectangleF(new PointF(pos, gridResult.Bounds.Bottom + 10), new SizeF(grid.Columns[3].Width - pos, 20)), new PdfStringFormat(PdfTextAlignment.Right)); gridResult.Page.Graphics.DrawString("Thank you for your business!", new PdfStandardFont(PdfFontFamily.TimesRoman, 12), new PdfSolidBrush(new PdfColor(0, 0, 0)), new PointF(pos - 210, gridResult.Bounds.Bottom + 60)); pos += grid.Columns[4].Width; gridResult.Page.Graphics.DrawString("$" + totalDue.ToString("#,###.00", CultureInfo.InvariantCulture), font, new PdfSolidBrush(new PdfColor(0, 0, 0)), new RectangleF(pos, gridResult.Bounds.Bottom + 10, grid.Columns[4].Width - pos, 20), new PdfStringFormat(PdfTextAlignment.Right)); document.Save("Invoice.pdf"); //Message box confirmation to view the created PDF document. if (MessageBox.Show("Do you want to view the PDF file?", "PDF File Created", MessageBoxButton.YesNo, MessageBoxImage.Information) == MessageBoxResult.Yes) { //Launching the PDF file using the default Application.[Acrobat Reader] System.Diagnostics.Process.Start("Invoice.pdf"); //this.Close(); } //else // Exit //this.Close(); document.Close(true); }
protected void btnDownload_Click(object sender, EventArgs e) { using (PdfDocument document = new PdfDocument()) { PdfPage page = document.Pages.Add(); PdfGraphics graphics = page.Graphics; //Drawing the logo image on the top PdfBitmap image = new PdfBitmap(Server.MapPath("~/Images/Logo.jpg")); graphics.DrawImage(image, 110, 0, 300, 100); //Drawing a rectangle below the logo in Coral color RectangleF bounds = new RectangleF(0, 100, graphics.ClientSize.Width, 30); PdfBrush brush = new PdfSolidBrush(new PdfColor(Color.Coral)); graphics.DrawRectangle(brush, bounds); //Loading data from Database into DataSet by calling the GetInvoiceDetails method ds = GetInvoiceDetails(ddlInvoice.SelectedValue); //Creating PdfTextElement objects and placing them on the document PdfFont textFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 14); PdfTextElement element = new PdfTextElement("INVOICE ID: " + ddlInvoice.SelectedValue, textFont); element.Brush = PdfBrushes.White; PdfLayoutResult result = element.Draw(page, new PointF(10, bounds.Top + 8)); string invoiceDate = "INVOICE DATE: " + ds.Tables[0].Rows[0][0]; SizeF textSize = textFont.MeasureString(invoiceDate); element = new PdfTextElement(invoiceDate, textFont); element.Brush = PdfBrushes.White; result = element.Draw(page, bounds.Width - textSize.Width - 10, bounds.Top + 8); element = new PdfTextElement("BILL TO:", textFont); element.Brush = PdfBrushes.Blue; result = element.Draw(page, 10, result.Bounds.Bottom + 20); //byte[] imageData = (byte[])ds.Tables[1].Rows[0][3]; //MemoryStream ms = new MemoryStream(imageData); //PdfBitmap customerImage = new PdfBitmap(ms); //graphics.DrawImage(customerImage, 410, result.Bounds.Bottom - 14, 100, 100); element = new PdfTextElement(ds.Tables[1].Rows[0][0].ToString(), textFont); result = element.Draw(page, 15, result.Bounds.Bottom); element = new PdfTextElement(ds.Tables[1].Rows[0][1].ToString(), textFont); result = element.Draw(page, 15, result.Bounds.Bottom); element = new PdfTextElement(ds.Tables[1].Rows[0][2].ToString(), textFont); result = element.Draw(page, 15, result.Bounds.Bottom); PdfPen linePen = new PdfPen(new PdfColor(Color.Coral), 0.75f); PointF startPoint = new PointF(0, result.Bounds.Bottom + 50); PointF endPoint = new PointF(graphics.ClientSize.Width, result.Bounds.Bottom + 50); graphics.DrawLine(linePen, startPoint, endPoint); DataTable invoiceDetails = ds.Tables[2]; PdfGrid grid = new PdfGrid(); grid.DataSource = invoiceDetails; PdfGridCellStyle headerStyle = new PdfGridCellStyle(); headerStyle.BackgroundBrush = new PdfSolidBrush(new PdfColor(126, 151, 173)); headerStyle.TextBrush = PdfBrushes.White; headerStyle.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 14f, PdfFontStyle.Regular); PdfGridRow header = grid.Headers[0]; for (int i = 0; i < header.Cells.Count; i++) { header.Cells[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); } header.ApplyStyle(headerStyle); PdfGridColumnCollection columns = grid.Columns; columns[2].Format.Alignment = PdfTextAlignment.Right; columns[3].Format.Alignment = PdfTextAlignment.Right; columns[4].Format.Alignment = PdfTextAlignment.Right; PdfGridStyle cellStyle = new PdfGridStyle(); cellStyle.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 12f, PdfFontStyle.Regular); cellStyle.TextBrush = PdfBrushes.DarkGreen; grid.Style = cellStyle; PdfGridLayoutResult gridResult = grid.Draw(page, new RectangleF(new PointF(0, result.Bounds.Bottom + 60), new SizeF(graphics.ClientSize.Width, graphics.ClientSize.Height - 100))); linePen = new PdfPen(new PdfColor(Color.Coral), 0.75f); startPoint = new PointF((gridResult.Bounds.Width / 5) * 4, gridResult.Bounds.Bottom + 20); endPoint = new PointF(graphics.ClientSize.Width, gridResult.Bounds.Bottom + 20); graphics.DrawLine(linePen, startPoint, endPoint); element = new PdfTextElement("TotalBill:", textFont); result = element.Draw(page, new PointF((gridResult.Bounds.Width / 5) * 3, gridResult.Bounds.Bottom + 25)); string billAmount = ds.Tables[3].Rows[0][0].ToString(); textSize = textFont.MeasureString(billAmount); element = new PdfTextElement(billAmount, textFont); result = element.Draw(page, new PointF(graphics.ClientSize.Width - textSize.Width, gridResult.Bounds.Bottom + 25)); linePen = new PdfPen(new PdfColor(Color.Coral), 0.75f); startPoint = new PointF((gridResult.Bounds.Width / 5) * 4, gridResult.Bounds.Bottom + 45); endPoint = new PointF(graphics.ClientSize.Width, gridResult.Bounds.Bottom + 45); graphics.DrawLine(linePen, startPoint, endPoint); string signature = "Signature"; textSize = textFont.MeasureString(signature); element = new PdfTextElement(signature, textFont); result = element.Draw(page, new PointF(graphics.ClientSize.Width - textSize.Width - 20, gridResult.Bounds.Bottom + 100)); linePen = new PdfPen(new PdfColor(Color.Coral), 0.75f); startPoint = new PointF(0, graphics.ClientSize.Height - 25); endPoint = new PointF(graphics.ClientSize.Width, graphics.ClientSize.Height - 25); graphics.DrawLine(linePen, startPoint, endPoint); graphics.DrawString("13-74/292, Diamond Towers, Jubliee Hills - 33. Phone: 2381 9999, Fax: 2381 8899", new PdfStandardFont(PdfFontFamily.Courier, 10), PdfBrushes.DarkCyan, new PointF(0, graphics.ClientSize.Height - 20)); graphics.DrawString("Email: [email protected], WebSite: www.paintcompany.com", new PdfStandardFont(PdfFontFamily.Courier, 10), PdfBrushes.DarkCyan, new PointF(0, graphics.ClientSize.Height - 10)); PdfGridLayoutFormat layoutFormat = new PdfGridLayoutFormat(); layoutFormat.Layout = PdfLayoutType.Paginate; document.Save("Invoice.pdf", HttpContext.Current.Response, HttpReadType.Save); }; }