private ByteArrayOutputStream getPdfData( JSONArray data, JSONArray res, HttpServletRequest request) throws ServiceException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { String[] colHeader = getColHeader(); String[] colIndex = getColIndexes(); String[] val = getColValues(colHeader, data); String[] resources = getResourcesColHeader(res, data); String[] mainHeader = {"Dates", "Duration", "Work", "Cost", "Tasks", "Resources"}; Document document = null; if (landscape) { Rectangle recPage = new Rectangle(PageSize.A4.rotate()); recPage.setBackgroundColor(new java.awt.Color(255, 255, 255)); document = new Document(recPage, 15, 15, 30, 30); } else { Rectangle recPage = new Rectangle(PageSize.A4); recPage.setBackgroundColor(new java.awt.Color(255, 255, 255)); document = new Document(recPage, 15, 15, 30, 30); } PdfWriter writer = PdfWriter.getInstance(document, baos); writer.setPageEvent(new EndPage()); document.open(); if (showLogo) { getCompanyDetails(request); addComponyLogo(document, request); } addTitleSubtitle(document); addTable(data, resources, colIndex, colHeader, mainHeader, val, document); document.close(); writer.close(); baos.close(); } catch (ConfigurationException ex) { throw ServiceException.FAILURE("ExportProjectReport.getPdfData", ex); } catch (DocumentException ex) { throw ServiceException.FAILURE("ExportProjectReport.getPdfData", ex); } catch (JSONException e) { throw ServiceException.FAILURE("ExportProjectReport.getPdfData", e); } catch (IOException e) { throw ServiceException.FAILURE("ExportProjectReport.getPdfData", e); } catch (Exception e) { throw ServiceException.FAILURE("ExportProjectReport.getPdfData", e); } return baos; }
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); }
/** * PdfTemplates can be wrapped in an Image. * * @param args no arguments needed */ public static void main(String[] args) { System.out.println("PdfTemplate wrapped in an Image"); // step 1: creation of a document-object Rectangle rect = new Rectangle(PageSize.A4); rect.setBackgroundColor(new Color(238, 221, 88)); Document document = new Document(rect, 50, 50, 50, 50); try { // step 2: we create a writer that listens to the document PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("templateImages.pdf")); // step 3: we open the document document.open(); // step 4: PdfTemplate template = writer.getDirectContent().createTemplate(20, 20); BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED); String text = "Vertical"; float size = 16; float width = bf.getWidthPoint(text, size); template.beginText(); template.setRGBColorFillF(1, 1, 1); template.setFontAndSize(bf, size); template.setTextMatrix(0, 2); template.showText(text); template.endText(); template.setWidth(width); template.setHeight(size + 2); template.sanityCheck(); Image img = Image.getInstance(template); img.setRotationDegrees(90); Chunk ck = new Chunk(img, 0, 0); PdfPTable table = new PdfPTable(3); table.setWidthPercentage(100); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE); PdfPCell cell = new PdfPCell(img); cell.setPadding(4); cell.setBackgroundColor(new Color(0, 0, 255)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell("I see a template on my right"); table.addCell(cell); table.addCell("I see a template on my left"); table.addCell(cell); table.addCell("I see a template everywhere"); table.addCell(cell); table.addCell("I see a template on my right"); table.addCell(cell); table.addCell("I see a template on my left"); Paragraph p1 = new Paragraph("This is a template "); p1.add(ck); p1.add(" just here."); p1.setLeading(img.getScaledHeight() * 1.1f); document.add(p1); document.add(table); Paragraph p2 = new Paragraph("More templates "); p2.setLeading(img.getScaledHeight() * 1.1f); p2.setAlignment(Element.ALIGN_JUSTIFIED); img.scalePercent(70); for (int k = 0; k < 20; ++k) p2.add(ck); document.add(p2); // step 5: we close the document document.close(); } catch (Exception de) { System.err.println(de.getMessage()); } }