コード例 #1
0
        private void addTitles()
        {
            PdfStringFormat stringFormat = new PdfStringFormat()
            {
                Alignment = PdfTextAlignment.Center
            };
            RectangleF rectangleCenterPage = new Syncfusion.Drawing.RectangleF(0, currentY, _document.PageSettings.Size.Width, titleFont.Height);

            //title
            _document.Pages[0].Graphics.DrawString(Title, titleFont, brushBlack, rectangleCenterPage, stringFormat);

            currentY += spaceDefault;
            rectangleCenterPage.Y = currentY;

            //Laurea
            _document.Pages[0].Graphics.DrawString(TitleFacolta, subTitleFontBold, brushBlack, rectangleCenterPage, stringFormat);

            currentY += spaceDefault;
            rectangleCenterPage.Y = currentY;

            //Info = anno semestre
            _document.Pages[0].Graphics.DrawString(TitleInfo, subTitleFont, brushBlack, rectangleCenterPage, stringFormat);
            currentY += spaceDefault;

//			rectangleCenterPage.Y = currentY;

            //subTitleInfo
            //_document.Pages[0].Graphics.DrawString(SubtitleInfo, subTitleFont, brushBlack, rectangleCenterPage, stringFormat);
            //currentY += spaceDefault;
        }
コード例 #2
0
        public ActionResult CreateDocumentWithoutFilter()
        {
            PdfFont font    = new PdfStandardFont(PdfFontFamily.Helvetica, 10);
            PdfFont bigFont = new PdfStandardFont(PdfFontFamily.Helvetica, 20);

            PdfBrush    brush = new PdfSolidBrush(Syncfusion.Drawing.Color.Black);
            PdfDocument doc   = new PdfDocument();
            //Add a page to the document.
            PdfPage page = doc.Pages.Add();
            //Create PDF graphics for the page
            PdfGraphics graphics = page.Graphics;
            PdfGrid     pdfGrid  = new PdfGrid();

            //Load the image as stream.
            FileStream imageStream = new FileStream("logo.png", FileMode.Open, FileAccess.Read);
            PdfBitmap  image       = new PdfBitmap(imageStream);

            graphics.DrawImage(image, 200, 200);

            //Draw the image
            Syncfusion.Drawing.RectangleF bounds    = new Syncfusion.Drawing.RectangleF(0, 0, doc.Pages[0].GetClientSize().Width, 50);
            PdfPageTemplateElement        header    = new PdfPageTemplateElement(bounds);
            PdfCompositeField             compField = new PdfCompositeField(bigFont, brush, "OZO - Izvjestaj o natjecajima");

            compField.Draw(header.Graphics, new Syncfusion.Drawing.PointF(120, 0));

            //Add the header at the top.

            doc.Template.Top = header;
            //Save the PDF document to stream
            //Create a Page template that can be used as footer.


            PdfPageTemplateElement footer = new PdfPageTemplateElement(bounds);



            //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, "Stranica {0} od {1}", pageNumber, count);

            compositeField.Bounds = footer.Bounds;

            //Draw the composite field in footer.

            compositeField.Draw(footer.Graphics, new Syncfusion.Drawing.PointF(450, 40));

            //Add the footer template at the bottom.

            doc.Template.Bottom = footer;

            MemoryStream  stream = new MemoryStream();
            List <object> data   = new List <object>();

            var natječaji = _context.Natječaj
                            .AsNoTracking()
                            .OrderBy(o => o.Id)
                            .ToList();

            foreach (var natječaj in natječaji)
            {
                var row = new { Naziv = natječaj.Naziv, Opis = natječaj.Opis, Cijena = natječaj.Cijena, Poslodavac = natječaj.Poslodavac, Status = natječaj.Status, Lokacija = natječaj.Lokacija, Kontakt = natječaj.Kontakt };
                data.Add(row);
            }



            //Add list to IEnumerable
            IEnumerable <object> dataTable = data;

            //Assign data source.
            pdfGrid.DataSource = dataTable;
            pdfGrid.Draw(page, new Syncfusion.Drawing.PointF(50, 50));
            doc.Save(stream);
            //If the position is not set to '0' then the PDF will be empty.
            stream.Position = 0;
            //Close the document.
            doc.Close(true);
            //Defining the ContentType for pdf file.
            string contentType = "application/pdf";
            //Define the file name.
            string fileName = "Natječaji-Reports.pdf";

            return(File(stream, contentType, fileName));
        }
