/Users/johnr/Desktop/IA pdf Downloads/Criteria__P__-_Coding_Project_Upload_all_2022-05-03/SPVCPortal_Panpan/src/main/java/com/mycompany/spvcportal/CreatePDF.java |
1
2 nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt
3 nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java
4
5 package com.mycompany.spvcportal;
6
7 import javax.swing.*;
8 import java.awt.*;
9 import java.io.FileNotFoundException;
10 import java.io.FileOutputStream;
11 import java.io.IOException;
12
13 import com.itextpdf.text.*;
14 import com.itextpdf.text.Font;
15 import com.itextpdf.text.pdf.PdfBody;
16 import com.itextpdf.text.pdf.PdfPCell;
17 import com.itextpdf.text.Image;
18 import com.itextpdf.text.pdf.PdfPTable;
19 import com.itextpdf.text.pdf.PdfWriter;
20 import java.util.LinkedList;
21
22
23
24 @author
25
26 public class CreatePDF {
27
28 public void create(int orderIndexToSummarize, LinkedList<Order> ordersList) throws IOException, DocumentException {
29 Document document = new Document();
30 PdfWriter.getInstance(document, new FileOutputStream("OrderID" + ordersList.get(orderIndexToSummarize).getOrderID() + ".pdf"));
31 document.open();
32
33 Font bodyFont = new Font(Font.FontFamily.HELVETICA, 14);
34 Paragraph companyHeading = new Paragraph();
35 companyHeading.add("SPVC International Co., Ltd.");
36 companyHeading.setAlignment(Element.ALIGN_RIGHT);
37
38
39 Paragraph address = new Paragraph(18);
40 address.add("759/9 Soi 39, Rama 3 Rd.\nBang Phong Phang, Yannawa\nBangkok, 10120");
41 address.setAlignment(Element.ALIGN_RIGHT);
42 address.setSpacingAfter(40);
43
44 Font headingFont = new Font(Font.FontFamily.HELVETICA, 25, Font.BOLD);
45 Paragraph documentHeading = new Paragraph("ORDER SUMMARY", headingFont);
46 documentHeading.setAlignment(Element.ALIGN_CENTER);
47 documentHeading.setSpacingAfter(5);
48
49 Paragraph orderIDHeader = new Paragraph("Reference Order ID: " + String.valueOf(ordersList.get(orderIndexToSummarize).getOrderID()), bodyFont);
50 orderIDHeader.setAlignment(Element.ALIGN_CENTER);
51 orderIDHeader.setSpacingAfter(20);
52
53
54 document.add(companyHeading);
55 document.add(address);
56 document.add(documentHeading);
57 document.add(orderIDHeader);
58
59 PdfPTable table = new PdfPTable(4);
60 table.setWidthPercentage(95);
61 Paragraph cell1Text = new Paragraph();
62 cell1Text.add("Products");
63 Paragraph cell2Text = new Paragraph();
64 cell2Text.add("Price");
65 Paragraph cell3Text = new Paragraph();
66 cell3Text.add("Quantity");
67 Paragraph cell4Text = new Paragraph();
68 cell4Text.add("Amt. Due");
69 Paragraph cell5Text = new Paragraph();
70 cell5Text.add(ordersList.get(orderIndexToSummarize).getProductName());
71 Paragraph cell6Text = new Paragraph();
72 cell6Text.add(String.valueOf(ordersList.get(orderIndexToSummarize).getPricePerUnit()));
73 Paragraph cell7Text = new Paragraph();
74 cell7Text.add(String.valueOf(ordersList.get(orderIndexToSummarize).getQuantityOfProduct()));
75 Paragraph cell8Text = new Paragraph();
76 double totalAmtDue = ordersList.get(orderIndexToSummarize).getPricePerUnit() * ordersList.get(orderIndexToSummarize).getQuantityOfProduct();
77 cell8Text.add(String.valueOf(totalAmtDue));
78 PdfPCell cell1 = new PdfPCell(cell1Text);
79 cell1.setPaddingTop(8);
80 cell1.setPaddingBottom(8);
81 cell1.setPaddingLeft(8);
82 cell1.setBackgroundColor(BaseColor.LIGHT_GRAY);
83 PdfPCell cell2 = new PdfPCell(cell2Text);
84 cell2.setPaddingTop(8);
85 cell2.setPaddingTop(8);
86 cell2.setPaddingLeft(8);
87 cell2.setBackgroundColor(BaseColor.LIGHT_GRAY);
88 PdfPCell cell3 = new PdfPCell(cell3Text);
89 cell3.setPaddingTop(8);
90 cell3.setPaddingTop(8);
91 cell3.setPaddingLeft(8);
92 cell3.setBackgroundColor(BaseColor.LIGHT_GRAY);
93 PdfPCell cell4 = new PdfPCell(cell4Text);
94 cell4.setPaddingTop(8);
95 cell4.setPaddingBottom(8);
96 cell4.setPaddingLeft(8);
97 cell4.setBackgroundColor(BaseColor.LIGHT_GRAY);
98 PdfPCell cell5 = new PdfPCell(cell5Text);
99 cell5.setPaddingTop(8);
100 cell5.setPaddingBottom(8);
101 cell5.setPaddingLeft(8);
102 PdfPCell cell6 = new PdfPCell(cell6Text);
103 cell6.setPaddingTop(8);
104 cell6.setPaddingBottom(8);
105 cell6.setPaddingLeft(8);
106 PdfPCell cell7 = new PdfPCell(cell7Text);
107 cell7.setPaddingTop(8);
108 cell7.setPaddingBottom(8);
109 cell7.setPaddingLeft(8);
110 PdfPCell cell8 = new PdfPCell(cell8Text);
111 cell8.setPaddingTop(8);
112 cell8.setPaddingBottom(8);
113 cell8.setPaddingLeft(8);
114
115 float[] widths = {4f, 1f, 1f, 1f};
116 table.setWidths(widths);
117
118 table.addCell(cell1);
119 table.addCell(cell2);
120 table.addCell(cell3);
121 table.addCell(cell4);
122 table.addCell(cell5);
123 table.addCell(cell6);
124 table.addCell(cell7);
125 table.addCell(cell8);
126 table.setSpacingAfter(20);
127
128 document.add(table);
129
130 Font boldBodyFont = new Font(Font.FontFamily.HELVETICA, 12, Font.BOLD);
131 Chunk orderInfoHeading = new Chunk("Order Information", boldBodyFont);
132 orderInfoHeading.setUnderline(1f, -2f);
133
134 Paragraph orderInfo = new Paragraph(20);
135 orderInfo.add("");
136 orderInfo.setIndentationLeft(20);
137 orderInfo.add(orderInfoHeading);
138 orderInfo.add("\nDate placed (mm/dd/yy): " + ordersList.get(orderIndexToSummarize).getDate());
139 orderInfo.add("\nOrdered through: " + ordersList.get(orderIndexToSummarize).getOrderChannel());
140 orderInfo.add("\nBuyer name: " + ordersList.get(orderIndexToSummarize).getBuyerName());
141
142 String orderChannel = ordersList.get(orderIndexToSummarize).getOrderChannel();
143 if(orderChannel.equalsIgnoreCase("Online")){
144 OnlineOrder onlineOrderTemp = (OnlineOrder)(ordersList.get(orderIndexToSummarize));
145 orderInfo.add("\nTracking no.: " + onlineOrderTemp.getTrackingNumber());
146 orderInfo.add("\nDeliver by: " + onlineOrderTemp.getDeliverByDate());
147 }else if(orderChannel.equalsIgnoreCase("Telephone")){
148 PhoneOrder phoneOrderTemp = (PhoneOrder)(ordersList.get(orderIndexToSummarize));
149 orderInfo.add("\nContact no.: " + phoneOrderTemp.getPhoneNum());
150 }
151
152 orderInfo.setSpacingAfter(80);
153 document.add(orderInfo);
154
155 Paragraph dateBox = new Paragraph("", boldBodyFont);
156 dateBox.setAlignment(Element.ALIGN_RIGHT);
157 dateBox.setIndentationRight(20);
158 dateBox.add("______/______/______");
159 dateBox.add("\nDate: (mm)/(dd)/(yyyy)");
160
161 document.add(dateBox);
162
163 Paragraph signatureBox = new Paragraph("", boldBodyFont);
164 signatureBox.setIndentationLeft(20);
165 signatureBox.add("____________________________");
166 signatureBox.add("\nEmployee responsible");
167
168 document.add(signatureBox);
169
170 document.close();
171
172
173 }
174 }
175