Beispiel #1
0
        /// <summary>
        /// Print product collection to PDF
        /// </summary>
        /// <param name="productCollection"></param>
        /// <param name="filePath"></param>
        public static void PrintProductsToPdf(List <Product> productCollection, string filePath)
        {
            if (String.IsNullOrEmpty(filePath))
            {
                throw new ArgumentNullException("filePath");
            }

            Document doc     = new Document();
            Section  section = doc.AddSection();

            int productNumber = 1;
            int prodCount     = productCollection.Count;

            foreach (var product in productCollection)
            {
                Paragraph p1 = section.AddParagraph(String.Format("{0}. {1}", productNumber, product.LocalizedName));
                p1.Format.Font.Bold  = true;
                p1.Format.Font.Color = Colors.Black;

                section.AddParagraph();

                section.AddParagraph(HtmlHelper.StripTags(HtmlHelper.ConvertHtmlToPlainText(product.LocalizedFullDescription)));

                section.AddParagraph();

                var pictures = PictureManager.GetPicturesByProductId(product.ProductId);
                if (pictures.Count > 0)
                {
                    Table table = section.AddTable();
                    table.Borders.Visible = false;

                    table.AddColumn(Unit.FromCentimeter(10));
                    table.AddColumn(Unit.FromCentimeter(10));

                    Row row = table.AddRow();
                    for (int i = 0; i < pictures.Count; i++)
                    {
                        int cellNum = i % 2;
                        var pic     = pictures[i];

                        if (pic != null && pic.LoadPictureBinary() != null && pic.LoadPictureBinary().Length > 0)
                        {
                            row.Cells[cellNum].AddImage(PictureManager.GetPictureLocalPath(pic, 200, true));
                        }

                        if (i != 0 && i % 2 == 0)
                        {
                            row = table.AddRow();
                        }
                    }

                    section.AddParagraph();
                }

                int pvNum = 1;

                foreach (var productVariant in product.ProductVariants)
                {
                    string pvName = String.IsNullOrEmpty(productVariant.LocalizedName) ? LocalizationManager.GetLocaleResourceString("PDFProductCatalog.UnnamedProductVariant") : productVariant.LocalizedName;
                    section.AddParagraph(String.Format("{0}.{1}. {2}", productNumber, pvNum, pvName));

                    section.AddParagraph();

                    if (!String.IsNullOrEmpty(productVariant.LocalizedDescription))
                    {
                        section.AddParagraph(HtmlHelper.StripTags(HtmlHelper.ConvertHtmlToPlainText(productVariant.LocalizedDescription)));
                        section.AddParagraph();
                    }

                    var pic = productVariant.Picture;
                    if (pic != null && pic.LoadPictureBinary() != null && pic.LoadPictureBinary().Length > 0)
                    {
                        section.AddImage(PictureManager.GetPictureLocalPath(pic, 200, true));
                    }

                    section.AddParagraph(String.Format("{0}: {1} {2}", LocalizationManager.GetLocaleResourceString("PDFProductCatalog.Price"), productVariant.Price, CurrencyManager.PrimaryStoreCurrency.CurrencyCode));
                    section.AddParagraph(String.Format("{0}: {1}", LocalizationManager.GetLocaleResourceString("PDFProductCatalog.SKU"), productVariant.SKU));

                    if (productVariant.Weight > Decimal.Zero)
                    {
                        section.AddParagraph(String.Format("{0}: {1} {2}", LocalizationManager.GetLocaleResourceString("PDFProductCatalog.Weight"), productVariant.Weight, MeasureManager.BaseWeightIn.Name));
                    }

                    if (productVariant.ManageInventory == (int)ManageInventoryMethodEnum.ManageStock)
                    {
                        section.AddParagraph(String.Format("{0}: {1}", LocalizationManager.GetLocaleResourceString("PDFProductCatalog.StockQuantity"), productVariant.StockQuantity));
                    }

                    section.AddParagraph();

                    pvNum++;
                }

                productNumber++;

                if (productNumber <= prodCount)
                {
                    section.AddPageBreak();
                }
            }

            PdfDocumentRenderer renderer = new PdfDocumentRenderer(true, PdfSharp.Pdf.PdfFontEmbedding.Always);

            renderer.Document = doc;
            renderer.RenderDocument();
            renderer.PdfDocument.Save(filePath);
        }