how to format PDF Invoice ?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
14 年 前
Hi, this drive me crazy.  I have not clue why it does not let me build or rebuild.  62 erros with metadata are there unless I leave it the way it is, without added codes.
Plz help.
14 年 前
anthny + crholliday

are both of you starting off with the same original PDFHelper code?

also, anthny did you copy the code from this thread - i have found ( maybe others experience is different) when i copy
code encapsulated like this, 
it pastes in one big long line rather than with the line breaks.

if this is the case for you, have you gone over every line to make sure your line breaks are all in the correct place?

;

- hayden
14 年 前
Hayden and crholliday,
I use the original pdfhelper code and add on the code that Holliday shows me.  
yes, the code is well put in straight line with ; at the end.
I don't understand that every time I add it in, it does not let me to build or rebuild.  it gives me all kind of metadata errors.
Tony
14 年 前
and did crholliday also use the same code?

also, is the code you are inserting in the correct position of the page? ie, not to near the top (just guesswork here !)
14 年 前
Anthony can you post the contents of the whole method?
14 年 前
haydie, First, I thought it was simple, I should use the orignal pdfhelp and add a simple code as I found on http://itextsharp.sourceforge.net/examples/Chap0601.cs
I added below
doc.Open();
Image gif = Image.getInstance(new Uri("http://www.something.com/images/logo.gif"));
document.Add(gif);
Result I get: errors...

With Holliday's help, I took out the above line and add his in.
Result I get: errors...

I even did a guess work and tried with team's codes and add on Holliday's code.
Result I get: errors...

2 main area of errors: one is Uri related, other one is metadata.

