List of usage examples for com.lowagie.text Font getStyle
public int getStyle()
From source file:com.amphisoft.epub2pdf.content.TextFactory.java
License:Open Source License
void modifyFontInPlace(Font font, StyleSpecText styleSpec) { int style = font.getStyle(); if (styleSpec.isBold()) { style |= Font.BOLD;// w w w . jav a2s .c o m } if (styleSpec.isItalic()) { style |= Font.ITALIC; } font.setStyle(style); }
From source file:com.amphisoft.epub2pdf.content.XhtmlHandler.java
License:Open Source License
@Override public void characters(char[] ch, int start, int length) { String content = new String(ch, start, length); String abridgedContent;//from w w w.ja va 2s.c o m if (content.length() <= 20) { abridgedContent = content; } else { abridgedContent = content.substring(0, 10); abridgedContent += " ... "; int tailStartIndex = content.length() - 10; int tailEndIndex = content.length(); abridgedContent += content.substring(tailStartIndex, tailEndIndex); } //printlnerr("chars:[" + abridgedContent + "]"); boolean printThese = true; for (String tag : nonPrintingTags) { if (tag.equals(currentTag)) { printThese = false; break; } } if (inStyleTag) { if (styleTagContents == null) { styleTagContents = new StringBuilder(); } styleTagContents.append(content); } else if (printThese) { Font font = null; if (XhtmlTags.PRE.equals(currentTag) || XhtmlTags.TT.equals(currentTag)) { font = textFactory.getDefaultFontMono(); } else { font = textFactory.getCurrentFont(); } if (currentITextStyle != font.getStyle()) { font.setStyle(currentITextStyle); } if (!(XhtmlTags.PRE.equals(currentTag) || XhtmlTags.TT.equals(currentTag))) { if (stack.size() > 0 && stack.peek() instanceof Paragraph) { content = changeLineBreaksToSpaces(content); } else { content = removeLineBreaks(content); } if (nothingButSpaces(content)) { SaxElement tagInProgress = saxElementStack.peek(); if (!tagInProgress.qName.equals("p")) { content = ""; } } boolean leadingSpace = content.length() >= 1 && content.startsWith(" ") && !freshParagraph; //content.charAt(1) != ' '; boolean trailingSpace = content.length() > 1 && content.endsWith(" "); //&& //content.charAt(content.length()-2) != ' '; content = content.trim(); if (leadingSpace) { content = " " + content; } if (trailingSpace) content = content + " "; } if (content.length() > 0) { if (currentChunk != null) { if (specialParagraph != null || currentChunk.getFont().compareTo(font) != 0) { pushToStack(currentChunk); currentChunk = null; } else { currentChunk.append(content); } } else { if (specialParagraph != null) { specialParagraph.add(new Chunk(content)); } else { currentChunk = new Chunk(content, font); } } } freshParagraph = false; } }
From source file:com.centurylink.mdw.designer.pages.ExportHelper.java
License:Apache License
private void printElementHtml(Element element, Object parent, int depth, Font font, int parentLevel) { String tag = element.getName(); Object av;/*from ww w.j a va2 s . co m*/ if (element instanceof HTMLDocument.RunElement) { HTMLDocument.RunElement re = (HTMLDocument.RunElement) element; int start = re.getStartOffset(); int end = re.getEndOffset(); try { String content = re.getDocument().getText(start, end - start); printAttributesHtml(re); av = re.getAttribute(CSS.Attribute.FONT_SIZE); String fontsize = av == null ? null : av.toString(); av = re.getAttribute(CSS.Attribute.FONT_FAMILY); String fontfamily = av == null ? null : av.toString(); av = re.getAttribute(CSS.Attribute.COLOR); String fontcolor = av == null ? null : av.toString(); if (fontcolor != null || fontsize != null || fontfamily != null) { if (fontfamily == null) fontfamily = font.getFamilyname(); float size = fontsize == null ? font.getSize() : (Float.parseFloat(fontsize) + 9); int style = font.getStyle(); Color color; if (fontcolor != null) { color = Color.decode(fontcolor); } else color = font.getColor(); font = FontFactory.getFont(fontfamily, size, style, color); } if (parent instanceof Paragraph) { ((Paragraph) parent).add(new Chunk(content, font)); } else { System.err.println("chunk with parent " + (parent == null ? "null" : parent.getClass().getName()) + ": " + content); } } catch (BadLocationException e) { e.printStackTrace(); } } else if (element instanceof HTMLDocument.BlockElement) { HTMLDocument.BlockElement be = (HTMLDocument.BlockElement) element; printAttributesHtml(be); av = be.getAttribute(javax.swing.text.html.HTML.Attribute.ALIGN); String align = av == null ? null : av.toString(); if (tag.equalsIgnoreCase("html")) { printElementChildrenHtml(element, parent, depth + 1, font, parentLevel); } else if (tag.equalsIgnoreCase("head")) { // do nothing } else if (tag.equalsIgnoreCase("body")) { printElementChildrenHtml(element, parent, depth + 1, font, parentLevel); } else if (tag.equalsIgnoreCase("p")) { if (parent instanceof Section) { Paragraph paragraph = new Paragraph(); if (align != null) { paragraph.setAlignment(align); } printElementChildrenHtml(element, paragraph, depth + 1, normalFont, parentLevel); ((Section) parent).add(paragraph); } else { System.err.println("p with parent " + (parent == null ? "null" : parent.getClass().getName())); } } else if (tag.equalsIgnoreCase("h1") || tag.equalsIgnoreCase("h2") || tag.equalsIgnoreCase("h3")) { if (parent instanceof Section) { Paragraph title = new Paragraph(); printElementChildrenHtml(element, title, depth + 1, subSectionFont, parentLevel); ((Section) parent).addSection(title, parentLevel == 0 ? 0 : (parentLevel + 1)); } else { System.err .println("list with parent " + (parent == null ? "null" : parent.getClass().getName())); } } else if (tag.equalsIgnoreCase("ul")) { if (parent instanceof Section) { com.lowagie.text.List list = new com.lowagie.text.List(false, false, 20.0f); printElementChildrenHtml(element, list, depth + 1, normalFont, parentLevel); ((Section) parent).add(list); } else { System.err .println("list with parent " + (parent == null ? "null" : parent.getClass().getName())); } } else if (tag.equalsIgnoreCase("ol")) { if (parent instanceof Section) { com.lowagie.text.List list = new com.lowagie.text.List(true, false, 20.0f); printElementChildrenHtml(element, list, depth + 1, normalFont, parentLevel); ((Section) parent).add(list); } else { System.err .println("list with parent " + (parent == null ? "null" : parent.getClass().getName())); } } else if (tag.equalsIgnoreCase("li")) { ListItem li = new ListItem(); li.setSpacingAfter(0.0f); printElementChildrenHtml(element, li, depth + 1, normalFont, parentLevel); ((com.lowagie.text.List) parent).add(li); } else if (tag.equalsIgnoreCase("p-implied")) { if (parent instanceof ListItem) { Paragraph paragraph = new Paragraph(); printElementChildrenHtml(element, paragraph, depth + 1, normalFont, parentLevel); ((ListItem) parent).add(paragraph); } else if (parent instanceof Cell) { Paragraph paragraph = new Paragraph(); printElementChildrenHtml(element, paragraph, depth + 1, normalFont, parentLevel); ((Cell) parent).add(paragraph); } } else if (tag.equalsIgnoreCase("table")) { try { Table table = new Table(3); table.setBorderWidth(1); table.setBorderColor(new Color(0, 128, 128)); table.setPadding(1.0f); table.setSpacing(0.5f); Cell c = new Cell("header"); c.setHeader(true); c.setColspan(3); table.addCell(c); table.endHeaders(); printElementChildrenHtml(element, table, depth + 1, normalFont, parentLevel); // TODO ((Section) parent).add(table); } catch (BadElementException e) { e.printStackTrace(); } } else if (tag.equalsIgnoreCase("tr")) { printElementChildrenHtml(element, parent, depth + 1, normalFont, parentLevel); // TODO } else if (tag.equalsIgnoreCase("td")) { Cell cell = new Cell(); printElementChildrenHtml(element, cell, depth + 1, normalFont, parentLevel); // TODO ((Table) parent).addCell(cell); } else { System.err.println("Unknown element " + element.getName()); printElementChildrenHtml(element, parent, depth + 1, normalFont, parentLevel); } } else { return; // could be BidiElement - not sure what it is } }
From source file:com.centurylink.mdw.designer.pages.ExportHelper.java
License:Apache License
private Object generateElementHtml(Element element, int depth, Font font) { String tag = element.getName(); Object myself;//from ww w . jav a 2s . c o m Object av; if (element instanceof HTMLDocument.RunElement) { HTMLDocument.RunElement re = (HTMLDocument.RunElement) element; int start = re.getStartOffset(); int end = re.getEndOffset(); try { String content = re.getDocument().getText(start, end - start); HtmlAttr htmlattr = printAttributesHtml(re); av = re.getAttribute(CSS.Attribute.FONT_SIZE); String fontsize = av == null ? null : av.toString(); av = re.getAttribute(CSS.Attribute.FONT_FAMILY); String fontfamily = av == null ? null : av.toString(); av = re.getAttribute(CSS.Attribute.COLOR); String fontcolor = av == null ? null : av.toString(); if (fontcolor != null || fontsize != null || fontfamily != null) { if (fontfamily == null) fontfamily = font.getFamilyname(); if (fontsize != null && fontsize.endsWith("pt")) fontsize = fontsize.substring(0, fontsize.indexOf("pt")); float size = fontsize == null ? font.getSize() : (Float.parseFloat(fontsize) + 8); int style = font.getStyle(); Color color; if (fontcolor != null) { color = Color.decode(fontcolor); } else color = font.getColor(); font = FontFactory.getFont(fontfamily, size, style, color); } else if (htmlattr.bold || htmlattr.italic) { String family = font.getFamilyname(); float size = font.getSize(); Color color = font.getColor(); if (htmlattr.bold && htmlattr.italic) font = FontFactory.getFont(family, size, Font.BOLDITALIC, color); else if (htmlattr.italic) font = FontFactory.getFont(family, size, Font.ITALIC, color); else if (htmlattr.bold) font = FontFactory.getFont(family, size, Font.BOLD); } myself = new Chunk(content, font); } catch (BadLocationException e) { e.printStackTrace(); myself = null; } } else if (element instanceof HTMLDocument.BlockElement) { HTMLDocument.BlockElement be = (HTMLDocument.BlockElement) element; HtmlAttr htmlattr = printAttributesHtml(be); if (htmlattr.bold) { System.out.println("+++BOLD!!!"); } av = be.getAttribute(javax.swing.text.html.HTML.Attribute.ALIGN); String align = av == null ? null : av.toString(); if (htmlattr.bold || htmlattr.italic) { String family = font.getFamilyname(); float size = font.getSize(); Color color = font.getColor(); if (htmlattr.bold && htmlattr.italic) font = FontFactory.getFont(family, size, Font.BOLDITALIC, color); else if (htmlattr.italic) font = FontFactory.getFont(family, size, Font.ITALIC, color); else if (htmlattr.bold) font = FontFactory.getFont(family, size, Font.BOLD, Color.blue); } if (tag.equalsIgnoreCase("html")) { myself = generateElementChildrenHtml(element, depth + 1, font); } else if (tag.equalsIgnoreCase("head")) { myself = null; } else if (tag.equalsIgnoreCase("body")) { myself = generateElementChildrenHtml(element, depth + 1, font); } else if (tag.equalsIgnoreCase("p") || tag.equalsIgnoreCase("p-implied")) { List<Object> children = generateElementChildrenHtml(element, depth + 1, normalFont); Paragraph paragraph = new Paragraph(); paragraph.setFirstLineIndent(0F); for (Object child : children) { if (child instanceof Chunk) { Chunk chunk = (Chunk) child; /*if (!chunk.getContent().equals("\n"))*/ paragraph.add(chunk); } else paragraph.add(child); } if (align != null) paragraph.setAlignment(align); myself = paragraph; } else if (tag.equalsIgnoreCase("h1") || tag.equalsIgnoreCase("h2") || tag.equalsIgnoreCase("h3")) { List<Object> children = generateElementChildrenHtml(element, depth + 1, subSectionFont); Paragraph title = new Paragraph(); for (Object child : children) { title.add(child); } myself = new TempSectionPdf(title); } else if (tag.equalsIgnoreCase("ul")) { com.lowagie.text.List list = new com.lowagie.text.List(false, false, 20.0f); list.setIndentationLeft(25.0f); List<Object> children = generateElementChildrenHtml(element, depth + 1, normalFont); for (Object child : children) { list.add(child); } myself = list; } else if (tag.equalsIgnoreCase("ol")) { com.lowagie.text.List list = new com.lowagie.text.List(true, false, 20.0f); list.setIndentationLeft(25.0f); List<Object> children = generateElementChildrenHtml(element, depth + 1, normalFont); for (Object child : children) { list.add(child); } myself = list; } else if (tag.equalsIgnoreCase("li")) { ListItem li = new ListItem(); li.setSpacingAfter(0.0f); List<Object> children = generateElementChildrenHtml(element, depth + 1, normalFont); for (Object child : children) { li.add(child); } myself = li; } else if (tag.equalsIgnoreCase("table")) { List<Object> rows = generateElementChildrenHtml(element, depth + 1, normalFont); try { int ncols = 0; for (Object row : rows) { if (row instanceof List<?>) { int n = ((List<?>) row).size(); if (n > ncols) ncols = n; } } Table table = new Table(2); table.setBorderWidth(1); table.setBorderColor(new Color(0, 128, 128)); table.setPadding(1.0f); table.setSpacing(0.5f); Cell c = new Cell("header"); c.setHeader(true); c.setColspan(ncols); table.addCell(c); table.endHeaders(); for (Object row : rows) { if (row instanceof List<?>) { for (Object cell : (List<?>) row) { if (cell instanceof Cell) table.addCell((Cell) cell); } } } myself = table; } catch (BadElementException e) { e.printStackTrace(); myself = null; } } else if (tag.equalsIgnoreCase("tr")) { List<Object> children = generateElementChildrenHtml(element, depth + 1, normalFont); myself = children; } else if (tag.equalsIgnoreCase("td")) { Cell cell = new Cell(); List<Object> children = generateElementChildrenHtml(element, depth + 1, normalFont); for (Object child : children) { cell.add(child); } myself = cell; } else if (tag.equalsIgnoreCase("div")) { List<Object> children = generateElementChildrenHtml(element, depth + 1, normalFont); Paragraph paragraph = new Paragraph(); paragraph.setFirstLineIndent(0F); for (Object child : children) { paragraph.add(child); } if (align != null) paragraph.setAlignment(align); myself = paragraph; } else { System.err.println("Unknown element " + element.getName()); myself = null; } } else { myself = null; // could be BidiElement - not sure what it is } return myself; }
From source file:fr.opensagres.odfdom.converter.pdf.internal.stylable.StylableAnchor.java
License:Open Source License
public void applyStyles(Style style) { this.lastStyleApplied = style; StyleTextProperties textProperties = style.getTextProperties(); if (textProperties != null) { // Font// w w w .ja v a 2 s . c o m Font font = textProperties.getFont(); if (font != null) { if (!font.isUnderlined()) { font = new Font(font); font.setStyle(font.getStyle() | Font.UNDERLINE); } super.setFont(font); } } }
From source file:fr.opensagres.odfdom.converter.pdf.internal.stylable.StylableAnchor.java
License:Open Source License
@SuppressWarnings("unchecked") public Element getElement() { // underline font if not explicitly set ArrayList<Chunk> chunks = getChunks(); for (Chunk chunk : chunks) { Font f = chunk.getFont(); if (f != null && !f.isUnderlined()) { f = new Font(f); f.setStyle(f.getStyle() | Font.UNDERLINE); chunk.setFont(f);/*from www .j a va 2s. co m*/ } } return this; }
From source file:fr.opensagres.odfdom.converter.pdf.internal.stylable.StylableParagraph.java
License:Open Source License
@SuppressWarnings("unchecked") private void postProcessLineHeightAndBaseline() { // adjust line height and baseline Font font = getMostOftenUsedFont(); if (font == null || font.getBaseFont() == null) { font = this.font; }/* w w w .j a va 2 s . co m*/ if (font != null && font.getBaseFont() != null) { // iText and open office computes proportional line height differently // [iText] line height = coefficient * font size // [open office] line height = coefficient * (font ascender + font descender + font extra margin) // we have to increase paragraph line height to generate pdf similar to open office document // this algorithm may be inaccurate if fonts with different multipliers are used in this paragraph float size = font.getSize(); float ascender = font.getBaseFont().getFontDescriptor(BaseFont.AWT_ASCENT, size); float descender = -font.getBaseFont().getFontDescriptor(BaseFont.AWT_DESCENT, size); // negative value float margin = font.getBaseFont().getFontDescriptor(BaseFont.AWT_LEADING, size); float multiplier = (ascender + descender + margin) / size; if (multipliedLeading > 0.0f) { setMultipliedLeading(getMultipliedLeading() * multiplier); } // iText seems to output text with baseline lower than open office // we raise all paragraph text by some amount // again this may be inaccurate if fonts with different size are used in this paragraph float itextdescender = -font.getBaseFont().getFontDescriptor(BaseFont.DESCENT, size); // negative float textRise = itextdescender + getTotalLeading() - font.getSize() * multiplier; ArrayList<Chunk> chunks = getChunks(); for (Chunk chunk : chunks) { Font f = chunk.getFont(); if (f != null) { // have to raise underline and strikethru as well float s = f.getSize(); if (f.isUnderlined()) { f.setStyle(f.getStyle() & ~Font.UNDERLINE); chunk.setUnderline(s * 1 / 17, s * -1 / 7 + textRise); } if (f.isStrikethru()) { f.setStyle(f.getStyle() & ~Font.STRIKETHRU); chunk.setUnderline(s * 1 / 17, s * 1 / 4 + textRise); } } chunk.setTextRise(chunk.getTextRise() + textRise); } } }
From source file:mesquite.lib.MesquitePDFFile.java
License:Open Source License
/** @arg s String holds the text that the pdf file will contain @arg font java.awt.Font the font is specified this way for compatibility with the similar method in MesquitePrintJob *///from ww w. j a v a 2 s.c o m public void printText(String s, java.awt.Font font) { final String exceptionMessage = "Error, an exception occurred while creating the pdf text document: "; if (s == null || font == null) return; //do the translation from logical to physical font here //currently, the only font this method ever gets called with is "Monospaced",PLAIN,10. So the //translation effort here will be minimal int desiredFontFamily; // "Monospaced" isn't defined in com.lowagie.text.Font if (font.getFamily().equals("Monospaced")) desiredFontFamily = com.lowagie.text.Font.COURIER; else desiredFontFamily = com.lowagie.text.Font.TIMES_ROMAN; com.lowagie.text.Font textFont; switch (font.getStyle()) { case java.awt.Font.BOLD: { textFont = new com.lowagie.text.Font(desiredFontFamily, font.getSize(), com.lowagie.text.Font.BOLD); break; } case java.awt.Font.ITALIC: { textFont = new com.lowagie.text.Font(desiredFontFamily, font.getSize(), com.lowagie.text.Font.ITALIC); break; } case java.awt.Font.PLAIN: { textFont = new com.lowagie.text.Font(desiredFontFamily, font.getSize(), com.lowagie.text.Font.NORMAL); break; } default: { textFont = new com.lowagie.text.Font(desiredFontFamily, font.getSize(), com.lowagie.text.Font.BOLDITALIC); } } document = new Document(); try { PdfWriter.getInstance(document, new FileOutputStream(pdfPathString)); addMetaData(document); document.open(); document.add(new Paragraph(s, textFont)); } catch (DocumentException de) { MesquiteTrunk.mesquiteTrunk.alert(exceptionMessage + de.getMessage()); } catch (IOException ioe) { MesquiteTrunk.mesquiteTrunk.alert(exceptionMessage + ioe.getMessage()); } this.end(); }
From source file:org.odftoolkit.odfdom.converter.internal.itext.stylable.StylableParagraph.java
License:Open Source License
@SuppressWarnings("unchecked") public Element getElement() { if (!elementPostProcessed) { elementPostProcessed = true;/*from ww w .j a va 2s .c o m*/ // add space if this paragraph is empty // otherwise it's height will be zero boolean empty = true; ArrayList<Chunk> chunks = getChunks(); for (Chunk chunk : chunks) { if (chunk.getImage() == null && chunk.getContent() != null && chunk.getContent().length() > 0) { empty = false; break; } } if (empty) { super.add(new Chunk("\u00A0")); // non breaking space } // adjust line height and baseline if (font != null && font.getBaseFont() != null) { // iText and open office computes proportional line height differently // [iText] line height = coefficient * font size // [open office] line height = coefficient * (font ascender + font descender + font extra margin) // we have to increase paragraph line height to generate pdf similar to open office document // this algorithm may be inaccurate if fonts with different multipliers are used in this paragraph float size = font.getSize(); float ascender = font.getBaseFont().getFontDescriptor(BaseFont.AWT_ASCENT, size); float descender = -font.getBaseFont().getFontDescriptor(BaseFont.AWT_DESCENT, size); // negative value float margin = font.getBaseFont().getFontDescriptor(BaseFont.AWT_LEADING, size); float multiplier = (ascender + descender + margin) / size; if (multipliedLeading > 0.0f) { setMultipliedLeading(getMultipliedLeading() * multiplier); } // iText seems to output text with baseline lower than open office // we raise all paragraph text by some amount // again this may be inaccurate if fonts with different size are used in this paragraph float itextdescender = -font.getBaseFont().getFontDescriptor(BaseFont.DESCENT, size); // negative float textRise = itextdescender + getTotalLeading() - font.getSize() * multiplier; chunks = getChunks(); for (Chunk chunk : chunks) { Font f = chunk.getFont(); if (f != null) { // have to raise underline and strikethru as well float s = f.getSize(); if (f.isUnderlined()) { f.setStyle(f.getStyle() & ~Font.UNDERLINE); chunk.setUnderline(s * 1 / 17, s * -1 / 7 + textRise); } if (f.isStrikethru()) { f.setStyle(f.getStyle() & ~Font.STRIKETHRU); chunk.setUnderline(s * 1 / 17, s * 1 / 4 + textRise); } } chunk.setTextRise(chunk.getTextRise() + textRise); } } // wrap this paragraph into a table if necessary if (wrapperCell != null) { // background color or borders were set wrapperCell.addElement(this); wrapperTable = createTable(wrapperCell); if (getIndentationLeft() > 0.0f || getIndentationRight() > 0.0f || getSpacingBefore() > 0.0f || getSpacingAfter() > 0.0f) { // margins were set, have to wrap the cell again PdfPCell outerCell = createCell(); outerCell.setPaddingLeft(getIndentationLeft()); setIndentationLeft(0.0f); outerCell.setPaddingRight(getIndentationRight()); setIndentationRight(0.0f); outerCell.setPaddingTop(getSpacingBefore()); setSpacingBefore(0.0f); outerCell.setPaddingBottom(getSpacingAfter()); setSpacingAfter(0.0f); outerCell.addElement(wrapperTable); wrapperTable = createTable(outerCell); } } } return wrapperTable != null ? wrapperTable : this; }
From source file:org.unitime.timetable.util.PdfFont.java
License:Open Source License
public static Font getFont(boolean bold, boolean italic, boolean underline, Color color) { Font font = getFont(bold, italic); if (underline) font.setStyle(font.getStyle() + Font.UNDERLINE); if (color != null) font.setColor(color);//w ww . j a va2 s . c om return font; }