@Override public void onEndPage(PdfWriter writer, Document document) { try { Rectangle page = document.getPageSize(); getHeaderFooter(document); // Add page header header.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin()); header.writeSelectedRows( 0, -1, document.leftMargin(), page.getHeight() - 10, writer.getDirectContent()); // Add page footer footer.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin()); footer.writeSelectedRows( 0, -1, document.leftMargin(), document.bottomMargin() - 5, writer.getDirectContent()); // Add page border int bmargin = 8; // border margin PdfContentByte cb = writer.getDirectContent(); cb.rectangle( bmargin, bmargin, page.getWidth() - bmargin * 2, page.getHeight() - bmargin * 2); cb.setColorStroke(Color.LIGHT_GRAY); cb.stroke(); } catch (JSONException e) { Logger.getLogger(ExportProjectReportServlet.class.getName()).log(Level.SEVERE, null, e); throw new ExceptionConverter(e); } }
/** * Método que dibuja los bordes de la página. * * @param writer Creador de documentos. * @param document Documento del informe. */ protected void drawBorders(PdfWriter writer, Document document) { PdfContentByte cb = writer.getDirectContent(); cb.saveState(); // Dibujar el borde de la página cb.setColorStroke(BORDER_COLOR); cb.setLineWidth(0.5f); cb.rectangle( document.left(), document.bottom(), document.right() - document.left(), document.top() - document.bottom()); cb.stroke(); cb.restoreState(); }
/** * Writing vertical text. * * @param args no arguments needed */ public static void main(String[] args) { Document document = new Document(PageSize.A4, 50, 50, 50, 50); try { texts[3] = convertCid(texts[0]); texts[4] = convertCid(texts[1]); texts[5] = convertCid(texts[2]); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("vertical.pdf")); int idx = 0; document.open(); PdfContentByte cb = writer.getDirectContent(); for (int j = 0; j < 2; ++j) { BaseFont bf = BaseFont.createFont("KozMinPro-Regular", encs[j], false); cb.setRGBColorStroke(255, 0, 0); cb.setLineWidth(0); float x = 400; float y = 700; float height = 400; float leading = 30; int maxLines = 6; for (int k = 0; k < maxLines; ++k) { cb.moveTo(x - k * leading, y); cb.lineTo(x - k * leading, y - height); } cb.rectangle(x, y, -leading * (maxLines - 1), -height); cb.stroke(); int status; VerticalText vt = new VerticalText(cb); vt.setVerticalLayout(x, y, height, maxLines, leading); vt.addText(new Chunk(texts[idx++], new Font(bf, 20))); vt.addText(new Chunk(texts[idx++], new Font(bf, 20, 0, Color.blue))); status = vt.go(); System.out.println(status); vt.setAlignment(Element.ALIGN_RIGHT); vt.addText(new Chunk(texts[idx++], new Font(bf, 20, 0, Color.orange))); status = vt.go(); System.out.println(status); document.newPage(); } document.close(); } catch (Exception de) { de.printStackTrace(); } }
/** * Creates documents with some simple annotations. * * @param args no arguments needed */ public static void main(String[] args) { System.out.println("Simple Annotations"); // step 1: creation of a document-object Document document1 = new Document(PageSize.A4, 10, 10, 10, 10); Document document2 = new Document(PageSize.A4, 10, 10, 10, 10); try { // step 2: PdfWriter writer1 = PdfWriter.getInstance(document1, new FileOutputStream("SimpleAnnotations1.pdf")); PdfWriter writer2 = PdfWriter.getInstance(document2, new FileOutputStream("SimpleAnnotations2.pdf")); // step 3: writer2.setPdfVersion(PdfWriter.VERSION_1_5); document1.open(); document2.open(); // step 4: document1.add(new Paragraph("Each square on this page represents an annotation.")); // document1 PdfContentByte cb1 = writer1.getDirectContent(); Annotation a1 = new Annotation( "authors", "Maybe it's because I wanted to be an author myself that I wrote iText.", 250f, 700f, 350f, 800f); document1.add(a1); Annotation a2 = new Annotation(250f, 550f, 350f, 650f, new URL("http://www.lowagie.com/iText/")); document1.add(a2); Annotation a3 = new Annotation(250f, 400f, 350f, 500f, "http://www.lowagie.com/iText"); document1.add(a3); Image image = Image.getInstance("iText.gif"); image.setAnnotation(a3); document1.add(image); Annotation a4 = new Annotation(250f, 250f, 350f, 350f, PdfAction.LASTPAGE); document1.add(a4); // draw rectangles to show where the annotations were added cb1.rectangle(250, 700, 100, 100); cb1.rectangle(250, 550, 100, 100); cb1.rectangle(250, 400, 100, 100); cb1.rectangle(250, 250, 100, 100); cb1.stroke(); // more content document1.newPage(); for (int i = 0; i < 5; i++) { document1.add(new Paragraph("blahblahblah")); } document1.add( new Annotation("blahblah", "Adding an annotation without specifying coordinates")); for (int i = 0; i < 3; i++) { document1.add(new Paragraph("blahblahblah")); } document1.newPage(); document1.add(new Chunk("marked chunk").setLocalDestination("mark")); // document2 document2.add(new Paragraph("Each square on this page represents an annotation.")); PdfContentByte cb2 = writer2.getDirectContent(); Annotation a5 = new Annotation(100f, 700f, 200f, 800f, "cards.mpg", "video/mpeg", true); document2.add(a5); Annotation a6 = new Annotation(100f, 550f, 200f, 650f, "SimpleAnnotations1.pdf", "mark"); document2.add(a6); Annotation a7 = new Annotation(100f, 400f, 200f, 500f, "SimpleAnnotations1.pdf", 2); document2.add(a7); Annotation a8 = new Annotation(100f, 250f, 200f, 350f, "C://windows/notepad.exe", null, null, null); document2.add(a8); // draw rectangles to show where the annotations were added cb2.rectangle(100, 700, 100, 100); cb2.rectangle(100, 550, 100, 100); cb2.rectangle(100, 400, 100, 100); cb2.rectangle(100, 250, 100, 100); cb2.stroke(); } catch (Exception de) { de.printStackTrace(); } // step 5: we close the document document1.close(); document2.close(); }
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); boolean b = false; if (request.getParameter("count") != null) { b = true; } List students = (List) session.getAttribute("students"); ByteArrayOutputStream ba = new ByteArrayOutputStream(); try { Document document = new Document(PageSize.A6.rotate(), 5, 5, 5, 5); PdfWriter writer = PdfWriter.getInstance(document, ba); document.open(); Image image = null; Clazz clazz; Student stmd; String SchoolName; String DeptName; StringBuilder StudentNo; Map map; Map StmdCardNum; // writer = PdfWriter.getInstance(document, ba); PdfContentByte cb = writer.getDirectContent(); BaseFont bf = BaseFont.createFont("/kaiu.ttf", "Identity-H", BaseFont.EMBEDDED); for (int i = 0; i < students.size(); i++) { stmd = (Student) manager .hqlGetBy( "FROM Student WHERE studentNo='" + ((Map) students.get(i)).get("student_no") + "'") .get(0); clazz = (Clazz) manager.hqlGetBy("FROM Clazz WHERE ClassNo='" + stmd.getDepartClass() + "'").get(0); map = manager.ezGetMap( "SELECT * FROM dept WHERE no='" + clazz.getClassNo().substring(0, 4) + "'"); try { // 系所名 DeptName = map.get("fname").toString(); } catch (Exception e) { DeptName = ""; } try { // 部制名 SchoolName = map.get("school_name").toString(); } catch (Exception e) { SchoolName = ""; } StudentNo = new StringBuilder(stmd.getStudentNo()); // List list=manager.hqlGetBy("FROM StdImage WHERE studentNo='"+StudentNo+"'"); /* if(b&&manager.testOnlineServer()){//寫入補發記錄 try{ //真實學號 manager.executeSql("INSERT INTO StmdCardNum (student_no, card_num)VALUES('"+stmd.getStudentNo()+"', 1)"); }catch(Exception e){ manager.executeSql("UPDATE StmdCardNum SET card_num=card_num+1 WHERE student_no='"+stmd.getStudentNo()+"'"); } StudentNo.append(manager.ezGetString("SELECT card_num FROM StmdCardNum WHERE student_no='"+stmd.getStudentNo()+"'")); sendMailToLib(stmd, StudentNo.toString(), request); }else{ //檢查是否已發卡 StmdCardNum=manager.ezGetMap("SELECT * FROM StmdCardNum WHERE student_no='"+stmd.getStudentNo()+"'"); if(StmdCardNum!=null){ StudentNo.append(manager.ezGetString("SELECT card_num FROM StmdCardNum WHERE student_no='"+stmd.getStudentNo()+"'")); } } */ StmdCardNum = manager.ezGetMap("SELECT * FROM StmdCardNum WHERE student_no='"); // 學號不滿8碼補空白, 未來改為9碼時? if (StudentNo.length() <= 8) { for (int j = StudentNo.length(); j < 8; j++) { StudentNo.append(" "); } } // 照片 try { image = getImage(StudentNo.toString()); } catch (Exception e) { continue; } if (image == null) continue; image.scaleAbsolute(54.94f, 74.37f); image.setAbsolutePosition(18, 166); // 條碼 Barcode39 code39 = new Barcode39(); code39.setCode(StudentNo.toString()); code39.setBarHeight(20); code39.setX(0.95f); code39.setStartStopText(false); code39.setGuardBars(false); code39.setExtended(false); code39.setChecksumText(false); code39.setSize(-1f); Image imageCode39 = code39.createImageWithBarcode(cb, null, null); imageCode39.setAbsolutePosition(83.15f, 152); cb.setColorStroke(Color.white); cb.rectangle(18, 150, 54.94f, 9); // 30 cb.setLineWidth(10); cb.stroke(); cb.closePath(); cb.closePathEoFillStroke(); cb.closePathFillStroke(); cb.closePathStroke(); // 文字 cb.beginText(); // 部制 cb.setFontAndSize(bf, 10); cb.showTextAligned(PdfContentByte.ALIGN_LEFT, SchoolName, 118, 229, 0); cb.endText(); cb.beginText(); // 科系 cb.setFontAndSize(bf, 10); if (DeptName.length() >= 9) { cb.setFontAndSize(bf, 8); } if (DeptName.length() >= 14) { cb.setFontAndSize(bf, 6); } cb.showTextAligned(PdfContentByte.ALIGN_LEFT, DeptName, 118, 215, 0); cb.endText(); cb.beginText(); // 學號 cb.setFontAndSize(bf, 10); cb.showTextAligned(PdfContentByte.ALIGN_LEFT, stmd.getStudentNo(), 118, 201, 0); cb.endText(); cb.beginText(); // 姓名 cb.setFontAndSize(bf, 10); cb.showTextAligned(PdfContentByte.ALIGN_LEFT, stmd.getStudentName(), 118, 187, 0); cb.endText(); document.add(image); document.add(imageCode39); document.newPage(); // bf=null; } document.close(); writer.close(); response.setHeader( "Content-Disposition", "attachment;filename=EmplCard" + Math.random() * 10 + ".pdf"); response.setContentLength(ba.size()); response.flushBuffer(); ServletOutputStream out = response.getOutputStream(); ba.writeTo(out); ba.close(); out.flush(); } catch (Exception e) { e.printStackTrace(); try { Document document = new Document(PageSize.A6.rotate(), 5, 5, 5, 5); PdfWriter writer = PdfWriter.getInstance(document, ba); document.open(); PdfContentByte cb = writer.getDirectContent(); BaseFont bf = BaseFont.createFont("/kaiu.ttf", "Identity-H", BaseFont.EMBEDDED); cb.beginText(); // 部制 cb.setFontAndSize(bf, 10); cb.showTextAligned(PdfContentByte.ALIGN_LEFT, "讀取資料有誤:" + e, 120, 226, 0); cb.endText(); // document.add(cb ); document.close(); document = null; } catch (DocumentException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } response.setContentType("text/html; charset=UTF-8"); response.setContentType("application/pdf"); response.setHeader("Content-disposition", "attachment;filename=TechTimetable.pdf"); response.setContentLength(ba.size()); ServletOutputStream out = response.getOutputStream(); ba.writeTo(out); out.flush(); } }