public static void SetGridPaddingByRow(PdfGridRow row, float left, float top, float right, float bottom) { int CellCount = row.Cells.Count; for (int i = 0; i < CellCount; i++) { row.Cells[i].Style.CellPadding = new PdfPaddings(left, right, top, bottom); } }
public static void SetGridPaddingByRow(PdfGridRow row, float all) { int CellCount = row.Cells.Count; for (int i = 0; i < CellCount; i++) { row.Cells[i].Style.CellPadding = new PdfPaddings(all, all, all, all); } }
public static void SetGridTextAlignmentByRow(PdfGridRow row, PdfTextAlignment alignment) { int CellCount = row.Cells.Count; for (int i = 0; i < CellCount; i++) { row.Cells[i].StringFormat.Alignment = alignment; } }
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 static void SetGridPaddingByRow(PdfGridRow row, float Horizontal, float Virtical) { int CellCount = row.Cells.Count; for (int i = 0; i < CellCount; i++) { row.Cells[i].Style.CellPadding = new PdfPaddings(Horizontal, Horizontal, Virtical, Virtical); } }
public static PdfPageTemplateElement AddFooter(PdfDocument doc, string strFooterLine1, string strFooterLine2, string strFooterLine3) { RectangleF rect = new RectangleF(0, 0, doc.Pages[0].GetClientSize().Width, 50); //Create a page template PdfPageTemplateElement footer = new PdfPageTemplateElement(rect); //Create a PdfGrid PdfGrid pdfGrid = new PdfGrid(); //Add three columns pdfGrid.Columns.Add(1); //create and customize the string formats PdfStringFormat format = new PdfStringFormat(); format.Alignment = PdfTextAlignment.Center; format.LineAlignment = PdfVerticalAlignment.Middle; //Create the font for setting the style PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 8, PdfFontStyle.Bold | PdfFontStyle.Italic); //Add rows PdfGridRow pdfGridRow = pdfGrid.Rows.Add(); pdfGridRow.Cells[0].Value = spFooterLine1; pdfGridRow.Cells[0].Style.Font = font; pdfGridRow.Cells[0].Style.TextBrush = PdfBrushes.Red; //Add rows PdfGridRow pdfGridRow1 = pdfGrid.Rows.Add(); pdfGridRow1.Cells[0].Value = spFooterLine2; //Add rows PdfGridRow pdfGridRow2 = pdfGrid.Rows.Add(); pdfGridRow2.Cells[0].Value = spFooterLine3; for (int i = 0; i < pdfGrid.Rows.Count; i++) { PdfGridRow row = pdfGrid.Rows[i]; for (int j = 0; j < row.Cells.Count; j++) { row.Cells[j].StringFormat = format; row.Cells[j].Style.Borders.All = PdfPens.Transparent; } } //Draw grid to the page of PDF document pdfGrid.Draw(footer.Graphics); return(footer); }
//Create and row for the grid. void AddProducts(string productId, string productName, double price, int quantity, double total, PdfGrid grid) { //Add a new row and set the product value to grid row cells. PdfGridRow row = grid.Rows.Add(); row.Cells[0].Value = productId; row.Cells[1].Value = productName; row.Cells[2].Value = price.ToString(); row.Cells[3].Value = quantity.ToString(); row.Cells[4].Value = total.ToString(); }
public static void SetGridBorderBrushByRow(PdfGridRow row, PdfBrush all) { int CellCount = row.Cells.Count; for (int i = 0; i < CellCount; i++) { row.Cells[i].Style.Borders.Left.Brush = all; row.Cells[i].Style.Borders.Top.Brush = all; row.Cells[i].Style.Borders.Right.Brush = all; row.Cells[i].Style.Borders.Bottom.Brush = all; } }
public static void SetGridBorderBrushByRow(PdfGridRow row, PdfBrush left, PdfBrush top, PdfBrush right, PdfBrush bottom) { int CellCount = row.Cells.Count; for (int i = 0; i < CellCount; i++) { row.Cells[i].Style.Borders.Left.Brush = left; row.Cells[i].Style.Borders.Top.Brush = top; row.Cells[i].Style.Borders.Right.Brush = right; row.Cells[i].Style.Borders.Bottom.Brush = bottom; } }
public static void SetGridBorderWidthByRow(PdfGridRow row, float all) { int CellCount = row.Cells.Count; for (int i = 0; i < CellCount; i++) { row.Cells[i].Style.Borders.Left.Width = all; row.Cells[i].Style.Borders.Top.Width = all; row.Cells[i].Style.Borders.Right.Width = all; row.Cells[i].Style.Borders.Bottom.Width = all; } }
public static void SetGridBorderWidthByRow(PdfGridRow row, float left, float top, float right, float bottom) { int CellCount = row.Cells.Count; for (int i = 0; i < CellCount; i++) { row.Cells[i].Style.Borders.Left.Width = left; row.Cells[i].Style.Borders.Top.Width = top; row.Cells[i].Style.Borders.Right.Width = right; row.Cells[i].Style.Borders.Bottom.Width = bottom; } }
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); }
public static PdfPageTemplateElement AddHeader(PdfDocument doc, string headerLine1, string headerLine2, string headerLine3, string waterMark, string expiryDays) { RectangleF rect = new RectangleF(0, 0, doc.Pages[0].GetClientSize().Width, 55); PdfPageTemplateElement header = new PdfPageTemplateElement(rect); PdfGrid pdfGrid = new PdfGrid(); //Add three columns pdfGrid.Columns.Add(3); PdfBrush brush = new PdfSolidBrush(Color.Black); PdfStringFormat format = new PdfStringFormat(); format.Alignment = PdfTextAlignment.Center; format.LineAlignment = PdfVerticalAlignment.Middle; string dtNow = DateTime.Now.ToShortDateString(); string strSop = "SOP-2023"; //Add rows PdfGridRow pdfGridRow = pdfGrid.Rows.Add(); pdfGridRow.Cells[0].Value = headerLine1 + ":" + strSop; pdfGridRow.Cells[1].Value = headerLine1; pdfGridRow.Cells[2].Value = headerLine3 + ":" + dtNow; //Create the font for setting the style PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 10, PdfFontStyle.Bold); for (int i = 0; i < pdfGrid.Rows.Count; i++) { PdfGridRow row = pdfGrid.Rows[i]; for (int j = 0; j < row.Cells.Count; j++) { row.Cells[j].Style.Font = font; row.Cells[j].StringFormat = format; row.Cells[j].Style.Borders.All = PdfPens.Transparent; } } pdfGrid.Draw(header.Graphics); return(header); }
private async void Reprtbtn_Clicked(object sender, EventArgs e) { //Create a new PDF document. PdfDocument doc = new PdfDocument(); //Add a page. PdfPage page = doc.Pages.Add(); Stream fontStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("IttezanPos.Assets.arial.ttf"); PdfTemplate header = PdfHelper.AddHeader(doc, "تقرير العملاء", "Ittezan Pos" + " " + DateTime.Now.ToString()); PdfCellStyle headerStyle = new PdfCellStyle(); headerStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center); page.Graphics.DrawPdfTemplate(header, new PointF()); //Create a PdfGrid. PdfGrid pdfGrid = new PdfGrid(); //String format // PdfFont pdfFont = new PdfTrueTypeFont(fontStream1, 12); //Create a DataTable. DataTable dataTable = new DataTable("EmpDetails"); List <Customer> customerDetails = new List <Customer>(); //Add columns to the DataTable dataTable.Columns.Add("ID"); dataTable.Columns.Add("Name"); dataTable.Columns.Add("Address"); //Add rows to the DataTable. foreach (var item in Clients) { Customer customer = new Customer(); customer.ID = item.id; customer.Name = item.name; customer.Address = item.address; customerDetails.Add(customer); dataTable.Rows.Add(new string[] { customer.ID.ToString(), customer.Name, customer.Address }); } //Assign data source. pdfGrid.DataSource = dataTable; pdfGrid.Headers.Add(1); PdfGridRow pdfGridRowHeader = pdfGrid.Headers[0]; pdfGridRowHeader.Cells[0].Value = "رقم العميل"; pdfGridRowHeader.Cells[1].Value = "إسم العميل"; pdfGridRowHeader.Cells[2].Value = "عنوان العميل"; PdfGridStyle pdfGridStyle = new PdfGridStyle(); pdfGridStyle.Font = new PdfTrueTypeFont(fontStream, 12); PdfGridLayoutFormat format1 = new PdfGridLayoutFormat(); format1.Break = PdfLayoutBreakType.FitPage; format1.Layout = PdfLayoutType.Paginate; PdfStringFormat format = new PdfStringFormat(); format.TextDirection = PdfTextDirection.RightToLeft; format.Alignment = PdfTextAlignment.Center; format.LineAlignment = PdfVerticalAlignment.Middle; pdfGrid.Columns[0].Format = format; pdfGrid.Columns[1].Format = format; pdfGrid.Columns[2].Format = format; pdfGrid.Style = pdfGridStyle; //Draw grid to the page of PDF document. pdfGrid.Draw(page, new Syncfusion.Drawing.Point(0, (int)header.Height + 10), format1); MemoryStream stream = new MemoryStream(); //Save the document. doc.Save(stream); //close the document doc.Close(true); await Xamarin.Forms.DependencyService.Get <ISave>().SaveAndView("تقرير العملاء.pdf", "application/pdf", stream); }
private void buttonRun_Click(object sender, RoutedEventArgs e) { SaveFileDialog saveFiledialog = new SaveFileDialog(); saveFiledialog.Filter = "PDF Document (*.pdf)|*.pdf"; bool?result = saveFiledialog.ShowDialog(); if (!result.HasValue || !result.Value) { return; } //create a pdf document PdfDocument document = new PdfDocument(); //margin PdfMargins margin = new PdfMargins(); margin.Top = ConvertUnits(1.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Bottom = margin.Top; margin.Left = ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Right = margin.Left; //create one page PdfPageBase page = document.Pages.Add(PdfPageSize.A4, margin, PdfPageRotateAngle.RotateAngle0, PdfPageOrientation.Landscape); float y = 0; //title PdfBrush brush1 = PdfBrushes.Black; PdfTrueTypeFont font1 = new PdfInternalFont(new Font("Arial", 16f, System.Drawing.FontStyle.Bold), true); PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center); page.Canvas.DrawString("Vendor List", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1); y = y + font1.MeasureString("Vendor List", format1).Height; y = y + 5; PdfGrid grid = new PdfGrid(); grid.Style.CellPadding = new PdfPaddings(1, 1, 1, 1); grid.Columns.Add(this.dataGridVendorList.Columns.Count); float width = page.Canvas.ClientSize.Width - (grid.Columns.Count + 1); grid.Columns[0].Width = width * 0.25f; grid.Columns[1].Width = width * 0.25f; grid.Columns[2].Width = width * 0.25f; grid.Columns[3].Width = width * 0.15f; grid.Columns[4].Width = width * 0.10f; //header of grid PdfGridRow headerRow = grid.Headers.Add(1)[0]; headerRow.Style.Font = new PdfInternalFont(new Font("Arial", 11f, System.Drawing.FontStyle.Bold), true); String[] header = { "Vendor Name", "Address", "City", "State", "Country" }; for (int i = 0; i < header.Length; i++) { headerRow.Cells[i].Value = header[i]; headerRow.Cells[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); } headerRow.Style.BackgroundBrush = PdfBrushes.SeaGreen; //datarow of grid int rowIndex = 1; foreach (Vendor vendor in this.dataSource) { PdfGridRow row = grid.Rows.Add(); row.Style.Font = new PdfInternalFont(new Font("Arial", 10f), true); row.Cells[0].Value = vendor.VendorName; row.Cells[1].Value = vendor.Address; row.Cells[2].Value = vendor.City; row.Cells[3].Value = vendor.State; row.Cells[4].Value = vendor.Country; row.Cells[0].Style.BackgroundBrush = PdfBrushes.LightGray; row.Cells[0].StringFormat = row.Cells[1].StringFormat = row.Cells[2].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); row.Cells[3].StringFormat = row.Cells[4].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); if (rowIndex % 2 == 0) { row.Style.BackgroundBrush = PdfBrushes.YellowGreen; } else { row.Style.BackgroundBrush = PdfBrushes.Bisque; } rowIndex++; } StringBuilder totalAmount = new StringBuilder(); var groupByCountry = this.dataSource.GroupBy(v => v.Country) .Select(g => new { Name = g.Key, Count = g.Count() }); foreach (var item in groupByCountry) { totalAmount.AppendFormat("{0}:\t{1}", item.Name, item.Count); totalAmount.AppendLine(); } PdfGridRow totalAmountRow = grid.Rows.Add(); totalAmountRow.Style.BackgroundBrush = PdfBrushes.Plum; totalAmountRow.Cells[0].Value = "Total Amount"; totalAmountRow.Cells[0].Style.Font = new PdfInternalFont(new Font("Arial", 10f, System.Drawing.FontStyle.Bold), true); totalAmountRow.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); totalAmountRow.Cells[1].ColumnSpan = 4; totalAmountRow.Cells[1].Value = totalAmount.ToString(); totalAmountRow.Cells[1].Style.Font = new PdfInternalFont(new Font("Arial", 10f, System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic), true); totalAmountRow.Cells[1].StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle); PdfLayoutResult resultl = grid.Draw(page, new PointF(0, y)); y = y + resultl.Bounds.Height + 5; PdfBrush brush2 = PdfBrushes.Gray; PdfTrueTypeFont font2 = new PdfInternalFont(new Font("Arial", 9f)); resultl.Page.Canvas.DrawString(String.Format("* All {0} vendors in the list", grid.Rows.Count - 1), font2, brush2, 5, y); //Save pdf file. using (Stream stream = saveFiledialog.OpenFile()) { document.SaveToStream(stream); } document.Close(); }
private float GenerateItemizedBodyWithGrid(GenerateInvoiceContext request, PdfGenerator pdf, float y) { y = pdf.IncrementY(y, 10, FOOTER_HEIGHT); //Create a new PdfGrid. PdfGrid pdfGrid = new PdfGrid(); //Add four columns. pdfGrid.Columns.Add(4); var columnFormat = new PdfStringFormat { Alignment = PdfTextAlignment.Center, LineAlignment = PdfVerticalAlignment.Middle }; pdfGrid.Columns[1].Format = columnFormat; pdfGrid.Columns[2].Format = columnFormat; pdfGrid.Columns[3].Format = columnFormat; //Add header. pdfGrid.Headers.Add(1); PdfGridRow pdfGridHeader = pdfGrid.Headers[0]; pdfGridHeader.Cells[0].Value = "Item"; pdfGridHeader.Cells[1].Value = "Cost"; pdfGridHeader.Cells[2].Value = "Qty"; pdfGridHeader.Cells[3].Value = "Total"; //Add rows. foreach (var item in request.Invoice.Items) { PdfGridRow pdfGridRow = pdfGrid.Rows.Add(); //NOTE: It seems that values MUST be string values pdfGridRow.Cells[0].Value = item.Name; pdfGridRow.Cells[1].Value = item.ItemAmount.ToString("n2"); pdfGridRow.Cells[2].Value = item.Quantity.ToString("n0"); pdfGridRow.Cells[3].Value = item.Amount.ToString("n2"); } var data = request.Invoice.Items.Select(x => x.ItemAmount.ToString("n2")); pdfGrid.Columns[1].SizeColumnToContent(data, pdf.PageWidth, pdf.NormalFont); data = request.Invoice.Items.Select(x => x.Quantity.ToString("n2")); pdfGrid.Columns[2].SizeColumnToContent(data, pdf.PageWidth, pdf.NormalFont); data = request.Invoice.Items.Select(x => x.Amount.ToString("n2")); pdfGrid.Columns[3].SizeColumnToContent(data, pdf.PageWidth, pdf.NormalFont); //Apply built-in table style //NOTE: that the accent2 color of #FFED7D31 is used in generating the total rectangle as well pdfGrid.ApplyBuiltinStyle(PdfGridBuiltinStyle.GridTable4Accent2); //Apply Custom Style //pdfGrid.Style = new PdfGridStyle //{ // //BackgroundBrush = pdf.AccentBrush, // TextBrush = pdf.AccentBrush, // //TextPen = new PdfPen(pdf.AccentBrush) //}; PdfGridLayoutFormat format = new PdfGridLayoutFormat(); format.Layout = PdfLayoutType.Paginate; format.PaginateBounds = new RectangleF(0, 0, pdf.CurrentPage.Graphics.ClientSize.Width, pdf.CurrentPage.Graphics.ClientSize.Height - FOOTER_HEIGHT); //Draw the PdfGrid. var result = pdfGrid.Draw(pdf.CurrentPage, new PointF(10, y), format); return(result.Bounds.Bottom); }
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 void button1_Click(object sender, EventArgs e) { //Create a pdf document PdfDocument doc = new PdfDocument(); //Margin PdfUnitConvertor unitCvtr = new PdfUnitConvertor(); PdfMargins margin = new PdfMargins(); margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Bottom = margin.Top; margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Right = margin.Left; //Create one page PdfPageBase page = doc.Pages.Add(PdfPageSize.A4); float y = 10; float x1 = page.Canvas.ClientSize.Width; //Title PdfBrush brush1 = PdfBrushes.Black; PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold), true); PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center); page.Canvas.DrawString("Vendor List", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1); y = y + font1.MeasureString("Vendor List", format1).Height; y = y + 5; String[] data = { "VendorName;Address1;City;State;Country", "Cacor Corporation;161 Southfield Rd;Southfield;OH;U.S.A.", "Underwater;50 N 3rd Street;Indianapolis;IN;U.S.A.", "J.W. Luscher Mfg.;65 Addams Street;Berkely;MA;U.S.A.", "Scuba Professionals;3105 East Brace;Rancho Dominguez;CA;U.S.A.", "Divers' Supply Shop;5208 University Dr;Macon;GA;U.S.A.", "Techniques;52 Dolphin Drive;Redwood City;CA;U.S.A.", "Perry Scuba;3443 James Ave;Hapeville;GA;U.S.A.", "Beauchat, Inc.;45900 SW 2nd Ave;Ft Lauderdale;FL;U.S.A.", "Amor Aqua;42 West 29th Street;New York;NY;U.S.A.", "Aqua Research Corp.;P.O. Box 998;Cornish;NH;U.S.A.", "B&K Undersea Photo;116 W 7th Street;New York;NY;U.S.A.", "Diving International Unlimited;1148 David Drive;San Diego;DA;U.S.A.", "Nautical Compressors;65 NW 167 Street;Miami;FL;U.S.A.", "Glen Specialties, Inc.;17663 Campbell Lane;Huntington Beach;CA;U.S.A.", "Dive Time;20 Miramar Ave;Long Beach;CA;U.S.A.", "Undersea Systems, Inc.;18112 Gotham Street;Huntington Beach;CA;U.S.A.", "Felix Diving;310 S Michigan Ave;Chicago;IL;U.S.A.", "Central Valley Skin Divers;160 Jameston Ave;Jamaica;NY;U.S.A.", "Parkway Dive Shop;241 Kelly Street;South Amboy;NJ;U.S.A.", "Marine Camera & Dive;117 South Valley Rd;San Diego;CA;U.S.A.", "Dive Canada;275 W Ninth Ave;Vancouver;British Columbia;Canada", "Dive & Surf;P.O. Box 20210;Indianapolis;IN;U.S.A.", "Fish Research Labs;29 Wilkins Rd Dept. SD;Los Banos;CA;U.S.A." }; //Create a grid PdfGrid grid = new PdfGrid(); //Set the cell padding grid.Style.CellPadding = new PdfPaddings(1, 1, 1, 1); String[] header = data[0].Split(';'); grid.Columns.Add(header.Length); float width = page.Canvas.ClientSize.Width - (grid.Columns.Count + 1); grid.Columns[0].Width = width * 0.25f; grid.Columns[1].Width = width * 0.25f; grid.Columns[2].Width = width * 0.25f; grid.Columns[3].Width = width * 0.15f; grid.Columns[4].Width = width * 0.10f; PdfGridRow headerRow = grid.Headers.Add(1)[0]; headerRow.Style.Font = new PdfTrueTypeFont(new Font("Arial", 11f, FontStyle.Bold), true); headerRow.Style.BackgroundBrush = new PdfLinearGradientBrush(new PointF(0, 0), new PointF(x1, 0), Color.Red, Color.Blue); for (int i = 0; i < header.Length; i++) { headerRow.Cells[i].Value = header[i]; headerRow.Cells[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); if (i == 0) { headerRow.Cells[i].Style.BackgroundBrush = PdfBrushes.Gray; } } Random random = new Random(); Dictionary <String, int> groupByCountry = new Dictionary <String, int>(); for (int r = 1; r < data.Length; r++) { PdfGridRow row = grid.Rows.Add(); row.Style.Font = new PdfTrueTypeFont(new Font("Arial", 10f), true); byte[] buffer = new byte[6]; random.NextBytes(buffer); PdfRGBColor color1 = new PdfRGBColor(buffer[0], buffer[1], buffer[2]); PdfRGBColor color2 = new PdfRGBColor(buffer[3], buffer[4], buffer[5]); row.Style.BackgroundBrush = new PdfLinearGradientBrush(new PointF(0, 0), new PointF(x1, 0), color1, color2); String[] rowData = data[r].Split(';'); for (int c = 0; c < rowData.Length; c++) { row.Cells[c].Value = rowData[c]; if (c == 0) { row.Cells[c].Style.BackgroundBrush = PdfBrushes.Gray; } if (c < 3) { row.Cells[c].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); } else { row.Cells[c].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); } if (c == 4) { if (groupByCountry.ContainsKey(rowData[c])) { groupByCountry[rowData[c]] = groupByCountry[rowData[c]] + 1; } else { groupByCountry[rowData[c]] = 1; } } } } StringBuilder totalAmount = new StringBuilder(); foreach (KeyValuePair <String, int> country in groupByCountry) { totalAmount.AppendFormat("{0}:\t{1}", country.Key, country.Value); totalAmount.AppendLine(); } //Add a row PdfGridRow totalAmountRow = grid.Rows.Add(); totalAmountRow.Style.BackgroundBrush = PdfBrushes.Plum; totalAmountRow.Cells[0].Value = "Total Amount"; totalAmountRow.Cells[0].Style.Font = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold), true); totalAmountRow.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); totalAmountRow.Cells[1].ColumnSpan = 4; totalAmountRow.Cells[1].Value = totalAmount.ToString(); totalAmountRow.Cells[1].Style.Font = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold | FontStyle.Italic), true); totalAmountRow.Cells[1].StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle); //Append product list PdfGrid productList = new PdfGrid(); productList.Style.Font = new PdfTrueTypeFont(new Font("Arial", 8f), true); //Fill data into the grid productList using (OleDbConnection conn = new OleDbConnection()) { conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\..\..\..\..\Data\demo.mdb"; OleDbCommand command = new OleDbCommand(); command.CommandText = " select p.Description " + " from vendors v " + " inner join parts p " + " on v.VendorNo = p.VendorNo " + " where v.VendorName = 'Cacor Corporation'"; command.Connection = conn; using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(command)) { DataTable dataTable = new DataTable(); dataAdapter.Fill(dataTable); productList.DataSource = dataTable; } } productList.Headers[0].Cells[0].Value = "Cacor Corporation"; productList.Headers[0].Cells[0].Style.Font = new PdfTrueTypeFont(new Font("Arial", 8f, FontStyle.Bold), true); productList.Headers[0].Cells[0].Style.Borders.All = new PdfPen(new PdfTilingBrush(new SizeF(1, 1)), 0); //Embed the gride productList into the cell of grid grid.Rows[0].Cells[0].Value = productList; grid.Rows[0].Cells[0].StringFormat.Alignment = PdfTextAlignment.Left; PdfLayoutResult result = grid.Draw(page, new PointF(0, y)); y = y + result.Bounds.Height + 5; PdfBrush brush2 = PdfBrushes.Gray; PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 9f)); result.Page.Canvas.DrawString(String.Format("* All {0} vendors in the list", grid.Rows.Count - 1), font2, brush2, 5, y); //Save pdf file doc.SaveToFile("Grid.pdf"); doc.Close(); //Launch the Pdf file PDFDocumentViewer("Grid.pdf"); }
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 ActionResult ExportPdfLINEPayRemittance(List <LINEPayRemittance> LINEPayRemittance, DateTime RemittanceDate) { // Load the PDF Template Stream pdfStream = System.IO.File.OpenRead(_hostingEnvironment.WebRootPath + @"\assets\templates\LINEPayRemittance.pdf"); PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, (float)6.5); PdfFont fontRemittanceDate = new PdfStandardFont(PdfFontFamily.Helvetica, (float)8); // Load a PDF document. PdfLoadedDocument loadedDocument = new PdfLoadedDocument(pdfStream); //Create a new PDF document. PdfDocument pdfDocument = new PdfDocument(); pdfDocument.ImportPage(loadedDocument, 0); PdfPage pdfPage = pdfDocument.Pages[0]; //Create a new PdfGrid. PdfGrid pdfGrid = new PdfGrid(); //Add three columns. pdfGrid.Columns.Add(9); //Add header. pdfGrid.Headers.Add(1); PdfGridRow pdfGridHeader = pdfGrid.Headers[0]; string[] headerStr = { "ERP ID", "Branch ID", "Report Date", "Branch Name", "TUC DCSP", "TUC", "TUP", "TUD", "Captured" }; int[] columnsWidth = { 40, 40, 50, 0, 50, 50, 50, 50, 30 }; pdfGridHeader.Style.Font = font; pdfGridHeader.Style.BackgroundBrush = PdfBrushes.LightGray; pdfGridHeader.Height = (float)11; for (int i = 0; i < headerStr.Count(); i++) { if (!i.Equals(3)) { pdfGrid.Columns[i].Width = columnsWidth[i]; } pdfGridHeader.Cells[i].StringFormat.Alignment = PdfTextAlignment.Center; pdfGridHeader.Cells[i].Value = headerStr[i]; pdfGridHeader.Cells[i].Style.CellPadding = new PdfPaddings((float)1.5, (float)1.5, (float)1.5, (float)1.5); pdfGridHeader.Cells[i].Style.Borders.Left.Width = (float)0.5; pdfGridHeader.Cells[i].Style.Borders.Right.Width = (float)0.5; pdfGridHeader.Cells[i].Style.Borders.Top.Width = (float)0.5; pdfGridHeader.Cells[i].Style.Borders.Bottom.Width = (float)0.5; } pdfGridHeader.Cells[4].StringFormat.Alignment = PdfTextAlignment.Right; pdfGridHeader.Cells[5].StringFormat.Alignment = PdfTextAlignment.Right; pdfGridHeader.Cells[6].StringFormat.Alignment = PdfTextAlignment.Right; PdfGridRow pdfGridRow; LINEPayRemittance.ForEach(line => { //Add rows. pdfGridRow = pdfGrid.Rows.Add(); pdfGridRow.Style.Font = font; pdfGridRow.Height = (float)11; pdfGridRow.Cells[0].Value = line.ERP_ID; pdfGridRow.Cells[1].Value = line.BranchID; pdfGridRow.Cells[2].Value = line.ReportDate.ToString("dd/MM/yyyy", enUS); pdfGridRow.Cells[3].Value = line.BranchName; pdfGridRow.Cells[4].Value = line.branch_type.Equals("DCSP-SHOP") ? line.TUC.ToString("#,0.00"):"-"; pdfGridRow.Cells[5].Value = !line.branch_type.Equals("DCSP-SHOP") ? line.TUC.ToString("#,0.00") : "-"; pdfGridRow.Cells[6].Value = line.TUP.ToString("#,0.00"); pdfGridRow.Cells[7].Value = line.TUD.ToString("#,0.00"); pdfGridRow.Cells[8].Value = line.TUDVerifyDate == null ? line.Captured : "Yes"; pdfGridRow.Cells[0].StringFormat.Alignment = PdfTextAlignment.Center; pdfGridRow.Cells[2].StringFormat.Alignment = PdfTextAlignment.Center; pdfGridRow.Cells[4].StringFormat.Alignment = PdfTextAlignment.Right; pdfGridRow.Cells[5].StringFormat.Alignment = PdfTextAlignment.Right; pdfGridRow.Cells[6].StringFormat.Alignment = PdfTextAlignment.Right; pdfGridRow.Cells[7].StringFormat.Alignment = PdfTextAlignment.Right; pdfGridRow.Cells[8].StringFormat.Alignment = PdfTextAlignment.Center; for (int i = 0; i < pdfGridRow.Cells.Count; i++) { pdfGridRow.Cells[i].Style.Borders.Left.Width = (float)0.5; pdfGridRow.Cells[i].Style.Borders.Right.Width = (float)0.5; pdfGridRow.Cells[i].Style.Borders.Top.Width = (float)0.5; pdfGridRow.Cells[i].Style.Borders.Bottom.Width = (float)0.5; pdfGridRow.Cells[i].Style.CellPadding = new PdfPaddings((float)1.5, (float)1.5, (float)1.5, (float)1.5); } }); //Add rows Sum pdfGridRow = pdfGrid.Rows.Add(); pdfGridRow.Cells[4].Value = LINEPayRemittance.Where(line => line.branch_type.Equals("DCSP-SHOP")).Sum(line => line.TUC).ToString("#,0.00"); pdfGridRow.Cells[5].Value = LINEPayRemittance.Where(line => !line.branch_type.Equals("DCSP-SHOP")).Sum(line => line.TUC).ToString("#,0.00"); pdfGridRow.Cells[6].Value = LINEPayRemittance.Sum(line => line.TUP).ToString("#,0.00"); pdfGridRow.Cells[7].Value = LINEPayRemittance.Sum(line => line.TUD).ToString("#,0.00"); pdfGridRow.Cells[4].Style.CellPadding = new PdfPaddings((float)1.5, (float)1.5, (float)1.5, (float)1.5); pdfGridRow.Cells[5].Style.CellPadding = new PdfPaddings((float)1.5, (float)1.5, (float)1.5, (float)1.5); pdfGridRow.Cells[6].Style.CellPadding = new PdfPaddings((float)1.5, (float)1.5, (float)1.5, (float)1.5); pdfGridRow.Cells[7].Style.CellPadding = new PdfPaddings((float)1.5, (float)1.5, (float)1.5, (float)1.5); for (int i = 0; i < pdfGridRow.Cells.Count; i++) { pdfGridRow.Cells[i].Style.Borders.Left.Color = PdfColor.Empty; pdfGridRow.Cells[i].Style.Borders.Right.Color = PdfColor.Empty; pdfGridRow.Cells[i].Style.Borders.Bottom.Color = PdfColor.Empty; pdfGridRow.Cells[i].Style.Borders.Left.Width = (float)0.5; pdfGridRow.Cells[i].Style.Borders.Right.Width = (float)0.5; pdfGridRow.Cells[i].Style.Borders.Top.Width = (float)0.5; pdfGridRow.Cells[i].Style.Borders.Bottom.Width = (float)0.5; } pdfGridRow.Cells[4].StringFormat.Alignment = PdfTextAlignment.Right; pdfGridRow.Cells[5].StringFormat.Alignment = PdfTextAlignment.Right; pdfGridRow.Cells[6].StringFormat.Alignment = PdfTextAlignment.Right; pdfGridRow.Cells[7].StringFormat.Alignment = PdfTextAlignment.Right; pdfGridRow.Height = (float)11; pdfGridRow.Style.Font = font; for (int i = 4; i <= 7; i++) { pdfGridRow.Cells[i].Style.Borders.Top.Color = new PdfColor(Color.Black); pdfGridRow.Cells[i].Style.Borders.Left.Color = new PdfColor(Color.Black); pdfGridRow.Cells[i].Style.Borders.Right.Color = new PdfColor(Color.Black); pdfGridRow.Cells[i].Style.Borders.Bottom.Color = new PdfColor(Color.Black); } //Add rows Total pdfGridRow = pdfGrid.Rows.Add(); pdfGridRow.Cells[3].Value = "Total"; pdfGridRow.Cells[4].Value = ( LINEPayRemittance.Sum(line => line.TUC) + LINEPayRemittance.Sum(line => line.TUP) + LINEPayRemittance.Sum(line => line.TUD)).ToString("#,0.00"); pdfGridRow.Cells[4].ColumnSpan = 4; pdfGridRow.Cells[3].Style.CellPadding = new PdfPaddings((float)1.5, (float)1.5, (float)1.5, (float)1.5); pdfGridRow.Cells[4].Style.CellPadding = new PdfPaddings((float)1.5, (float)1.5, (float)1.5, (float)1.5); pdfGridRow.Cells[3].StringFormat.Alignment = PdfTextAlignment.Right; pdfGridRow.Cells[4].StringFormat.Alignment = PdfTextAlignment.Center; pdfGridRow.Height = (float)11; pdfGridRow.Style.Font = font; for (int i = 0; i < pdfGridRow.Cells.Count; i++) { pdfGridRow.Cells[i].Style.Borders.Top.Color = PdfColor.Empty; pdfGridRow.Cells[i].Style.Borders.Left.Color = PdfColor.Empty; pdfGridRow.Cells[i].Style.Borders.Right.Color = PdfColor.Empty; pdfGridRow.Cells[i].Style.Borders.Bottom.Color = PdfColor.Empty; } pdfGridRow.Cells[4].Style.Borders.Top.Color = new PdfColor(Color.Black); pdfGridRow.Cells[4].Style.Borders.Left.Color = new PdfColor(Color.Black); pdfGridRow.Cells[4].Style.Borders.Right.Color = new PdfColor(Color.Black); pdfGridRow.Cells[4].Style.Borders.Bottom.Color = new PdfColor(Color.Black); pdfGridRow.Cells[4].Style.Borders.Left.Width = (float)0.5; pdfGridRow.Cells[4].Style.Borders.Right.Width = (float)0.5; pdfGridRow.Cells[4].Style.Borders.Top.Width = (float)0.5; pdfGridRow.Cells[4].Style.Borders.Bottom.Width = (float)0.5; //Create PDF graphics for the page. PdfGraphics graphics = pdfPage.Graphics; //Draw the text. graphics.DrawString(RemittanceDate.ToString("dd MMM yyyy", enUS), fontRemittanceDate, PdfBrushes.Black, new PointF((float)458, (float)57.5)); //Draw the PdfGrid. //pdfGrid.Draw(pdfPage, (float)20, (float)90, (float)555.28,); //pdfGrid.Draw(pdfPage, new RectangleF(20, 90, pdfDocument.Pages[0].GetClientSize().Width - 40, pdfDocument.Pages[0].GetClientSize().Height - 140)); //Set properties to paginate the table. PdfGridLayoutFormat layoutFormat = new PdfGridLayoutFormat(); layoutFormat.Break = PdfLayoutBreakType.FitElement; layoutFormat.Layout = PdfLayoutType.Paginate; layoutFormat.PaginateBounds = new RectangleF(20, 20, pdfDocument.Pages[0].GetClientSize().Width - 40, pdfDocument.Pages[0].GetClientSize().Height - 50); //Draw PdfLightTable. pdfGrid.Draw(pdfPage, 20f, 90f, (pdfDocument.Pages[0].GetClientSize().Width - 40), layoutFormat); //Create a Page template that can be used as footer. RectangleF bounds = new RectangleF(0, 0, pdfDocument.Pages[0].GetClientSize().Width, 50); PdfPageTemplateElement footer = new PdfPageTemplateElement(bounds); PdfBrush brush = new PdfSolidBrush(Color.Black); //Create page number field. PdfPageNumberField pageNumber = new PdfPageNumberField(font, brush); //Create page count field. PdfPageCountField count = new PdfPageCountField(font, brush); //Add the fields in composite fields. PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumber, count); string printDate = DateTime.Now.ToString("dd MMM yyyy HH:mm:ss", enUS); PdfCompositeField compositePrintDate = new PdfCompositeField(font, brush, string.Format("Printed Date : {0}", printDate)); compositeField.Bounds = footer.Bounds; //Draw the composite field in footer. compositeField.Draw(footer.Graphics, new PointF(pdfDocument.Pages[0].GetClientSize().Width - 50, 30)); compositePrintDate.Draw(footer.Graphics, new PointF(20, 30)); //Add the footer template at the bottom. pdfDocument.Template.Bottom = footer; MemoryStream ms = new MemoryStream(); pdfDocument.Save(ms); ms.Position = 0; //Close the document pdfDocument.Close(true); //Save the document. return(File(ms, "Application/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); } }
public async Task <IActionResult> OnPostAsync(Invoice model) //public IActionResult OnPost(Invoice model) { // ComponentInfo.SetLicense("FREE-LICENSE-KEY"); if (!ModelState.IsValid) { return(Page()); } _context.Invoice.Add(Invoice); await _context.SaveChangesAsync(); /* SaveOptions options = GetSaveOptions(model.SelectedFormat); * DocumentModel document = this.Process(model); * * * return File(GetBytes(document, options), options.ContentType, "Invoice." + model.SelectedFormat.ToLowerInvariant()); * // return RedirectToPage("./Index"); */ // string webRootPath = _hostingEnvironment.WebRootPath; string fileName = @"Invoice.pdf"; // string URL = string.Format("{0}://{1}/{2}", Request.Scheme, Request.Host, fileName); // FileInfo file = new FileInfo(Path.Combine(fileName)); var memoryStream = new MemoryStream(); //create a PDF document PdfDocument doc = new PdfDocument(); doc.PageSettings.Size = PdfPageSize.A4; //reset the default margins to 0 doc.PageSettings.Margins = new PdfMargins(0); //create a PdfMargins object, the parameters indicate the page margins you want to set PdfMargins margins = new PdfMargins(0, 40, 0, 40); //create a header template with content and apply it to page template doc.Template.Top = CreateHeaderTemplate(doc, margins); doc.Template.Bottom = CreateFooterTemplate(doc, margins); //apply blank templates to other parts of page template // doc.Template.Bottom = new PdfPageTemplateElement(doc.PageSettings.Size.Width, margins.Bottom); doc.Template.Left = new PdfPageTemplateElement(margins.Left, doc.PageSettings.Size.Height); doc.Template.Right = new PdfPageTemplateElement(margins.Right, doc.PageSettings.Size.Height); PdfPageBase page = doc.Pages.Add(); PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 18f, FontStyle.Bold)); PdfSolidBrush brush = new PdfSolidBrush(Color.Gray); page.Canvas.DrawString(String.Format("#{0}", Invoice.ID), font, brush, new PointF(520, 0)); PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 11f, FontStyle.Bold)); PdfTrueTypeFont font3 = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold)); PdfSolidBrush brush2 = new PdfSolidBrush(Color.Black); PdfSolidBrush brush3 = new PdfSolidBrush(Color.White); page.Canvas.DrawString("Date:\nValid Until: ", font2, brush, new PointF(420, 25)); page.Canvas.DrawString(String.Format("{0}\n{1}", DateTime.Now.ToString("MMM dd, yyyy"), Invoice.Date.ToString("MMM dd, yyyy")), font2, brush2, new PointF(490, 25)); page.Canvas.DrawString("Bill To:", font3, brush, new PointF(70, 55)); page.Canvas.DrawString(String.Format("{0}\n{1}\n{2}, {3}", Invoice.Name, Invoice.Project, Invoice.City, Invoice.Country), font3, brush2, new PointF(70, 70)); /* String[] data * = { * "Item;Quantity;Rate;Amount", * String.Format("{0};{1};{2};{3}",Invoice.Item1,Invoice.Quantity1,Invoice.Rate1,Invoice.Amount1) * }; * String[][] dataSource * = new String[data.Length][]; * for (int i = 0; i < data.Length; i++) * { * dataSource[i] = data[i].Split(';'); * } * * PdfTable table = new PdfTable(); * table.Style.CellPadding = 3; * table.Style.BorderPen = new PdfPen(brush3, 0.75f); * table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center); * table.Style.HeaderSource = PdfHeaderSource.Rows; * table.Style.HeaderRowCount = 1; * table.Style.ShowHeader = true; * table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.Black; * table.DataSource = dataSource; * foreach (PdfColumn column in table.Columns) * { * column.StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); * } * table.Draw(page, new PointF(100, 135)); */ PdfGrid grid = new PdfGrid(); grid.Columns.Add(5); float width = page.Canvas.ClientSize.Width - (grid.Columns.Count + 1); for (int j = 0; j < grid.Columns.Count; j++) { grid.Columns[j].Width = width * 0.20f; } PdfGridRow row0 = grid.Rows.Add(); PdfGridRow row1 = grid.Rows.Add(); PdfGridRow row2 = grid.Rows.Add(); PdfGridRow row3 = grid.Rows.Add(); PdfGridRow row4 = grid.Rows.Add(); PdfGridRow row5 = grid.Rows.Add(); PdfGridRow row6 = grid.Rows.Add(); PdfGridRow row7 = grid.Rows.Add(); float height = 20.0f; for (int i = 0; i < grid.Rows.Count; i++) { grid.Rows[i].Height = height; } //SET CAP ~56 LETTERS row0.Style.Font = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold), true); row0.Style.TextBrush = new PdfSolidBrush(Color.White); row1.Style.Font = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold), true); row0.Cells[0].Value = "\tItem"; row0.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); row0.Cells[0].Style.BackgroundBrush = PdfBrushes.Black; // row0.Cells[0].RowSpan = 2; row0.Cells[0].ColumnSpan = 2; // row0.Cells[0].Style.BackgroundBrush = PdfBrushes.Black; row1.Cells[0].Value = String.Format(" {0}", Invoice.Item1); row1.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); row1.Cells[0].ColumnSpan = 2; row2.Cells[0].Value = String.Format(" {0}", Invoice.Item2); row2.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); row2.Cells[0].ColumnSpan = 2; row3.Cells[0].Value = String.Format(" {0}", Invoice.Item3); row3.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); row3.Cells[0].ColumnSpan = 2; row4.Cells[0].Value = String.Format(" {0}", Invoice.Item4); row4.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); row4.Cells[0].ColumnSpan = 2; row5.Cells[0].Value = String.Format(" {0}", Invoice.Item5); row5.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); row5.Cells[0].ColumnSpan = 2; row6.Cells[0].Value = String.Format(" {0}", Invoice.Item6); row6.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); row6.Cells[0].ColumnSpan = 2; row7.Cells[0].Value = String.Format(" {0}", Invoice.Item7); row7.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); row7.Cells[0].ColumnSpan = 2; row0.Cells[2].Value = "Quantity"; row0.Cells[2].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); row0.Cells[2].Style.BackgroundBrush = PdfBrushes.Black; row1.Cells[2].Value = String.Format("{0}", Invoice.Quantity1); row1.Cells[2].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); row2.Cells[2].Value = String.Format("{0}", Invoice.Quantity2); row2.Cells[2].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); row3.Cells[2].Value = String.Format("{0}", Invoice.Quantity3); row3.Cells[2].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); row4.Cells[2].Value = String.Format("{0}", Invoice.Quantity4); row4.Cells[2].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); row5.Cells[2].Value = String.Format("{0}", Invoice.Quantity5); row5.Cells[2].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); row6.Cells[2].Value = String.Format("{0}", Invoice.Quantity6); row6.Cells[2].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); row7.Cells[2].Value = String.Format("{0}", Invoice.Quantity7); row7.Cells[2].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); row0.Cells[3].Value = "Rate [AED]"; row0.Cells[3].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); row0.Cells[3].Style.BackgroundBrush = PdfBrushes.Black; // row0.Cells[1].ColumnSpan = 3; row1.Cells[3].Value = String.Format("{0}", Invoice.Rate1); row1.Cells[3].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); row2.Cells[3].Value = String.Format("{0}", Invoice.Rate2); row2.Cells[3].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); row3.Cells[3].Value = String.Format("{0}", Invoice.Rate3); row3.Cells[3].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); row4.Cells[3].Value = String.Format("{0}", Invoice.Rate4); row4.Cells[3].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); row5.Cells[3].Value = String.Format("{0}", Invoice.Rate5); row5.Cells[3].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); row6.Cells[3].Value = String.Format("{0}", Invoice.Rate6); row6.Cells[3].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); row7.Cells[3].Value = String.Format("{0}", Invoice.Rate7); row7.Cells[3].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); row0.Cells[4].Value = "Amount [AED]"; // row0.Cells[4].Style.Font = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold | FontStyle.Italic), true); row0.Cells[4].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); row0.Cells[4].Style.BackgroundBrush = PdfBrushes.Black; row1.Cells[4].Value = String.Format("{0}", Invoice.Amount1); row1.Cells[4].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); row2.Cells[4].Value = String.Format("{0}", Invoice.Amount2); row2.Cells[4].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); row3.Cells[4].Value = String.Format("{0}", Invoice.Amount3); row3.Cells[4].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); row4.Cells[4].Value = String.Format("{0}", Invoice.Amount4); row4.Cells[4].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); row5.Cells[4].Value = String.Format("{0}", Invoice.Amount5); row5.Cells[4].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); row6.Cells[4].Value = String.Format("{0}", Invoice.Amount6); row6.Cells[4].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); row7.Cells[4].Value = String.Format("{0}", Invoice.Amount7); row7.Cells[4].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); // row1.Cells[1].Value = "Amount [AED]"; // row1.Cells[1].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); // row1.Cells[1].ColumnSpan = 4; PdfBorders border = new PdfBorders(); border.All = new PdfPen(Color.Transparent); foreach (PdfGridRow pgr in grid.Rows) { foreach (PdfGridCell pgc in pgr.Cells) { pgc.Style.Borders = border; } } grid.Draw(page, new PointF(2, 135)); /* row0.Cells[0].Value = "Item"; * row0.Cells[0].ColumnSpan = 2; * * * row0.Cells[2].Value = "Quantity"; * // row0.Cells[2].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); * * * row0.Cells[3].Value = "Rate [AED]"; * // row0.Cells[4].Style.Font = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold | FontStyle.Italic), true); * // row0.Cells[3].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); * // row0.Cells[3].Style.BackgroundBrush = PdfBrushes.LightGreen; * * * row0.Cells[4].Value = "Amount [AED]"; * // row1.Cells[4].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); * // row1.Cells[4].ColumnSpan = 4; * * row1.Cells[0].Value = "This is an Item yo"; * row1.Cells[0].ColumnSpan = 2; */ //save the file doc.SaveToFile("Invoice.pdf"); /* * PdfDocument destDoc = new PdfDocument(); * float top = 20; * float bottom = 20; * float left = 20; * float right = 20; * * foreach (PdfPageBase page in doc.Pages) * { * PdfPageBase newPage = destDoc.Pages.Add(page.Size, new PdfMargins(0)); * newPage.Canvas.ScaleTransform((page.ActualSize.Width - left - right) / page.ActualSize.Width, * (page.ActualSize.Height - top - bottom) / page.ActualSize.Height); * newPage.Canvas.DrawTemplate(page.CreateTemplate(), new PointF(left, top)); * } * * destDoc.SaveToFile("result.pdf", FileFormat.PDF);*/ // System.Diagnostics.Process.Start("PdfHeader.pdf"); //let the user save it and open it and yeah // save the file with a unique name each time -- why? let it get over written each time np // return Page(); using (var fileStream = new FileStream(Path.Combine(fileName), FileMode.Open)) { await fileStream.CopyToAsync(memoryStream); } memoryStream.Position = 0; return(File(memoryStream, "application/pdf", fileName)); }
private async void Button_Clicked(object sender, EventArgs e) { //Create a new PDF document. PdfDocument doc = new PdfDocument(); //Add a page. PdfPage page = doc.Pages.Add(); Stream fontStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("IttezanPos.Assets.arial.ttf"); PdfTemplate header = PdfHelper.AddHeader(doc, "الأرصدة الإفتتاحية", "Ittezan Pos" + " " + DateTime.Now.ToString()); PdfCellStyle headerStyle = new PdfCellStyle(); headerStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center); page.Graphics.DrawPdfTemplate(header, new PointF()); //Create a PdfGrid. PdfGrid pdfGrid = new PdfGrid(); //String format // PdfFont pdfFont = new PdfTrueTypeFont(fontStream1, 12); //Create a DataTable. DataTable dataTable = new DataTable("EmpDetails"); List <SuplierTotalAmount> customerDetails = new List <SuplierTotalAmount>(); //Add columns to the DataTable dataTable.Columns.Add("ID"); dataTable.Columns.Add("Name"); dataTable.Columns.Add("Address"); dataTable.Columns.Add("Total"); //Add rows to the DataTable. foreach (var item in suppliers) { SuplierTotalAmount customer = new SuplierTotalAmount(); customer.name = item.name; customer.remaining = item.remaining; customer.creditorit = item.creditorit; customer.total_amount = item.total_amount; customerDetails.Add(customer); dataTable.Rows.Add(new string[] { customer.total_amount.ToString(), customer.remaining.ToString(), customer.creditorit.ToString(), customer.name }); } //Assign data source. pdfGrid.DataSource = dataTable; pdfGrid.Headers.Add(1); PdfGridRow pdfGridRowHeader = pdfGrid.Headers[0]; pdfGridRowHeader.Cells[3].Value = "الإسم"; pdfGridRowHeader.Cells[2].Value = "المبلغ دائن/ له"; pdfGridRowHeader.Cells[1].Value = "المبلغ المدين / عليه"; pdfGridRowHeader.Cells[0].Value = "الرصيد"; PdfGridStyle pdfGridStyle = new PdfGridStyle(); pdfGridStyle.Font = new PdfTrueTypeFont(fontStream, 12); PdfGridLayoutFormat format1 = new PdfGridLayoutFormat(); format1.Break = PdfLayoutBreakType.FitPage; format1.Layout = PdfLayoutType.Paginate; PdfStringFormat format = new PdfStringFormat(); format.TextDirection = PdfTextDirection.RightToLeft; format.Alignment = PdfTextAlignment.Center; format.LineAlignment = PdfVerticalAlignment.Middle; pdfGrid.Columns[0].Format = format; pdfGrid.Columns[1].Format = format; pdfGrid.Columns[2].Format = format; pdfGrid.Columns[3].Format = format; pdfGrid.Style = pdfGridStyle; //Draw grid to the page of PDF document. pdfGrid.Draw(page, new Syncfusion.Drawing.Point(0, (int)header.Height + 10), format1); MemoryStream stream = new MemoryStream(); //Save the document. doc.Save(stream); //close the document doc.Close(true); await Xamarin.Forms.DependencyService.Get <ISave>().SaveAndView("الأرصدة الإفتتاحية .pdf", "application/pdf", stream); }
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"); } }
/// <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 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); }
/// <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); }
public void CreatePriceListC() { PdfDocument doc = new PdfDocument(); //load the font from Assets Stream fontStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("KatalogPiw.UWP.Assets.Helvetica-Bold.ttf"); PdfTrueTypeFont font = new PdfTrueTypeFont(fontStream, 6); doc.PageSettings.Orientation = PdfPageOrientation.Landscape; PdfPage page = doc.Pages.Add(); RectangleF bounds = new RectangleF(0, 0, doc.Pages[0].GetClientSize().Width, 50); PdfPageTemplateElement header = new PdfPageTemplateElement(bounds); Stream imageStream = File.OpenRead("Assets/chmielove.png"); PdfBitmap image = new PdfBitmap(imageStream); PdfGraphics graphics = page.Graphics; graphics.DrawImage(image, 0, 0, 60, 60); // format 60x50 this.AddHeader(page, doc, "cos"); PdfGrid pdfGrid = new PdfGrid(); pdfGrid.BeginCellLayout += PdfGrid_BeginCellLayout; pdfGrid.Columns.Add(10); PdfStringFormat format = new PdfStringFormat(); format.Alignment = PdfTextAlignment.Center; format.LineAlignment = PdfVerticalAlignment.Middle; for (int i = 0; i < 10; i++) { pdfGrid.Columns[i].Format = format; } pdfGrid.Columns[0].Width = 50; pdfGrid.Columns[1].Width = 95; pdfGrid.Columns[2].Width = 50; pdfGrid.Columns[3].Width = 30; pdfGrid.Columns[4].Width = 318; pdfGrid.Columns[5].Width = 38; pdfGrid.Columns[6].Width = 38; pdfGrid.Columns[7].Width = 38; pdfGrid.Columns[8].Width = 38; pdfGrid.Headers.Add(1); PdfGridRow pdfGridHeader = pdfGrid.Headers[0]; pdfGridHeader.Cells[0].Value = "Kod"; pdfGridHeader.Cells[1].Value = "Nazwa"; pdfGridHeader.Cells[2].Value = "Browar"; pdfGridHeader.Cells[3].Value = "Gatunek"; pdfGridHeader.Cells[4].Value = "Opis 2"; pdfGridHeader.Cells[5].Value = "Alkohol"; pdfGridHeader.Cells[6].Value = "Plato"; pdfGridHeader.Cells[7].Value = "Cena Netto"; pdfGridHeader.Cells[8].Value = "Cena Brutto"; pdfGridHeader.Cells[9].Value = "Zdjecie"; pdfGridHeader.Style.Font = font; //add rows List <Beer> OutBeerList = new List <Beer>(); for (int i = 0; i < _beers.Count; i++) { if (_beers[i].IsSelect == true) { OutBeerList.Add(_beers[i]); } } for (int i = 0; i < OutBeerList.Count; i++) { PdfGridRow pdfGridRow = pdfGrid.Rows.Add(); pdfGridRow.Height = 38; pdfGridRow.Style.Font = font; pdfGridRow.Cells[0].Value = OutBeerList[i].EanCode; pdfGridRow.Cells[1].Value = OutBeerList[i].BeerName; pdfGridRow.Cells[2].Value = OutBeerList[i].BrewerName; pdfGridRow.Cells[3].Value = OutBeerList[i].TypeName; pdfGridRow.Cells[4].Value = OutBeerList[i].Description; pdfGridRow.Cells[5].Value = OutBeerList[i].Parameters; pdfGridRow.Cells[6].Value = OutBeerList[i].Plato; double outputValue = OutBeerList[i].NetPriceWithoutDiscout * 0.90; outputValue = FileOpenViewModel.UptoTwoDecimalPoints(outputValue); pdfGridRow.Cells[7].Value = outputValue.ToString(); double grossValue = outputValue * 1.23; grossValue = FileOpenViewModel.UptoTwoDecimalPoints(grossValue); pdfGridRow.Cells[8].Value = grossValue.ToString(); if (OutBeerList[i].PhotoPath == " ") { } else { //pdfGridRow.Cells[10].ImagePosition = PdfGridImagePosition.Fit; //dodac try przy otwieraniu pliku Stream imageStreamPhoto = File.OpenRead(OutBeerList[i].PhotoPath); PdfBitmap imagePhoto = new PdfBitmap(imageStreamPhoto); //pdfGridRow.Cells[10].Style.CellPadding.Left = 5; //pdfGridRow.Cells[10].Style.CellPadding.Right = 5; pdfGridRow.Cells[9].ImagePosition = PdfGridImagePosition.Fit; pdfGridRow.Cells[9].Style.BackgroundImage = imagePhoto; pdfGridRow.Cells[9].Value = 0; //pdfGridRow.Cells[10].Value = OutBeerList[i].Image; } } pdfGrid.Draw(page, 0, 80); //Save and close MemoryStream stream = new MemoryStream(); doc.Save(stream); Xamarin.Forms.DependencyService.Get <Services.ISave>().SaveTextAsync("CennikC.pdf", "application/pdf", stream); doc.Close(true); }