public Document createDocument(String source) { Document document = null; try { document = DocumentHelper.parseText(source); } catch (DocumentException e) { e.printStackTrace(); // To change body of catch statement use File | // Settings | File Templates. } return document; }
/** * Simple parsing and display of the response. This method uses dom4j for parsing XML, feel free * to use anything that comes handy. */ @SuppressWarnings("unchecked") private static void displayResults(InputStream results) throws IOException { try { final SAXReader reader = new SAXReader(); final Document document = reader.read(results); final Iterator<Element> i = document.getRootElement().elementIterator("group"); while (i.hasNext()) { final Element group = i.next(); display(group, 1); } System.out.println(); } catch (DocumentException e) { throw new IOException("Could not parse response: " + e.getMessage()); } finally { if (results != null) { results.close(); } } }
private void generateInvoicePDFByExamination( Examination examination, HttpServletResponse response) throws FileNotFoundException { Document document = new Document(); response.setHeader("Accept-ranges", "bytes"); response.setContentType("application/pdf"); response.setHeader("Expires", "0"); response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0"); response.setHeader("Content-Description", "File Transfer"); response.setHeader("Content-Transfer-Encoding:", "binary"); try { PdfWriter.getInstance(document, response.getOutputStream()); document.open(); addMetaData(document); addTitlePage(document, examination); document.close(); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
public void generatePdf( UserShopForm sForm, List pItems, StoreData pStore, OutputStream pOut, String pImageName, boolean personal, boolean pCatalogOnly, String catalogLocaleCd) throws IOException { mSkuTag = ClwI18nUtil.getMessage(mRequest, "shop.og.text.ourSkuNum", null); try { catalogOnly = pCatalogOnly; initColumnsAdnWidths(sForm.getAppUser()); mSiteData = sForm.getAppUser().getSite(); AccountData mAccount = sForm.getAppUser().getUserAccount(); String ogInvDisplay = mAccount.getPropertyValue(RefCodeNames.PROPERTY_TYPE_CD.INVENTORY_OG_LIST_UI); boolean modernShoppingFl = mSiteData.hasModernInventoryShopping(); // loop through the items to check if the footer needs to have the pack // Disclaimer printed on each page. boolean printProblemPackDisclaimer = false, invItems = false, invAutoOrderItems = false, nonInvItems = false, addNewPage = false; for (int i = 0; i < pItems.size(); i++) { ShoppingCartItemData sci = (ShoppingCartItemData) pItems.get(i); if (sci.getProduct().isPackProblemSku()) { printProblemPackDisclaimer = true; } if (sci.getIsaInventoryItem()) { invItems = true; if (null != mSiteData && mSiteData.isAnInventoryAutoOrderItem(sci.getProduct().getProductId())) { invAutoOrderItems = true; } } else { nonInvItems = true; if (invItems) addNewPage = true; } } Phrase headPhrase = new Phrase(makeChunk("", heading, true)); if (catalogOnly) { String catalogStr = ClwI18nUtil.getMessage(mRequest, "shop.og.text.catalog", null); headPhrase.add(makeChunk(catalogStr, heading, true)); } else { if (sForm.getAppUser().getSite() != null && sForm.getAppUser().getSite().getBSC() != null && sForm.getAppUser().getSite().getBSC().getBusEntityData() != null && sForm.getAppUser().getSite().getBSC().getBusEntityData().getShortDesc() != null) { String subname = sForm.getAppUser().getSite().getBSC().getBusEntityData().getShortDesc() + " "; headPhrase.add(makeChunk(subname, heading, true)); headPhrase.add(makeChunk(" ", heading, true)); } String orderGuideStr = ClwI18nUtil.getMessage(mRequest, "shop.og.text.orderGuide", null); headPhrase.add(makeChunk(orderGuideStr, heading, true)); headPhrase.add(makeChunk(" ", heading, true)); headPhrase.add( makeChunk( sForm.getAppUser().getUserAccount().getBusEntity().getShortDesc(), heading, true)); } HeaderFooter header = new HeaderFooter(headPhrase, true); header.setAlignment(HeaderFooter.ALIGN_RIGHT); // setup the document // pageSize, marginLeft, marginRight, marginTop, marginBottom Document document = new Document(PageSize.A4, 10, 15, 30, 15); PdfWriter writer = PdfWriter.getInstance(document, pOut); String addr = pStore.getPrimaryAddress().getAddress1() + " " + pStore.getPrimaryAddress().getAddress2(); Chunk disclaimer = null; String fmsg = ""; if (invItems) { fmsg += ClwI18nUtil.getMessage(mRequest, "shop.og.text.i-inventoryItem", null) + " "; } if (invAutoOrderItems) { fmsg += ClwI18nUtil.getMessage(mRequest, "shop.og.text.a-autoOrderItem", null) + " "; } if (printProblemPackDisclaimer) { fmsg += ClwI18nUtil.getMessage(mRequest, "shop.og.text.*packAndOrUomMayDifferByRegion", null); } if (fmsg.length() > 0) { disclaimer = makeChunk(fmsg, smallItalic, true); } Phrase footPhrase = makeStoreFooter(pStore, disclaimer, null); HeaderFooter footer = new HeaderFooter(footPhrase, false); footer.setAlignment(HeaderFooter.ALIGN_CENTER); // setup the borders from the header header.setBorder(borderType); footer.setBorder(HeaderFooter.TOP); document.setHeader(header); document.setFooter(footer); document.open(); // voc message String vocMessage = ClwI18nUtil.getMessage(mRequest, "shop.message.vocNotIncluded", null); document.add(makePhrase(vocMessage, smallHeading, true)); document.add(makePhrase(null, smallHeading, true)); drawOGHeader(document, sForm, pImageName, personal); if (modernShoppingFl && !"SEPARATED LIST".equals(ogInvDisplay)) { drawHeader( document, pageNumber, pStore.getStoreBusinessName().getValue(), pImageName, true); String prevCat = null; for (int i = 0; i < pItems.size(); i++) { ShoppingCartItemData sci = (ShoppingCartItemData) pItems.get(i); Table itmTable = makeItemElement(sci); // if the item data will not fit onto the page, // make a new page, and redraw the header. if (writer.fitsPage(itmTable, document.bottomMargin() + 10)) { if (sForm.getOrderBy() == Constants.ORDER_BY_CATEGORY) { if (null == prevCat || !prevCat.equals(sci.getCategoryPath())) { document.add(makePhrase(sci.getCategoryPath(), smallHeading, true)); document.add(makePhrase(null, smallHeading, true)); } prevCat = sci.getCategoryPath(); } } if (!writer.fitsPage(itmTable, document.bottomMargin() + 10)) { document.newPage(); pageNumber = pageNumber + 1; drawHeader( document, pageNumber, pStore.getStoreBusinessName().getValue(), pImageName, true); if (sForm.getOrderBy() == Constants.ORDER_BY_CATEGORY) { document.add(makePhrase(sci.getCategoryPath(), smallHeading, true)); document.add(makePhrase(null, smallHeading, true)); prevCat = sci.getCategoryPath(); } } document.add(itmTable); } } else { if (invItems) { drawHeader( document, pageNumber, pStore.getStoreBusinessName().getValue(), pImageName, true); String prevCat = null; for (int i = 0; i < pItems.size(); i++) { ShoppingCartItemData sci = (ShoppingCartItemData) pItems.get(i); if (sci.getIsaInventoryItem() == false) { continue; } if (sci.getInventoryParValuesSum() <= 0) { // This is an inventory item that is not // allowed for this location. continue; } Table itmTable = makeItemElement(sci); // if the item data will not fit onto the page, // make a new page, and redraw the header. if (writer.fitsPage(itmTable, document.bottomMargin() + 10)) { if (sForm.getOrderBy() == Constants.ORDER_BY_CATEGORY) { if (null == prevCat || !prevCat.equals(sci.getCategoryPath())) { document.add(makePhrase(sci.getCategoryPath(), smallHeading, true)); document.add(makePhrase(null, smallHeading, true)); } prevCat = sci.getCategoryPath(); } } if (!writer.fitsPage(itmTable, document.bottomMargin() + 10)) { document.newPage(); pageNumber = pageNumber + 1; drawHeader( document, pageNumber, pStore.getStoreBusinessName().getValue(), pImageName, true); if (sForm.getOrderBy() == Constants.ORDER_BY_CATEGORY) { document.add(makePhrase(sci.getCategoryPath(), smallHeading, true)); document.add(makePhrase(null, smallHeading, true)); prevCat = sci.getCategoryPath(); } } document.add(itmTable); } } if (nonInvItems) { if (addNewPage) { document.newPage(); pageNumber = pageNumber + 1; } drawHeader( document, pageNumber, pStore.getStoreBusinessName().getValue(), pImageName, false); document.add(makeBlankLine()); String prevCat = null; for (int i = 0; i < pItems.size(); i++) { ShoppingCartItemData sci = (ShoppingCartItemData) pItems.get(i); if (sci.getIsaInventoryItem()) { continue; } Table itmTable = makeItemElement(sci); // Check to see if the category needs to be put out. if (writer.fitsPage(itmTable, document.bottomMargin() + 10)) { if (sForm.getOrderBy() == Constants.ORDER_BY_CATEGORY) { if (null == prevCat || !prevCat.equals(sci.getCategoryPath())) { document.add(makePhrase(sci.getCategoryPath(), smallHeading, true)); document.add(makePhrase(null, smallHeading, true)); } prevCat = sci.getCategoryPath(); } } // if the item data will not fit onto the page, // make a new page, and redraw the header. if (!writer.fitsPage(itmTable, document.bottomMargin() + 10)) { document.newPage(); pageNumber = pageNumber + 1; drawHeader( document, pageNumber, pStore.getStoreBusinessName().getValue(), pImageName, false); if (sForm.getOrderBy() == Constants.ORDER_BY_CATEGORY) { document.add(makePhrase(sci.getCategoryPath(), smallHeading, true)); document.add(makePhrase(null, smallHeading, true)); prevCat = sci.getCategoryPath(); } } document.add(itmTable); } } } // close out the document document.close(); } catch (DocumentException e) { e.printStackTrace(); throw new IOException(e.getMessage()); } }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext()); CourseManager manager = (CourseManager) ctx.getBean("courseManager"); HttpSession session = request.getSession(false); List selectFilterCouList = (List) session.getAttribute("selectFilterCouList"); Date date = new Date(); DateFormat dateFormat = new SimpleDateFormat("yyyy年 MM月 dd日 HH:mm"); String stuTmp[] = new String[selectFilterCouList.size()]; List students = new ArrayList(); for (int i = 0; i < selectFilterCouList.size(); i++) { stuTmp[i] = ((Map) selectFilterCouList.get(i)).get("dtimeOid").toString(); } students = manager.getSeldStuFilterBy(stuTmp, true); Document document = new Document(PageSize.A4, 72, 72, 35, 35); ByteArrayOutputStream ba = new ByteArrayOutputStream(); String year = manager.getNowBy("School_year"); String term = manager.getNowBy("School_term"); // 如果是下學期, 學年要+1 if (Integer.parseInt(term) == 2) { Integer tmpYear = (Integer.parseInt(year) + 1); year = tmpYear.toString(); term = "1"; } else { term = "2"; } try { PdfWriter writer = PdfWriter.getInstance(document, ba); // TODO 路徑 // Image // image=Image.getInstance("http://cap.chit.edu.tw/CIS/pages/images/2002chitWatermark.gif"); // Watermark wamark=new Watermark(image, ((PageSize.A4).width()-image.plainHeight())/2, // ((PageSize.A4).height()-image.plainHeight())/2); // document.add(wamark); Phrase now = doEncode("課程管理系統 " + dateFormat.format(date)); HeaderFooter header = new HeaderFooter(now, false); document.setHeader(header); document.setFooter(header); // Image logo=Image.getInstance("http://192.192.231.32:8080/CIS/pages/images/art.gif"); document.open(); for (int i = 0; i < students.size(); i++) { String buf[] = {((Map) students.get(i)).get("student_no").toString()}; List tmp = manager.getSeldStuFilterBy(buf, false); if (i % 2 != 0) { document.add(new Paragraph(doEncode("\n"))); document.add(new Paragraph(doEncode("\n"))); document.add(new Paragraph(doEncode("\n"))); } document.add(new Paragraph("")); Table table = new Table(6); table.setWidth(100); table.setBorderWidth(0); table.setPadding(5); // table.setSpacing(5); // table.setDefaultHorizontalAlignment(table.ALIGN_CENTER); // table.setDefaultHorizontalAlignment(table.ALIGN_MIDDLE); // table.setDefaultCellBorderColor(new Color(255, 255, 255)); Cell cell = new Cell(); cell.add(doEncode(year)); cell.add(doEncode("學年 第")); cell.add(doEncode(term)); cell.add(doEncode("學期 網路預選失敗通知單")); // cell.addElement(logo); cell.setColspan(6); cell.setHorizontalAlignment(cell.ALIGN_CENTER); cell.setBorderColor(new Color(255, 255, 255)); table.addCell(cell); // ((Map)students.get(i)).get("ClassName2"); cell = new Cell(); // cell.add(doEncode("學生班級: ")); cell.add(doEncode(((Map) students.get(i)).get("ClassName").toString())); cell.add(doEncode("\t學號: ")); cell.add(doEncode(((Map) students.get(i)).get("student_no").toString())); cell.add(doEncode("\t姓名: ")); cell.add(doEncode(((Map) students.get(i)).get("student_name").toString())); cell.setColspan(6); cell.setHorizontalAlignment(cell.ALIGN_LEFT); cell.setBorderColor(new Color(255, 255, 255)); table.addCell(cell); cell = new Cell(); cell.add(doEncode("您在第一階段網路選課所選的下列課程並未成功,請按照規定時間進行網路第二階段選課,以免影響您的權益。")); cell.setColspan(6); cell.setHorizontalAlignment(cell.ALIGN_LEFT); cell.setBorderColor(new Color(255, 255, 255)); table.addCell(cell); document.add(table); float[] widths = {0.2f, 0.3f, 0.1f, 0.1f, 0.1f, 0.2f}; PdfPTable tables = new PdfPTable(widths); tables.setWidthPercentage(100); PdfPCell cells = new PdfPCell(doEncode("開課班級")); tables.addCell(cells); cells = new PdfPCell(doEncode("科目名稱")); tables.addCell(cells); cells = new PdfPCell(doEncode("選別")); tables.addCell(cells); cells = new PdfPCell(doEncode("學分")); tables.addCell(cells); cells = new PdfPCell(doEncode("時數")); tables.addCell(cells); cells = new PdfPCell(doEncode("上課時間")); tables.addCell(cells); for (int j = 0; j < tmp.size(); j++) { cells = new PdfPCell(); cells = new PdfPCell(doEncode(((Map) tmp.get(j)).get("ClassName2").toString())); tables.addCell(cells); cells = new PdfPCell(doEncode(((Map) tmp.get(j)).get("chi_name").toString())); tables.addCell(cells); cells = new PdfPCell(doEncode(getOpt(((Map) tmp.get(j)).get("opt").toString()))); tables.addCell(cells); cells = new PdfPCell(doEncode(((Map) tmp.get(j)).get("credit").toString())); tables.addCell(cells); cells = new PdfPCell(doEncode(((Map) tmp.get(j)).get("thour").toString())); tables.addCell(cells); List dcTmp = manager.getDtimeClassListForOpenCourse(((Map) tmp.get(j)).get("dtimeOid").toString()); StringBuffer strBuf = new StringBuffer(); DtimeClass dtimeClass; for (int x = 0; x < dcTmp.size(); x++) { dtimeClass = (DtimeClass) dcTmp.get(x); strBuf.append("週" + dtimeClass.getWeek() + ":"); strBuf.append(dtimeClass.getBegin() + "~" + dtimeClass.getEnd() + " "); } cells = new PdfPCell(doEncode(strBuf.toString())); tables.addCell(cells); } for (int k = 13; k > tmp.size(); k--) { cells = new PdfPCell(doEncode("\t")); tables.addCell(cells); cells = new PdfPCell(doEncode("\t")); tables.addCell(cells); cells = new PdfPCell(doEncode("\t")); tables.addCell(cells); cells = new PdfPCell(doEncode("\t")); tables.addCell(cells); cells = new PdfPCell(doEncode("\t")); tables.addCell(cells); cells = new PdfPCell(doEncode("\t")); tables.addCell(cells); } document.add(tables); if (i % 2 == 1) { document.newPage(); } } } catch (DocumentException de) { de.printStackTrace(); System.err.println("A Document error:" + de.getMessage()); } document.close(); response.setContentType("application/pdf"); response.setContentLength(ba.size()); ServletOutputStream out = response.getOutputStream(); ba.writeTo(out); ba.close(); out.flush(); out.close(); }