List of usage examples for com.lowagie.text.pdf BaseFont createFont
public static BaseFont createFont(String name, String encoding, boolean embedded) throws DocumentException, IOException
From source file:de.unigoettingen.sub.commons.contentlib.pdflib.PDFTitlePage.java
License:Apache License
/************************************************************************************ * get font as {@link Font} from given font name and size * //from w ww .j av a2 s . co m * @param fontname name of font * @param fontsize size of font * * @throws PDFManagerException ************************************************************************************/ private Font getPDFFont(int fontsize) throws PDFManagerException { Font resultfont = null; // set the base font try { if (this.ttffontpath == null) { // don't use TTF LOGGER.debug("Do not use TrueType Font... instead standard Arial is used"); resultfont = FontFactory.getFont("Arial", BaseFont.CP1252, BaseFont.EMBEDDED, fontsize); // String[] codePages = basefont.getCodePagesSupported(); // System.out.println("All available encodings for font:\n\n"); // for (int i = 0; i < codePages.length; i++) { // System.out.println(codePages[i]); // } } else { // load font, embedd it - use unicode LOGGER.debug("Use TrueType Font... at:" + this.ttffontpath); BaseFont bf = BaseFont.createFont(this.ttffontpath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED); resultfont = new Font(bf, fontsize); } } catch (Exception e) { LOGGER.error("DocumentException while creating title page for PDF", e); throw new PDFManagerException("Exception while creating Titlepage for PDF", e); } return resultfont; }
From source file:demo.dwr.simple.UploadDownload.java
License:Apache License
/** * Generates a PDF file with the given text * http://itext.ugent.be/itext-in-action/ * @return A PDF file as a byte array/*from w ww .j av a 2 s . co m*/ */ public FileTransfer downloadPdfFile(String contents) throws Exception { if (contents == null || contents.length() == 0) { contents = "[BLANK]"; } ByteArrayOutputStream buffer = new ByteArrayOutputStream(); Document document = new Document(); PdfWriter.getInstance(document, buffer); // ?itext-asian BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); // ?WINDOW c:\windows\Fonts\xxx.ttf // ?classpath: /src/main/resources/fonts/simsong.ttf Font fontChinese = new Font(bfChinese, 12, Font.NORMAL); document.addCreator("DWR.war using iText"); document.open(); document.add(new Paragraph(contents, fontChinese)); document.close(); return new FileTransfer("example.pdf", "application/pdf", buffer.toByteArray()); }
From source file:domain.reports.menu.PDFReportMenu.java
License:LGPL
@Override protected void createPDF(GenericTransaction data, ByteArrayOutputStream buf) throws Throwable { //inicializar documento: tamano de pagina, orientacion, margenes Document doc = new Document(); PdfWriter docWriter = PdfWriter.getInstance(doc, buf); doc.setPageSize(PageSize.LETTER.rotate()); doc.setMargins(30, 30, 30, 40);/*from ww w. j a va2 s. c o m*/ doc.open(); //crear fonts por defecto tblHeaderFont = new Font(Font.HELVETICA, 10f, Font.BOLD); tblBodyFont = new Font(Font.HELVETICA, 10f, Font.NORMAL); //definir pie de pagina del lado izquierdo String footerText = this.getFooter(); //read it from config.xml or web.xml String reportDate = StringUtil.formatDate(new java.util.Date(), "dd-MM-yyyy HH:mm"); //crear template (objeto interno de IText) y manejador de evento //para imprimir el pie de pagina bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); cb = docWriter.getDirectContent(); tpl = cb.createTemplate(20, 14); docWriter.setPageEvent(new PDFPageEvents(footerText, pageXofY, tpl, bf, cb, reportDate)); //titulo - lo lee de config.xml por defecto reportTitle = getReportTitle(); Paragraph t = new Paragraph(reportTitle, new Font(Font.HELVETICA, 14f, Font.BOLD)); t.setAlignment(Rectangle.ALIGN_RIGHT); doc.add(t); //logo img = Image.getInstance(getImage(this.getServerBaseURL() + logoPath, false)); img.scalePercent(100); float imgY = doc.top() - img.getHeight(); float imgX = doc.left(); img.setAbsolutePosition(imgX, imgY); doc.add(img); //blank line doc.add(new Paragraph(" ")); //blank line doc.add(new Paragraph(" ")); //blank line doc.add(new Paragraph(" ")); //for each master record print a master/detail section MasterDetailReader dataobj = (MasterDetailReader) data; Recordset master = dataobj.getRecordset("master"); master.top(); while (master.next()) { //blank line doc.add(new Paragraph(" ")); //print master section doc.add(getGroupMaster(master)); //print detail section doc.add(getGroupDetail(master, dataobj.getDetail(master))); } //print grand total doc.add(new Paragraph(" ")); doc.close(); docWriter.close(); }
From source file:edtscol.client.Imprime.java
License:CeCILL license
private void ecrireTexteHor(String texte, float x, float y, float l, float h, float ph, float pv, int szf) { float x1, y1; try {//from w w w .j ava 2s . c o m BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); cb.setFontAndSize(bf, szf); x1 = x + ph; y1 = y - pv; if (bf.getWidthPoint(texte, szf) > l) { cesure(bf, szf, texte, x1, y1, l, h); } else { cb.beginText(); cb.setTextMatrix(x1, y1); cb.showText(texte); cb.endText(); } } catch (Exception e) { e.printStackTrace(); } }
From source file:edtscol.client.Imprime.java
License:CeCILL license
private void graduationEdt(float x, float y, float l, float h, int nb) { float taille = l / (nb - 1); float x1, y1; y1 = y;//from w w w . ja v a 2 s . c o m x1 = x; for (int i = 0; i < nb; i++) { try { if (i > 0) { x1 = x1 + taille; } BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); if (i == 0 || i == nb - 1) { cb.setLineWidth(1.0f); } else { cb.setLineWidth(0.1f); } cb.moveTo(x1, y1); cb.lineTo(x1, y1 - (h * 7.0f)); cb.stroke(); cb.setLineWidth(0.2f); if (i < nb - 1) { for (int j = 1; j < 4; j++) { float tq = (taille / 04.f) * j; cb.moveTo(x1 + tq, y1); cb.lineTo(x1 + tq, y1 - (h * 7.0f)); cb.stroke(); } } cb.setLineWidth(1.0f); cb.beginText(); bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); cb.setFontAndSize(bf, 12); cb.setTextMatrix(x1 - 5.0f, y1 + 1.0f); int heure = i * 2 + 8; String texte = new Integer(heure).toString() + "h"; cb.showText(texte); cb.endText(); } catch (Exception e) { } } // ligne de fin de journe y1 = y1 - taille; x1 = x + l; cb.moveTo(x1, y1); cb.lineTo(x1 + lgs, y1); cb.stroke(); }
From source file:fr.aliasource.webmail.server.export.ConversationPdfEventHandler.java
License:GNU General Public License
/** * @see com.lowagie.text.pdf.PdfPageEventHelper#onOpenDocument(com.lowagie.text.pdf.PdfWriter, * com.lowagie.text.Document)//w w w .ja v a 2 s. c o m */ public void onOpenDocument(PdfWriter writer, Document document) { try { headerImage = Image.getInstance(getLogoUrl()); table = new PdfPTable(new float[] { 1f, 2f }); Phrase p = new Phrase(); Chunk ck = new Chunk(cr.getTitle(), new Font(Font.HELVETICA, 16, Font.BOLD)); p.add(ck); p.add(Chunk.NEWLINE); ck = new Chunk(ConversationExporter.formatName(account), new Font(Font.HELVETICA, 12, Font.BOLDITALIC)); p.add(ck); p.add(Chunk.NEWLINE); ck = new Chunk(cm.length + " messages", new Font(Font.HELVETICA, 10)); p.add(ck); table.getDefaultCell().setBorder(0); table.addCell(new Phrase(new Chunk(headerImage, 0, 0))); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT); table.addCell(p); // initialization of the template tpl = writer.getDirectContent().createTemplate(100, 100); tpl.setBoundingBox(new Rectangle(-20, -20, 100, 100)); // initialization of the font helv = BaseFont.createFont("Helvetica", BaseFont.WINANSI, false); } catch (Exception e) { throw new ExceptionConverter(e); } }
From source file:gov.anl.aps.cdb.portal.plugins.support.icmsLink.IcmsWatermarkUtility.java
License:Open Source License
/** * Adds a stamp of some metadata to ICMS documents. * * Function Credit: Thomas Fors/* www .j a v a2s. co m*/ * * @return byte array ologgerf the stamped PDF file * @throws DocumentException - Error loading pdfstamper or creating font * @throws IOException - Error performing IO operation * @throws Base64DecodingException - Error converting downloadContent string to byte[] */ private byte[] addWatermarkToPDFFile() throws Base64DecodingException, DocumentException, IOException { byte[] pdfBytes = Base64.decode(downloadContentBase64Encoded); DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM/dd/yy hh:mm:ss a"); LocalDateTime now = LocalDateTime.now(); String downloadTime = dtf.format(now); UserInfo user = (UserInfo) SessionUtility.getUser(); String username = null; if (user != null) { username = user.getUsername(); } else { username = "unknown user"; } String bottomMessage = "Downloaded via APS CDB by: " + username + " at " + downloadTime; controlledRev = updateOptionalValue(controlledRev); dnsCollectionId = updateOptionalValue(dnsCollectionId); dnsDocNumber = updateOptionalValue(dnsDocNumber); String watermarkContents = "Content ID: " + docName; watermarkContents += " Rev: " + controlledRev; watermarkContents += " Released: " + date; watermarkContents += " DNS Collection ID: " + dnsCollectionId; watermarkContents += " DNS Document ID: " + dnsDocNumber; PdfReader pdfReader = new PdfReader(pdfBytes); int n = pdfReader.getNumberOfPages(); ByteArrayOutputStream out = new ByteArrayOutputStream(); PdfStamper stamp = new PdfStamper(pdfReader, out); PdfContentByte over; BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED); for (int i = 0; i < n; i++) { over = stamp.getOverContent(i + 1); over.beginText(); over.setTextMatrix(30, 30); over.setFontAndSize(bf, 10); over.setColorFill(new Color(0x80, 0x80, 0x80)); over.showTextAligned(Element.ALIGN_LEFT, watermarkContents, 25, 25, 90); over.showTextAligned(Element.ALIGN_LEFT, bottomMessage, 50, 10, 0); if (status.equals(ICMS_UNDER_REV_STATUS)) { over.setColorFill(new Color(0xFF, 0x00, 0x00)); } //over.showTextAligned(Element.ALIGN_LEFT, status, 25, 25 + bf.getWidthPoint(watermarkContents + " - ", 10), 90); over.endText(); } stamp.close(); return out.toByteArray(); }
From source file:ilarkesto.integration.itext.Paragraph.java
License:Open Source License
private Font createFont(String name, FontStyle fontStyle) { Font font;/* w w w. j a v a 2s .c om*/ try { font = new Font(BaseFont.createFont(name, BaseFont.IDENTITY_H, BaseFont.EMBEDDED)); } catch (Exception ex) { throw new RuntimeException("Loading font failed: " + name, ex); } if (fontStyle != null) { font.setStyle(createStyle(fontStyle)); font.setSize(PdfBuilder.mmToPoints(fontStyle.getSize())); font.setColor(fontStyle.getColor()); } return font; }
From source file:include.nseer_cookie.MakePdf.java
License:Open Source License
public void make(String database, String tablename, String sql1, String sql2, String filename, int everypage, HttpSession session) {// w ww .j a v a 2 s.co m try { nseer_db aaa = new nseer_db(database); nseer_db demo_db = new nseer_db(database); ServletContext context = session.getServletContext(); String path = context.getRealPath("/"); Masking reader = new Masking(configFile); Vector columnNames = new Vector(); Vector tables = reader.getTableNicks(); Iterator loop = tables.iterator(); while (loop.hasNext()) { String tablenick = (String) loop.next(); columnNames = reader.getColumnNames(tablenick); } int cpage = 1; //? int spage = 1; int ipage = everypage; String pagesql = sql1; //? ResultSet pagers = demo_db.executeQuery(pagesql); pagers.next(); int allCol = pagers.getInt("A"); allpage = (int) Math.ceil((allCol + ipage - 1) / ipage); // for (int m = 1; m <= allpage; m++) { spage = (m - 1) * ipage; String sql = sql2 + " limit " + spage + "," + ipage; ResultSet bbb = aaa.executeQuery(sql); //ResultSetMetaData tt=bbb.getMetaData(); // int b = columnNames.size(); // int a = 0; while (bbb.next()) { a++; } // bbb.first(); // ?? Rectangle rectPageSize = new Rectangle(PageSize.A4);// rectPageSize = rectPageSize.rotate(); Document document = new Document(rectPageSize, 20, 20, 20, 20); //? Document PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path + filename + m + ".pdf")); //?PDF?? document.open(); // BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); //? com.lowagie.text.Font FontChinese = new com.lowagie.text.Font(bfChinese, 8, com.lowagie.text.Font.NORMAL); // Paragraph title1 = new Paragraph("nseer ERP", FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLDITALIC)); Chapter chapter1 = new Chapter(title1, 1); // ? chapter1.setNumberDepth(0); Paragraph title11 = new Paragraph(tablename, FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD)); Section section1 = chapter1.addSection(title11); //? Table t = new Table(b, a); // ? t.setPadding(1); // t.setSpacing(0); // ? t.setBorderWidth(1); // do { // // for (int k = 0; k < b; k++) { // Cell cell = new Cell( new Paragraph(bbb.getString((String) columnNames.elementAt(k)), FontChinese)); //? // t.addCell(cell); // ? // } // } while (bbb.next()); // section1.add(t); // document.add(chapter1); // document.close(); } // ? } catch (Exception pp) { pp.printStackTrace(); } }
From source file:io.vertigo.dynamo.plugins.export.pdf.PDFAdvancedPageNumberEvents.java
License:Apache License
/** {@inheritDoc} */ @Override//from www.j a v a 2s.c o m public void onOpenDocument(final PdfWriter writer, final Document document) { try { bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); cb = writer.getDirectContent(); template = cb.createTemplate(50, 50); } catch (final DocumentException | IOException e) { throw new RuntimeException(e); } }