private static void DrawHorizontalLines(PdfGraphics g, PdfPen pen, double x, double y, double width, double height) { for (double i = 0; i < height; i = i + 5) { g.DrawLine(pen, x, y + i, x + width, y + i); } }
/// <summary> /// Create Zugferd Invoice Pdf /// </summary> /// <param name="document"></param> /// <returns></returns> public PdfDocument CreateZugferdInvoicePDF(PdfDocument document) { //Add page to the PDF PdfPage page = document.Pages.Add(); PdfGraphics graphics = page.Graphics; //Create border color PdfColor borderColor = Color.FromArgb(255, 142, 170, 219); //Get the page width and height float pageWidth = page.GetClientSize().Width; float pageHeight = page.GetClientSize().Height; //Set the header height float headerHeight = 90; PdfColor lightBlue = Color.FromArgb(255, 91, 126, 215); PdfBrush lightBlueBrush = new PdfSolidBrush(lightBlue); PdfColor darkBlue = Color.FromArgb(255, 65, 104, 209); PdfBrush darkBlueBrush = new PdfSolidBrush(darkBlue); PdfBrush whiteBrush = new PdfSolidBrush(Color.White); PdfTrueTypeFont robotoFont = new PdfTrueTypeFont(new Font("Roboto Light", 30, FontStyle.Regular), true); PdfTrueTypeFont arialRegularFont = new PdfTrueTypeFont(new Font("Arial", 18, FontStyle.Regular), true); PdfTrueTypeFont arialBoldFont = new PdfTrueTypeFont(new Font("arial", 9f, FontStyle.Bold), true); //Create string format. PdfStringFormat format = new PdfStringFormat(); format.Alignment = PdfTextAlignment.Center; format.LineAlignment = PdfVerticalAlignment.Middle; float y = 0; float x = 0; //Set the margins of address. float margin = 30; //Set the line space float lineSpace = 7; PdfPen borderPen = new PdfPen(borderColor, 1f); //Draw page border graphics.DrawRectangle(borderPen, new RectangleF(0, 0, pageWidth, pageHeight)); PdfGrid grid = new PdfGrid(); grid.DataSource = GetZugferdDataset(); #region Header //Fill the header with light Brush graphics.DrawRectangle(lightBlueBrush, new RectangleF(0, 0, pageWidth, headerHeight)); string title = "INVOICE"; SizeF textSize = robotoFont.MeasureString(title); RectangleF headerTotalBounds = new RectangleF(400, 0, pageWidth - 400, headerHeight); graphics.DrawString(title, robotoFont, whiteBrush, new RectangleF(0, 0, textSize.Width + 50, headerHeight), format); graphics.DrawRectangle(darkBlueBrush, headerTotalBounds); graphics.DrawString("$" + GetTotalAmount(grid).ToString(), arialRegularFont, whiteBrush, new RectangleF(400, 0, pageWidth - 400, headerHeight + 10), format); arialRegularFont = new PdfTrueTypeFont(new Font("Arial", 9, FontStyle.Regular), true); format.LineAlignment = PdfVerticalAlignment.Bottom; graphics.DrawString("Amount", arialRegularFont, whiteBrush, new RectangleF(400, 0, pageWidth - 400, headerHeight / 2 - arialRegularFont.Height), format); #endregion SizeF size = arialRegularFont.MeasureString("Invoice Number: 2058557939"); y = headerHeight + margin; x = (pageWidth - margin) - size.Width; graphics.DrawString("Invoice Number: 2058557939", arialRegularFont, PdfBrushes.Black, new PointF(x, y)); size = arialRegularFont.MeasureString("Date :" + DateTime.Now.ToString("dddd dd, MMMM yyyy")); x = (pageWidth - margin) - size.Width; y += arialRegularFont.Height + lineSpace; graphics.DrawString("Date: " + DateTime.Now.ToString("dddd dd, MMMM yyyy"), arialRegularFont, PdfBrushes.Black, new PointF(x, y)); y = headerHeight + margin; x = margin; graphics.DrawString("Bill To:", arialRegularFont, PdfBrushes.Black, new PointF(x, y)); y += arialRegularFont.Height + lineSpace; graphics.DrawString("Abraham Swearegin,", arialRegularFont, PdfBrushes.Black, new PointF(x, y)); y += arialRegularFont.Height + lineSpace; graphics.DrawString("United States, California, San Mateo,", arialRegularFont, PdfBrushes.Black, new PointF(x, y)); y += arialRegularFont.Height + lineSpace; graphics.DrawString("9920 BridgePointe Parkway,", arialRegularFont, PdfBrushes.Black, new PointF(x, y)); y += arialRegularFont.Height + lineSpace; graphics.DrawString("9365550136", arialRegularFont, PdfBrushes.Black, new PointF(x, y)); #region Grid grid.Columns[0].Width = 110; grid.Columns[1].Width = 150; grid.Columns[2].Width = 110; grid.Columns[3].Width = 70; grid.Columns[4].Width = 100; for (int i = 0; i < grid.Headers.Count; i++) { grid.Headers[i].Height = 20; for (int j = 0; j < grid.Columns.Count; j++) { PdfStringFormat pdfStringFormat = new PdfStringFormat(); pdfStringFormat.LineAlignment = PdfVerticalAlignment.Middle; pdfStringFormat.Alignment = PdfTextAlignment.Left; if (j == 0 || j == 2) { grid.Headers[i].Cells[j].Style.CellPadding = new PdfPaddings(30, 1, 1, 1); } grid.Headers[i].Cells[j].StringFormat = pdfStringFormat; grid.Headers[i].Cells[j].Style.Font = arialBoldFont; } grid.Headers[0].Cells[0].Value = "Product Id"; } for (int i = 0; i < grid.Rows.Count; i++) { grid.Rows[i].Height = 23; for (int j = 0; j < grid.Columns.Count; j++) { PdfStringFormat pdfStringFormat = new PdfStringFormat(); pdfStringFormat.LineAlignment = PdfVerticalAlignment.Middle; pdfStringFormat.Alignment = PdfTextAlignment.Left; if (j == 0 || j == 2) { grid.Rows[i].Cells[j].Style.CellPadding = new PdfPaddings(30, 1, 1, 1); } grid.Rows[i].Cells[j].StringFormat = pdfStringFormat; grid.Rows[i].Cells[j].Style.Font = arialRegularFont; } } grid.ApplyBuiltinStyle(PdfGridBuiltinStyle.ListTable4Accent5); grid.BeginCellLayout += Grid_BeginCellLayout; PdfGridLayoutResult result = grid.Draw(page, new PointF(0, y + 40)); y = result.Bounds.Bottom + lineSpace; format = new PdfStringFormat(); format.Alignment = PdfTextAlignment.Center; RectangleF bounds = new RectangleF(QuantityCellBounds.X, y, QuantityCellBounds.Width, QuantityCellBounds.Height); page.Graphics.DrawString("Grand Total:", arialBoldFont, PdfBrushes.Black, bounds, format); bounds = new RectangleF(TotalPriceCellBounds.X, y, TotalPriceCellBounds.Width, TotalPriceCellBounds.Height); page.Graphics.DrawString(GetTotalAmount(grid).ToString(), arialBoldFont, PdfBrushes.Black, bounds); #endregion borderPen.DashStyle = PdfDashStyle.Custom; borderPen.DashPattern = new float[] { 3, 3 }; graphics.DrawLine(borderPen, new PointF(0, pageHeight - 100), new PointF(pageWidth, pageHeight - 100)); y = pageHeight - 100 + margin; size = arialRegularFont.MeasureString("800 Interchange Blvd."); x = pageWidth - size.Width - margin; graphics.DrawString("800 Interchange Blvd.", arialRegularFont, PdfBrushes.Black, new PointF(x, y)); y += arialRegularFont.Height + lineSpace; size = arialRegularFont.MeasureString("Suite 2501, Austin, TX 78721"); x = pageWidth - size.Width - margin; graphics.DrawString("Suite 2501, Austin, TX 78721", arialRegularFont, PdfBrushes.Black, new PointF(x, y)); y += arialRegularFont.Height + lineSpace; size = arialRegularFont.MeasureString("Any Questions? [email protected]"); x = pageWidth - size.Width - margin; graphics.DrawString("Any Questions? [email protected]", arialRegularFont, PdfBrushes.Black, new PointF(x, y)); return(document); }
/// <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); } } }
/// <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] #if !NETCORE System.Diagnostics.Process.Start("Invoice.pdf"); #else ProcessStartInfo psi = new ProcessStartInfo { FileName = "cmd", WindowStyle = ProcessWindowStyle.Hidden, UseShellExecute = false, CreateNoWindow = true, Arguments = "/c start Invoice.pdf" }; Process.Start(psi); #endif //this.Close(); } //else // Exit //this.Close(); document.Close(true); }
async private void PrintInvoice_Tapped(object sender, EventArgs e) { order.PaymentType = 0; double finalPayment = Convert.ToDouble(lblFinalPayment.Text); if (finalPayment == 0) { await DisplayAlert("صدور فاکتور", "فاکتور به مبلغ صفر نمیتواتد در سیستم ثبت گردد", "باشه"); } else { await SandoghcheController._connection.InsertAsync(order); await SandoghcheController._connection.InsertAllAsync(order.OrderDetails); await SandoghcheController._connection.UpdateWithChildrenAsync(order); await UpdateProductsAmount(); if (Device.RuntimePlatform == Device.iOS) { //iOS stuff } else if (Device.RuntimePlatform == Device.Android) { PdfDocument doc = new PdfDocument(); doc.PageSettings.Size = new Syncfusion.Drawing.SizeF(300, 800); PdfMargins margins = new PdfMargins(); margins.All = 10; doc.PageSettings.Margins = margins; PdfPage page = doc.Pages.Add(); PdfGraphics graphics = page.Graphics; //var test = GetType().GetTypeInfo().Assembly.GetManifestResourceNames(); Stream fontStream = typeof(EditPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sandoghche.Images.IRANSans(FaNum).ttf"); PdfFont font = new PdfTrueTypeFont(fontStream, 10); PdfStringFormat format = new PdfStringFormat(); format.TextDirection = PdfTextDirection.RightToLeft; format.Alignment = PdfTextAlignment.Center; PdfStringFormat format2 = new PdfStringFormat(); format2.TextDirection = PdfTextDirection.RightToLeft; format2.Alignment = PdfTextAlignment.Right; PdfStringFormat format3 = new PdfStringFormat(); format3.TextDirection = PdfTextDirection.RightToLeft; format3.Alignment = PdfTextAlignment.Left; graphics.DrawString(lblClient.Text, font, PdfBrushes.Black, new RectangleF(0, 0, page.GetClientSize().Width, page.GetClientSize().Height), format); graphics.DrawString("فاکتور فروش", font, PdfBrushes.Black, new RectangleF(0, 20, page.GetClientSize().Width, page.GetClientSize().Height), format); graphics.DrawLine(new PdfPen(PdfBrushes.Black, .5f), new PointF(0, 40), new PointF(300, 40)); graphics.DrawString("تاریخ :" + SandoghcheController.GetPersianDate(Convert.ToDateTime(order.DateCreated)), font, PdfBrushes.Black, new RectangleF(0, 42, page.GetClientSize().Width, page.GetClientSize().Height), format); graphics.DrawLine(new PdfPen(PdfBrushes.Black, .5f), new PointF(0, 60), new PointF(300, 60)); graphics.DrawString("ردیف عنوان تعداد قیمت واحد قیمت کل ", font, PdfBrushes.Black, new RectangleF(0, 60, page.GetClientSize().Width, page.GetClientSize().Height), format); graphics.DrawLine(new PdfPen(PdfBrushes.Black, .5f), new PointF(0, 75), new PointF(300, 75)); string source = ""; int length = 5; string result = source.PadRight(length).Substring(0, length); int height = 75; foreach (var item in order.OrderDetails) { string rowNumber = item.RowNumber.ToString().PadLeft(5).Substring(0, 5); string productText = item.ProductText.PadRight(20).Substring(0, 20); string Number = item.Number.ToString().PadLeft(5).Substring(0, 5); string Price = item.Price.ToString().PadLeft(12).Substring(0, 12); string TotalPrice = item.TotalPrice.ToString().PadLeft(12).Substring(0, 12); graphics.DrawString(rowNumber, font, PdfBrushes.Black, 285, height, format2); graphics.DrawString(productText, font, PdfBrushes.Black, 255, height, format2); graphics.DrawString(Number, font, PdfBrushes.Black, 130, height, format2); graphics.DrawString(Price, font, PdfBrushes.Black, 110, height, format2); graphics.DrawString(TotalPrice, font, PdfBrushes.Black, 55, height, format2); height += 15; graphics.DrawLine(new PdfPen(PdfBrushes.Black, .5f), new PointF(0, height), new PointF(300, height)); productText = ""; } graphics.DrawString("مالیات : " + (order.Tax1 + order.Tax2).ToString(), font, PdfBrushes.Black, new RectangleF(0, height, page.GetClientSize().Width, page.GetClientSize().Height), format3); graphics.DrawLine(new PdfPen(PdfBrushes.Black, .5f), new PointF(0, height), new PointF(100, height)); height += 15; graphics.DrawString("سرویس : " + order.TotalServiceFee.ToString(), font, PdfBrushes.Black, new RectangleF(0, height, page.GetClientSize().Width, page.GetClientSize().Height), format3); graphics.DrawLine(new PdfPen(PdfBrushes.Black, .5f), new PointF(0, height), new PointF(100, height)); height += 15; graphics.DrawString("پیک : " + order.DeliveryFee.ToString(), font, PdfBrushes.Black, new RectangleF(0, height, page.GetClientSize().Width, page.GetClientSize().Height), format3); graphics.DrawLine(new PdfPen(PdfBrushes.Black, .5f), new PointF(0, height), new PointF(100, height)); height += 15; graphics.DrawString("تخفیف : " + order.TotalDiscount.ToString(), font, PdfBrushes.Black, new RectangleF(0, height, page.GetClientSize().Width, page.GetClientSize().Height), format3); graphics.DrawLine(new PdfPen(PdfBrushes.Black, .5f), new PointF(0, height), new PointF(100, height)); height += 15; graphics.DrawString("توضیحات : " + order.Comment, font, PdfBrushes.Black, new RectangleF(0, height, page.GetClientSize().Width, page.GetClientSize().Height), format3); graphics.DrawLine(new PdfPen(PdfBrushes.Black, .5f), new PointF(0, height), new PointF(100, height)); height += 15; graphics.DrawString("جمع کل : " + order.FinalPayment.ToString(), font, PdfBrushes.Black, new RectangleF(0, height, page.GetClientSize().Width, page.GetClientSize().Height), format3); graphics.DrawLine(new PdfPen(PdfBrushes.Black, .5f), new PointF(0, height), new PointF(300, height)); MemoryStream stream = new MemoryStream(); doc.Save(stream); byte[] test = stream.ToArray(); doc.Close(true); DependencyService.Get <IPrintPdf>().PrintPdf(test, order.OrderId); } else if (Device.RuntimePlatform == Device.UWP) { DependencyService.Get <IPrint>().Print(order, "فاکتور فروش", clientName); } await setOrderNumber(); await setReceiptNumber(); lblTax.Text = "0"; lblDiscount.Text = "0"; lblService.Text = "0"; lblTax.Text = "0"; lblFinalPayment.Text = "0"; lblTotalPrice.Text = "0"; lblDelivery.Text = "0"; ProductsDataGrid.ItemsSource = null; lstProducts.ItemsSource = null; await getCategories(); order = new Order(); } }
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); }; }
public ActionResult DigitalSignature(string Browser, string password, HttpPostedFileBase pdfdocument, HttpPostedFileBase certificate, string RadioButtonList2, string NewPDF, string submit, string mySignature) { if (mySignature != "" && submit != "Create PDF") { if (pdfdocument != null && pdfdocument.ContentLength > 0 && certificate != null && certificate.ContentLength > 0 && password != string.Empty) { string pfxPath = string.Empty; if (pdfdocument.FileName.Contains(".pdf")) { ldoc = new PdfLoadedDocument(pdfdocument.InputStream); } if (certificate.FileName.Contains(".pfx")) { pfxPath = Path.Combine(Server.MapPath("~/App_Data"), certificate.FileName); certificate.SaveAs(pfxPath); } bmp = new PdfBitmap(new MemoryStream(Convert.FromBase64String(mySignature.Substring(22)))); PdfPageBase page = ldoc.Pages[0]; page.Graphics.DrawImage(bmp, new PointF(page.Graphics.ClientSize.Width - 105, page.Graphics.ClientSize.Height - 80), new SizeF(100, 75)); if (password != string.Empty) pdfCert = new PdfCertificate(pfxPath, password); signature = new PdfSignature(ldoc, page, pdfCert, "Signature"); signature.Bounds = new RectangleF(new PointF(page.Graphics.ClientSize.Width - 105, page.Graphics.ClientSize.Height - 80), new SizeF(100, 75)); string validto = "Valid To: " + signature.Certificate.ValidTo.ToString(); string validfrom = "Valid From: " + signature.Certificate.ValidFrom.ToString(); // Save the pdf document to the Stream. MemoryStream stream = new MemoryStream(); ldoc.Save(stream); Response.ClearContent(); Response.Expires = 0; Response.Buffer = true; string disposition = "content-disposition"; Response.AddHeader(disposition, "attachment; filename=Sample.pdf"); Response.AddHeader("Content-Type", "application/pdf"); Response.Clear(); stream.WriteTo(Response.OutputStream); Response.End(); ldoc.Close(); return View(); } else { ViewBag.lab = "NOTE: Fill all fields and then create PDF"; return View(); } } else { PdfDocument doc = new PdfDocument(); PdfPage page = doc.Pages.Add(); PdfSolidBrush brush = new PdfSolidBrush(Color.Black); PdfPen pen = new PdfPen(brush, 0.2f); PdfFont font = new PdfStandardFont(PdfFontFamily.Courier, 12, PdfFontStyle.Regular); try { pdfCert = new PdfCertificate(ResolveApplicationDataPath("PDF.pfx"), "password123"); signature = new PdfSignature(page, pdfCert, "Signature"); bmp = new PdfBitmap(ResolveApplicationImagePath("syncfusion_logo.gif")); signature.Bounds = new RectangleF(new PointF(5, 5), page.Size); signature.ContactInfo = "*****@*****.**"; signature.LocationInfo = "Honolulu, Hawaii"; signature.Reason = "I am author of this document."; if (RadioButtonList2 == "Standard") signature.Certificated = true; else signature.Certificated = false; graphics = signature.Appearence.Normal.Graphics; } catch (Exception) { graphics = signature.Appearence.Normal.Graphics; Response.Write("Warning Certificate not found \"Cannot sign This Document\""); //Draw the Text at specified location. graphics.DrawString("Warning this document is not signed", font, brush, new PointF(0, 20)); graphics.DrawString("Create a self signed Digital ID to sign this document", font, brush, new PointF(20, 40)); graphics.DrawLine(pen, new PointF(0, 100), new PointF(page.GetClientSize().Width, 200)); graphics.DrawLine(pen, new PointF(0, 200), new PointF(page.GetClientSize().Width, 100)); } string validto = "Valid To: " + signature.Certificate.ValidTo.ToString(); string validfrom = "Valid From: " + signature.Certificate.ValidFrom.ToString(); graphics.DrawImage(bmp, 0, 0); doc.Pages[0].Graphics.DrawString(validfrom, font, pen, brush, 0, 90); doc.Pages[0].Graphics.DrawString(validto, font, pen, brush, 0, 110); doc.Pages[0].Graphics.DrawString(" Protected Document. Digitally signed Document.", font, pen, brush, 0, 130); doc.Pages[0].Graphics.DrawString("* To validate Signature click on the signature on this page \n * To check Document Status \n click document status icon on the bottom left of the acrobat reader.", font, pen, brush, 0, 150); // Save the pdf document to the Stream. MemoryStream stream = new MemoryStream(); doc.Save(stream); Response.ClearContent(); Response.Expires = 0; Response.Buffer = true; string disposition = "content-disposition"; Response.AddHeader(disposition, "attachment; filename=Sample.pdf"); Response.AddHeader("Content-Type", "application/pdf"); Response.Clear(); stream.WriteTo(Response.OutputStream); Response.End(); doc.Close(); return View(); } }