コード例 #3
0
        public ActionResult CreateDocument(string id, string vrsta, string znak)
        {
            PdfFont font    = new PdfStandardFont(PdfFontFamily.Helvetica, 10);
            PdfFont bigFont = new PdfStandardFont(PdfFontFamily.Helvetica, 20);

            PdfBrush    brush = new PdfSolidBrush(Syncfusion.Drawing.Color.Black);
            PdfDocument doc   = new PdfDocument();
            //Add a page to the document.
            PdfPage page = doc.Pages.Add();
            //Create PDF graphics for the page
            PdfGraphics graphics = page.Graphics;
            PdfGrid     pdfGrid  = new PdfGrid();

            //Load the image as stream.
            FileStream imageStream = new FileStream("logo.png", FileMode.Open, FileAccess.Read);
            PdfBitmap  image       = new PdfBitmap(imageStream);

            graphics.DrawImage(image, 200, 200);

            //Draw the image
            Syncfusion.Drawing.RectangleF bounds    = new Syncfusion.Drawing.RectangleF(0, 0, doc.Pages[0].GetClientSize().Width, 50);
            PdfPageTemplateElement        header    = new PdfPageTemplateElement(bounds);
            PdfCompositeField             compField = new PdfCompositeField(bigFont, brush, "OZO - Izvjestaj o natjecajima");

            compField.Draw(header.Graphics, new Syncfusion.Drawing.PointF(120, 0));

            //Add the header at the top.

            doc.Template.Top = header;
            //Save the PDF document to stream
            //Create a Page template that can be used as footer.


            PdfPageTemplateElement footer = new PdfPageTemplateElement(bounds);



            //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, "Stranica {0} od {1}", pageNumber, count);

            compositeField.Bounds = footer.Bounds;

            //Draw the composite field in footer.

            compositeField.Draw(footer.Graphics, new Syncfusion.Drawing.PointF(450, 40));

            //Add the footer template at the bottom.

            doc.Template.Bottom = footer;

            MemoryStream stream = new MemoryStream();
            //Add values to list
            List <object> data = new List <object>();

            switch (vrsta)
            {
            case "Naziv":

                var natječaji = _context.Natječaj
                                .AsNoTracking()
                                .Where(n => n.Naziv.Equals(id))
                                .OrderBy(o => o.Id)
                                .ToList();
                ispisi(natječaji, data);

                break;

            case "Cijena":
                int cijena = int.Parse(id);

                if (znak == "manje")
                {
                    natječaji = _context.Natječaj
                                .AsNoTracking()
                                .Where(n => n.Cijena < cijena)
                                .OrderBy(o => o.Id)
                                .ToList();
                    ispisi(natječaji, data);
                }
                else if (znak == "vise")
                {
                    natječaji = _context.Natječaj
                                .AsNoTracking()
                                .Where(n => n.Cijena > cijena)
                                .OrderBy(o => o.Id)
                                .ToList();
                    ispisi(natječaji, data);
                }
                else
                {
                    natječaji = _context.Natječaj
                                .AsNoTracking()
                                .Where(n => n.Cijena.Equals(cijena))
                                .OrderBy(o => o.Id)
                                .ToList();
                    ispisi(natječaji, data);
                }
                break;

            case "Poslodavac":
                natječaji = _context.Natječaj
                            .AsNoTracking()
                            .Where(n => n.Poslodavac.Equals(id))
                            .OrderBy(o => o.Id)
                            .ToList();
                ispisi(natječaji, data);

                break;

            case "Status":
                bool stanje = bool.Parse(id);
                natječaji = _context.Natječaj
                            .AsNoTracking()
                            .Where(n => n.Status.Equals(stanje))
                            .OrderBy(o => o.Id)
                            .ToList();
                ispisi(natječaji, data);

                break;

            case "Lokacija":
                natječaji = _context.Natječaj
                            .AsNoTracking()
                            .Where(n => n.Lokacija.Equals(id))
                            .OrderBy(o => o.Id)
                            .ToList();
                ispisi(natječaji, data);

                break;

            default:
                Console.WriteLine("Niste unijeli string");
                break;
            }



            //Add list to IEnumerable
            IEnumerable <object> dataTable = data;

            //Assign data source.
            pdfGrid.DataSource = dataTable;
            pdfGrid.Draw(page, new Syncfusion.Drawing.PointF(50, 50));

            doc.Save(stream);
            //If the position is not set to '0' then the PDF will be empty.
            stream.Position = 0;
            //Close the document.
            doc.Close(true);
            //Defining the ContentType for pdf file.
            string contentType = "application/pdf";
            //Define the file name.
            string fileName = "Natječaji-Reports.pdf";

            return(File(stream, contentType, fileName));
        }
