private async void button2_Click(object sender, RoutedEventArgs e) { //Create a new PDF document. PdfDocument pdfDocument = new PdfDocument(); //Create the page PdfPage pdfPage = pdfDocument.Pages.Add(); //Create the parent grid PdfGrid parentPdfGrid = new PdfGrid(); parentPdfGrid.Columns.Add(CellGrid.ColumnCount); //Add the rows for (int i = 0; i < CellGrid.RowCount; i++) { PdfGridRow row1 = parentPdfGrid.Rows.Add(); row1.Height = 50; for (int j = 0; j < CellGrid.ColumnCount; j++) { var style = CellGrid.Model[i, j]; PdfGridCell pdfGridCell = parentPdfGrid.Rows[i].Cells[j]; pdfGridCell.Value = style.CellValue; var brush = (style.Background as SolidColorBrush); //Export with style if (brush != null) { pdfGridCell.Style.BackgroundBrush = new PdfSolidBrush(new PdfColor(System.Drawing.Color.FromArgb(brush.Color.A, brush.Color.R, brush.Color.G, brush.Color.B))); } } } //Draw the PdfGrid. parentPdfGrid.Draw(pdfPage, PointF.Empty); StorageFile storageFile; StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder; storageFile = await local.CreateFileAsync("Sample.pdf", CreationCollisionOption.ReplaceExisting); //Save the document. await pdfDocument.SaveAsync(storageFile); FileStream stream = File.OpenRead(storageFile.Path); this.pdfViewer.Unload(); this.pdfViewer.LoadDocument(stream); //Print the pdf file. this.pdfViewer.Print(); this.pdfViewer.Visibility = Visibility.Collapsed; }
private async void button2_Click(object sender, RoutedEventArgs e) { //Create a new PDF document. PdfDocument pdfDocument = new PdfDocument(); //Create the page PdfPage pdfPage = pdfDocument.Pages.Add(); //Create the parent grid PdfGrid parentPdfGrid = new PdfGrid(); //Add the rows for (int i = 0; i < CellGrid.RowCount; i++) { PdfGridRow row1 = parentPdfGrid.Rows.Add(); row1.Height = 50; parentPdfGrid.Columns.Add(CellGrid.ColumnCount); for (int j = 0; j < CellGrid.ColumnCount; j++) { var style = CellGrid.Model[i, j]; PdfGridCell pdfGridCell = parentPdfGrid.Rows[i].Cells[j]; pdfGridCell.Value = style.CellValue; var brush = (style.Background as SolidColorBrush); //Export with style //if (brush != null) // pdfGridCell.Style.BackgroundBrush } } //Draw the PdfGrid. parentPdfGrid.Draw(pdfPage, PointF.Empty); StorageFile storageFile; StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder; storageFile = await local.CreateFileAsync("Sample.pdf", CreationCollisionOption.ReplaceExisting); //Save the document. await pdfDocument.SaveAsync(storageFile); }
/// <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); }
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"); }
/// <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); } } }
private async void TableFeaturesSample() { #region Field Definitions //Load product data. IEnumerable <Products> products = DataProvider.GetProducts(); //Create a new PDF standard font PdfStandardFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 8f); PdfStandardFont smallFont = new PdfStandardFont(font, 5f); PdfFont bigFont = new PdfStandardFont(font, 16f); //Create a new PDF solid brush PdfBrush orangeBrush = new PdfSolidBrush(new PdfColor(247, 148, 29)); PdfBrush grayBrush = new PdfSolidBrush(new PdfColor(170, 171, 171)); //Create a new PDF pen PdfPen borderPen = new PdfPen(PdfBrushes.DarkGray, .3f); borderPen.LineCap = PdfLineCap.Square; PdfPen transparentPen = new PdfPen(PdfBrushes.Transparent, .3f); transparentPen.LineCap = PdfLineCap.Square; float margin = 40f; #endregion MemoryStream stream = new MemoryStream(); //Create a new PDF document using (PdfDocument document = new PdfDocument()) { //Set the margins document.PageSettings.Margins.All = 0; //Add a new PDF page PdfPage page = document.Pages.Add(); //Add the document header AddHeader(document, "Syncfusion Essential PDF"); //Add the document footer AddFooter(document, font, grayBrush); //Draw the text to the PDF page page.Graphics.DrawString("What You Get with Syncfusion", bigFont, orangeBrush, new PointF(10, 10)); #region PdfGrid //Create a new PDF grid PdfGrid pdfGrid = new PdfGrid(); IEnumerable <Report> report = DataProvider.GetReport(); pdfGrid.DataSource = report; pdfGrid.Headers.Clear(); pdfGrid.Columns[0].Width = 80; pdfGrid.Style.Font = font; pdfGrid.Style.CellSpacing = 15f; for (int i = 0; i < pdfGrid.Rows.Count; i++) { if (i % 2 == 0) { PdfGridCell cell = pdfGrid.Rows[i].Cells[0]; cell.RowSpan = 2; Stream imgStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("SampleBrowser.Samples.PDF.Assets." + cell.Value.ToString().ToLower() + ".jpg"); //Set cell background image cell.Style.BackgroundImage = new PdfBitmap(imgStream); cell.Value = ""; cell = pdfGrid.Rows[i].Cells[1]; cell.Style.Font = bigFont; } for (int j = 0; j < pdfGrid.Columns.Count; j++) { pdfGrid.Rows[i].Cells[j].Style.Borders.All = transparentPen; } } //Create a PDF grid layout format PdfGridLayoutFormat gridLayoutFormat = new PdfGridLayoutFormat(); //Set pagination gridLayoutFormat.Layout = PdfLayoutType.Paginate; //Draw the grid to the PDF document pdfGrid.Draw(page, new RectangleF(new PointF(margin, 75), new SizeF(page.Graphics.ClientSize.Width - (2 * margin), page.Graphics.ClientSize.Height - margin)), gridLayoutFormat); #endregion //Save the PDF document document.Save(stream); } stream.Position = 0; if (IsToggled) { PdfViewerUI pdfViewer = new SampleBrowser.PdfViewerUI(); pdfViewer.PdfDocumentStream = stream; if (Device.Idiom != TargetIdiom.Phone && Device.OS == TargetPlatform.Windows) { await PDFViewModel.Navigation.PushModalAsync(new NavigationPage(pdfViewer)); } else { await PDFViewModel.Navigation.PushAsync(pdfViewer); } } else { if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows) { Xamarin.Forms.DependencyService.Get <ISaveWindowsPhone>().Save("TableFeatures.pdf", "application/pdf", stream); } else { Xamarin.Forms.DependencyService.Get <ISave>().Save("TableFeatures.pdf", "application/pdf", stream); } } }
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); }