public Chunk getCunck(String htmlTags, String text) { String cleanedText = text.replaceAll("\\<.*?\\>", "").replaceAll("(?m)^[ \t]*\r?\n", "").trim(); Chunk result = null; float h1_size = 14; float h2_size = 13; float h3_size = 12; float default_size = 11; String default_font_style = FontFactory.COURIER; int default_font = Font.NORMAL; boolean bold = false; boolean italic = false; boolean href = false; boolean underline = false; if (htmlTags.indexOf("<b>") != -1) { bold = true; } if (htmlTags.indexOf("href") != -1) { href = true; underline = true; } if (htmlTags.indexOf("<strong>") != -1) { bold = true; } if (htmlTags.indexOf("<em>") != -1) { italic = true; } if (htmlTags.indexOf("<i>") != -1) { italic = true; } if (htmlTags.indexOf("<h1>") != -1) { bold = true; default_size = h1_size; } if (htmlTags.indexOf("<h2>") != -1) { bold = true; default_size = h2_size; } if (htmlTags.indexOf("<h3>") != -1) { bold = true; default_size = h3_size; } if (bold && italic) { default_font = Font.BOLDITALIC; } else if (italic) { default_font = Font.ITALIC; } else if (bold) { default_font = Font.BOLD; } else if (underline) { default_font = Font.UNDERLINE; } result = new Chunk( cleanedText, FontFactory.getFont( default_font_style, default_size, default_font, new Color(0, 0, 0))); if (href) { result.setAnchor(StringUtils.substringBetween(htmlTags, "href=\"", "\"")); } return result; }
/** * Constructs a <CODE>PdfOutline</CODE>. * * <p>This is the constructor for an <CODE>outline entry</CODE>. * * @param parent the parent of this outline item * @param destination the destination for this outline item * @param title the title of this outline item * @param open <CODE>true</CODE> if the children are visible */ public PdfOutline(PdfOutline parent, PdfDestination destination, Paragraph title, boolean open) { super(); StringBuffer buf = new StringBuffer(); for (Iterator i = title.getChunks().iterator(); i.hasNext(); ) { Chunk chunk = (Chunk) i.next(); buf.append(chunk.getContent()); } this.destination = destination; initOutline(parent, buf.toString(), open); }
/** * Constructs a RtfParagraph belonging to a RtfDocument based on a Paragraph. * * @param doc The RtfDocument this RtfParagraph belongs to * @param paragraph The Paragraph that this RtfParagraph is based on */ public RtfParagraph(RtfDocument doc, Paragraph paragraph) { super(doc); RtfFont baseFont = null; if (paragraph.getFont() instanceof RtfParagraphStyle) { this.paragraphStyle = this.document .getDocumentHeader() .getRtfParagraphStyle(((RtfParagraphStyle) paragraph.getFont()).getStyleName()); baseFont = this.paragraphStyle; } else { baseFont = new RtfFont(this.document, paragraph.getFont()); this.paragraphStyle = new RtfParagraphStyle( this.document, this.document.getDocumentHeader().getRtfParagraphStyle("Normal")); this.paragraphStyle.setAlignment(paragraph.getAlignment()); this.paragraphStyle.setFirstLineIndent( (int) (paragraph.getFirstLineIndent() * RtfElement.TWIPS_FACTOR)); this.paragraphStyle.setIndentLeft( (int) (paragraph.getIndentationLeft() * RtfElement.TWIPS_FACTOR)); this.paragraphStyle.setIndentRight( (int) (paragraph.getIndentationRight() * RtfElement.TWIPS_FACTOR)); this.paragraphStyle.setSpacingBefore( (int) (paragraph.getSpacingBefore() * RtfElement.TWIPS_FACTOR)); this.paragraphStyle.setSpacingAfter( (int) (paragraph.getSpacingAfter() * RtfElement.TWIPS_FACTOR)); if (paragraph.hasLeading()) { this.paragraphStyle.setLineLeading( (int) (paragraph.getLeading() * RtfElement.TWIPS_FACTOR)); } this.paragraphStyle.setKeepTogether(paragraph.getKeepTogether()); } for (int i = 0; i < paragraph.size(); i++) { Element chunk = (Element) paragraph.get(i); if (chunk instanceof Chunk) { ((Chunk) chunk).setFont(baseFont.difference(((Chunk) chunk).getFont())); } else if (chunk instanceof RtfImage) { ((RtfImage) chunks.get(i)).setAlignment(this.paragraphStyle.getAlignment()); } try { RtfBasicElement[] rtfElements = doc.getMapper().mapElement(chunk); for (int j = 0; j < rtfElements.length; j++) { chunks.add(rtfElements[j]); } } catch (DocumentException de) { } } }
/** * Constructs a RtfChunk based on the content of a Chunk * * @param doc The RtfDocument that this Chunk belongs to * @param chunk The Chunk that this RtfChunk is based on */ public RtfChunk(RtfDocument doc, Chunk chunk) { super(doc); if (chunk == null) { return; } if (chunk.getAttributes() != null && chunk.getAttributes().get(Chunk.SUBSUPSCRIPT) != null) { this.superSubScript = ((Float) chunk.getAttributes().get(Chunk.SUBSUPSCRIPT)).floatValue(); } if (chunk.getAttributes() != null && chunk.getAttributes().get(Chunk.BACKGROUND) != null) { this.background = new RtfColor( this.document, (Color) ((Object[]) chunk.getAttributes().get(Chunk.BACKGROUND))[0]); } font = new RtfFont(doc, chunk.getFont()); content = chunk.getContent(); }
public Chunk createChunk(String text, ChainedProperties props) { Chunk ck = new Chunk(text, getFont(props)); if (props.hasProperty("sub")) ck.setTextRise(-6); else if (props.hasProperty("sup")) ck.setTextRise(6); return ck; }
/** * Constructs a <CODE>PdfChunk</CODE>-object. * * @param chunk the original <CODE>Chunk</CODE>-object * @param action the <CODE>PdfAction</CODE> if the <CODE>Chunk</CODE> comes from an <CODE>Anchor * </CODE> */ PdfChunk(Chunk chunk, PdfAction action) { thisChunk[0] = this; value = chunk.getContent(); Font f = chunk.getFont(); float size = f.getSize(); if (size == Font.UNDEFINED) size = 12; baseFont = f.getBaseFont(); int style = f.getStyle(); if (style == Font.UNDEFINED) { style = Font.NORMAL; } if (baseFont == null) { // translation of the font-family to a PDF font-family baseFont = f.getCalculatedBaseFont(false); } else { // bold simulation if ((style & Font.BOLD) != 0) attributes.put( Chunk.TEXTRENDERMODE, new Object[] { new Integer(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE), new Float(size / 30f), null }); // italic simulation if ((style & Font.ITALIC) != 0) attributes.put(Chunk.SKEW, new float[] {0, ITALIC_ANGLE}); } font = new PdfFont(baseFont, size); // other style possibilities HashMap attr = chunk.getAttributes(); if (attr != null) { for (Iterator i = attr.entrySet().iterator(); i.hasNext(); ) { Map.Entry entry = (Map.Entry) i.next(); Object name = entry.getKey(); if (keysAttributes.containsKey(name)) { attributes.put(name, entry.getValue()); } else if (keysNoStroke.containsKey(name)) { noStroke.put(name, entry.getValue()); } } if ("".equals(attr.get(Chunk.GENERICTAG))) { attributes.put(Chunk.GENERICTAG, chunk.getContent()); } } if (f.isUnderlined()) { Object obj[] = {null, new float[] {0, 1f / 15, 0, -1f / 3, 0}}; Object unders[][] = Utilities.addToArray((Object[][]) attributes.get(Chunk.UNDERLINE), obj); attributes.put(Chunk.UNDERLINE, unders); } if (f.isStrikethru()) { Object obj[] = {null, new float[] {0, 1f / 15, 0, 1f / 3, 0}}; Object unders[][] = Utilities.addToArray((Object[][]) attributes.get(Chunk.UNDERLINE), obj); attributes.put(Chunk.UNDERLINE, unders); } if (action != null) attributes.put(Chunk.ACTION, action); // the color can't be stored in a PdfFont noStroke.put(Chunk.COLOR, f.getColor()); noStroke.put(Chunk.ENCODING, font.getFont().getEncoding()); Object obj[] = (Object[]) attributes.get(Chunk.IMAGE); if (obj == null) { image = null; } else { attributes.remove(Chunk.HSCALE); // images are scaled in other ways image = (Image) obj[0]; offsetX = ((Float) obj[1]).floatValue(); offsetY = ((Float) obj[2]).floatValue(); changeLeading = ((Boolean) obj[3]).booleanValue(); } font.setImage(image); Float hs = (Float) attributes.get(Chunk.HSCALE); if (hs != null) font.setHorizontalScaling(hs.floatValue()); encoding = font.getFont().getEncoding(); splitCharacter = (SplitCharacter) noStroke.get(Chunk.SPLITCHARACTER); if (splitCharacter == null) splitCharacter = DefaultSplitCharacter.DEFAULT; }