List of usage examples for com.lowagie.text.pdf PdfContentByte setFontAndSize
public void setFontAndSize(BaseFont bf, float size)
From source file:com.aryjr.nheengatu.testes.AbsolutePosition.java
License:Open Source License
public static void main(final String[] args) { // System.out.println("My First PdfPTable"); final Document document = new Document(); try {/* ww w . j av a 2 s. c om*/ final PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("/home/aryjr/MyFirstTable.pdf")); document.open(); // PdfPTable table = createTable(); // document.add(table); // table.writeSelectedRows(0, -1, 50, 200, // writer.getDirectContent()); final PdfContentByte cb = writer.getDirectContent(); cb.concatCTM(1f, 0f, 0f, -1f, 0f, 0f); // Paragraph text = new Paragraph("Ary Junior", // FontFactory.getFont(Style.DEFAULT_FONT_FAMILY, // Style.DEFAULT_FONT_SIZE, Font.NORMAL, Style.DEFAULT_FONT_COLOR)); final BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); cb.beginText(); cb.setFontAndSize(bf, 12); cb.showText("Ary Junior"); cb.endText(); Runtime.getRuntime().exec("acroread /home/aryjr/MyFirstTable.pdf"); } catch (final DocumentException de) { System.err.println(de.getMessage()); } catch (final IOException ioe) { System.err.println(ioe.getMessage()); } document.close(); }
From source file:com.estate.pdf.PageBorder.java
public void draw(Document doc, int iconNum, String pageNum, String toolName) { try {/*from w w w .ja v a 2 s. c o m*/ PdfContentByte cb = writer.getDirectContentUnder(); BaseFont fontBold = BaseFont.createFont(Locations.getFontLocation() + "timesbd.ttf", BaseFont.CP1252, BaseFont.EMBEDDED); BaseFont font = BaseFont.createFont(Locations.getFontLocation() + "times.ttf", BaseFont.CP1252, BaseFont.EMBEDDED); Rectangle rct = new Rectangle(doc.getPageSize()); float iconBase = (1.25f * 72); // This is the base of the icons on // the page Image icon = Image.getInstance(Locations.getImageLocation() + icons[iconNum]); // Image box = Image.getInstance(Locations.ImageLocation() + // "blueBOX.png"); icon.scalePercent(23); float scale = .23f; float iconLeft = (icon.getWidth() / 2) * scale; float boxSize = (.1875f * 72); // Adjust the top rct.setTop(rct.getTop() - (.5f * 72)); // Place the Icon icon.setAbsolutePosition((1.25f * 72) - iconLeft, rct.getTop() - iconBase); doc.add(icon); // Set our line color cb.setRGBColorStroke(0, 72, 117); cb.setLineWidth(.75f); // do the bottom line cb.moveTo(1.25f * 72, (.5f * 72)); cb.lineTo(rct.getRight() - (.5f * 72), (.5f * 72)); cb.moveTo(1.25f * 72, (.5f * 72)); cb.lineTo(1.25f * 72, rct.getTop()); // stroke the lines cb.stroke(); // Do the lower left box cb.rectangle((1.25f - 0.09375f) * 72, (.5f - 0.09375f) * 72, boxSize, boxSize); cb.setRGBColorFill(0, 72, 117); cb.closePathFillStroke(); // Do the lower right box Rectangle pnRect = new Rectangle(0, 0); pnRect.setLeft(rct.getRight() - ((.5f + 0.09375f) * 72)); pnRect.setTop((.5f - 0.09375f) * 72); pnRect.setRight(pnRect.getLeft() + boxSize); pnRect.setBottom(pnRect.getTop() - boxSize); // cb.rectangle(rct.getRight() - ((.5f + 0.09375f) * 72), (.5f - // 0.09375f) * 72, (.1875f * 72), (.1875f * 72)); cb.rectangle(pnRect.getLeft(), pnRect.getTop(), boxSize, boxSize); cb.setRGBColorFill(0, 72, 117); cb.closePathFillStroke(); // Now we do the page number if one is supplied. if (pageNum.length() > 0) { float pnHeight = fontBold.getAscentPoint(pageNum, 9) - fontBold.getDescentPoint(pageNum, 9); float pnWidth = fontBold.getWidthPoint(pageNum, 9); float l = pnRect.getLeft() + ((boxSize - pnWidth) / 2); float b = pnRect.getTop() + ((boxSize - pnHeight) / 2); cb.beginText(); cb.setFontAndSize(fontBold, 9); cb.setRGBColorFill(255, 255, 255); cb.setTextMatrix(l, b); cb.showText(pageNum); cb.endText(); } // Display the copyright SimpleDateFormat df = new SimpleDateFormat("yyyy"); char cs = 0x00a9; // Unicode for the copyright symbol String copyRight = com.estate.constants.StringConstants.copyRight + cs + " " + df.format(new Date()); float crWidth; float crLeft; crWidth = font.getWidthPoint(copyRight, 8); cb.beginText(); cb.setFontAndSize(font, 8); cb.setRGBColorFill(0, 0, 0); crLeft = (doc.getPageSize().getRight() - crWidth) / 2; cb.setTextMatrix(crLeft, .375f * 72); // Place the base of the // copyright at 3/8" up from // the bottom cb.showText(copyRight); cb.endText(); if (toolName.length() > 0) { cb.beginText(); cb.setFontAndSize(font, 8); cb.setRGBColorFill(0, 0, 0); cb.setTextMatrix((1.25f * 72) + boxSize, .375f * 72); cb.showText(toolName); cb.endText(); } // Fix a licensee at left cb.beginText(); cb.setFontAndSize(font, 8); cb.setRGBColorFill(0, 0, 0); cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, " Licensee: " + getLicense(), (10.2f * 72), .375f * 72, 0); cb.endText(); } catch (Exception e) { System.out.println(e.getMessage()); } }
From source file:com.estate.pdf.PageBorder.java
public void drawNoBorder(Document doc, String pageNum) { try {// ww w . ja va 2 s .co m PdfContentByte cb = writer.getDirectContentUnder(); BaseFont fontBold = BaseFont.createFont(Locations.getFontLocation() + "timesbd.ttf", BaseFont.CP1252, BaseFont.EMBEDDED); BaseFont font = BaseFont.createFont(Locations.getFontLocation() + "times.ttf", BaseFont.CP1252, BaseFont.EMBEDDED); Rectangle rct = new Rectangle(doc.getPageSize()); float boxSize = (.1875f * 72); // Adjust the top rct.setTop(rct.getTop() - (.5f * 72)); // Do the lower right box Rectangle pnRect = new Rectangle(0, 0); pnRect.setLeft(rct.getRight() - ((.5f + 0.09375f) * 72)); pnRect.setTop(((.5f - 0.09375f) * 72) - 9); pnRect.setRight(pnRect.getLeft() + boxSize); pnRect.setBottom(pnRect.getTop() - boxSize); cb.rectangle(pnRect.getLeft(), pnRect.getTop(), boxSize, boxSize); cb.setRGBColorFill(0, 72, 117); cb.closePathFillStroke(); // Now we do the page number if one is supplied. if (pageNum.length() > 0) { float pnHeight = fontBold.getAscentPoint(pageNum, 9) - fontBold.getDescentPoint(pageNum, 9) - 4.5f; float pnWidth = fontBold.getWidthPoint(pageNum, 9); float l = pnRect.getLeft() + ((boxSize - pnWidth) / 2); float b = pnRect.getTop() + ((boxSize - pnHeight) / 2) - 4.5f; cb.beginText(); cb.setFontAndSize(fontBold, 9); cb.setRGBColorFill(255, 255, 255); cb.setTextMatrix(l, b); cb.showText(pageNum); cb.endText(); } // Display the copyright SimpleDateFormat df = new SimpleDateFormat("yyyy"); char cs = 0x00a9; // Unicode for the copyright symbol String copyRight = "Advanced Practice Network " + cs + " " + df.format(new Date()); float crWidth; float crLeft; crWidth = font.getWidthPoint(copyRight, 8); cb.beginText(); cb.setFontAndSize(font, 8); cb.setRGBColorFill(0, 0, 0); crLeft = (doc.getPageSize().getRight() - crWidth) / 2; cb.setTextMatrix(crLeft, .375f * 72); // Place the base of the // copyright at 3/8" up from // the bottom cb.showText(copyRight); cb.endText(); if (license != null) { // Fix a licensee at left cb.beginText(); cb.setFontAndSize(font, 8); cb.setRGBColorFill(0, 0, 0); cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, " Licensee: " + getLicense(), (10.2f * 72), .375f * 72, 0); cb.endText(); } } catch (Exception e) { System.out.println(e.getMessage()); } }
From source file:com.estate.report.Scenario.java
public void drawLifeExp() { PdfContentByte cb = writer.getDirectContent(); char c = userInfo.getClientFirstName().charAt(0); char s = ' '; if (!userInfo.isSingle()) { s = userInfo.getSpouseFirstName().charAt(0); }//from w w w . jav a2 s . c om String cText = Character.toString(c) + ":" + userInfo.getClientFirstName() + " actuarial life expectancy - " + Integer.toString(userInfo.getClientLifeExpectancy()) + " years"; String sText = Character.toString(s) + ":" + userInfo.getSpouseFirstName() + " actuarial life expectancy - " + Integer.toString(userInfo.getSpouseLifeExpectancy()) + " years"; try { BaseFont font = BaseFont.createFont(Locations.getFontLocation() + "times.ttf", BaseFont.CP1252, BaseFont.EMBEDDED); cb.beginText(); cb.setFontAndSize(font, 8); cb.setRGBColorFill(255, 0, 0); cb.setTextMatrix(72 * .5f, .5f * 72); cb.showText(cText); cb.endText(); if (!userInfo.isSingle()) { cb.beginText(); cb.setFontAndSize(font, 8); cb.setRGBColorFill(255, 0, 0); cb.setTextMatrix(72 * .5f, .35f * 72); cb.showText(sText); cb.endText(); } } catch (Exception e) { System.err.println("Error printing Life Expectancy"); } }
From source file:com.geek.tutorial.itext.acroform.RadioCheckBoxForm.java
License:Open Source License
public RadioCheckBoxForm() throws Exception { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("RadioCheckBoxForm.pdf")); document.open();//from w w w . java 2 s . c o m PdfContentByte cb = writer.getDirectContent(); BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED); Rectangle rect; // Code 1 create radio button String[] radios = { "Radio1", "Radio2", "Radio3" }; PdfFormField radioField = PdfFormField.createRadioButton(writer, true); radioField.setFieldName("radioField"); radioField.setValueAsName(radios[0]); for (int i = 0; i < radios.length; i++) { rect = new Rectangle(40, 815 - i * 30, 60, 797 - i * 30); addRadioButton(writer, rect, radioField, radios[i], i == 0); cb.beginText(); cb.setFontAndSize(bf, 12); cb.showTextAligned(Element.ALIGN_LEFT, radios[i], 70, 802 - i * 30, 0); cb.endText(); } writer.addAnnotation(radioField); // Code 2 create checkbox button String[] options = { "Check1", "Check2", "Check3" }; for (int i = 0; i < options.length; i++) { rect = new Rectangle(160, 815 - i * 30, 180, 797 - i * 30); addCheckbox(writer, rect, options[i]); cb.beginText(); cb.setFontAndSize(bf, 12); cb.showTextAligned(Element.ALIGN_LEFT, options[i], 190, 802 - i * 30, 0); cb.endText(); } // Code 3 add function and button for showing state writer.addJavaScript( "function showState(){" + "app.alert('Radio:'+ this.getField('radioField').value +'\\n\\n'+" + "'Check1:'+this.getField('Check1').value +'\\n'+" + "'Check2:'+this.getField('Check2').value +'\\n'+" + "'Check3:'+this.getField('Check3').value);" + "}"); PushbuttonField push = new PushbuttonField(writer, new Rectangle(80, 710, 150, 730), "pushAction"); push.setBackgroundColor(Color.LIGHT_GRAY); push.setBorderColor(Color.GRAY); push.setText("Show State"); push.setBorderStyle(PdfBorderDictionary.STYLE_BEVELED); push.setTextColor(Color.BLACK); PdfFormField pushbutton = push.getField(); pushbutton.setAction(PdfAction.javaScript("showState()", writer)); writer.addAnnotation(pushbutton); document.close(); }
From source file:com.ikon.util.PDFUtils.java
License:Open Source License
public static void stampText(InputStream input, String text, int layer, float opacity, int size, Color color, int rotation, int align, String exprX, String exprY, OutputStream output) throws FileNotFoundException, DocumentException, EvalError, IOException { log.debug("stampText({}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {})", new Object[] { input, text, Integer.valueOf(layer), Float.valueOf(opacity), Integer.valueOf(size), color, Integer.valueOf(rotation), Integer.valueOf(align), exprX, exprY, output }); BaseFont bf = getBaseFont();// w w w . jav a2 s .co m PdfReader reader = new PdfReader(input); PdfStamper stamper = new PdfStamper(reader, output); PdfGState gs = new PdfGState(); gs.setFillOpacity(opacity); gs.setStrokeOpacity(opacity); int numPages = reader.getNumberOfPages(); int count = 0; while (count++ < numPages) { int pageCenter = (int) reader.getPageSizeWithRotation(count).getWidth() / 2; int pageMiddle = (int) reader.getPageSizeWithRotation(count).getHeight() / 2; Interpreter i = new Interpreter(); i.set("PAGE_WIDTH", (int) reader.getPageSizeWithRotation(count).getWidth()); i.set("PAGE_HEIGHT", (int) reader.getPageSizeWithRotation(count).getHeight()); i.set("PAGE_CENTER", pageCenter); i.set("PAGE_MIDDLE", pageMiddle); Integer evalX = (Integer) i.eval(exprX); Integer evalY = (Integer) i.eval(exprY); if (evalX == null) evalX = Integer.valueOf(pageCenter); if (evalY == null) { evalY = Integer.valueOf(pageMiddle); } log.debug("evalX: {}", evalX); log.debug("evalY: {}", evalY); PdfContentByte cb = null; if (layer == LAYER_UNDER_CONTENT) cb = stamper.getUnderContent(count); else if (layer == LAYER_OVER_CONTENT) cb = stamper.getOverContent(count); else { throw new IllegalArgumentException(); } cb.saveState(); cb.setColorFill(color); cb.setGState(gs); cb.beginText(); cb.setFontAndSize(bf, size); cb.showTextAligned(align, text, evalX.intValue(), evalY.intValue(), rotation); cb.endText(); cb.restoreState(); } stamper.close(); reader.close(); }
From source file:com.logiware.accounting.reports.ArDisputeReportCreator.java
@Override public void onEndPage(PdfWriter writer, Document document) { PdfContentByte cb = writer.getDirectContent(); cb.saveState();/*w ww . jav a 2s.c om*/ String text = "Page " + writer.getPageNumber() + " of "; float textBase = document.bottom() - 20; float textSize = helvFont.getWidthPoint(text, 12); cb.beginText(); cb.setFontAndSize(helvFont, 12); cb.setTextMatrix((document.right() / 2) - (textSize / 2), textBase); cb.showText(text); cb.endText(); cb.addTemplate(pageTemplate, (document.right() / 2) + (textSize / 2), textBase); cb.restoreState(); }
From source file:com.moss.pdf.template.core.Renderer.java
License:Open Source License
public void render(InputStream in, List<? extends PropertyMapping> fields, OutputStream out) throws Exception { PdfReader reader = new PdfReader(in); Document document = new Document(reader.getPageSizeWithRotation(1)); PdfWriter writer = PdfWriter.getInstance(document, out); document.open();/*from ww w.j a v a 2 s. c om*/ for (int i = 1; i <= reader.getNumberOfPages(); i++) { PdfContentByte cb = writer.getDirectContent(); PdfImportedPage customPage = writer.getImportedPage(reader, i); /* * add the page to our new document, turning this page to its * original rotation */ int pageRotation = reader.getPageRotation(i); if (pageRotation > 0) { System.out.println("page rotation found: " + pageRotation); double angle = -((2 * Math.PI) * pageRotation / 360); // double angle = -(Math.PI / 2); cb.addTemplate(customPage, (float) Math.cos(angle), (float) Math.sin(angle), (float) -Math.sin(angle), (float) Math.cos(angle), 0f, // x document.top() + document.topMargin() // y ); } else { cb.addTemplate(customPage, 0f, 0f); } Map<FontName, BaseFont> fonts = new HashMap<FontName, BaseFont>(); for (PropertyMapping field : fields) { if (field.getPageNumber() != i) { continue; } /* * Only builtin fonts are supported at the moment */ BaseFont font; int fontSize; int alignment; String text; float x, y; float rotation; { font = fonts.get(field.getFontName()); if (font == null) { FontName e = field.getFontName(); String name = null; if (FontName.COURIER == e) { name = BaseFont.COURIER; } else if (FontName.COURIER_BOLD == e) { name = BaseFont.COURIER_BOLD; } else if (FontName.COURIER_BOLD_OBLIQUE == e) { name = BaseFont.COURIER_BOLDOBLIQUE; } else if (FontName.COURIER_OBLIQUE == e) { name = BaseFont.COURIER_OBLIQUE; } else if (FontName.HELVETICA == e) { name = BaseFont.HELVETICA; } else if (FontName.HELVETICA_BOLD == e) { name = BaseFont.HELVETICA_BOLD; } else if (FontName.HELVETICA_BOLD_OBLIQUE == e) { name = BaseFont.HELVETICA_BOLDOBLIQUE; } else if (FontName.HELVETICA_OBLIQUE == e) { name = BaseFont.HELVETICA_OBLIQUE; } else if (FontName.TIMES_BOLD == e) { name = BaseFont.TIMES_BOLD; } else if (FontName.TIMES_BOLD_ITALIC == e) { name = BaseFont.TIMES_BOLDITALIC; } else if (FontName.TIMES_ITALIC == e) { name = BaseFont.TIMES_ITALIC; } else if (FontName.TIMES_ROMAN == e) { name = BaseFont.TIMES_ROMAN; } if (name == null) { throw new RuntimeException("Unknown font type: " + e); } font = BaseFont.createFont(name, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED); fonts.put(field.getFontName(), font); } fontSize = field.getFontSize(); if (TextAlignment.LEFT == field.getAlignment()) { alignment = PdfContentByte.ALIGN_LEFT; } else if (TextAlignment.CENTER == field.getAlignment()) { alignment = PdfContentByte.ALIGN_CENTER; } else if (TextAlignment.RIGHT == field.getAlignment()) { alignment = PdfContentByte.ALIGN_RIGHT; } else { alignment = PdfContentByte.ALIGN_LEFT; } Object value = p.eval(field.getExpr()); if (value == null) { text = ""; } else { text = value.toString(); } x = field.getX() * POINTS_IN_A_CM; y = field.getY() * POINTS_IN_A_CM; rotation = 0; } cb.beginText(); cb.setFontAndSize(font, fontSize); cb.showTextAligned(alignment, text, x, y, rotation); cb.endText(); } document.newPage(); } reader.close(); document.close(); }
From source file:com.orange.atk.atkUI.corecli.utils.PdfUtilities.java
License:Apache License
/** * Adds a text in the given pdf file./*from w ww. j a v a 2 s . com*/ * @param pdfFileName * @param text * @param x * @param y * @param rotation * @param page * @throws Exception */ public void addText(String pdfFileName, String text, int x, int y, int rotation, int page) throws Exception { // see example on http://itextdocs.lowagie.com/examples/com/lowagie/examples/general/copystamp/AddWatermarkPageNumbers.java // 1. copy File tmpPDFFile = new File(tmpDir, "tmpPDF.pdf"); copyFile(new File(pdfFileName), tmpPDFFile); // 2. add text // we create a reader for a certain document PdfReader reader = new PdfReader(tmpPDFFile.getAbsolutePath()); // we create a stamper that will copy the document to a new file PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(pdfFileName)); // adding content to each page PdfContentByte over; BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED); over = stamp.getOverContent(page); over.beginText(); over.setFontAndSize(bf, 32); over.setTextMatrix(30, 30); over.setColorFill(java.awt.Color.RED); over.showTextAligned(Element.ALIGN_LEFT, text, x, y, rotation); over.endText(); // closing PdfStamper will generate the new PDF file stamp.close(); }
From source file:com.qcadoo.report.api.pdf.PdfPageNumbering.java
License:Open Source License
private void buildFooter(final PdfWriter writer, final Document document) { PdfContentByte cb = writer.getDirectContent(); cb.saveState();//from ww w . ja v a 2 s. c o m String text = footer.getPage() + " " + writer.getPageNumber() + " " + footer.getIn() + " "; float textBase = document.bottom() - 25; float textSize = FontUtils.getDejavu().getWidthPoint(text, 7); cb.setColorFill(ColorUtils.getLightColor()); cb.setColorStroke(ColorUtils.getLightColor()); cb.setLineWidth(1); cb.setLineDash(2, 2, 1); cb.moveTo(document.left(), document.bottom() - 10); cb.lineTo(document.right(), document.bottom() - 10); cb.stroke(); cb.beginText(); cb.setFontAndSize(FontUtils.getDejavu(), 7); float adjust = FontUtils.getDejavu().getWidthPoint("0", 7); cb.setTextMatrix(document.right() - textSize - adjust, textBase); cb.showText(text); textSize = FontUtils.getDejavu().getWidthPoint(footer.getGeneratedBy(), 7); cb.setTextMatrix(document.right() - textSize, textBase - 10); cb.showText(footer.getGeneratedBy()); textSize = FontUtils.getDejavu().getWidthPoint(generationDate, 7); cb.setTextMatrix(document.right() - textSize, textBase - 20); cb.showText(generationDate); cb.endText(); try { textSize = FontUtils.getDejavu().getWidthPoint(footer.getAdditionalText(), 7); ColumnText ct = new ColumnText(cb); ct.setSimpleColumn(new Phrase(footer.getAdditionalText(), FontUtils.getDejavuRegular7Light()), document.left() + 240, textBase + 10, document.left() + 390, textBase - 25, 10, Element.ALIGN_LEFT); ct.go(); } catch (DocumentException e) { LOG.warn("Problem with additional text generation in report footer."); } try { ColumnText ct = new ColumnText(cb); ct.setSimpleColumn(document.left(), textBase + 10, document.left() + 230, textBase - 25, 10, Element.ALIGN_LEFT); ct.addText(new Phrase(footer.getCompanyName() + "\n", FontUtils.getDejavuRegular7Light())); if (!"".equals(footer.getAddress())) { ct.addText(new Phrase(footer.getAddress() + "\n", FontUtils.getDejavuRegular7Light())); } if (!"".equals(footer.getPhoneEmail())) { ct.addText(new Phrase(footer.getPhoneEmail(), FontUtils.getDejavuRegular7Light())); } ct.go(); } catch (DocumentException e) { LOG.warn("Problem with company text generation in report footer."); } cb.addTemplate(total, document.right() - adjust, textBase); cb.restoreState(); }