Haydie, I am not a programmer, I am learnning..
I am very thankful for your and Holliday's help.
Tony
14 年 前
Just see your post, Holliday.
here is original pdfhelper.cs


    public partial class PDFHelper
    {
        #region Methods

        /// <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");

            Document doc = new Document();

            PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(FilePath, FileMode.Create));
            doc.Open();


            This is where I put the one I found or yours

            doc.Add(new Paragraph(String.Format(LocalizationManager.GetLocaleResourceString("PDFInvoice.Order#", LanguageID), order.OrderID)));
            doc.Add(new Paragraph(String.Format(LocalizationManager.GetLocaleResourceString("PDFInvoice.OrderDate", LanguageID), order.CreatedOn)));
            
            //billing info
            doc.Add(new Paragraph(" "));
            doc.Add(new Paragraph(LocalizationManager.GetLocaleResourceString("PDFInvoice.BillingInformation", LanguageID)));
            doc.Add(new Paragraph("   " + String.Format(LocalizationManager.GetLocaleResourceString("PDFInvoice.Name", LanguageID), order.BillingFullName)));
            doc.Add(new Paragraph("   " + String.Format(LocalizationManager.GetLocaleResourceString("PDFInvoice.Phone", LanguageID), order.BillingPhoneNumber)));
            if (!String.IsNullOrEmpty(order.BillingFaxNumber))
                doc.Add(new Paragraph("   " + String.Format(LocalizationManager.GetLocaleResourceString("PDFInvoice.Fax", LanguageID), order.BillingFaxNumber)));
            doc.Add(new Paragraph("   " + String.Format(LocalizationManager.GetLocaleResourceString("PDFInvoice.Address", LanguageID), order.BillingAddress1)));
            if (!String.IsNullOrEmpty(order.BillingAddress2))
                doc.Add(new Paragraph("   " + String.Format(LocalizationManager.GetLocaleResourceString("PDFInvoice.Address2", LanguageID), order.BillingAddress2)));
            doc.Add(new Paragraph("   " + String.Format("{0}, {1}", order.BillingCountry, order.BillingStateProvince)));
            doc.Add(new Paragraph("   " + String.Format("{0}, {1}", order.BillingCity, order.BillingZipPostalCode)));
            doc.Add(new Paragraph(" "));

            //shipping info
            if (order.ShippingStatus != ShippingStatusEnum.ShippingNotRequired)
            {
                doc.Add(new Paragraph(" "));
                doc.Add(new Paragraph(LocalizationManager.GetLocaleResourceString("PDFInvoice.ShippingInformation", LanguageID)));
                doc.Add(new Paragraph("   " + String.Format(LocalizationManager.GetLocaleResourceString("PDFInvoice.Name", LanguageID), order.ShippingFullName)));
                doc.Add(new Paragraph("   " + String.Format(LocalizationManager.GetLocaleResourceString("PDFInvoice.Phone", LanguageID), order.ShippingPhoneNumber)));
                if (!String.IsNullOrEmpty(order.ShippingFaxNumber))
                    doc.Add(new Paragraph("   " + String.Format(LocalizationManager.GetLocaleResourceString("PDFInvoice.Fax", LanguageID), order.ShippingFaxNumber)));
                doc.Add(new Paragraph("   " + String.Format(LocalizationManager.GetLocaleResourceString("PDFInvoice.Address", LanguageID), order.ShippingAddress1)));
                if (!String.IsNullOrEmpty(order.ShippingAddress2))
                    doc.Add(new Paragraph("   " + String.Format(LocalizationManager.GetLocaleResourceString("PDFInvoice.Address2", LanguageID), order.ShippingAddress2)));
                doc.Add(new Paragraph("   " + String.Format("{0}, {1}", order.ShippingCountry, order.ShippingStateProvince)));
                doc.Add(new Paragraph("   " + String.Format("{0}, {1}", order.ShippingCity, order.ShippingZipPostalCode)));
                doc.Add(new Paragraph(" "));
            }




            //products
            doc.Add(new Paragraph(LocalizationManager.GetLocaleResourceString("PDFInvoice.Product(s)", LanguageID)));
            doc.Add(new Paragraph(" "));

            OrderProductVariantCollection productCollection = order.OrderProductVariants;
            Table tbl = new Table(4, productCollection.Count + 1);
            tbl.AutoFillEmptyCells = true;
            tbl.Cellpadding = 2f;
            tbl.SetWidths(new int[4] { 40, 25, 10, 25 });

            Cell c1 = new Cell(LocalizationManager.GetLocaleResourceString("PDFInvoice.ProductName", LanguageID));
            c1.Header = true;
            c1.HorizontalAlignment = Element.ALIGN_CENTER;
            c1.VerticalAlignment = Element.ALIGN_MIDDLE;
            tbl.AddCell(c1, 0, 0);

            Cell c2 = new Cell(LocalizationManager.GetLocaleResourceString("PDFInvoice.ProductPrice", LanguageID));
            c2.Header = true;
            c2.HorizontalAlignment = Element.ALIGN_CENTER;
            c2.VerticalAlignment = Element.ALIGN_MIDDLE;
            tbl.AddCell(c2, 0, 1);

            Cell c3 = new Cell(LocalizationManager.GetLocaleResourceString("PDFInvoice.ProductQuantity", LanguageID));
            c3.Header = true;
            c3.HorizontalAlignment = Element.ALIGN_CENTER;
            c3.VerticalAlignment = Element.ALIGN_MIDDLE;
            tbl.AddCell(c3, 0, 2);

            Cell c4 = new Cell(LocalizationManager.GetLocaleResourceString("PDFInvoice.ProductTotal", LanguageID));
            c4.Header = true;
            c4.HorizontalAlignment = Element.ALIGN_CENTER;
            c4.VerticalAlignment = Element.ALIGN_MIDDLE;
            tbl.AddCell(c4, 0, 3);

            tbl.Width = 90f;

            for (int i = 0; i < productCollection.Count; i++)
            {
                OrderProductVariant orderProductVariant = productCollection[i];
                int row = i + 1;

                string name = String.Format("Not available. ID={0}", orderProductVariant.ProductVariantID);
                ProductVariant pv = ProductManager.GetProductVariantByID(orderProductVariant.ProductVariantID);
                if (pv != null)
                {
                    name = pv.FullProductName;
                }
                tbl.AddCell(new Cell(name), row, 0);

                string unitPrice = string.Empty;
                switch (order.CustomerTaxDisplayType)
                {
                    case TaxDisplayTypeEnum.ExcludingTax:
                        unitPrice = PriceHelper.FormatPrice(orderProductVariant.UnitPriceExclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, NopContext.Current.WorkingLanguage, false);
                        break;
                    case TaxDisplayTypeEnum.IncludingTax:
                        unitPrice = PriceHelper.FormatPrice(orderProductVariant.UnitPriceInclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, NopContext.Current.WorkingLanguage, true);
                        break;
                }

                tbl.AddCell(new Cell(unitPrice), row, 1);

                tbl.AddCell(new Cell(orderProductVariant.Quantity.ToString()), row, 2);

                string subTotal = string.Empty;
                switch (order.CustomerTaxDisplayType)
                {
                    case TaxDisplayTypeEnum.ExcludingTax:
                        subTotal = PriceHelper.FormatPrice(orderProductVariant.PriceExclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, NopContext.Current.WorkingLanguage, false);
                        break;
                    case TaxDisplayTypeEnum.IncludingTax:
                        subTotal = PriceHelper.FormatPrice(orderProductVariant.PriceInclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, NopContext.Current.WorkingLanguage, true);
                        break;
                }
                tbl.AddCell(new Cell(subTotal), row, 3);
            }

            doc.Add(tbl);



            //subtotal
            doc.Add(new Paragraph(" "));
            Paragraph p1 = null;
            switch (order.CustomerTaxDisplayType)
            {
                case TaxDisplayTypeEnum.ExcludingTax:
                    {
                        string orderSubtotalExclTaxStr = PriceHelper.FormatPrice(order.OrderSubtotalExclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, NopContext.Current.WorkingLanguage, false);
                        p1 = new Paragraph(String.Format("{0} {1}", LocalizationManager.GetLocaleResourceString("PDFInvoice.Sub-Total", LanguageID), orderSubtotalExclTaxStr));
                    }
                    break;
                case TaxDisplayTypeEnum.IncludingTax:
                    {
                        string orderSubtotalInclTaxStr = PriceHelper.FormatPrice(order.OrderSubtotalInclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, NopContext.Current.WorkingLanguage, true);
                        p1 = new Paragraph(String.Format("{0} {1}", LocalizationManager.GetLocaleResourceString("PDFInvoice.Sub-Total", LanguageID), orderSubtotalInclTaxStr));
                    }
                    break;
            }
            if (p1 != null)
            {
                p1.Alignment = Element.ALIGN_RIGHT;
                doc.Add(p1);
            }

            //shipping
            Paragraph p2 = null;
            switch (order.CustomerTaxDisplayType)
            {
                case TaxDisplayTypeEnum.ExcludingTax:
                    {
                        string orderShippingExclTaxStr = PriceHelper.FormatShippingPrice(order.OrderShippingExclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, NopContext.Current.WorkingLanguage, false);
                        p2 = new Paragraph(String.Format("{0} {1}", LocalizationManager.GetLocaleResourceString("PDFInvoice.Shipping", LanguageID), orderShippingExclTaxStr));
                    }
                    break;
                case TaxDisplayTypeEnum.IncludingTax:
                    {
                        string orderShippingInclTaxStr = PriceHelper.FormatShippingPrice(order.OrderShippingInclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, NopContext.Current.WorkingLanguage, true);
                        p2 = new Paragraph(String.Format("{0} {1}", LocalizationManager.GetLocaleResourceString("PDFInvoice.Shipping", LanguageID), orderShippingInclTaxStr));
                    }
                    break;
            }
            
            if (p2 != null)
            {
                p2.Alignment = Element.ALIGN_RIGHT;
                doc.Add(p2);
            }

            //payment fee
            bool displayPaymentMethodFee = true;
            if (order.PaymentMethodAdditionalFeeExclTaxInCustomerCurrency == decimal.Zero)
            {
                displayPaymentMethodFee = false;
            }
            if (displayPaymentMethodFee)
            {
                Paragraph p3 = null;
                switch (order.CustomerTaxDisplayType)
                {
                    case TaxDisplayTypeEnum.ExcludingTax:
                        {
                            string paymentMethodAdditionalFeeExclTaxStr = PriceHelper.FormatPaymentMethodAdditionalFee(order.PaymentMethodAdditionalFeeExclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, NopContext.Current.WorkingLanguage, false);
                            p3 = new Paragraph(String.Format("{0} {1}", LocalizationManager.GetLocaleResourceString("PDFInvoice.PaymentMethodAdditionalFee", LanguageID), paymentMethodAdditionalFeeExclTaxStr));
                        }
                        break;
                    case TaxDisplayTypeEnum.IncludingTax:
                        {
                            string paymentMethodAdditionalFeeInclTaxStr = PriceHelper.FormatPaymentMethodAdditionalFee(order.PaymentMethodAdditionalFeeInclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, NopContext.Current.WorkingLanguage, true);
                            p3 = new Paragraph(String.Format("{0} {1}", LocalizationManager.GetLocaleResourceString("PDFInvoice.PaymentMethodAdditionalFee", LanguageID), paymentMethodAdditionalFeeInclTaxStr));
                        }
                        break;
                }
                if (p3 != null)
                {
                    p3.Alignment = Element.ALIGN_RIGHT;
                    doc.Add(p3);
                }
            }

            string taxStr = string.Empty;
            bool displayTax = true;
            if (TaxManager.HideTaxInOrderSummary && order.CustomerTaxDisplayType == TaxDisplayTypeEnum.IncludingTax)
            {
                displayTax = false;
            }
            else
            {
                if (order.OrderTax == 0 && TaxManager.HideZeroTax)
                {
                    displayTax = false;
                }
                else
                {
                    taxStr = string.Format("{0} ({1})", order.OrderTaxInCustomerCurrency.ToString("N"), order.CustomerCurrencyCode);
                }
            }
            if (displayTax)
            {
                Paragraph p4 = new Paragraph(String.Format("{0} {1}", LocalizationManager.GetLocaleResourceString("PDFInvoice.Tax", LanguageID), taxStr));
                p4.Alignment = Element.ALIGN_RIGHT;
                doc.Add(p4);
            }

            string totalStr = string.Format("{0} ({1})", order.OrderTotalInCustomerCurrency.ToString("N"), order.CustomerCurrencyCode);
            Paragraph p5 = new Paragraph(String.Format("{0} {1}", LocalizationManager.GetLocaleResourceString("PDFInvoice.OrderTotal", LanguageID), totalStr));
            p5.Alignment = Element.ALIGN_RIGHT;
            doc.Add(p5);

            doc.Close();
        }
        #endregion
    }
}
14 年 前
Yep, I changed my PDF code a while ago.

You will want to add the logo above your other stuff. Above your line commented with //billing info, add the following:
    
Image logo = Image.GetInstance( new Uri( "http://www.yourstore.com/images/logo_graphic.gif" ) );      
logo.ScalePercent( 50 );      
doc.Add( logo );

This is going to simply add your logo whereever you happen to add this code. To place it in a specific location, you may have to either add a cell or a paragraph and align the image to suit your needs.

Try that out and see if it works, then get back to us.
14 年 前
Holliday, it does not give me metadata errors any more.
while I put the code in, I see Uri with curly line say some thing like "the type or name space name 'Uri' could not be found...
I can build or rebuild the solutions now.
Tried it out, there is no logo picture yet.
Tony
14 年 前
Do you have a reference to:

using System;

At the top? If not, add it.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.