protected void rptrTaxRates_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                var item = (KeyValuePair <decimal, decimal>)e.Item.DataItem;

                var lTaxRateTitle = e.Item.FindControl("lTaxRateTitle") as Literal;
                lTaxRateTitle.Text = String.Format(GetLocaleResourceString("Order.Totals.TaxRate"), PriceHelper.FormatTaxRate(item.Key));

                var lTaxRateValue = e.Item.FindControl("lTaxRateValue") as Literal;
                lTaxRateValue.Text = PriceHelper.FormatPrice(item.Value, true, order.CustomerCurrencyCode, false);
            }
        }
        /// <summary>
        /// Print an order to PDF
        /// </summary>
        /// <param name="order">Order</param>
        /// <param name="languageId">Language identifier</param>
        /// <param name="filePath">File path</param>
        public static void PrintOrderToPdf(Order order, int languageId, string filePath)
        {
            if (order == null)
            {
                throw new ArgumentNullException("order");
            }

            if (String.IsNullOrEmpty(filePath))
            {
                throw new ArgumentNullException("filePath");
            }

            Language lang = IoC.Resolve <ILanguageService>().GetLanguageById(languageId);

            if (lang == null)
            {
                throw new NopException("Language could not be loaded");
            }

            var localizationManager = IoC.Resolve <ILocalizationManager>();

            Document doc = new Document();

            Section sec = doc.AddSection();

            Table table = sec.AddTable();

            table.Borders.Visible = false;

            bool logoExists = File.Exists(PDFHelper.LogoFilePath);

            table.AddColumn(Unit.FromCentimeter(10));
            if (logoExists)
            {
                table.AddColumn(Unit.FromCentimeter(10));
            }

            Row ordRow = table.AddRow();

            int       rownum = logoExists ? 1 : 0;
            Paragraph p1     = ordRow[rownum].AddParagraph(String.Format(localizationManager.GetLocaleResourceString("PDFInvoice.Order#", languageId), order.OrderId));

            p1.Format.Font.Bold  = true;
            p1.Format.Font.Color = Colors.Black;
            ordRow[rownum].AddParagraph(IoC.Resolve <ISettingManager>().StoreUrl.Trim(new char[] { '/' })).AddHyperlink(IoC.Resolve <ISettingManager>().StoreUrl, HyperlinkType.Url);
            ordRow[rownum].AddParagraph(String.Format(localizationManager.GetLocaleResourceString("PDFInvoice.OrderDate", languageId), DateTimeHelper.ConvertToUserTime(order.CreatedOn, DateTimeKind.Utc).ToString("D")));

            if (File.Exists(PDFHelper.LogoFilePath))
            {
                ordRow[0].AddImage(PDFHelper.LogoFilePath);
            }


            var addressTable = sec.AddTable();

            if (order.ShippingStatus != ShippingStatusEnum.ShippingNotRequired)
            {
                addressTable.AddColumn(Unit.FromCentimeter(9));
                addressTable.AddColumn(Unit.FromCentimeter(9));
            }
            else
            {
                addressTable.AddColumn(Unit.FromCentimeter(18));
            }
            addressTable.Borders.Visible = false;
            Row row = addressTable.AddRow();

            //billing info
            row.Cells[0].AddParagraph();
            Paragraph p2 = row.Cells[0].AddParagraph(localizationManager.GetLocaleResourceString("PDFInvoice.BillingInformation", languageId));

            p2.Format.Font.Bold  = true;
            p2.Format.Font.Color = Colors.Black;


            if (!String.IsNullOrEmpty(order.BillingCompany))
            {
                row.Cells[0].AddParagraph("   " + String.Format(localizationManager.GetLocaleResourceString("PDFInvoice.Company", languageId), order.BillingCompany));
            }
            row.Cells[0].AddParagraph("   " + String.Format(localizationManager.GetLocaleResourceString("PDFInvoice.Name", languageId), order.BillingFullName));
            row.Cells[0].AddParagraph("   " + String.Format(localizationManager.GetLocaleResourceString("PDFInvoice.Phone", languageId), order.BillingPhoneNumber));
            if (!String.IsNullOrEmpty(order.BillingFaxNumber))
            {
                row.Cells[0].AddParagraph("   " + String.Format(localizationManager.GetLocaleResourceString("PDFInvoice.Fax", languageId), order.BillingFaxNumber));
            }
            row.Cells[0].AddParagraph("   " + String.Format(localizationManager.GetLocaleResourceString("PDFInvoice.Address", languageId), order.BillingAddress1));
            if (!String.IsNullOrEmpty(order.BillingAddress2))
            {
                row.Cells[0].AddParagraph("   " + String.Format(localizationManager.GetLocaleResourceString("PDFInvoice.Address2", languageId), order.BillingAddress2));
            }
            row.Cells[0].AddParagraph("   " + String.Format("{0}, {1}", order.BillingCountry, order.BillingStateProvince));
            row.Cells[0].AddParagraph("   " + String.Format("{0}, {1}", order.BillingCity, order.BillingZipPostalCode));
            //VAT number
            if (!String.IsNullOrEmpty(order.VatNumber))
            {
                row.Cells[0].AddParagraph("   " + String.Format(localizationManager.GetLocaleResourceString("PDFInvoice.VATNumber", languageId), order.VatNumber));
            }
            row.Cells[0].AddParagraph();

            //shipping info
            if (order.ShippingStatus != ShippingStatusEnum.ShippingNotRequired)
            {
                row.Cells[1].AddParagraph();
                Paragraph p3 = row.Cells[1].AddParagraph(localizationManager.GetLocaleResourceString("PDFInvoice.ShippingInformation", languageId));
                p3.Format.Font.Bold  = true;
                p3.Format.Font.Color = Colors.Black;

                if (!String.IsNullOrEmpty(order.ShippingCompany))
                {
                    row.Cells[1].AddParagraph("   " + String.Format(localizationManager.GetLocaleResourceString("PDFInvoice.Company", languageId), order.ShippingCompany));
                }
                row.Cells[1].AddParagraph("   " + String.Format(localizationManager.GetLocaleResourceString("PDFInvoice.Name", languageId), order.ShippingFullName));
                row.Cells[1].AddParagraph("   " + String.Format(localizationManager.GetLocaleResourceString("PDFInvoice.Phone", languageId), order.ShippingPhoneNumber));
                if (!String.IsNullOrEmpty(order.ShippingFaxNumber))
                {
                    row.Cells[1].AddParagraph("   " + String.Format(localizationManager.GetLocaleResourceString("PDFInvoice.Fax", languageId), order.ShippingFaxNumber));
                }
                row.Cells[1].AddParagraph("   " + String.Format(localizationManager.GetLocaleResourceString("PDFInvoice.Address", languageId), order.ShippingAddress1));
                if (!String.IsNullOrEmpty(order.ShippingAddress2))
                {
                    row.Cells[1].AddParagraph("   " + String.Format(localizationManager.GetLocaleResourceString("PDFInvoice.Address2", languageId), order.ShippingAddress2));
                }
                row.Cells[1].AddParagraph("   " + String.Format("{0}, {1}", order.ShippingCountry, order.ShippingStateProvince));
                row.Cells[1].AddParagraph("   " + String.Format("{0}, {1}", order.ShippingCity, order.ShippingZipPostalCode));
                row.Cells[1].AddParagraph();
                row.Cells[1].AddParagraph("   " + String.Format(localizationManager.GetLocaleResourceString("PDFInvoice.ShippingMethod", languageId), order.ShippingMethod));
                row.Cells[1].AddParagraph();
            }

            sec.AddParagraph();


            //products
            Paragraph p4 = sec.AddParagraph(localizationManager.GetLocaleResourceString("PDFInvoice.Product(s)", languageId));

            p4.Format.Font.Bold  = true;
            p4.Format.Font.Color = Colors.Black;

            sec.AddParagraph();

            var productCollection = order.OrderProductVariants;
            var tbl = sec.AddTable();

            tbl.Borders.Visible = true;
            tbl.Borders.Width   = 1;

            tbl.AddColumn(Unit.FromCentimeter(8));
            tbl.AddColumn(Unit.FromCentimeter(4));
            tbl.AddColumn(Unit.FromCentimeter(2));
            tbl.AddColumn(Unit.FromCentimeter(4));

            Row header = tbl.AddRow();

            header.Cells[0].AddParagraph(localizationManager.GetLocaleResourceString("PDFInvoice.ProductName", languageId));
            header.Cells[0].Format.Alignment = ParagraphAlignment.Center;

            header.Cells[1].AddParagraph(localizationManager.GetLocaleResourceString("PDFInvoice.ProductPrice", languageId));
            header.Cells[1].Format.Alignment = ParagraphAlignment.Center;

            header.Cells[2].AddParagraph(localizationManager.GetLocaleResourceString("PDFInvoice.ProductQuantity", languageId));
            header.Cells[2].Format.Alignment = ParagraphAlignment.Center;

            header.Cells[3].AddParagraph(localizationManager.GetLocaleResourceString("PDFInvoice.ProductTotal", languageId));
            header.Cells[3].Format.Alignment = ParagraphAlignment.Center;

            for (int i = 0; i < productCollection.Count; i++)
            {
                var orderProductVariant = productCollection[i];
                Row prodRow             = tbl.AddRow();

                string name = String.Format("Not available. Id={0}", orderProductVariant.ProductVariantId);
                var    pv   = IoC.Resolve <IProductService>().GetProductVariantById(orderProductVariant.ProductVariantId);
                if (pv != null)
                {
                    name = pv.GetLocalizedFullProductName(languageId);
                }

                prodRow.Cells[0].AddParagraph(name);
                Paragraph p5 = prodRow.Cells[0].AddParagraph(HtmlHelper.ConvertHtmlToPlainText(orderProductVariant.AttributeDescription, true));
                p5.Format.Font.Italic             = true;
                prodRow.Cells[0].Format.Alignment = ParagraphAlignment.Left;

                string unitPrice = string.Empty;
                switch (order.CustomerTaxDisplayType)
                {
                case TaxDisplayTypeEnum.ExcludingTax:
                    unitPrice = PriceHelper.FormatPrice(orderProductVariant.UnitPriceExclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, lang, false);
                    break;

                case TaxDisplayTypeEnum.IncludingTax:
                    unitPrice = PriceHelper.FormatPrice(orderProductVariant.UnitPriceInclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, lang, true);
                    break;
                }

                prodRow.Cells[1].AddParagraph(unitPrice);

                prodRow.Cells[2].AddParagraph(orderProductVariant.Quantity.ToString());

                string subTotal = string.Empty;
                switch (order.CustomerTaxDisplayType)
                {
                case TaxDisplayTypeEnum.ExcludingTax:
                    subTotal = PriceHelper.FormatPrice(orderProductVariant.PriceExclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, lang, false);
                    break;

                case TaxDisplayTypeEnum.IncludingTax:
                    subTotal = PriceHelper.FormatPrice(orderProductVariant.PriceInclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, lang, true);
                    break;
                }
                prodRow.Cells[3].AddParagraph(subTotal);
            }

            //checkout attributes
            if (!String.IsNullOrEmpty(order.CheckoutAttributeDescription))
            {
                sec.AddParagraph();
                Paragraph pCheckoutAttributes = null;
                string    attributes          = HtmlHelper.ConvertHtmlToPlainText(order.CheckoutAttributeDescription, true);
                pCheckoutAttributes = sec.AddParagraph(attributes);
                if (pCheckoutAttributes != null)
                {
                    pCheckoutAttributes.Format.Alignment = ParagraphAlignment.Right;
                }
            }

            //subtotal
            sec.AddParagraph();
            Paragraph p6 = null;

            switch (order.CustomerTaxDisplayType)
            {
            case TaxDisplayTypeEnum.ExcludingTax:
            {
                string orderSubtotalExclTaxStr = PriceHelper.FormatPrice(order.OrderSubtotalExclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, lang, false);
                p6 = sec.AddParagraph(String.Format("{0} {1}", localizationManager.GetLocaleResourceString("PDFInvoice.Sub-Total", languageId), orderSubtotalExclTaxStr));
            }
            break;

            case TaxDisplayTypeEnum.IncludingTax:
            {
                string orderSubtotalInclTaxStr = PriceHelper.FormatPrice(order.OrderSubtotalInclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, lang, true);
                p6 = sec.AddParagraph(String.Format("{0} {1}", localizationManager.GetLocaleResourceString("PDFInvoice.Sub-Total", languageId), orderSubtotalInclTaxStr));
            }
            break;
            }
            if (p6 != null)
            {
                p6.Format.Alignment = ParagraphAlignment.Right;
            }
            //discount (applied to order subtotal)
            if (order.OrderSubTotalDiscountExclTaxInCustomerCurrency > decimal.Zero)
            {
                switch (order.CustomerTaxDisplayType)
                {
                case TaxDisplayTypeEnum.ExcludingTax:
                {
                    string    orderSubTotalDiscountInCustomerCurrencyStr = PriceHelper.FormatPrice(-order.OrderSubTotalDiscountExclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, lang, false);
                    Paragraph p6a = sec.AddParagraph(String.Format("{0} {1}", localizationManager.GetLocaleResourceString("PDFInvoice.Discount", languageId), orderSubTotalDiscountInCustomerCurrencyStr));
                    p6a.Format.Alignment = ParagraphAlignment.Right;
                }
                break;

                case TaxDisplayTypeEnum.IncludingTax:
                {
                    string    orderSubTotalDiscountInCustomerCurrencyStr = PriceHelper.FormatPrice(-order.OrderSubTotalDiscountInclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, lang, true);
                    Paragraph p6a = sec.AddParagraph(String.Format("{0} {1}", localizationManager.GetLocaleResourceString("PDFInvoice.Discount", languageId), orderSubTotalDiscountInCustomerCurrencyStr));
                    p6a.Format.Alignment = ParagraphAlignment.Right;
                }
                break;
                }
            }

            //shipping
            if (order.ShippingStatus != ShippingStatusEnum.ShippingNotRequired)
            {
                Paragraph p9 = null;
                switch (order.CustomerTaxDisplayType)
                {
                case TaxDisplayTypeEnum.ExcludingTax:
                {
                    string orderShippingExclTaxStr = PriceHelper.FormatShippingPrice(order.OrderShippingExclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, lang, false);
                    p9 = sec.AddParagraph(String.Format("{0} {1}", localizationManager.GetLocaleResourceString("PDFInvoice.Shipping", languageId), orderShippingExclTaxStr));
                }
                break;

                case TaxDisplayTypeEnum.IncludingTax:
                {
                    string orderShippingInclTaxStr = PriceHelper.FormatShippingPrice(order.OrderShippingInclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, lang, true);
                    p9 = sec.AddParagraph(String.Format("{0} {1}", localizationManager.GetLocaleResourceString("PDFInvoice.Shipping", languageId), orderShippingInclTaxStr));
                }
                break;
                }

                if (p9 != null)
                {
                    p9.Format.Alignment = ParagraphAlignment.Right;
                }
            }

            //payment fee
            if (order.PaymentMethodAdditionalFeeExclTaxInCustomerCurrency > decimal.Zero)
            {
                Paragraph p10 = null;
                switch (order.CustomerTaxDisplayType)
                {
                case TaxDisplayTypeEnum.ExcludingTax:
                {
                    string paymentMethodAdditionalFeeExclTaxStr = PriceHelper.FormatPaymentMethodAdditionalFee(order.PaymentMethodAdditionalFeeExclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, lang, false);
                    p10 = sec.AddParagraph(String.Format("{0} {1}", localizationManager.GetLocaleResourceString("PDFInvoice.PaymentMethodAdditionalFee", languageId), paymentMethodAdditionalFeeExclTaxStr));
                }
                break;

                case TaxDisplayTypeEnum.IncludingTax:
                {
                    string paymentMethodAdditionalFeeInclTaxStr = PriceHelper.FormatPaymentMethodAdditionalFee(order.PaymentMethodAdditionalFeeInclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, lang, true);
                    p10 = sec.AddParagraph(String.Format("{0} {1}", localizationManager.GetLocaleResourceString("PDFInvoice.PaymentMethodAdditionalFee", languageId), paymentMethodAdditionalFeeInclTaxStr));
                }
                break;
                }
                if (p10 != null)
                {
                    p10.Format.Alignment = ParagraphAlignment.Right;
                }
            }

            //tax
            string taxStr = string.Empty;
            SortedDictionary <decimal, decimal> taxRates = new SortedDictionary <decimal, decimal>();
            bool displayTax      = true;
            bool displayTaxRates = true;

            if (IoC.Resolve <ITaxService>().HideTaxInOrderSummary&& order.CustomerTaxDisplayType == TaxDisplayTypeEnum.IncludingTax)
            {
                displayTax = false;
            }
            else
            {
                if (order.OrderTax == 0 && IoC.Resolve <ITaxService>().HideZeroTax)
                {
                    displayTax      = false;
                    displayTaxRates = false;
                }
                else
                {
                    taxRates = order.TaxRatesDictionaryInCustomerCurrency;

                    displayTaxRates = IoC.Resolve <ITaxService>().DisplayTaxRates&& taxRates.Count > 0;
                    displayTax      = !displayTaxRates;

                    taxStr = PriceHelper.FormatPrice(order.OrderTaxInCustomerCurrency, true, order.CustomerCurrencyCode, false);
                }
            }
            if (displayTax)
            {
                var p11 = sec.AddParagraph(String.Format("{0} {1}", localizationManager.GetLocaleResourceString("PDFInvoice.Tax", languageId), taxStr));
                p11.Format.Alignment = ParagraphAlignment.Right;
            }
            if (displayTaxRates)
            {
                foreach (var item in taxRates)
                {
                    string taxRate  = String.Format(localizationManager.GetLocaleResourceString("PDFInvoice.Totals.TaxRate"), PriceHelper.FormatTaxRate(item.Key));
                    string taxValue = PriceHelper.FormatPrice(item.Value, true, false);

                    var p13 = sec.AddParagraph(String.Format("{0} {1}", taxRate, taxValue));
                    p13.Format.Alignment = ParagraphAlignment.Right;
                }
            }

            //discount (applied to order total)
            if (order.OrderDiscountInCustomerCurrency > decimal.Zero)
            {
                string    orderDiscountInCustomerCurrencyStr = PriceHelper.FormatPrice(-order.OrderDiscountInCustomerCurrency, true, order.CustomerCurrencyCode, false);
                Paragraph p7 = sec.AddParagraph(String.Format("{0} {1}", localizationManager.GetLocaleResourceString("PDFInvoice.Discount", languageId), orderDiscountInCustomerCurrencyStr));
                p7.Format.Alignment = ParagraphAlignment.Right;
            }

            //gift cards
            var gcuhC = IoC.Resolve <IOrderService>().GetAllGiftCardUsageHistoryEntries(null, null, order.OrderId);

            foreach (var giftCardUsageHistory in gcuhC)
            {
                string    gcTitle     = string.Format(localizationManager.GetLocaleResourceString("PDFInvoice.GiftCardInfo", languageId), giftCardUsageHistory.GiftCard.GiftCardCouponCode);
                string    gcAmountStr = PriceHelper.FormatPrice(-giftCardUsageHistory.UsedValueInCustomerCurrency, true, order.CustomerCurrencyCode, false);
                Paragraph p8          = sec.AddParagraph(String.Format("{0} {1}", gcTitle, gcAmountStr));
                p8.Format.Alignment = ParagraphAlignment.Right;
            }

            //reward points
            if (order.RedeemedRewardPoints != null)
            {
                string rpTitle  = string.Format(localizationManager.GetLocaleResourceString("PDFInvoice.RewardPoints", languageId), -order.RedeemedRewardPoints.Points);
                string rpAmount = PriceHelper.FormatPrice(-order.RedeemedRewardPoints.UsedAmountInCustomerCurrency, true, order.CustomerCurrencyCode, false);

                var p11 = sec.AddParagraph(String.Format("{0} {1}", rpTitle, rpAmount));
                p11.Format.Alignment = ParagraphAlignment.Right;
            }

            //order total
            string orderTotalStr = PriceHelper.FormatPrice(order.OrderTotalInCustomerCurrency, true, order.CustomerCurrencyCode, false);
            var    p12           = sec.AddParagraph(String.Format("{0} {1}", localizationManager.GetLocaleResourceString("PDFInvoice.OrderTotal", languageId), orderTotalStr));

            p12.Format.Font.Bold  = true;
            p12.Format.Font.Color = Colors.Black;
            p12.Format.Alignment  = ParagraphAlignment.Right;

            //order notes
            bool printOrderNotesToPdf = IoC.Resolve <ISettingManager>().GetSettingValueBoolean("PDFInvoice.RenderOrderNotes");

            if (printOrderNotesToPdf)
            {
                var orderNotes = IoC.Resolve <IOrderService>().GetOrderNoteByOrderId(order.OrderId, false);
                if (orderNotes.Count > 0)
                {
                    Paragraph p14 = sec.AddParagraph(localizationManager.GetLocaleResourceString("PDFInvoice.OrderNotes", languageId));
                    p14.Format.Font.Bold  = true;
                    p14.Format.Font.Color = Colors.Black;

                    sec.AddParagraph();

                    var tbl1 = sec.AddTable();

                    tbl1.Borders.Visible = true;
                    tbl1.Borders.Width   = 1;

                    tbl1.AddColumn(Unit.FromCentimeter(6));
                    tbl1.AddColumn(Unit.FromCentimeter(12));

                    Row header1 = tbl1.AddRow();

                    header1.Cells[0].AddParagraph(localizationManager.GetLocaleResourceString("PDFInvoice.OrderNotes.CreatedOn", languageId));
                    header1.Cells[0].Format.Alignment = ParagraphAlignment.Center;

                    header1.Cells[1].AddParagraph(localizationManager.GetLocaleResourceString("PDFInvoice.OrderNotes.Note", languageId));
                    header1.Cells[1].Format.Alignment = ParagraphAlignment.Center;

                    foreach (var orderNote in orderNotes)
                    {
                        Row noteRow = tbl1.AddRow();

                        noteRow.Cells[0].AddParagraph(DateTimeHelper.ConvertToUserTime(orderNote.CreatedOn, DateTimeKind.Utc).ToString());
                        noteRow.Cells[1].AddParagraph(HtmlHelper.ConvertHtmlToPlainText(orderNote.FormatOrderNoteText(), true));
                    }
                }
            }

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

            renderer.Document = doc;
            renderer.RenderDocument();
            renderer.PdfDocument.Save(filePath);
        }
        protected void rptrTaxRates_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                var item = (KeyValuePair <decimal, decimal>)e.Item.DataItem;

                var lTaxRateTitle = e.Item.FindControl("lTaxRateTitle") as Literal;
                lTaxRateTitle.Text = String.Format(GetLocaleResourceString("ShoppingCart.Totals.TaxRate"), PriceHelper.FormatTaxRate(item.Key));

                var     lTaxRateValue = e.Item.FindControl("lTaxRateValue") as Literal;
                decimal taxValue      = this.CurrencyService.ConvertCurrency(item.Value, this.CurrencyService.PrimaryStoreCurrency, NopContext.Current.WorkingCurrency);
                lTaxRateValue.Text = PriceHelper.FormatPrice(taxValue, true, false);
            }
        }