Steps to adding three more columns to the PDF invoice.In this case the new columns are: Lane, Bay, and stock code.
Please note that I am using nopcommerce version 2.4.
File to edit: PdfService.cs
Libraries - > Nop.Services - > Common - > PdfService.cs
Step #1First go to the following code. This is near the top.
/// <summary>
/// Print an order to PDF
/// </summary>
/// <param name="orders">Orders</param>
/// <param name="lang">Language</param>
/// <param name="filePath">File path</param>
From here scroll down to “#region Products”#region Products
//products
doc.Add(new Paragraph(_localizationService.GetResource("PDFInvoice.Product(s)", lang.Id), titleFont));
doc.Add(new Paragraph(" "));
var orderProductVariants = _orderService.GetAllOrderProductVariants(order.Id, null, null, null, null, null, null);
var productsTable = new PdfPTable(4);
productsTable.WidthPercentage = 100f;
productsTable.SetWidths(new[] { 40, 20, 20, 20 });
Look closely at the last three lines of code I copied above. var productsTable = new PdfPTable(4);
productsTable.WidthPercentage = 100f;
productsTable.SetWidths(new[] { 40, 20, 20, 20 });
This is the code that designates the amount of columns that are on the PDF invoice and also as you see in the last line, the width of each column. You will need to change this as follows:var productsTable = new PdfPTable(7);
productsTable.WidthPercentage = 100f;
productsTable.SetWidths(new[] { 15, 15, 10, 35, 10, 5, 10});
Note: If you look closely at the changes, all we did was change the first line of the last three to designate 7 columns instead of 4. In the last line, we set the width of each of the columns. The first number “15” designates the width of the column on the left hand side on the Invoice going through to the last one “10” which designates the width of the column on the right hand side of the invoice.
You can change the widths as you need to but make sure that all the widths of the columns add up to 100. For example: 15+15+10+35+10+5+10=100
Step #2Right below the code that we just altered and before the following code//product name
cell = new PdfPCell(new Phrase(_localizationService.GetResource("PDFInvoice.ProductName", lang.Id), font));
cell.BackgroundColor = BaseColor.LIGHT_GRAY;
cell.HorizontalAlignment = Element.ALIGN_CENTER;
productsTable.AddCell(cell);
you need to add the following://CCCLane
cell = new PdfPCell(new Phrase(_localizationService.GetResource("PDFInvoice.ProductCCCLane", lang.Id), font));
cell.BackgroundColor = BaseColor.LIGHT_GRAY;
cell.HorizontalAlignment = Element.ALIGN_CENTER;
productsTable.AddCell(cell);
//CCCBay
cell = new PdfPCell(new Phrase(_localizationService.GetResource("PDFInvoice.ProductCCCBay", lang.Id), font));
cell.BackgroundColor = BaseColor.LIGHT_GRAY;
cell.HorizontalAlignment = Element.ALIGN_CENTER;
productsTable.AddCell(cell);
//CCCStockCode
cell = new PdfPCell(new Phrase(_localizationService.GetResource("PDFInvoice.ProductCCCStockCode", lang.Id), font));
cell.BackgroundColor = BaseColor.LIGHT_GRAY;
cell.HorizontalAlignment = Element.ALIGN_CENTER;
productsTable.AddCell(cell);
Note: This is the step that you had done with your code, if the code that you wrote is not in this place make sure to move it and delete the old code. If you look below the code we just entered, you will notice that the next few blocks of code are similar to what we just entered.
//product name
cell = new PdfPCell(new Phrase(_localizationService.GetResource("PDFInvoice.ProductName", lang.Id), font));
cell.BackgroundColor = BaseColor.LIGHT_GRAY;
cell.HorizontalAlignment = Element.ALIGN_CENTER;
productsTable.AddCell(cell);
//price
...
//qty
...
and so on.
Note: Each of these blocks of code is setting the title of the column, and the background of the title to gray. The order in which they are placed corresponds to the order of the widths we set in Step #1. For example: the width of the first column on the left of the invoice “Lane” will have a width of 15, Bay will have a width of 15, and stock code will have a width of 10 etc. Just remember that for each of these steps: Step #1, Step #2, and Step #3 the order you put the code for the new three columns is important, as well as the order of the code in relation to that of the other columns. If you ever what to change the order of the columns on the invoice, for instance to put the stock code on the left hand side of the invoice, you would need to reorganize the code for all the columns in Step #1, Step #2, and Step #3.
Important information on Step #3 Note: I am not sure how you reference the new database entries that you have written. However, if you look at Step #3 on the”Steps to adding the SKU to the PDF invoice” above you will see that I referenced the SKU by: orderProductVariant.ProductVariant.Sku
In this step I will reference the data Lane, Bay, and Stock code that you have added to nopcommerce as:XXXXX.XXX. ProductCCCLane
XXXXX.XXX. ProductCCCBay
and
XXXXX.XXX. ProductCCCStockCode
But these mock references to the data will need to be replaced with how you reference the new data you have written. Step #3From where we entered the code in step#2 //CCCLane
cell = new PdfPCell(new Phrase(_localizationService.GetResource("PDFInvoice.ProductCCCLane", lang.Id), font));
cell.BackgroundColor = BaseColor.LIGHT_GRAY;
cell.HorizontalAlignment = Element.ALIGN_CENTER;
productsTable.AddCell(cell);
//CCCBay
cell = new PdfPCell(new Phrase(_localizationService.GetResource("PDFInvoice.ProductCCCBay", lang.Id), font));
cell.BackgroundColor = BaseColor.LIGHT_GRAY;
cell.HorizontalAlignment = Element.ALIGN_CENTER;
productsTable.AddCell(cell);
//CCCStockCode
...
//product name
...
and so on
Scroll down to the following for (int i = 0; i < orderProductVariants.Count; i++)
{
var orderProductVariant = orderProductVariants[i];
var pv = orderProductVariant.ProductVariant;
//product name
…
Right before the “//product name” in the above code add the following code://Lane
cell = new PdfPCell(new Phrase(XXXXX.XXX.ProductCCCLane , font));
cell.HorizontalAlignment = Element.ALIGN_LEFT;
productsTable.AddCell(cell);
//Bay
cell = new PdfPCell(new Phrase(XXXXX.XXX.ProductCCCBay, font));
cell.HorizontalAlignment = Element.ALIGN_LEFT;
productsTable.AddCell(cell);
//Stock code
cell = new PdfPCell(new Phrase(XXXXX.XXX.ProductCCCStockCode, font));
cell.HorizontalAlignment = Element.ALIGN_LEFT;
productsTable.AddCell(cell);
Remember: you will need to change XXXXX.XXX.ProductCCCLane, XXXXX.XXX.ProductCCCBay, and XXXXX.XXX.ProductCCCStockCode to the real references of the new data that you have in your database.
Now all you have to do is run nopcommerce and go to
Admin > Sales > Orders
Click to view one of the orders: try opening the “Invoice (PDF)” and see if it works.
If one of the columns is too narrow, go back to Step #1 and adjust the widths but make sure all the widths add up to 100. If you make one column larger you will need to make a different one smaller.
If the new columns are empty, check to make sure the way you referenced your data is correct in Step #3.
Finally, if something else goes wrong or if it does not work let me know and I will check to make sure I have typed up all the steps right. :)
wertyuio1 wrote:
Can add any amount of blocks to this so if i need 3 more i can??
You can add as many as you need or change the order they are shown on the invoice. However, if you add too many the columns, they will have to be very narrow in order for them all to fit on the width of the paper.
I hope this helps.
Let me know if it works!
P.S. Just out of curiosity, if you don't mind, what is the name of your company? I am not that familiar with the financial industy but from the sounds of it are you a stock exchange? If you wouldn't mind giving me a link, I thought it would neat to see your website and all that you were selling.