List of usage examples for com.lowagie.text Font Font
public Font(int family, float size, int style)
From source file:com.afunms.report.abstraction.ExcelReport1.java
public void createReport_OperEquipmentWord(String filename, String opername, String contactname, String contactphone) throws DocumentException, IOException { if (impReport.getTable() == null) { fileName = null;//from w w w . j a v a 2 s . c o m return; } try { // Document document = new Document(PageSize.A4); // (Writer)document(Writer) RtfWriter2.getInstance(document, new FileOutputStream(filename)); document.open(); // new File(filename) BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); // Font titleFont = new Font(bfChinese, 12, Font.BOLD); // Font contextFont = new Font(bfChinese, 12, Font.NORMAL); // title.setFont(titleFont); List equipmentlist = (List) reportHash.get("equipmentlist"); Paragraph context = new Paragraph(); // context.setAlignment(Element.ALIGN_CENTER); // context.setFont(contextFont); // // context.setSpacingBefore(0); // // context.setFirstLineIndent(6); // document.add(context); // document.add(new Paragraph("\n")); Table aTable = new Table(7); // float[] widths = { 220f, 220f, 220f, 110f, 110f, 110f, 110f, 220f }; // aTable.setWidths(widths); aTable.setWidth(100); // 90% aTable.setAlignment(Element.ALIGN_CENTER);// aTable.setAutoFillEmptyCells(true); // // aTable.setBorderWidth(1); // // aTable.setBorderColor(new Color(0, 125, 255)); // // aTable.setPadding(2);// // aTable.setSpacing(4);// // aTable.setBorder(2);// Cell cell = null; cell = new Cell(new Phrase(opername + "", titleFont)); cell.setColspan(7); cell.setHorizontalAlignment(Element.ALIGN_CENTER); // cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // aTable.addCell(cell); cell = new Cell(new Phrase(":" + opername, titleFont)); cell.setColspan(2); this.setCellFormat(cell, false); cell.setHorizontalAlignment(Element.ALIGN_LEFT); // cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // aTable.addCell(cell); cell = new Cell(new Phrase(":" + contactname, titleFont)); cell.setColspan(2); this.setCellFormat(cell, false); cell.setHorizontalAlignment(Element.ALIGN_LEFT); // cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // aTable.addCell(cell); cell = new Cell(new Phrase(":" + contactphone, titleFont)); cell.setColspan(3); this.setCellFormat(cell, false); cell.setHorizontalAlignment(Element.ALIGN_LEFT); // cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // aTable.addCell(cell); cell = new Cell(new Phrase("", titleFont)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); // cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // aTable.addCell(cell); cell = new Cell(new Phrase("", titleFont)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); // cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // aTable.addCell(cell); cell = new Cell(new Phrase("U", titleFont)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); // cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // aTable.addCell(cell); cell = new Cell(new Phrase("", titleFont)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); // cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // aTable.addCell(cell); cell = new Cell(new Phrase("IP", titleFont)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); // cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // aTable.addCell(cell); cell = new Cell(new Phrase("", titleFont)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); // cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // aTable.addCell(cell); cell = new Cell(new Phrase("", titleFont)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); // cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // aTable.addCell(cell); OperCabinet operCabinet = null; if (equipmentlist != null && equipmentlist.size() > 0) { for (int i = 0; i < equipmentlist.size(); i++) { operCabinet = (OperCabinet) equipmentlist.get(i); cell = new Cell(new Phrase(operCabinet.getRoomname(), contextFont)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); // cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // aTable.addCell(cell); cell = new Cell(new Phrase(operCabinet.getCabinetname(), contextFont)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); // cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // aTable.addCell(cell); cell = new Cell(new Phrase(operCabinet.getUseu(), contextFont)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); // cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // aTable.addCell(cell); cell = new Cell(new Phrase(operCabinet.getEquipmentname(), contextFont)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); // cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // aTable.addCell(cell); cell = new Cell(new Phrase(operCabinet.getIpaddress(), contextFont)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); // cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // aTable.addCell(cell); cell = new Cell(new Phrase(operCabinet.getCabinetname(), contextFont)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); // cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // aTable.addCell(cell); cell = new Cell(new Phrase(operCabinet.getContactphone(), contextFont)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); // cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // aTable.addCell(cell); } } document.add(aTable); document.close(); } catch (Exception e) { // TODO: handle exception } }
From source file:com.allinfinance.system.util.PdfUtil.java
License:Open Source License
public static void create(String mchtId, String selMchtId, String path, List<Object[]> list, LinkedHashMap<String, List<Object[]>> map, Set<String> set) throws Exception { BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); ////from w ww . jav a 2 s . co m Font font17 = new Font(bfChinese, 17, Font.BOLD); Font font8 = new Font(bfChinese, 8, Font.NORMAL); Font font9 = new Font(bfChinese, 9, Font.NORMAL); Font font9Bold = new Font(bfChinese, 9, Font.BOLD); Font font8Red = new Font(bfChinese, 8, Font.NORMAL); font8Red.setColor(Color.RED); Font font8Green = new Font(bfChinese, 8, Font.NORMAL); font8Green.setColor(Color.GREEN); logger.info("Starting build document..."); Document document = new Document(PageSize.A4, 36, 36, 36, 36); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path)); document.open(); LINECANVAS border = new LINECANVAS(); // float[] widths = { 0.1f, 0.35f, 0.35f, 0.1f, 0.1f }; PdfPTable table = new PdfPTable(widths); table.setWidthPercentage(100); table.getDefaultCell().setBorder(PdfPCell.NO_BORDER); table.getDefaultCell().setFixedHeight(12); //CELL PdfPCell cellMchntId = new PdfPCell(new Paragraph(mchtId, font8)); cellMchntId.setBorder(PdfPCell.BOTTOM); cellMchntId.setHorizontalAlignment(Element.ALIGN_CENTER); PdfPCell cellMchntName = new PdfPCell(new Paragraph(InformationUtil.getMchtName(mchtId), font8)); cellMchntName.setBorder(PdfPCell.BOTTOM); cellMchntName.setHorizontalAlignment(Element.ALIGN_CENTER); String point = InformationUtil.getCurPaperPoint(selMchtId); PdfPCell cellPoint; cellPoint = new PdfPCell(new Paragraph(point, font8Red)); cellPoint.setBorder(PdfPCell.NO_BORDER); cellPoint.setHorizontalAlignment(Element.ALIGN_CENTER); PdfPCell cellLevel; String level = InformationUtil.getCurPaperLevel(selMchtId); if (Integer.valueOf(point) >= 60) { cellLevel = new PdfPCell(new Paragraph(level, font8Green)); } else { cellLevel = new PdfPCell(new Paragraph(level, font8Red)); } cellLevel.setBorder(PdfPCell.NO_BORDER); cellLevel.setHorizontalAlignment(Element.ALIGN_CENTER); // Image img = Image.getInstance( ServletActionContext.getServletContext().getResource("/ext/resources/images/Title_logo.gif")); img.scalePercent(70); float w = img.getScaledWidth(); float h = img.getScaledHeight(); writer.getDirectContent().addImage(img, w, 0, 0, h, 36, PageSize.A4.getHeight() - 36 - h); // PdfPCell cell = new PdfPCell(new Paragraph("?", font17)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setColspan(5); cell.setFixedHeight(h); cell.setBorder(PdfPCell.NO_BORDER); table.addCell(cell); cell = new PdfPCell(); cell.setFixedHeight(20); cell.setColspan(5); cell.setBorder(PdfPCell.NO_BORDER); table.addCell(cell); table.addCell(new Paragraph("?", font8)); table.addCell(cellMchntId); table.addCell(" "); table.addCell(new Paragraph(": ", font8)); table.addCell(cellPoint); table.addCell(new Paragraph("??", font8)); table.addCell(cellMchntName); table.addCell(" "); table.addCell(new Paragraph(": ", font8)); table.addCell(cellLevel); document.add(table); document.add(new Paragraph("\n\n")); //? PdfPTable t = new PdfPTable(1); Iterator<Object[]> it0 = list.iterator(); int i = 1; while (it0.hasNext()) { Object[] obj = it0.next(); PdfPCell c = new PdfPCell(); c.addElement(new Paragraph("Q" + String.valueOf(i++) + "" + obj[1].toString(), font9Bold)); List<Object[]> opts = map.get(obj[0].toString()); String opt = ""; Iterator<Object[]> it1 = opts.iterator(); while (it1.hasNext()) { Object[] o = it1.next(); if (set.contains(o[0])) { opt += "? "; opt += o[1].toString(); opt += " "; } else { opt += " "; opt += o[1].toString(); opt += " "; } } c.addElement(new Paragraph(opt.trim(), font9)); c.setBorder(PdfPCell.NO_BORDER); if (i - 1 != list.size()) { c.setCellEvent(border); } t.addCell(c); } PdfPTable oTable = new PdfPTable(1); oTable.setWidthPercentage(100); PdfPCell ce = new PdfPCell(t); ce.setBorderColor(Color.GRAY); oTable.addCell(ce); document.add(oTable); document.close(); logger.info("Finish build document..."); }
From source file:com.allinfinance.system.util.PdfUtil.java
License:Open Source License
public static void create(String path, List<Object[]> list, LinkedHashMap<String, List<Object[]>> map) throws Exception { BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); ///* www .ja v a 2 s . co m*/ Font font17 = new Font(bfChinese, 17, Font.BOLD); Font font8 = new Font(bfChinese, 8, Font.NORMAL); Font font10 = new Font(bfChinese, 10, Font.NORMAL); Font font10Bold = new Font(bfChinese, 10, Font.BOLD); Font font8Red = new Font(bfChinese, 8, Font.NORMAL); font8Red.setColor(Color.RED); Font font8Green = new Font(bfChinese, 8, Font.NORMAL); font8Green.setColor(Color.GREEN); logger.info("Starting build document..."); Document document = new Document(PageSize.A4, 36, 36, 36, 36); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path)); document.open(); LINECANVAS border = new LINECANVAS(); // float[] widths = { 0.1f, 0.35f, 0.55f }; PdfPTable table = new PdfPTable(widths); table.setWidthPercentage(100); table.getDefaultCell().setBorder(PdfPCell.NO_BORDER); table.getDefaultCell().setFixedHeight(12); //CELL PdfPCell cellMchntId = new PdfPCell(new Paragraph("XXXXXXXXXXXXXXX", font8)); cellMchntId.setBorder(PdfPCell.BOTTOM); cellMchntId.setHorizontalAlignment(Element.ALIGN_CENTER); PdfPCell cellMchntName = new PdfPCell(new Paragraph("?", font8)); cellMchntName.setBorder(PdfPCell.BOTTOM); cellMchntName.setHorizontalAlignment(Element.ALIGN_CENTER); // Image img = Image.getInstance( ServletActionContext.getServletContext().getResource("/ext/resources/images/Title_logo.gif")); img.scalePercent(70); float w = img.getScaledWidth(); float h = img.getScaledHeight(); writer.getDirectContent().addImage(img, w, 0, 0, h, 36, PageSize.A4.getHeight() - 36 - h); // PdfPCell cell = new PdfPCell(new Paragraph("?", font17)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setColspan(3); cell.setFixedHeight(h); cell.setBorder(PdfPCell.NO_BORDER); table.addCell(cell); cell = new PdfPCell(); cell.setFixedHeight(20); cell.setColspan(3); cell.setBorder(PdfPCell.NO_BORDER); table.addCell(cell); table.addCell(new Paragraph("?", font8)); table.addCell(cellMchntId); table.addCell(" "); table.addCell(new Paragraph("??", font8)); table.addCell(cellMchntName); table.addCell(" "); document.add(table); document.add(new Paragraph("\n\n")); //? PdfPTable t = new PdfPTable(1); Iterator<Object[]> it0 = list.iterator(); int i = 1; while (it0.hasNext()) { Object[] obj = it0.next(); PdfPCell c = new PdfPCell(); c.addElement(new Paragraph("Q" + String.valueOf(i++) + "" + obj[1].toString(), font10Bold)); List<Object[]> opts = map.get(obj[0].toString()); String opt = ""; Iterator<Object[]> it1 = opts.iterator(); while (it1.hasNext()) { Object[] o = it1.next(); opt += " "; opt += o[1].toString(); opt += " "; } c.addElement(new Paragraph(opt.trim(), font10)); c.setBorder(PdfPCell.NO_BORDER); if (i - 1 != list.size()) { c.setCellEvent(border); } t.addCell(c); } PdfPTable oTable = new PdfPTable(1); oTable.setWidthPercentage(100); PdfPCell ce = new PdfPCell(t); ce.setBorderColor(Color.GRAY); oTable.addCell(ce); document.add(oTable); document.close(); logger.info("Finish build document..."); }
From source file:com.aripd.clms.service.ContractServiceBean.java
@Override public void generatePdf(ContractEntity contract) { String baseFontUrl = "/fonts/Quivira.otf"; FontFactory.register(baseFontUrl);//from ww w . jav a 2 s .c om ByteArrayOutputStream output = new ByteArrayOutputStream(); try { BaseFont bf = BaseFont.createFont(baseFontUrl, BaseFont.IDENTITY_H, BaseFont.EMBEDDED); Font font18n = new Font(bf, 18, Font.NORMAL); Font font12n = new Font(bf, 12, Font.NORMAL); Font font8n = new Font(bf, 8, Font.NORMAL); Font font8nbu = new Font(bf, 8, Font.BOLD | Font.UNDERLINE); Font font8ng = new Font(bf, 8, Font.NORMAL, Color.DARK_GRAY); Font font6n = new Font(bf, 6, Font.NORMAL); Document document = new Document(PageSize.A4); PdfWriter writer = PdfWriter.getInstance(document, output); document.open(); addMetaData(document); addTitlePage(document, contract); Image imgBlue = Image.getInstance(1, 1, 3, 8, new byte[] { (byte) 0, (byte) 0, (byte) 255, }); imgBlue.scaleAbsolute(document.getPageSize().getWidth(), 10); imgBlue.setAbsolutePosition(0, document.getPageSize().getHeight() - imgBlue.getScaledHeight()); PdfImage stream = new PdfImage(imgBlue, "", null); stream.put(new PdfName("ITXT_SpecialId"), new PdfName("123456789")); PdfIndirectObject ref = writer.addToBody(stream); imgBlue.setDirectReference(ref.getIndirectReference()); document.add(imgBlue); PdfPTable table = new PdfPTable(2); table.setWidthPercentage(100); PdfPCell cell = new PdfPCell(new Paragraph(contract.getName(), font18n)); cell.setBorder(Rectangle.NO_BORDER); cell.setColspan(2); cell.setPadding(5); table.addCell(cell); cell = new PdfPCell(new Paragraph("Version: " + contract.getVersion(), font8n)); cell.setBorder(Rectangle.NO_BORDER); cell.setColspan(2); cell.setPadding(5); table.addCell(cell); cell = new PdfPCell(new Paragraph("Review: " + contract.getReview(), font8n)); cell.setBorder(Rectangle.NO_BORDER); cell.setColspan(2); cell.setPadding(5); table.addCell(cell); cell = new PdfPCell(new Paragraph(contract.getRemark(), font12n)); cell.setBorder(Rectangle.NO_BORDER); cell.setColspan(2); cell.setPadding(5); table.addCell(cell); document.add(table); // Start a new page document.newPage(); HTMLWorker htmlWorker = new HTMLWorker(document); htmlWorker.parse(new StringReader(contract.getRemark())); // Start a new page document.newPage(); document.add(new Paragraph("Review Board", font18n)); document.add(new LineSeparator(0.5f, 100, null, 0, -5)); table = new PdfPTable(3); table.setWidthPercentage(100); cell = new PdfPCell(new Paragraph("Review Board", font18n)); cell.setColspan(3); table.addCell(cell); cell = new PdfPCell(new Paragraph("Version", font12n)); table.addCell(cell); cell = new PdfPCell(new Paragraph("Date", font12n)); table.addCell(cell); cell = new PdfPCell(new Paragraph("Review", font12n)); table.addCell(cell); for (HistoryContractEntity history : historyContractService.listing(contract)) { cell = new PdfPCell(new Paragraph(history.getVersion().toString(), font8n)); table.addCell(cell); cell = new PdfPCell(new Paragraph(history.getStartdate().toString(), font8n)); table.addCell(cell); cell = new PdfPCell(new Paragraph(history.getReview(), font8n)); table.addCell(cell); } document.add(table); document.close(); FacesContext context = FacesContext.getCurrentInstance(); HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse(); response.reset(); response.addHeader("Content-Type", "application/force-download"); String filename = URLEncoder.encode(contract.getName() + ".pdf", "UTF-8"); // response.addHeader("Content-Disposition", "attachment; filename=\"" + filename + "\""); response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + filename); response.getOutputStream().write(output.toByteArray()); response.getOutputStream().flush(); context.responseComplete(); context.renderResponse(); } catch (BadPdfFormatException | IOException ex) { Logger.getLogger(ContractServiceBean.class.getName()).log(Level.SEVERE, null, ex); } catch (DocumentException ex) { Logger.getLogger(ContractServiceBean.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:com.fcore.base.fileSystem.utils.FileUtil.java
/** * convert txt file to pdf //w ww. j a v a 2 s . c om * @param originalPath * @param dirPath */ public static void txt2pdf(String originalPath, String dirPath) { com.lowagie.text.Document document = null; BufferedReader read = null; long old = System.currentTimeMillis(); try { document = new com.lowagie.text.Document(PageSize.A4, 80, 80, 60, 30); PdfWriter.getInstance(document, new FileOutputStream(dirPath)); document.open(); BaseFont bfChinese = BaseFont.createFont(ReadCreatePdf.class.getResource("/") + "/simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); Font FontChinese = new Font(bfChinese, 18, Font.NORMAL); FileInputStream fstream = new FileInputStream(originalPath); DataInputStream in = new DataInputStream(fstream); read = new BufferedReader(new InputStreamReader(in, "gb2312")); String line = null; while ((line = read.readLine()) != null) { Paragraph t = new Paragraph(line, FontChinese); t.setAlignment(Element.ALIGN_LEFT); t.setLeading(20.0f); document.add(t); } } catch (Exception e) { e.printStackTrace(); } finally { try { read.close(); document.close(); } catch (IOException e) { e.printStackTrace(); } } long now = System.currentTimeMillis(); System.out.println("" + ((now - old) / 1000.0) + "\n\n" + "?:" + dirPath); }
From source file:com.geek.tutorial.itext.text.Chunk_Example.java
License:Open Source License
public Chunk_Example() throws Exception { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream("chunk_example.pdf")); document.open();/*from w w w . j a v a 2s. c o m*/ Font font = new Font(Font.COURIER, 10, Font.BOLD); // 1 font.setColor(new Color(0x92, 0x90, 0x83)); Chunk chunk = new Chunk("testing text element", font); // 2 chunk.setBackground(new Color(0xff, 0xe4, 0x00)); // 3 document.add(chunk); // 4 document.close(); }
From source file:com.geek.tutorial.itext.text.Paragraph_Example.java
License:Open Source License
public Paragraph_Example() throws Exception { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream("paragraph_example.pdf")); document.open();//from w ww .j av a 2 s .c o m Font font = new Font(Font.COURIER, 10, Font.BOLD); font.setColor(new Color(0x92, 0x90, 0x83)); Chunk chunk = new Chunk("testing text element ", font); chunk.setBackground(new Color(0xff, 0xe4, 0x00)); Phrase phrase = new Phrase(20, "This is initial text. "); for (int i = 0; i < 10; i++) { phrase.add(chunk); } Paragraph paragraph = new Paragraph(); // 1 paragraph.add(phrase); // 2 document.add(paragraph); // 3 document.add(paragraph); // 4 document.close(); }
From source file:com.geek.tutorial.itext.text.Phrase_Example.java
License:Open Source License
public Phrase_Example() throws Exception { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream("phrase_example.pdf")); document.open();//w w w . j a va2 s. c o m Font font = new Font(Font.COURIER, 10, Font.BOLD); font.setColor(new Color(0x92, 0x90, 0x83)); Chunk chunk = new Chunk("testing text element ", font); chunk.setBackground(new Color(0xff, 0xe4, 0x00)); Phrase phrase = new Phrase(20, "This is initial text. "); // 1 for (int i = 0; i < 10; i++) { phrase.add(chunk); // 2 } document.add(phrase); // 3 document.close(); }
From source file:com.huateng.system.util.PdfUtil.java
License:Open Source License
public static void create(String mchtId, String selMchtId, String path, List<Object[]> list, LinkedHashMap<String, List<Object[]>> map, Set<String> set) throws Exception { BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); ///*from w w w. j a v a 2 s . com*/ Font font17 = new Font(bfChinese, 17, Font.BOLD); Font font8 = new Font(bfChinese, 8, Font.NORMAL); Font font9 = new Font(bfChinese, 9, Font.NORMAL); Font font9Bold = new Font(bfChinese, 9, Font.BOLD); Font font8Red = new Font(bfChinese, 8, Font.NORMAL); font8Red.setColor(Color.RED); Font font8Green = new Font(bfChinese, 8, Font.NORMAL); font8Green.setColor(Color.GREEN); logger.info("Starting build document..."); Document document = new Document(PageSize.A4, 36, 36, 36, 36); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path)); document.open(); LINECANVAS border = new LINECANVAS(); // float[] widths = { 0.1f, 0.35f, 0.35f, 0.1f, 0.1f }; PdfPTable table = new PdfPTable(widths); table.setWidthPercentage(100); table.getDefaultCell().setBorder(PdfPCell.NO_BORDER); table.getDefaultCell().setFixedHeight(12); //CELL PdfPCell cellMchntId = new PdfPCell(new Paragraph(mchtId, font8)); cellMchntId.setBorder(PdfPCell.BOTTOM); cellMchntId.setHorizontalAlignment(Element.ALIGN_CENTER); PdfPCell cellMchntName = new PdfPCell(new Paragraph(InformationUtil.getMchtName(mchtId), font8)); cellMchntName.setBorder(PdfPCell.BOTTOM); cellMchntName.setHorizontalAlignment(Element.ALIGN_CENTER); String point = InformationUtil.getCurPaperPoint(selMchtId); PdfPCell cellPoint; cellPoint = new PdfPCell(new Paragraph(point, font8Red)); cellPoint.setBorder(PdfPCell.NO_BORDER); cellPoint.setHorizontalAlignment(Element.ALIGN_CENTER); PdfPCell cellLevel; String level = InformationUtil.getCurPaperLevel(selMchtId); if (Integer.valueOf(point) >= 60) { cellLevel = new PdfPCell(new Paragraph(level, font8Green)); } else { cellLevel = new PdfPCell(new Paragraph(level, font8Red)); } cellLevel.setBorder(PdfPCell.NO_BORDER); cellLevel.setHorizontalAlignment(Element.ALIGN_CENTER); // Image img = Image.getInstance( ServletActionContext.getServletContext().getResource("/ext/resources/images/Title_logo.gif")); img.scalePercent(70); float w = img.getScaledWidth(); float h = img.getScaledHeight(); writer.getDirectContent().addImage(img, w, 0, 0, h, 36, PageSize.A4.getHeight() - 36 - h); // PdfPCell cell = new PdfPCell(new Paragraph("?", font17)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setColspan(5); cell.setFixedHeight(h); cell.setBorder(PdfPCell.NO_BORDER); table.addCell(cell); cell = new PdfPCell(); cell.setFixedHeight(20); cell.setColspan(5); cell.setBorder(PdfPCell.NO_BORDER); table.addCell(cell); table.addCell(new Paragraph("?", font8)); table.addCell(cellMchntId); table.addCell(" "); table.addCell(new Paragraph(": ", font8)); table.addCell(cellPoint); table.addCell(new Paragraph("??", font8)); table.addCell(cellMchntName); table.addCell(" "); table.addCell(new Paragraph(": ", font8)); table.addCell(cellLevel); document.add(table); document.add(new Paragraph("\n\n")); //? PdfPTable t = new PdfPTable(1); Iterator<Object[]> it0 = list.iterator(); int i = 1; while (it0.hasNext()) { Object[] obj = it0.next(); PdfPCell c = new PdfPCell(); c.addElement(new Paragraph("Q" + String.valueOf(i++) + "" + obj[1].toString(), font9Bold)); List<Object[]> opts = map.get(obj[0].toString()); String opt = ""; Iterator<Object[]> it1 = opts.iterator(); while (it1.hasNext()) { Object[] o = it1.next(); if (set.contains(o[0])) { opt += "? "; opt += o[1].toString(); opt += " "; } else { opt += " "; opt += o[1].toString(); opt += " "; } } c.addElement(new Paragraph(opt.trim(), font9)); c.setBorder(PdfPCell.NO_BORDER); if (i - 1 != list.size()) { c.setCellEvent(border); } t.addCell(c); } PdfPTable oTable = new PdfPTable(1); oTable.setWidthPercentage(100); PdfPCell ce = new PdfPCell(t); ce.setBorderColor(Color.GRAY); oTable.addCell(ce); document.add(oTable); document.close(); logger.info("Finish build document..."); }
From source file:com.huateng.system.util.PdfUtil.java
License:Open Source License
public static void create(String path, List<Object[]> list, LinkedHashMap<String, List<Object[]>> map) throws Exception { BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); //// w w w . j a v a2 s . c o m Font font17 = new Font(bfChinese, 17, Font.BOLD); Font font8 = new Font(bfChinese, 8, Font.NORMAL); Font font10 = new Font(bfChinese, 10, Font.NORMAL); Font font10Bold = new Font(bfChinese, 10, Font.BOLD); Font font8Red = new Font(bfChinese, 8, Font.NORMAL); font8Red.setColor(Color.RED); Font font8Green = new Font(bfChinese, 8, Font.NORMAL); font8Green.setColor(Color.GREEN); logger.info("Starting build document..."); Document document = new Document(PageSize.A4, 36, 36, 36, 36); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path)); document.open(); LINECANVAS border = new LINECANVAS(); // float[] widths = { 0.1f, 0.35f, 0.55f }; PdfPTable table = new PdfPTable(widths); table.setWidthPercentage(100); table.getDefaultCell().setBorder(PdfPCell.NO_BORDER); table.getDefaultCell().setFixedHeight(12); //CELL PdfPCell cellMchntId = new PdfPCell(new Paragraph("XXXXXXXXXXXXXXX", font8)); cellMchntId.setBorder(PdfPCell.BOTTOM); cellMchntId.setHorizontalAlignment(Element.ALIGN_CENTER); PdfPCell cellMchntName = new PdfPCell(new Paragraph("?", font8)); cellMchntName.setBorder(PdfPCell.BOTTOM); cellMchntName.setHorizontalAlignment(Element.ALIGN_CENTER); // Image img = Image.getInstance( ServletActionContext.getServletContext().getResource("/ext/resources/images/Title_logo.gif")); img.scalePercent(70); float w = img.getScaledWidth(); float h = img.getScaledHeight(); writer.getDirectContent().addImage(img, w, 0, 0, h, 36, PageSize.A4.getHeight() - 36 - h); // PdfPCell cell = new PdfPCell(new Paragraph("?", font17)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setColspan(3); cell.setFixedHeight(h); cell.setBorder(PdfPCell.NO_BORDER); table.addCell(cell); cell = new PdfPCell(); cell.setFixedHeight(20); cell.setColspan(3); cell.setBorder(PdfPCell.NO_BORDER); table.addCell(cell); table.addCell(new Paragraph("?", font8)); table.addCell(cellMchntId); table.addCell(" "); table.addCell(new Paragraph("??", font8)); table.addCell(cellMchntName); table.addCell(" "); document.add(table); document.add(new Paragraph("\n\n")); //? PdfPTable t = new PdfPTable(1); Iterator<Object[]> it0 = list.iterator(); int i = 1; while (it0.hasNext()) { Object[] obj = it0.next(); PdfPCell c = new PdfPCell(); c.addElement(new Paragraph("Q" + String.valueOf(i++) + "" + obj[1].toString(), font10Bold)); List<Object[]> opts = map.get(obj[0].toString()); String opt = ""; Iterator<Object[]> it1 = opts.iterator(); while (it1.hasNext()) { Object[] o = it1.next(); opt += " "; opt += o[1].toString(); opt += " "; } c.addElement(new Paragraph(opt.trim(), font10)); c.setBorder(PdfPCell.NO_BORDER); if (i - 1 != list.size()) { c.setCellEvent(border); } t.addCell(c); } PdfPTable oTable = new PdfPTable(1); oTable.setWidthPercentage(100); PdfPCell ce = new PdfPCell(t); ce.setBorderColor(Color.GRAY); oTable.addCell(ce); document.add(oTable); document.close(); logger.info("Finish build document..."); }