public void dispose() { if (document != null) { g2.dispose(); document.close(); // can't be done in finalize, not always called document = null; } // new Exception().printStackTrace(System.out); }
public void pv() { try { Document document = new Document(PageSize.A4, 36, 72, 108, 180); PdfWriter.getInstance(document, new FileOutputStream("pdfFile.pdf")); File f = new File("abc.txt"); FileReader fr = new FileReader(f); BufferedReader br = new BufferedReader(fr); String i; while ((i = br.readLine()) != null) { document.open(); document.add(new Paragraph(i)); } System.out.println("fetched database is inserted into pdf file"); document.close(); } catch (Exception e) { } }
public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { regService = new RegService(); nuser = (RegForm) form; nuser = regService.authenticate(nuser); String uname = nuser.getUserName(); String pwd = nuser.getFpassword(); String cpwd = nuser.getCpassword(); String acc = nuser.getAccno(); String fn = nuser.getFirstName(); String ln = nuser.getLastName(); String sx = nuser.getSex(); String add = nuser.getAddress(); String co = nuser.getCountry(); String zp = nuser.getZip(); String mb = nuser.getMobile(); String em = nuser.getEmail(); RegForm regForm = (RegForm) form; Rectangle pageSize = new Rectangle(400, 400); pageSize.setBackgroundColor(new java.awt.Color(0xDF, 0xFF, 0xDE)); Document document = new Document(pageSize); PdfWriter.getInstance( document, new FileOutputStream("D:/kowthal training/java/31rep/Report.pdf")); document.open(); PdfPTable table = new PdfPTable(2); table.addCell("UserName"); table.addCell(uname); table.addCell("Password"); table.addCell(pwd); table.addCell("Confirm Password"); table.addCell(cpwd); table.addCell("First Name"); table.addCell(fn); table.addCell("Last Name"); table.addCell(ln); table.addCell("Sex"); table.addCell(sx); table.addCell("Address"); table.addCell(add); table.addCell("Country"); table.addCell(co); table.addCell("Zip Code"); table.addCell(zp); table.addCell("Mobile"); table.addCell(mb); table.addCell("E-mail id"); table.addCell(em); System.out.println("before fetch"); document.add(table); System.out.println("after fetch"); document.add(new Paragraph("Created by Kowthal(947)")); document.close(); target = "success"; return mapping.findForward(target); }
/** * @param shiftsToAccount * @param month * @param year * @return */ public static String createAccounting(ShiftInstance[] shiftsToAccount, int month, int year) { // check if personal Data exists if (!PersonalData.getInstance().isDataSet()) { UtilityBox.getInstance() .displayInfoPopup( "Fehlende Daten", "Um die " + "Abrechnung zu erstellen müssen\npersönliche Daten gespeichert sein."); return null; } boolean success = false; String filePath = "Abrechnungen/" + year; // create directory if nessessary UtilityBox.createDirectory(filePath); // change filePath from directory to file filePath = filePath + "/Abrechnung" + UtilityBox.getMonthString(month) + year + ".pdf"; Document accounting = new Document(); ArrayList<ShiftInstance> rd = new ArrayList<ShiftInstance>(); ArrayList<ShiftInstance> ktp = new ArrayList<ShiftInstance>(); ArrayList<ShiftInstance> baby = new ArrayList<ShiftInstance>(); ArrayList<ShiftInstance> breisach = new ArrayList<ShiftInstance>(); ArrayList<ShiftInstance> kiza = new ArrayList<ShiftInstance>(); ArrayList<ShiftInstance> event = new ArrayList<ShiftInstance>(); ArrayList<ShiftInstance> sc = new ArrayList<ShiftInstance>(); ArrayList<ShiftInstance> concert_hall = new ArrayList<ShiftInstance>(); ArrayList<ShiftInstance> kvs = new ArrayList<ShiftInstance>(); ArrayList<ShiftInstance> elw = new ArrayList<ShiftInstance>(); // add shifts to seperate shift lists for (int i = 0; i < shiftsToAccount.length; i++) { switch (shiftsToAccount[i].getType()) { case KTW: ktp.add(shiftsToAccount[i]); break; case RTW: rd.add(shiftsToAccount[i]); break; case HINTERGRUND: rd.add(shiftsToAccount[i]); break; case ELW: elw.add(shiftsToAccount[i]); break; case EVENT: event.add(shiftsToAccount[i]); break; case SC: sc.add(shiftsToAccount[i]); break; case CONCERT_HALL: concert_hall.add(shiftsToAccount[i]); break; case KVS: kvs.add(shiftsToAccount[i]); break; case BREISACH: breisach.add(shiftsToAccount[i]); break; case KIZA: kiza.add(shiftsToAccount[i]); break; default: break; } } ArrayList<ShiftInstance>[] allShifts = (ArrayList<ShiftInstance>[]) (new ArrayList[] {rd, elw, ktp, baby, breisach, kiza, event, sc, concert_hall, kvs}); try { accounting.setPageSize(PageSize.A4); PdfWriter pdfWriter = PdfWriter.getInstance(accounting, new FileOutputStream(filePath)); accounting.open(); for (int i = 0; i < allShifts.length; i++) { if (!allShifts[i].isEmpty()) { int numberOfPages = ((int) (allShifts[i].size() / 13)) + 1; int counter = allShifts[i].size() - 1; for (int j = 1; j <= numberOfPages; j++) { ArrayList<ShiftInstance> tempShiftInstances = new ArrayList<ShiftInstance>(); for (int k = 0; k < 13; k++) { if (counter < 0) { break; } tempShiftInstances.add(allShifts[i].get(counter)); counter--; } accounting.newPage(); success = createSingleAccounting(accounting, pdfWriter, tempShiftInstances, i * 10 + j); } } } // TODO: for JDK7 use Multicatch } catch (Exception e) { // DocumentException | IOException e) { UtilityBox.getInstance() .displayErrorPopup( "Abrechnung", "Fehler beim " + "Erstellen der Abrechnung:\n" + e.getMessage()); filePath = null; } finally { try { accounting.close(); } catch (Exception e) { UtilityBox.getInstance() .displayErrorPopup( "Abrechnung", "Fehler beim " + "Erstellen der Abrechnung:\nDokument nicht geschlossen:\n" + "" + e.getMessage()); filePath = null; } } return filePath; }