コード例 #4
0
        /// <summary>
        /// Gets PDF file for invoice using Syncfusion.Pdf
        /// </summary>
        /// <param name="invoiceId">invoice Id for which file should be generated</param>
        /// <returns>PDF file for invoice</returns>
        public FileStreamResult GetInvoicePdf(string invoiceId)
        {
            //Declaring colors
            var black     = Color.FromArgb(255, 0, 0, 0);
            var white     = Color.FromArgb(255, 255, 255, 255);
            var lightGray = Color.FromArgb(255, 220, 220, 220);

            var model = InvoiceManager.GetInvoiceViewModelById(invoiceId);

            model.InvoiceLines = InvoiceManager.GetInvoiceLineViewModels(invoiceId);

            var invoiceNo      = model.InvoiceNumber;
            var invoiceHdrText = $"Faktura VAT: {invoiceNo}";

            var customer = model.Customer;

            var invoiceLines = model.InvoiceLines;

            var lightGrayBrush = new PdfSolidBrush(lightGray);

            //Creating new PDF document instance
            PdfDocument document = new PdfDocument();

            //Setting margin
            document.PageSettings.Margins.All = 20;

            //Adding a new page
            PdfPage     page = document.Pages.Add();
            PdfGraphics g    = page.Graphics;

            //Load the custom font as stream
            Stream fontStream = System.IO.File.OpenRead("wwwroot/fonts/Roboto-Regular.ttf");

            //Create a new PDF true type font.
            PdfTrueTypeFont customFont8      = new PdfTrueTypeFont(fontStream, 8, PdfFontStyle.Regular);
            PdfTrueTypeFont customFont8Bold  = new PdfTrueTypeFont(fontStream, 8, PdfFontStyle.Bold);
            PdfTrueTypeFont customFont10     = new PdfTrueTypeFont(fontStream, 10, PdfFontStyle.Regular);
            PdfTrueTypeFont customFont12     = new PdfTrueTypeFont(fontStream, 12, PdfFontStyle.Regular);
            PdfTrueTypeFont customFont12Bold = new PdfTrueTypeFont(fontStream, 12, PdfFontStyle.Bold);
            PdfTrueTypeFont customFont30     = new PdfTrueTypeFont(fontStream, 30, PdfFontStyle.Regular);

            //Creating font instances
            PdfFont headerFont = customFont30;
            //PdfFont standardText12 = new PdfStandardFont(PdfFontFamily.Helvetica, 12);
            PdfFont standardText12     = customFont12;
            PdfFont standardText12Bold = customFont12Bold;
            PdfFont standardText10     = customFont10;
            PdfFont standardText8      = customFont8;
            PdfFont standardText8Bold  = customFont8Bold;

            //Set page size
            document.PageSettings.Size = PdfPageSize.A4;

            //Set page orientation
            document.PageSettings.Orientation = PdfPageOrientation.Landscape;

            //pen
            var pen = new PdfPen(Color.Black)
            {
                Width = 0.5F
            };

            //Invoice number printing
            var invoiceNoRectangle = new Syncfusion.Drawing.RectangleF(180, 30, 200, 100);

            g.DrawRectangle(lightGrayBrush, invoiceNoRectangle);
            var result = BodyContent(invoiceHdrText, headerFont, black, PdfTextAlignment.Center, PdfVerticalAlignment.Middle, page, invoiceNoRectangle);

            //Customer data printing
            var customerDataHeaderRectangle  = new Syncfusion.Drawing.RectangleF(10, 200, 200, 15);
            var customerDataContentRectangle = new Syncfusion.Drawing.RectangleF(10, 215, 200, 100);

            g.DrawRectangle(lightGrayBrush, customerDataHeaderRectangle);
            result = BodyContent("Klient", standardText12Bold, black, PdfTextAlignment.Center, PdfVerticalAlignment.Middle, page, customerDataHeaderRectangle);

            g.DrawRectangle(pen, new PdfSolidBrush(white), customerDataContentRectangle);
            var rect = new RectangleF(12, 215, 200, 25);

            result = BodyContent(customer.Name, standardText10, black, PdfTextAlignment.Left, PdfVerticalAlignment.Top, page, rect);
            rect   = new RectangleF(12, result.Bounds.Bottom + 5, 200, 25);
            result = BodyContent($"{customer.Street}/{customer.BuildingNumber}", standardText10, black, PdfTextAlignment.Left, PdfVerticalAlignment.Top, page, rect);
            rect   = new RectangleF(12, result.Bounds.Bottom, 200, 25);
            result = BodyContent($"{customer.PostalCode} {customer.City}", standardText10, black, PdfTextAlignment.Left, PdfVerticalAlignment.Top, page, rect);

            //Dates data printing
            var datesX      = page.Graphics.ClientSize.Width - customerDataHeaderRectangle.Width - customerDataHeaderRectangle.X;
            var datesY      = customerDataHeaderRectangle.Y;
            var datesWidth  = customerDataHeaderRectangle.Width;
            var datesHeight = customerDataHeaderRectangle.Height;

            //issue date
            var issueDateHeaderRectangle      = new Syncfusion.Drawing.RectangleF(datesX, datesY, datesWidth, datesHeight);
            var issueDateDataContentRectangle = new Syncfusion.Drawing.RectangleF(datesX, datesY + datesHeight, datesWidth, 20);

            g.DrawRectangle(lightGrayBrush, issueDateHeaderRectangle);
            BodyContent("Data wydania", standardText12Bold, black, PdfTextAlignment.Center, PdfVerticalAlignment.Middle, page, issueDateHeaderRectangle);

            g.DrawRectangle(pen, new PdfSolidBrush(white), issueDateDataContentRectangle);
            result = BodyContent("2018-10-04", standardText10, black, PdfTextAlignment.Center, PdfVerticalAlignment.Middle, page, issueDateDataContentRectangle);

            //receipt date
            var receiptDateHeaderRectangle      = new Syncfusion.Drawing.RectangleF(datesX, result.Bounds.Bottom + 9, datesWidth, datesHeight);
            var receiptDateDataContentRectangle = new Syncfusion.Drawing.RectangleF(datesX, result.Bounds.Bottom + 9 + datesHeight, datesWidth, 20);

            g.DrawRectangle(lightGrayBrush, receiptDateHeaderRectangle);
            BodyContent("Data przyjęcia", standardText12Bold, black, PdfTextAlignment.Center, PdfVerticalAlignment.Middle, page, receiptDateHeaderRectangle);

            g.DrawRectangle(pen, new PdfSolidBrush(white), receiptDateDataContentRectangle);
            result = BodyContent("2018-10-04", standardText10, black, PdfTextAlignment.Center, PdfVerticalAlignment.Middle, page, receiptDateDataContentRectangle);

            //receipt date
            var paymentDateHeaderRectangle      = new Syncfusion.Drawing.RectangleF(datesX, result.Bounds.Bottom + 9, datesWidth, datesHeight);
            var paymentDateDataContentRectangle = new Syncfusion.Drawing.RectangleF(datesX, result.Bounds.Bottom + 9 + datesHeight, datesWidth, 20);

            g.DrawRectangle(lightGrayBrush, paymentDateHeaderRectangle);
            BodyContent("Data płatności", standardText12Bold, black, PdfTextAlignment.Center, PdfVerticalAlignment.Middle, page, paymentDateHeaderRectangle);

            g.DrawRectangle(pen, new PdfSolidBrush(white), paymentDateDataContentRectangle);
            result = BodyContent("2018-10-04", standardText10, black, PdfTextAlignment.Center, PdfVerticalAlignment.Middle, page, paymentDateDataContentRectangle);

            //adding grid for invoice lines
            PdfGrid grid = new PdfGrid();

            grid.Style.AllowHorizontalOverflow = false;

            //Set Data source for invoice lines
            if (invoiceLines.Count() == 0)
            {
                invoiceLines = new List <InvoiceLineViewModel>()
                {
                    new InvoiceLineViewModel()
                };
            }

            grid.DataSource = invoiceLines.Select(x => new
            {
                ItemName    = x.ItemName,
                Description = x.Description,
                Quantity    = x.Quantity,
                Price       = x.Price.ToString("N"),
                TaxRate     = x.TaxRate.ToString("##"),
                NetValue    = x.BaseNetto.ToString("N"),
                TaxValue    = x.BaseTax.ToString("N"),
                GrossValue  = x.BaseGross.ToString("N")
            });

            //grid styling
            var styleName = "GridTable4";
            PdfGridBuiltinStyleSettings setting = new PdfGridBuiltinStyleSettings();

            setting.ApplyStyleForHeaderRow     = false;
            setting.ApplyStyleForBandedRows    = false;
            setting.ApplyStyleForBandedColumns = false;
            setting.ApplyStyleForFirstColumn   = false;
            setting.ApplyStyleForLastColumn    = false;
            setting.ApplyStyleForLastRow       = false;

            var gridHeaderStyle = new PdfGridRowStyle()
            {
                BackgroundBrush = new PdfSolidBrush(lightGray),
                Font            = standardText8Bold
            };

            //Set layout properties
            PdfLayoutFormat format = new PdfLayoutFormat();

            format.Break  = PdfLayoutBreakType.FitElement;
            format.Layout = PdfLayoutType.Paginate;

            PdfGridBuiltinStyle style = (PdfGridBuiltinStyle)Enum.Parse(typeof(PdfGridBuiltinStyle), styleName);

            grid.ApplyBuiltinStyle(style, setting);
            grid.Style.Font                    = standardText8;
            grid.Style.CellPadding.All         = 2;
            grid.Style.AllowHorizontalOverflow = false;

            //set header texts
            PdfGridRow pdfGridHeader = grid.Headers[0];

            pdfGridHeader.Style          = gridHeaderStyle;
            pdfGridHeader.Cells[0].Value = "Kod pozycji";
            pdfGridHeader.Cells[1].Value = "Opis";
            pdfGridHeader.Cells[2].Value = "Ilość";
            pdfGridHeader.Cells[3].Value = "Cena";
            pdfGridHeader.Cells[4].Value = "VAT(%)";
            pdfGridHeader.Cells[5].Value = "Netto";
            pdfGridHeader.Cells[6].Value = "VAT";
            pdfGridHeader.Cells[7].Value = "Brutto";

            //grid location
            var gridStartLocation = new PointF(0, result.Bounds.Bottom + 50);

            //Draw table
            result = grid.Draw(page, gridStartLocation, format);

            var summaryByTaxRate = invoiceLines
                                   .GroupBy(x => x.TaxRate)
                                   .Select(x => new
            {
                TaxRate    = x.Key.ToString("##"),
                NetValue   = x.Sum(y => y.BaseNetto).ToString("N"),
                TaxValue   = x.Sum(y => y.BaseTax).ToString("N"),
                GrossValue = x.Sum(y => y.BaseGross).ToString("N")
            });

            //adding summary grid
            PdfGrid summaryGrid = new PdfGrid();

            summaryGrid.Style.AllowHorizontalOverflow = false;

            //Set Data source
            summaryGrid.DataSource = summaryByTaxRate;

            summaryGrid.ApplyBuiltinStyle(style, setting);
            summaryGrid.Style.Font                    = standardText8;
            summaryGrid.Style.CellPadding.All         = 2;
            summaryGrid.Style.AllowHorizontalOverflow = false;


            //set header texts
            PdfGridRow pdfSummaryGridHeader = summaryGrid.Headers[0];

            pdfSummaryGridHeader.Style          = gridHeaderStyle;
            pdfSummaryGridHeader.Cells[0].Value = "według stawki VAT";
            pdfSummaryGridHeader.Cells[1].Value = "wartość netto";
            pdfSummaryGridHeader.Cells[2].Value = "wartość VAT";
            pdfSummaryGridHeader.Cells[3].Value = "wartość brutto";

            summaryGrid.Columns[0].Width = 80;
            summaryGrid.Columns[1].Width = 60;
            summaryGrid.Columns[2].Width = 60;
            summaryGrid.Columns[3].Width = 60;

            //create summary total GridRow
            var summaryTotalNetto = invoiceLines.Sum(x => x.BaseNetto);
            var summaryTotalTax   = invoiceLines.Sum(x => x.BaseTax);
            var summaryTotalGross = invoiceLines.Sum(x => x.BaseGross);

            var summaryTotalRow = new PdfGridRow(summaryGrid);

            summaryGrid.Rows.Add(summaryTotalRow);
            summaryTotalRow.Cells[0].Value      = "Razem:";
            summaryTotalRow.Cells[0].Style.Font = customFont8Bold;
            summaryTotalRow.Cells[1].Value      = summaryTotalNetto.ToString("N");
            summaryTotalRow.Cells[2].Value      = summaryTotalTax.ToString("N");
            summaryTotalRow.Cells[3].Value      = summaryTotalGross.ToString("N");

            float columnsWidth = 0;

            foreach (PdfGridColumn column in summaryGrid.Columns)
            {
                columnsWidth += column.Width;
            }

            //Summary grid location
            var summaryGridStartLocation = new PointF(page.GetClientSize().Width - columnsWidth, result.Bounds.Bottom + 20);

            //Draw table
            result = summaryGrid.Draw(page, summaryGridStartLocation, format);

            //Saving the PDF to the MemoryStreamcolumnsWidth
            MemoryStream ms = new MemoryStream();

            document.Save(ms);

            //If the position is not set to '0' then the PDF will be empty.
            ms.Position = 0;

            var contentType = "application/pdf";
            var fileName    = $"{invoiceNo.Replace("/", "_")}_{DateHelper.GetCurrentDatetime():yyyyMMdd}.pdf";

            //Download the PDF document in the browser.
            FileStreamResult fileStreamResult = new FileStreamResult(ms, contentType);

            fileStreamResult.FileDownloadName = fileName;
            return(fileStreamResult);
        }