Пример #1
0
        public FileResult DownloadRecipe(int recipeId)
        {
            Recipe        recipe        = _recipeProvider.GetRecipeById(recipeId);
            Category      category      = _categoryProvider.GetCategoryById(recipe.CategoryId);
            RecipeDetails recipeDetails = _recipeProvider.GetRecipeDetailsByRecipeId(recipeId);
            IEnumerable <RecipeIngridientView> recipeIngridients = _recipeProvider.GetRecipeIngridientsByRecipeId(recipeId);

            string       fileName     = recipe.Name + ".pdf";
            Document     document     = new Document();
            MemoryStream memoryStream = new MemoryStream();
            PdfWriter    pdfWriter    = PdfWriter.GetInstance(document, memoryStream);

            pdfWriter.CloseStream = false;
            document.Open();

            BaseFont baseFont       = BaseFont.CreateFont(@"C:\Windows\Fonts\arial.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            Font     fontDefault    = new Font(baseFont, 16, Font.NORMAL);
            Font     fontMainHeader = new Font(baseFont, 24, Font.BOLD);
            Font     fontHeader     = new Font(baseFont, 20, Font.NORMAL);

            Paragraph par = new Paragraph(recipe.Name, fontMainHeader);

            par.Alignment = Element.ALIGN_CENTER;
            document.Add(par);

            par           = new Paragraph(category.Name, fontHeader);
            par.Alignment = Element.ALIGN_CENTER;
            document.Add(par);

            if (recipeDetails.Description != null && recipeDetails.Description != "")
            {
                par           = new Paragraph("Description", fontHeader);
                par.Alignment = Element.ALIGN_CENTER;
                document.Add(par);
                par           = new Paragraph(recipeDetails.Description, fontDefault);
                par.Alignment = Element.ALIGN_CENTER;
                document.Add(par);
            }

            Image image;

            if (recipe.ImageUrl != null && recipe.ImageUrl != "")
            {
                image = Image.GetInstance(recipe.ImageUrl);
            }
            else
            {
                image = Image.GetInstance(@"E:\GitRepos\CookBookRep\CookBook\CookBook.Web\Content\NoImage.png");
            }
            image.Alignment   = Element.ALIGN_CENTER;
            image.Border      = Rectangle.BOX;
            image.BorderColor = BaseColor.BLACK;
            image.BorderWidth = 3f;
            image.ScaleAbsolute(400f, 300f);
            document.Add(image);

            par = new Paragraph("\n");
            document.Add(par);

            PdfPTable table = new PdfPTable(2);

            PdfPCell cell = new PdfPCell(new Phrase("Cooking Time", fontHeader));

            cell.BackgroundColor     = BaseColor.LIGHT_GRAY;
            cell.HorizontalAlignment = 1;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase("Cooking Temperature", fontHeader));
            cell.BackgroundColor     = BaseColor.LIGHT_GRAY;
            cell.HorizontalAlignment = 1;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase(recipeDetails.CookingTime, fontDefault));
            cell.HorizontalAlignment = 1;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase(recipeDetails.CookingTemperature, fontDefault));
            cell.HorizontalAlignment = 1;
            table.AddCell(cell);

            document.Add(table);

            par           = new Paragraph("Ingridients", fontHeader);
            par.Alignment = Element.ALIGN_CENTER;
            document.Add(par);

            par = new Paragraph("\n");
            document.Add(par);

            table = new PdfPTable(2);

            cell = new PdfPCell(new Phrase("Ingridient", fontHeader));
            cell.BackgroundColor     = BaseColor.LIGHT_GRAY;
            cell.HorizontalAlignment = 1;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase("Weight", fontHeader));
            cell.BackgroundColor     = BaseColor.LIGHT_GRAY;
            cell.HorizontalAlignment = 1;
            table.AddCell(cell);

            foreach (RecipeIngridientView item in recipeIngridients)
            {
                cell = new PdfPCell(new Phrase(item.Name, fontDefault));
                cell.HorizontalAlignment = 1;
                table.AddCell(cell);

                cell = new PdfPCell(new Phrase(item.Weight, fontDefault));
                cell.HorizontalAlignment = 1;
                table.AddCell(cell);
            }

            document.Add(table);

            par           = new Paragraph("Sequencing", fontHeader);
            par.Alignment = Element.ALIGN_CENTER;
            document.Add(par);

            par           = new Paragraph(recipeDetails.Sequencing, fontDefault);
            par.Alignment = Element.ALIGN_JUSTIFIED;
            document.Add(par);

            document.Close();
            memoryStream.Flush();
            memoryStream.Position = 0;

            return(File(memoryStream, "application/pdf", fileName));
        }