Biblioteca Java - Blame information for rev 9
Subversion Repositories:
(root)/Spring/SpringWebMVC/InventoryHibernate/src/main/java/com/linkscreens/inventory/pdfgenerator/PDFGenerator.java
Rev | Author | Line No. | Line |
---|---|---|---|
9 | mihai | 1 | package com.linkscreens.inventory.pdfgenerator; |
2 | |||
3 | import java.io.FileNotFoundException; | ||
4 | import java.io.FileOutputStream; | ||
5 | import java.util.List; | ||
6 | |||
7 | import com.itextpdf.text.*; | ||
8 | import com.itextpdf.text.pdf.PdfPCell; | ||
9 | import com.itextpdf.text.pdf.PdfPTable; | ||
10 | import com.itextpdf.text.pdf.PdfWriter; | ||
11 | import com.linkscreens.inventory.entity.InventoryItem; | ||
12 | |||
13 | public class PDFGenerator { | ||
14 | |||
15 | private static Font TIME_ROMAN = | ||
16 | new Font(Font.FontFamily.TIMES_ROMAN, 18,Font.BOLD); | ||
17 | private static Font TIME_ROMAN_SMALL = | ||
18 | new Font(Font.FontFamily.TIMES_ROMAN, 12,Font.BOLD); | ||
19 | private static Font TIMES_ROMAN_SMALLEST = | ||
20 | new Font(Font.FontFamily.TIMES_ROMAN, 11,Font.NORMAL); | ||
21 | private static Font TIMES_ROMAN_SMALLEST_BOLD = | ||
22 | new Font(Font.FontFamily.TIMES_ROMAN, 11,Font.BOLD); | ||
23 | |||
24 | |||
25 | public static Document createPDF(String file, List<InventoryItem> items, boolean isFull) { | ||
26 | |||
27 | Document document = null; | ||
28 | |||
29 | try { | ||
30 | document = new Document(PageSize.LETTER.rotate()); | ||
31 | PdfWriter.getInstance(document, new FileOutputStream(file)); | ||
32 | document.open(); | ||
33 | addMetaData(document); | ||
34 | |||
35 | addTitlePage(document, items.get(0).getAnLunaAdaugare()); | ||
36 | |||
37 | createTable(document, items, isFull); | ||
38 | |||
39 | addRowAfter(document); | ||
40 | |||
41 | document.close(); | ||
42 | |||
43 | } catch (FileNotFoundException e) { | ||
44 | e.printStackTrace(); | ||
45 | } catch (DocumentException e) { | ||
46 | e.printStackTrace(); | ||
47 | } | ||
48 | return document; | ||
49 | |||
50 | } | ||
51 | |||
52 | private static void addMetaData(Document document) { | ||
53 | document.addTitle("Generare raport PDF"); | ||
54 | document.addSubject("Generare raport PDF"); | ||
55 | document.addAuthor("LinkScreens"); | ||
56 | document.addCreator("LinkScreens"); | ||
57 | } | ||
58 | |||
59 | private static void addRowAfter(Document document) throws DocumentException { | ||
60 | Paragraph rowAfter = new Paragraph(); | ||
61 | creteEmptyLine(rowAfter, 1); | ||
62 | rowAfter.add(new Paragraph("Director General", TIME_ROMAN)); | ||
63 | document.add(rowAfter); | ||
64 | } | ||
65 | |||
66 | private static void addTitlePage(Document document, String yearMonthAdded) | ||
67 | throws DocumentException { | ||
68 | |||
69 | Paragraph preface = new Paragraph(); | ||
70 | creteEmptyLine(preface, 1); | ||
71 | preface.add(new Paragraph("LINKSCREENS SRL", TIME_ROMAN)); | ||
72 | |||
73 | creteEmptyLine(preface, 1); | ||
74 | |||
75 | preface.add(new Paragraph("Adresa: str.VIDRARU 9-11, Ap.104 Localitatea CLUJ NAPOCA Judetul Cluj", TIME_ROMAN_SMALL)); | ||
76 | preface.add(new Paragraph("Cod Fiscal: RO31223800", TIME_ROMAN_SMALL)); | ||
77 | preface.add(new Paragraph("Reg. comertului: J12/455/13.02.2013", TIME_ROMAN_SMALL)); | ||
78 | creteEmptyLine(preface, 1); | ||
79 | |||
80 | String month = getMonth(yearMonthAdded); | ||
81 | Paragraph title = new Paragraph("Registru inventar " + month + " " + yearMonthAdded.split("/")[0], TIME_ROMAN); | ||
82 | title.setAlignment(Element.ALIGN_CENTER); | ||
83 | preface.add(title); | ||
84 | document.add(preface); | ||
85 | |||
86 | document.addHeader("Registru inventar " + month + " " + yearMonthAdded.split("/")[0] + "(continuare)", | ||
87 | "Registru inventar " + month + " " + yearMonthAdded.split("/")[0] + "(continuare)"); | ||
88 | } | ||
89 | |||
90 | private static void creteEmptyLine(Paragraph paragraph, | ||
91 | int number) { | ||
92 | for (int i = 0; i < number; i++) { | ||
93 | paragraph.add(new Paragraph(" ")); | ||
94 | } | ||
95 | } | ||
96 | |||
97 | private static String getMonth(String yearMonthAdded) { | ||
98 | String month = ""; | ||
99 | if (yearMonthAdded.split("/")[1].equalsIgnoreCase("01")) { | ||
100 | month = "Ianuarie"; | ||
101 | } else if (yearMonthAdded.split("/")[1].equalsIgnoreCase("02")) { | ||
102 | month = "Februarie"; | ||
103 | } else if (yearMonthAdded.split("/")[1].equalsIgnoreCase("03")) { | ||
104 | month = "Martie"; | ||
105 | } else if (yearMonthAdded.split("/")[1].equalsIgnoreCase("04")) { | ||
106 | month = "Aprilie"; | ||
107 | } else if (yearMonthAdded.split("/")[1].equalsIgnoreCase("05")) { | ||
108 | month = "Mai"; | ||
109 | } else if (yearMonthAdded.split("/")[1].equalsIgnoreCase("06")) { | ||
110 | month = "Iunie"; | ||
111 | } else if (yearMonthAdded.split("/")[1].equalsIgnoreCase("07")) { | ||
112 | month = "Iulie"; | ||
113 | } else if (yearMonthAdded.split("/")[1].equalsIgnoreCase("08")) { | ||
114 | month = "August"; | ||
115 | } else if (yearMonthAdded.split("/")[1].equalsIgnoreCase("09")) { | ||
116 | month = "Septembrie"; | ||
117 | } else if (yearMonthAdded.split("/")[1].equalsIgnoreCase("10")) { | ||
118 | month = "Octombrie"; | ||
119 | } else if (yearMonthAdded.split("/")[1].equalsIgnoreCase("11")) { | ||
120 | month = "Noiembrie"; | ||
121 | } else if (yearMonthAdded.split("/")[1].equalsIgnoreCase("12")) { | ||
122 | month = "Decembrie"; | ||
123 | } | ||
124 | return month; | ||
125 | } | ||
126 | |||
127 | private static void createTable(Document document, List<InventoryItem> items, boolean isFull) | ||
128 | throws DocumentException { | ||
129 | Paragraph paragraph = new Paragraph(); | ||
130 | creteEmptyLine(paragraph, 2); | ||
131 | document.add(paragraph); | ||
132 | PdfPTable table = null; | ||
133 | if (isFull) { | ||
134 | table = new PdfPTable(17); | ||
135 | } else { | ||
136 | table = new PdfPTable(10); | ||
137 | } | ||
138 | |||
139 | PdfPCell c1 = new PdfPCell(new Phrase("Nr. Crt.", TIME_ROMAN_SMALL)); | ||
140 | c1.setHorizontalAlignment(Element.ALIGN_CENTER); | ||
141 | table.addCell(c1); | ||
142 | |||
143 | c1 = new PdfPCell(new Phrase("Simbol clasa mijloc fix", TIME_ROMAN_SMALL)); | ||
144 | c1.setHorizontalAlignment(Element.ALIGN_CENTER); | ||
145 | table.addCell(c1); | ||
146 | |||
147 | c1 = new PdfPCell(new Phrase("Mijloc fix", TIME_ROMAN_SMALL)); | ||
148 | c1.setHorizontalAlignment(Element.ALIGN_CENTER); | ||
149 | table.addCell(c1); | ||
150 | |||
151 | c1 = new PdfPCell(new Phrase("Nr. Inv.", TIME_ROMAN_SMALL)); | ||
152 | c1.setHorizontalAlignment(Element.ALIGN_CENTER); | ||
153 | table.addCell(c1); | ||
154 | |||
155 | c1 = new PdfPCell(new Phrase("Cont", TIME_ROMAN_SMALL)); | ||
156 | c1.setHorizontalAlignment(Element.ALIGN_CENTER); | ||
157 | table.addCell(c1); | ||
158 | |||
159 | c1 = new PdfPCell(new Phrase("Cant.", TIME_ROMAN_SMALL)); | ||
160 | c1.setHorizontalAlignment(Element.ALIGN_CENTER); | ||
161 | table.addCell(c1); | ||
162 | |||
163 | c1 = new PdfPCell(new Phrase("UM", TIME_ROMAN_SMALL)); | ||
164 | c1.setHorizontalAlignment(Element.ALIGN_CENTER); | ||
165 | table.addCell(c1); | ||
166 | |||
167 | c1 = new PdfPCell(new Phrase("Pret", TIME_ROMAN_SMALL)); | ||
168 | c1.setHorizontalAlignment(Element.ALIGN_CENTER); | ||
169 | table.addCell(c1); | ||
170 | |||
171 | c1 = new PdfPCell(new Phrase("Valoare", TIME_ROMAN_SMALL)); | ||
172 | c1.setHorizontalAlignment(Element.ALIGN_CENTER); | ||
173 | table.addCell(c1); | ||
174 | |||
175 | if (isFull) { | ||
176 | c1 = new PdfPCell(new Phrase("Amortizare precedenta", TIME_ROMAN_SMALL)); | ||
177 | c1.setHorizontalAlignment(Element.ALIGN_CENTER); | ||
178 | table.addCell(c1); | ||
179 | |||
180 | c1 = new PdfPCell(new Phrase("Amortizare curenta", TIME_ROMAN_SMALL)); | ||
181 | c1.setHorizontalAlignment(Element.ALIGN_CENTER); | ||
182 | table.addCell(c1); | ||
183 | |||
184 | c1 = new PdfPCell(new Phrase("Amortizare totala", TIME_ROMAN_SMALL)); | ||
185 | c1.setHorizontalAlignment(Element.ALIGN_CENTER); | ||
186 | table.addCell(c1); | ||
187 | |||
188 | c1 = new PdfPCell(new Phrase("Valoare ramasa", TIME_ROMAN_SMALL)); | ||
189 | c1.setHorizontalAlignment(Element.ALIGN_CENTER); | ||
190 | table.addCell(c1); | ||
191 | |||
192 | c1 = new PdfPCell(new Phrase("Durata norm. (ani)", TIME_ROMAN_SMALL)); | ||
193 | c1.setHorizontalAlignment(Element.ALIGN_CENTER); | ||
194 | table.addCell(c1); | ||
195 | |||
196 | c1 = new PdfPCell(new Phrase("Durata ramasa (luni)", TIME_ROMAN_SMALL)); | ||
197 | c1.setHorizontalAlignment(Element.ALIGN_CENTER); | ||
198 | table.addCell(c1); | ||
199 | |||
200 | c1 = new PdfPCell(new Phrase("An luna prima rata", TIME_ROMAN_SMALL)); | ||
201 | c1.setHorizontalAlignment(Element.ALIGN_CENTER); | ||
202 | table.addCell(c1); | ||
203 | } | ||
204 | |||
205 | c1 = new PdfPCell(new Phrase("An luna adaugare", TIME_ROMAN_SMALL)); | ||
206 | c1.setHorizontalAlignment(Element.ALIGN_CENTER); | ||
207 | table.addCell(c1); | ||
208 | |||
209 | table.setHeaderRows(1); | ||
210 | |||
211 | table.setWidthPercentage(100); | ||
212 | table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); | ||
213 | table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE); | ||
214 | |||
215 | for (InventoryItem item : items) { | ||
216 | if (item.getId() == null || item.getId() == 0L) { | ||
217 | PdfPCell totalName = new PdfPCell(new Phrase(item.getSimbolClasaMijlocFix(), TIMES_ROMAN_SMALLEST_BOLD)); | ||
218 | totalName.setColspan(8); | ||
219 | totalName.setHorizontalAlignment(Element.ALIGN_LEFT); | ||
220 | table.addCell(totalName); | ||
221 | |||
222 | PdfPCell totalValue = new PdfPCell(new Phrase(item.getValoare().toString(), TIMES_ROMAN_SMALLEST_BOLD)); | ||
223 | totalValue.setHorizontalAlignment(Element.ALIGN_RIGHT); | ||
224 | table.addCell(totalValue); | ||
225 | |||
226 | if (isFull) { | ||
227 | PdfPCell totalPast = new PdfPCell(new Phrase(item.getAmortizarePrecedenta().toString(), TIMES_ROMAN_SMALLEST_BOLD)); | ||
228 | totalPast.setHorizontalAlignment(Element.ALIGN_RIGHT); | ||
229 | table.addCell(totalPast); | ||
230 | |||
231 | PdfPCell totalCurrent = new PdfPCell(new Phrase(item.getAmortizareCurenta().toString(), TIMES_ROMAN_SMALLEST_BOLD)); | ||
232 | totalCurrent.setHorizontalAlignment(Element.ALIGN_RIGHT); | ||
233 | table.addCell(totalCurrent); | ||
234 | |||
235 | PdfPCell totalAmortization = new PdfPCell(new Phrase(item.getAmortizareTotala().toString(), TIMES_ROMAN_SMALLEST_BOLD)); | ||
236 | totalAmortization.setHorizontalAlignment(Element.ALIGN_RIGHT); | ||
237 | table.addCell(totalAmortization); | ||
238 | |||
239 | PdfPCell remainingValue = new PdfPCell(new Phrase(item.getValoareRamasa().toString(), TIMES_ROMAN_SMALLEST_BOLD)); | ||
240 | remainingValue.setHorizontalAlignment(Element.ALIGN_RIGHT); | ||
241 | table.addCell(remainingValue); | ||
242 | |||
243 | PdfPCell remainingCells = new PdfPCell(new Phrase("")); | ||
244 | remainingCells.setColspan(3); | ||
245 | table.addCell(remainingCells); | ||
246 | } | ||
247 | |||
248 | PdfPCell remainingCell = new PdfPCell(new Phrase("")); | ||
249 | remainingCell.setColspan(1); | ||
250 | table.addCell(remainingCell); | ||
251 | |||
252 | } else { | ||
253 | |||
254 | PdfPCell cell = new PdfPCell(new Phrase(String.valueOf(item.getNrCrt()), TIMES_ROMAN_SMALLEST)); | ||
255 | cell.setHorizontalAlignment(Element.ALIGN_RIGHT); | ||
256 | table.addCell(cell); | ||
257 | |||
258 | cell = new PdfPCell(new Phrase(String.valueOf(item.getSimbolClasaMijlocFix()), TIMES_ROMAN_SMALLEST)); | ||
259 | cell.setHorizontalAlignment(Element.ALIGN_CENTER); | ||
260 | table.addCell(cell); | ||
261 | |||
262 | cell = new PdfPCell(new Phrase(String.valueOf(item.getMijlocFix()), TIMES_ROMAN_SMALLEST)); | ||
263 | cell.setHorizontalAlignment(Element.ALIGN_LEFT); | ||
264 | table.addCell(cell); | ||
265 | |||
266 | cell = new PdfPCell(new Phrase(String.valueOf(item.getNrInventar()), TIMES_ROMAN_SMALLEST)); | ||
267 | cell.setHorizontalAlignment(Element.ALIGN_RIGHT); | ||
268 | table.addCell(cell); | ||
269 | |||
270 | cell = new PdfPCell(new Phrase(item.getCont(), TIMES_ROMAN_SMALLEST)); | ||
271 | cell.setHorizontalAlignment(Element.ALIGN_CENTER); | ||
272 | table.addCell(cell); | ||
273 | |||
274 | cell = new PdfPCell(new Phrase(String.valueOf(item.getCantitate()), TIMES_ROMAN_SMALLEST)); | ||
275 | cell.setHorizontalAlignment(Element.ALIGN_RIGHT); | ||
276 | table.addCell(cell); | ||
277 | |||
278 | cell = new PdfPCell(new Phrase(item.getUM(), TIMES_ROMAN_SMALLEST)); | ||
279 | cell.setHorizontalAlignment(Element.ALIGN_CENTER); | ||
280 | table.addCell(cell); | ||
281 | |||
282 | cell = new PdfPCell(new Phrase(item.getPret().toString(), TIMES_ROMAN_SMALLEST)); | ||
283 | cell.setHorizontalAlignment(Element.ALIGN_RIGHT); | ||
284 | table.addCell(cell); | ||
285 | |||
286 | cell = new PdfPCell(new Phrase(item.getValoare().toString(), TIMES_ROMAN_SMALLEST)); | ||
287 | cell.setHorizontalAlignment(Element.ALIGN_RIGHT); | ||
288 | table.addCell(cell); | ||
289 | |||
290 | if (isFull) { | ||
291 | cell = new PdfPCell(new Phrase(item.getAmortizarePrecedenta().toString(), TIMES_ROMAN_SMALLEST)); | ||
292 | cell.setHorizontalAlignment(Element.ALIGN_RIGHT); | ||
293 | table.addCell(cell); | ||
294 | |||
295 | cell = new PdfPCell(new Phrase(item.getAmortizareCurenta().toString(), TIMES_ROMAN_SMALLEST)); | ||
296 | cell.setHorizontalAlignment(Element.ALIGN_RIGHT); | ||
297 | table.addCell(cell); | ||
298 | |||
299 | cell = new PdfPCell(new Phrase(item.getAmortizareTotala().toString(), TIMES_ROMAN_SMALLEST)); | ||
300 | cell.setHorizontalAlignment(Element.ALIGN_RIGHT); | ||
301 | table.addCell(cell); | ||
302 | |||
303 | cell = new PdfPCell(new Phrase(item.getValoareRamasa().toString(), TIMES_ROMAN_SMALLEST)); | ||
304 | cell.setHorizontalAlignment(Element.ALIGN_RIGHT); | ||
305 | table.addCell(cell); | ||
306 | |||
307 | cell = new PdfPCell(new Phrase(String.valueOf(item.getDurataNormala()), TIMES_ROMAN_SMALLEST)); | ||
308 | cell.setHorizontalAlignment(Element.ALIGN_RIGHT); | ||
309 | table.addCell(cell); | ||
310 | |||
311 | cell = new PdfPCell(new Phrase(String.valueOf(item.getDurataRamasa()), TIMES_ROMAN_SMALLEST)); | ||
312 | cell.setHorizontalAlignment(Element.ALIGN_RIGHT); | ||
313 | table.addCell(cell); | ||
314 | |||
315 | cell = new PdfPCell(new Phrase(String.valueOf(item.getAnLunaPrimaRata()), TIMES_ROMAN_SMALLEST)); | ||
316 | cell.setHorizontalAlignment(Element.ALIGN_RIGHT); | ||
317 | table.addCell(cell); | ||
318 | } | ||
319 | |||
320 | cell = new PdfPCell(new Phrase(String.valueOf(item.getAnLunaAdaugare()), TIMES_ROMAN_SMALLEST)); | ||
321 | cell.setHorizontalAlignment(Element.ALIGN_RIGHT); | ||
322 | table.addCell(cell); | ||
323 | } | ||
324 | } | ||
325 | document.add(table); | ||
326 | } | ||
327 | |||
328 | } |