List of usage examples for com.itextpdf.text.pdf PdfContentByte setFontAndSize
public void setFontAndSize(final BaseFont bf, final float size)
From source file:org.javad.pdf.TitlePageContent.java
License:Apache License
@Override public OutputBounds generate(PdfContentByte content) { if (isSkipped()) { return new OutputBounds(getX(), getY(), 0, 0); }//from ww w . j a v a2s .c o m int maxWidth = 0; float top = getY() - PdfUtil.convertFromMillimeters( (configuration.getHeight() - configuration.getMarginBottom() - configuration.getMarginTop()) / 2); if (getImage() != null) { try { com.itextpdf.text.Image img = determineScaledImage(getImage()); if (img != null) { content.addImage(img); top = img.getAbsoluteY() - PdfUtil.convertFromMillimeters(25.0f); } } catch (Exception e) { logger.log(Level.FINER, "An error occured scaling the image. ", e); } } content.setColorStroke(BaseColor.BLACK); Font f = FontRegistry.getInstance().getFont(PdfFontDefinition.AlbumTitle); content.setFontAndSize(f.getBaseFont(), f.getSize()); content.setHorizontalScaling(110.0f); if (getTitle() != null && !getTitle().isEmpty()) { maxWidth = (int) f.getBaseFont().getWidthPoint(getTitle().toUpperCase(), f.getSize()); PdfUtil.renderConstrainedText(content, getTitle().toUpperCase(), f, getX(), top, maxWidth); } if (getSubTitle() != null && !getSubTitle().isEmpty()) { Font subFont = FontRegistry.getInstance().getFont(PdfFontDefinition.AlbumSubtitle); top -= subFont.getCalculatedSize() + PdfUtil.convertFromMillimeters(3.0f); content.setFontAndSize(subFont.getBaseFont(), subFont.getSize()); maxWidth = Math.max(maxWidth, (int) content.getEffectiveStringWidth(getSubTitle().toUpperCase(), false)); PdfUtil.renderConstrainedText(content, getSubTitle().toUpperCase(), subFont, getX(), top, maxWidth); } if (getDescription() != null && !getDescription().isEmpty()) { Font subFont = FontRegistry.getInstance().getFont(PdfFontDefinition.AlbumDescription); top -= PdfUtil.convertFromMillimeters(15.0f); float width = PdfUtil.convertFromMillimeters( (configuration.getWidth() - configuration.getMarginLeft() - configuration.getMarginRight()) / 2); content.setFontAndSize(subFont.getBaseFont(), subFont.getSize()); top += PdfUtil.renderConstrainedText(content, getDescription(), subFont, width + PdfUtil.convertFromMillimeters(configuration.getMarginLeft()), top, (int) (width * 0.7f)); } if (!getItems().isEmpty()) { Font subFont = FontRegistry.getInstance().getFont(PdfFontDefinition.AlbumContents); top -= subFont.getCalculatedSize() + PdfUtil.convertFromMillimeters(6.0f); content.setFontAndSize(subFont.getBaseFont(), subFont.getSize()); for (String s : getItems()) { maxWidth = Math.max(maxWidth, (int) subFont.getBaseFont().getWidthPoint(s, subFont.getSize())); PdfUtil.renderConstrainedText(content, s, subFont, getX(), top, maxWidth); top -= PdfUtil.convertFromMillimeters(3.0f); } } content.setHorizontalScaling(100.0f); OutputBounds rect = new OutputBounds(getX() - maxWidth / 2, getY(), maxWidth, getY() - top); return rect; }
From source file:org.javad.pdf.util.PdfUtil.java
License:Apache License
protected static void improveRenderText(PdfContentByte content, String str, Font f, float x, float y) { BaseFont bf = f.getCalculatedBaseFont(false); if (i18Font == null && (System.currentTimeMillis() - LAST_CHECK_TIME > 10000)) { i18Font = FontRegistry.getInstance().getFont(PdfFontDefinition.ExtendedCharacters) .getCalculatedBaseFont(false); LAST_CHECK_TIME = System.currentTimeMillis(); }//from w w w. j a v a2 s .co m StringBuilder buf = new StringBuilder(str.length()); boolean extendedCodePoint = false; float effWidth = 0.0f; for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); int codePoint = Character.codePointAt("" + c, 0); boolean curCodePoint = (!bf.charExists(codePoint)) ? true : false; if (curCodePoint != extendedCodePoint) { content.setFontAndSize((extendedCodePoint) ? i18Font : bf, f.getSize()); effWidth += content.getEffectiveStringWidth(buf.toString(), false); buf = new StringBuilder(); extendedCodePoint = curCodePoint; } buf.append(c); } content.setFontAndSize(bf, f.getSize()); effWidth += content.getEffectiveStringWidth(buf.toString(), false); float x_pos = x - (effWidth / 2.0f); // eq. to showTextAligned content.beginText(); content.setTextMatrix(x_pos, y); BaseFont lastFont = bf; @SuppressWarnings("UnusedAssignment") BaseFont currentFont = lastFont; buf = new StringBuilder(); for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); int codePoint = Character.codePointAt("" + c, 0); if (!bf.charExists(codePoint)) { currentFont = i18Font; } else { currentFont = bf; } if (currentFont != lastFont) { content.showText(buf.toString()); buf = new StringBuilder(); content.setFontAndSize(currentFont, f.getSize()); lastFont = currentFont; } buf.append(c); } if (buf.length() > 0) { content.showText(buf.toString()); } content.endText(); }
From source file:org.javad.stamp.pdf.ColumnSet.java
License:Apache License
@Override public OutputBounds generate(PdfContentByte content) { if (isSkipped()) { return new OutputBounds(getX(), getY(), 0, 0); }/*w ww . j av a 2 s. c om*/ float totalWidth = 0.0f; float maxHeight = 0.0f; float cur_x = getX(); float top = getY(); if (getIssue() != null && !getIssue().isEmpty()) { top -= PdfUtil.convertFromMillimeters(5.0f); Font f = FontRegistry.getInstance().getFont(PdfFontDefinition.SetIssue); content.setFontAndSize(f.getBaseFont(), f.getSize()); String is = getIssue().replace("\\n", "\n"); StringTokenizer tokenizer = new StringTokenizer(is, "\n", true); float x = PdfUtil .convertFromMillimeters(configuration.getUsableWidth() / 2 + configuration.getMarginLeft()); while (tokenizer.hasMoreTokens()) { is = tokenizer.nextToken(); if (is.equals("\n")) { top -= f.getCalculatedSize() + 2; } else { PdfUtil.renderConstrainedText(content, is, f, x, top, (int) PdfUtil.convertFromMillimeters(configuration.getUsableWidth())); if (tokenizer.hasMoreTokens()) { top -= f.getCalculatedSize() + 2; } } } top -= PdfUtil.convertFromMillimeters(3.0f); } float spacing = PdfUtil.convertFromMillimeters(getSpacingMode().getSpacing(box_spacing)); int count = 0; for (int i = 0; i < columns.size(); i++) { StampSet set = columns.get(i); if (set.isSkipped()) { continue; } float s_w = PdfUtil.findMaximumWidth(set, content); set.setX(cur_x + s_w / 2); set.setY(top); OutputBounds rect = set.generate(content); totalWidth += rect.width + ((count > 0) ? spacing : 0.0f); cur_x += rect.width + spacing; maxHeight = Math.max(maxHeight, rect.height); count++; } return new OutputBounds(getX() - (totalWidth / 2.0f), getY(), totalWidth, maxHeight + (getY() - top)); }
From source file:org.javad.stamp.pdf.CompositeRow.java
License:Apache License
@Override public OutputBounds generate(PdfContentByte content) { if (isSkipped()) { return new OutputBounds(getX(), getY(), 0, 0); }//from www . j ava 2 s . c o m float maxHeight = 0.0f; float maxWidth = 0.0f; float top = getY(); if (getDescription() != null && !getDescription().isEmpty()) { content.setColorStroke(BaseColor.BLACK); Font descFont = FontRegistry.getInstance().getFont(PdfFontDefinition.CompositeSetDescription); content.setFontAndSize(descFont.getBaseFont(), descFont.getSize()); top -= descFont.getCalculatedSize(); int count = 0; int tc = getDescription().split("\n").length; for (String desc : getDescription().split("\n")) { maxWidth = Math.max(maxWidth, content.getEffectiveStringWidth(desc, false)); PdfUtil.renderConstrainedText(content, desc, descFont, getX(), top, (int) maxWidth); count++; top -= descFont.getCalculatedSize() + ((count < tc) ? 2 : 4); } } float totalWidth = 0.0f; float totalHeight = getY() - top; float spacing = PdfUtil.convertFromMillimeters(getSpacingMode().getSpacing(box_spacing)); int count = 0; for (int i = 0; i < rows.size(); i++) { StampRow set = rows.get(i); if (set.isSkipped()) { continue; } totalWidth += PdfUtil.findMaximumWidth(set, content) + ((count > 0) ? spacing : 0.0f); count++; } float cur_x = getX() - totalWidth / 2.0f; count = 0; for (StampRow set : rows) { if (set.isSkipped()) { continue; } set.setX(cur_x + PdfUtil.findMaximumWidth(set, content) / 2.0f); set.setY(top); OutputBounds rect = set.generate(content); count++; cur_x += rect.width + ((count > 0) ? spacing : 0); maxHeight = Math.max(maxHeight, rect.height); } maxWidth = Math.max(maxWidth, totalWidth); totalHeight += maxHeight; return new OutputBounds(getX() - (maxWidth / 2.0f), getY(), maxWidth, totalHeight); }
From source file:org.javad.stamp.pdf.StampRow.java
License:Apache License
@Override public OutputBounds generate(PdfContentByte content) { if (isSkipped()) { return new OutputBounds(getX(), getY(), 0, 0); }//from w w w . jav a 2s . c o m float maxWidth = 0; float top = getY(); if (getDescription() != null && !getDescription().isEmpty()) { content.setColorStroke(BaseColor.BLACK); Font descFont = FontRegistry.getInstance().getFont(PdfFontDefinition.RowDescription); content.setFontAndSize(descFont.getBaseFont(), descFont.getSize()); top -= descFont.getCalculatedSize(); int count = 0; int tc = getDescription().split("\n").length; for (String desc : getDescription().split("\n")) { maxWidth = Math.max(maxWidth, (int) descFont.getBaseFont().getWidthPoint(desc, descFont.getSize())); PdfUtil.renderConstrainedText(content, desc, descFont, getX(), top, (int) (maxWidth * 1.10)); count++; top -= descFont.getCalculatedSize() + ((count < tc) ? 2 : 4); } } float totalWidth = 0; int maxHeight = 0; int count = 0; for (int i = 0; i < stampContents.size(); i++) { if (stampContents.get(i).isSkipped()) { continue; } totalWidth += stampContents.get(i).getWidth() + stampContents.get(i).getPadding(); if (count > 0) { totalWidth += padding; } count++; maxHeight = Math.max(maxHeight, stampContents.get(i).getHeight()); } totalWidth = PdfUtil.convertFromMillimeters(totalWidth); float start_x = getX() - (totalWidth / 2.0f); float cur_x = start_x; float totalHeight = getY() - top; float deltaHeight = totalHeight; count = 0; for (IStampContent s : stampContents) { if (s.isSkipped()) { continue; } if (count > 0) { cur_x += PdfUtil.convertFromMillimeters(padding); } s.setX(cur_x); int delta_y = getVerticalAlignmentOffset(s, maxHeight); s.setY(top - PdfUtil.convertFromMillimeters(delta_y)); OutputBounds r = s.generate(content); totalHeight = Math.max(r.height + deltaHeight, totalHeight); cur_x += r.width; count++; } maxWidth = (int) Math.max(maxWidth, totalWidth); return new OutputBounds(start_x, getY(), maxWidth, totalHeight); }
From source file:org.javad.stamp.pdf.StampSet.java
License:Apache License
@Override public OutputBounds generate(PdfContentByte content) { if (isSkipped()) { return new OutputBounds(getX(), getY(), 0, 0); }/*from ww w . j a v a 2s .c om*/ float maxWidth = 0; content.setColorStroke(BaseColor.BLACK); float top = getY(); if (getIssue() != null && !getIssue().isEmpty()) { top -= PdfUtil.convertFromMillimeters(5.0f); Font f = FontRegistry.getInstance().getFont(PdfFontDefinition.SetIssue); content.setFontAndSize(f.getBaseFont(), f.getSize()); String is = getIssue().replace("\\n", "\n"); StringTokenizer tokenizer = new StringTokenizer(is, "\n", true); while (tokenizer.hasMoreTokens()) { is = tokenizer.nextToken(); maxWidth = Math.max(maxWidth, content.getEffectiveStringWidth(is, false)); if (is.equals("\n")) { top -= f.getCalculatedSize() + 2; } else { PdfUtil.renderConstrainedText(content, is, f, getX(), top, (int) maxWidth); if (tokenizer.hasMoreTokens()) { top -= f.getCalculatedSize() + 2; } } } top -= PdfUtil.convertFromMillimeters(3.0f); } if (getDescription() != null && !getDescription().isEmpty()) { Font descFont = FontRegistry.getInstance().getFont(PdfFontDefinition.SetDescription); content.setFontAndSize(descFont.getBaseFont(), descFont.getSize()); top -= descFont.getCalculatedSize(); int count = 0; int tc = getDescription().split("\n").length; for (String desc : getDescription().split("\n")) { maxWidth = Math.max(maxWidth, content.getEffectiveStringWidth(desc, false)); PdfUtil.renderConstrainedText(content, desc, descFont, getX(), top, (int) maxWidth); count++; if (count < tc) { top -= descFont.getCalculatedSize() + 2; } } } if (getDescriptionSecondary() != null && !getDescriptionSecondary().isEmpty()) { Font secFont = FontRegistry.getInstance().getFont(PdfFontDefinition.SetDescriptionSecondary); content.setFontAndSize(secFont.getBaseFont(), secFont.getSize()); top -= secFont.getCalculatedSize() + PdfUtil.convertFromMillimeters(3.0f); int count = 0; int tc = getDescriptionSecondary().split("\n").length; for (String desc : getDescriptionSecondary().split("\n")) { maxWidth = Math.max(maxWidth, content.getEffectiveStringWidth(desc, false)); PdfUtil.renderConstrainedText(content, desc, secFont, getX(), top, (int) maxWidth); count++; if (count < tc) { top -= secFont.getCalculatedSize() + 2; } } } if (!rows.isEmpty()) { top -= ((top != getY()) ? PdfUtil.convertFromMillimeters(3.0f) : 0); int count = 0; for (int i = 0; i < rows.size(); i++) { ISetContent row = rows.get(i); if (row.isSkipped()) { continue; } top -= ((count > 0) ? (PdfUtil.convertFromMillimeters(verticalPadding)) : 0.0f); row.setX(getX()); row.setY(top); OutputBounds bounds = row.generate(content); count++; top -= bounds.height; maxWidth = Math.max(maxWidth, bounds.width); } } if (getComment() != null && !getComment().isEmpty()) { top -= PdfUtil.convertFromMillimeters((int) (0.75 * verticalPadding)); Font cFont = FontRegistry.getInstance().getFont(PdfFontDefinition.SetComment); content.setFontAndSize(cFont.getBaseFont(), cFont.getSize()); for (String s : getComment().split("\n")) { top -= cFont.getCalculatedSize(); maxWidth = Math.max(maxWidth, content.getEffectiveStringWidth(s, false)); PdfUtil.renderConstrainedText(content, s, cFont, getX(), top, (int) maxWidth); } } OutputBounds rect = new OutputBounds(getX() - maxWidth / 2, getY(), maxWidth, getY() - top); return rect; }
From source file:org.openlmis.web.view.pdf.PdfPageEventHandler.java
License:Open Source License
private void addPageFooterInfo(PdfWriter writer, Document document) { PdfContentByte contentByte = writer.getDirectContent(); contentByte.saveState();//from www.j a v a 2 s .co m contentByte.setFontAndSize(baseFont, FOOTER_TEXT_SIZE); contentByte.beginText(); writeCurrentDate(document, contentByte); writePageNumber(writer, document, contentByte); contentByte.endText(); contentByte.restoreState(); }
From source file:org.primaresearch.pdf.PageToPdfConverter.java
License:Apache License
/** * Adds the text of the given page to the current PDF page * @param writer// w w w . java 2 s .c o m * @param page */ private void addText(PdfWriter writer, Page page) { if (textLevel == null) return; int pageHeight = page.getLayout().getHeight(); try { PdfContentByte cb = writer.getDirectContentUnder(); cb.saveState(); for (ContentIterator it = page.getLayout().iterator(textLevel); it.hasNext();) { ContentObject obj = it.next(); if (obj == null || !(obj instanceof TextObject)) continue; TextObject textObj = (TextObject) obj; if (textObj.getText() != null && !textObj.getText().isEmpty()) { List<String> strings = new ArrayList<String>(); List<Rect> boxes = new ArrayList<Rect>(); float fontSize = 1.0f; //Collect if (textObj instanceof LowLevelTextObject) { strings.add(textObj.getText()); Rect boundingBox = obj.getCoords().getBoundingBox(); boxes.add(boundingBox); fontSize = calculateFontSize(textObj.getText(), boundingBox.getWidth(), boundingBox.getHeight()); } else { fontSize = splitTextRegion((TextRegion) obj, strings, boxes); } //Render for (int i = 0; i < strings.size(); i++) { String text = strings.get(i); Rect boundingBox = boxes.get(i); //Calculate vertical transition (text is rendered at baseline -> descending bits are below the chosen position) int descent = (int) font.getDescentPoint(text, fontSize); int ascent = (int) font.getAscentPoint(text, fontSize); int textHeight = Math.abs(descent) + ascent; int transY = descent; if (textHeight < boundingBox.getHeight()) { transY = descent - (boundingBox.getHeight() - textHeight) / 2; } cb.beginText(); //cb.moveText(boundingBox.left, pageHeight - boundingBox.bottom); cb.setTextMatrix(boundingBox.left, pageHeight - boundingBox.bottom - transY); cb.setFontAndSize(font, fontSize); cb.showText(text); cb.endText(); //Debug //cb.moveTo(boundingBox.left, pageHeight - boundingBox.bottom - transY); //cb.lineTo(boundingBox.right, pageHeight - boundingBox.bottom - transY); //cb.moveTo(boundingBox.left, pageHeight - boundingBox.bottom); //cb.lineTo(boundingBox.right, pageHeight - boundingBox.bottom); } } } cb.restoreState(); } catch (Exception e) { e.printStackTrace(); } }
From source file:pdf.alterLetter.java
public static void main(String args[]) { try {// w ww . ja v a2s. c o m PdfReader pdfReader; pdfReader = new PdfReader("C:\\Users\\asus\\Desktop\\web\\Appointment letter.pdf"); //pdfReader = new PdfReader("C:\\Users\\asus\\Desktop\\TFMsystem\\Appointment letter.pdf"); //Create PdfStamper instance. PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream( "C:\\Users\\asus\\Desktop\\TFMsystem\\web\\Modified appointment letter.pdf")); //new FileOutputStream("C:\\Users\\asus\\Desktop\\TFMsystem\\Modified appointment letter.pdf")); //Create BaseFont instance. BaseFont baseFont = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1257, BaseFont.NOT_EMBEDDED); //Get the number of pages in pdf. int pages = pdfReader.getNumberOfPages(); //Iterate the pdf through pages. for (int i = 1; i <= pages; i++) { //Contain the pdf data. PdfContentByte pageContentByte = pdfStamper.getOverContent(i); pageContentByte.beginText(); //Set text font and size. pageContentByte.setFontAndSize(baseFont, 12); //Write text pageContentByte.setTextMatrix(120, 706); pageContentByte.showText("[no rujukan(enter by admin/opai)]"); pageContentByte.setTextMatrix(500, 706); pageContentByte.showText("[current date]"); //address pageContentByte.setTextMatrix(46, 641); pageContentByte.showText("[name]"); pageContentByte.setTextMatrix(46, 629); pageContentByte.showText("[position]"); pageContentByte.setTextMatrix(46, 617); pageContentByte.showText("[department]"); pageContentByte.setTextMatrix(155, 493); pageContentByte.showText("[status(penyelaras/ahli),taskforce name]"); pageContentByte.setTextMatrix(178, 433); pageContentByte.showText("[start date]"); pageContentByte.setTextMatrix(290, 433); pageContentByte.showText("[end date] ."); pageContentByte.setTextMatrix(46, 248); pageContentByte.showText("[name]"); pageContentByte.setTextMatrix(46, 236); pageContentByte.showText("[post]"); pageContentByte.setTextMatrix(46, 224); pageContentByte.showText("[faculty]"); pageContentByte.setTextMatrix(46, 212); pageContentByte.showText("[email]"); pageContentByte.endText(); } //Close the pdfStamper. pdfStamper.close(); System.out.println("PDF modified successfully."); } catch (Exception e) { e.printStackTrace(); } }
From source file:pdf.letter.java
public boolean AlterLetter(String rujukan, String name, String position, String department, String gStatus, String sDate, String eDate, String taskName, String postHolderName, String postHolderEmail, String postName) {/*from ww w .j ava 2s.c o m*/ DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); Date today = Calendar.getInstance().getTime(); String currDate = df.format(today); try { PdfReader pdfReader; pdfReader = new PdfReader("C:\\Users\\on\\Desktop\\AD\\TFMsystem\\web\\Appointment letter.pdf"); //C:\\Users\\on\\Desktop\\AD\\TFMsystem\\web\\Appointment letter.pdf //Create PdfStamper instance. PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream( "C:\\Users\\on\\Desktop\\AD\\TFMsystem\\web\\Modified appointment letter.pdf")); //C:\\Users\\on\\Desktop\\AD\\TFMsystem\\web\\Modified Appointment letter.pdf //Create BaseFont instance. BaseFont baseFont = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1257, BaseFont.NOT_EMBEDDED); //Get the number of pages in pdf. int pages = pdfReader.getNumberOfPages(); //Iterate the pdf through pages. for (int i = 1; i <= pages; i++) { //Contain the pdf data. PdfContentByte pageContentByte = pdfStamper.getOverContent(i); pageContentByte.beginText(); //Set text font and size. pageContentByte.setFontAndSize(baseFont, 11); //Write text pageContentByte.setTextMatrix(120, 706); pageContentByte.showText(rujukan); pageContentByte.setTextMatrix(500, 706); pageContentByte.showText(currDate); //address pageContentByte.setTextMatrix(46, 641); pageContentByte.showText(name); pageContentByte.setTextMatrix(46, 629); pageContentByte.showText(position); pageContentByte.setTextMatrix(46, 617); pageContentByte.showText(department); String gstatus; pageContentByte.setTextMatrix(157, 493); String changeCase = gStatus + ", " + taskName; pageContentByte.showText(changeCase.toUpperCase()); pageContentByte.setTextMatrix(250, 444); pageContentByte.showText(gStatus + " " + taskName + " ."); pageContentByte.setTextMatrix(180, 432); pageContentByte.showText(sDate); pageContentByte.setTextMatrix(290, 432); pageContentByte.showText(eDate + " ."); pageContentByte.setTextMatrix(46, 248); pageContentByte.showText(postHolderName); pageContentByte.setTextMatrix(46, 236); pageContentByte.showText(postName); pageContentByte.setTextMatrix(46, 224); pageContentByte.showText("Fakulti Komputeran"); pageContentByte.setTextMatrix(46, 212); pageContentByte.showText(postHolderEmail); pageContentByte.endText(); } //Close the pdfStamper. pdfStamper.close(); System.out.println("PDF modified successfully."); return true; } catch (Exception e) { e.printStackTrace(); return false; } }