List of usage examples for com.lowagie.text TextElementArray add
public boolean add(Object o);
TextElementArray
. From source file:br.gov.jfrj.siga.ex.util.GeradorRTF.java
License:Open Source License
private TextElementArray percorreProximoBloco(TextElementArray element, int level, boolean considerarPTags) throws Exception { String tagName = parser.getName(); while (!(parser.getEventType() == XmlPullParser.END_TAG && parser.getName() != null && parser.getName().equals(tagName))) { parser.nextToken();/* ww w .j a v a 2 s.c om*/ // insere um texto comum if (parser.getEventType() == XmlPullParser.TEXT) element.add(parser.getText().toUpperCase()); // Insere um pargrafo if (parser.getEventType() == XmlPullParser.START_TAG) if (parser.getName().toUpperCase().equals("P") && considerarPTags /* * || parser.getName().toUpperCase().equals("UL") || * parser.getName().toUpperCase().equals("OL") || * parser.getName().toUpperCase().equals("DL") */) { Paragraph paragrafo = new Paragraph(); paragrafo.add("\t"); // paragrafo.setFirstLineIndent(((Paragraph)element).indentationLeft() // + 29); // paragrafo.setSpacingAfter(200); // paragrafo.setSpacingBefore(200); paragrafo = (Paragraph) percorreProximoBloco(paragrafo, level + 1, true); paragrafo.add("\n"); element.add(paragrafo); } if (parser.getEventType() == XmlPullParser.START_TAG) if (parser.getName().toUpperCase().equals("TABLE") /* * || parser.getName().toUpperCase().equals("UL") || * parser.getName().toUpperCase().equals("OL") || * parser.getName().toUpperCase().equals("DL") */) { Paragraph paragrafo = new Paragraph(); paragrafo.add("\t"); // paragrafo.setFirstLineIndent(((Paragraph)element).indentationLeft() // + 29); // paragrafo.setSpacingAfter(200); // paragrafo.setSpacingBefore(200); paragrafo = (Paragraph) percorreProximoBloco(paragrafo, level + 1, false); paragrafo.add("\n"); element.add(paragrafo); } // Insere um item de lista if (parser.getEventType() == XmlPullParser.START_TAG) if (parser.getName().toUpperCase().equals("LI")) { Paragraph paragrafo = new Paragraph(); for (int k = 0; k <= level + 1; k++) paragrafo.add("\t"); paragrafo = (Paragraph) percorreProximoBloco(paragrafo, level + 1, false); element.add(paragrafo); } // Pula linha ao fim da lista if (parser.getEventType() == XmlPullParser.END_TAG) if (parser.getName().toUpperCase().equals("UL") || parser.getName().toUpperCase().equals("OL") || parser.getName().toUpperCase().equals("DL")) { element.add("\n"); } if (parser.getEventType() == XmlPullParser.START_TAG) if (parser.getName().toUpperCase().equals("TD")) { element.add("\t"); percorreProximoBloco(element, level + 1, false); } if (parser.getEventType() == XmlPullParser.START_TAG) if (parser.getName().toUpperCase().equals("TR")) { element.add("\n\n"); } if (parser.getEventType() == XmlPullParser.START_TAG) if (parser.getName().toUpperCase().equals("BR")) { element.add("\n\n\t"); } } return element; }
From source file:com.amphisoft.epub2pdf.content.XhtmlHandler.java
License:Open Source License
@Override public void startElement(String uri, String localName, String qName, Attributes attributes) { /*//from w ww . j av a 2s. co m if("ol".equals(qName) || "ul".equals(qName) || "li".equals(qName)) { System.err.print(qName + " "); } */ currentSaxElemId = saxElemIdCounter; Map<String, String> attrMap = new HashMap<String, String>(); // parse attributes for (int ai = 0; ai < attributes.getLength(); ai++) { attrMap.put(attributes.getQName(ai), attributes.getValue(ai)); } String idAttr = attrMap.get("id"); if (idAttr == null) { idAttr = ""; } String className = attrMap.get("class"); if (className == null) { className = ""; } SaxElement sE = new SaxElement(qName, saxElemIdCounter++, className, idAttr, currentITextStyle); //printlnerr("startElement: " + sE.toString()); saxElementStack.push(sE); try { if (attrMap.get("class") != null) { String[] elemClasses = attrMap.get("class").split(" "); for (String eClass : elemClasses) { StyleSpecText classTextStyles = styleMap.getTextStyleSpecFor(qName, eClass); if (classTextStyles != null) { sE.applyTextStyles(classTextStyles); } } } if (attrMap.get("style") != null) { // TODO this needs more thought, and careful tracking of which tags are still open, etc. //String styleSource = attrMap.get("style"); //CssStyleMap styleTagStyles = cssParser.getStylesFromStyleTag(styleSource); // ... } if (sE.textStyles == null) { try { int stackSize = saxElementStack.size(); if (stackSize > 1) { SaxElement enclosingElement = saxElementStack.elementAt(stackSize - 2); StyleSpecText enclosingSST = enclosingElement.textStyles; if (enclosingSST != null) sE.applyTextStyles(enclosingSST); } } catch (Exception e) { } } StyleSpecText currentTextStyles = sE.textStyles; if (currentTextStyles != null) { if (currentTextStyles.isBold()) { currentITextStyle |= Font.BOLD; } if (currentTextStyles.isItalic()) { currentITextStyle |= Font.ITALIC; } } //System.err.println("PUSH -> " + saxElementStack); previousTag = currentTag; currentTag = qName; if (document.isOpen()) { if (XhtmlTags.NEWLINE.equals(qName)) { if (stack.size() > 0) { TextElementArray currentTEA = (TextElementArray) stack.peek(); currentTEA.add(Chunk.NEWLINE); } else if (specialParagraph != null) { specialParagraph.add(Chunk.NEWLINE); } } updateStack(); String xmlElementId = attrMap.get("id"); if (XhtmlTags.ANCHOR.equals(qName)) { //concession to nonconformists... if (xmlElementId == null) { xmlElementId = attrMap.get("name"); } Anchor anchor = textFactory.newAnchor(); String ref = attrMap.get(XhtmlTags.REFERENCE); if (ref != null) { int aNameStartIdx = ref.lastIndexOf("#") + 1; ref = ref.substring(aNameStartIdx); anchor.setReference(ref); } if (xmlElementId != null) { anchor.setName(xmlElementId); } pushToStack(anchor); } else { if (xmlElementId != null) { //flushStack(); Anchor dest = textFactory.newAnchor(); dest.setName(xmlElementId); pushToStack(dest); //flushStack(); } for (int i = 0; i < 6; i++) { if (XhtmlTags.H[i].equals(qName)) { flushStack(); freshParagraph = true; currentITextStyle |= Font.BOLD; specialParagraph = textFactory.newHeadline(i + 1); return; } } if ("blockquote".equals(qName)) { flushStack(); freshParagraph = true; Paragraph p = textFactory.newParagraph(); p.setIndentationLeft(50); p.setIndentationRight(20); p.setAlignment(defaultAlignment); pushToStack(p); } else if (XhtmlTags.PARAGRAPH.equals(qName)) { flushStack(); freshParagraph = true; Paragraph p = textFactory.newParagraph(); pushToStack(p); } else if (XhtmlTags.DIV.equals(qName)) { if (stack.size() > 0 && stack.peek().getChunks().size() > 0) { flushStack(); } if (stack.size() == 0) { Paragraph brandNewParagraph = textFactory.newParagraph(); pushToStack(brandNewParagraph); freshParagraph = true; } } else if (XhtmlTags.PRE.equals(qName)) { flushStack(); freshParagraph = true; Paragraph p = textFactory.newParagraphPre(); pushToStack(p); } else if (XhtmlTags.ORDEREDLIST.equals(qName)) { flushStack(); List oList = new List(List.ORDERED, 10); pushToStack(oList); } else if (XhtmlTags.UNORDEREDLIST.equals(qName)) { flushStack(); List uList = new List(List.UNORDERED, 10); pushToStack(uList); } else if (XhtmlTags.LISTITEM.equals(qName)) { freshParagraph = true; ListItem listItem = new ListItem(); pushToStack(listItem); } else if (XhtmlTags.IMAGE.equals(qName)) { handleImage(attributes); } else if (qName != null && qName.endsWith("image")) { handleSvgImage(attributes); } else if (XhtmlTags.LINK.equals(qName)) { // if it's a stylesheet, parse it & update current-style if ("stylesheet".equals(attrMap.get("rel")) && "text/css".equals(attrMap.get("type")) && attrMap.get("href") != null) { String cssHref = xhtmlDir.getAbsoluteFile().toURI().toString() + attrMap.get("href"); CssStyleMap stylesFromLink = cssParser.getStylesFromFileURI(cssHref); if (stylesFromLink != null) { styleMap.updateWith(stylesFromLink); } } } else if (XhtmlTags.STYLE.equals(qName)) { inStyleTag = true; } else if (XhtmlTags.EM.equals(qName) || "I".equals(qName.toUpperCase())) { currentITextStyle |= Font.ITALIC; } else if (XhtmlTags.STRONG.equals(currentTag) || "B".equals(qName.toUpperCase())) { currentITextStyle |= Font.BOLD; } } } else if (XhtmlTags.BODY.equals(qName)) { document.open(); freshParagraph = true; } } catch (Exception e) { e.printStackTrace(); } //printlnerr("leaving startElement " + localName + "; stack: " + stackStatus()); }
From source file:com.amphisoft.epub2pdf.content.XhtmlHandler.java
License:Open Source License
private TextElementArray appendToTEA(TextElementArray previous, Element element) { //debugAppendToTEA(previous,element); @SuppressWarnings("unused") boolean success = previous.add(element); //debugAppendedTEA(previous,success); return previous; }
From source file:com.amphisoft.epub2pdf.content.XhtmlHandler.java
License:Open Source License
/** * If the current Chunk is not null, its constituents are forwarded to the stack and it is then made * null./*from ww w. j ava2s . co m*/ */ private void updateStack() { //printlnerr("entering updateStack; stack: " + stackStatus()); java.util.List<String> noNewSpaceTagsColl = getNoNewSpaceTagsList(); if (currentChunk != null) { TextElementArray current; try { current = (TextElementArray) popFromStack(); if ((!(current instanceof Paragraph) || !((Paragraph) current).isEmpty()) && !(noNewSpaceTagsColl.contains(currentTag))) { current = appendToTEA(current, new Chunk(" ")); } } catch (EmptyStackException ese) { current = textFactory.newParagraph(); } //printlnerr("*** CHUNK {" + currentChunk.getContent() + "}"); current.add(currentChunk); pushToStack(current); currentChunk = null; } //printlnerr("leaving updateStack; stack: " + stackStatus()); }
From source file:nl.knaw.dans.common.lang.pdf.ExtendedHtmlWorker.java
License:Apache License
public void endElement(String tag) { if (!tagsSupported.containsKey(tag)) return;/*from ww w .j av a 2s.c o m*/ try { String follow = (String) FactoryProperties.followTags.get(tag); if (follow != null) { cprops.removeChain(follow); return; } if (tag.equals("font") || tag.equals("span")) { cprops.removeChain(tag); return; } if (tag.equals("a")) { if (currentParagraph == null) currentParagraph = new Paragraph(); ALink i = null; boolean skip = false; if (interfaceProps != null) { i = (ALink) interfaceProps.get("alink_interface"); if (i != null) skip = i.process(currentParagraph, cprops); } if (!skip) { String href = cprops.getProperty("href"); if (href != null) { ArrayList chunks = currentParagraph.getChunks(); for (int k = 0; k < chunks.size(); ++k) { Chunk ck = (Chunk) chunks.get(k); ck.setAnchor(href); } } } Paragraph tmp = (Paragraph) stack.pop(); Phrase tmp2 = new Phrase(); tmp2.add(currentParagraph); tmp.add(tmp2); currentParagraph = tmp; cprops.removeChain("a"); return; } if (tag.equals("br")) { return; } if (currentParagraph != null) { if (stack.empty()) document.add(currentParagraph); else { Object obj = stack.pop(); if (obj instanceof TextElementArray) { TextElementArray current = (TextElementArray) obj; current.add(currentParagraph); } stack.push(obj); } } currentParagraph = null; if (tag.equals("ul") || tag.equals("ol")) { if (pendingLI) endElement("li"); skipText = false; cprops.removeChain(tag); if (stack.empty()) return; Object obj = stack.pop(); if (!(obj instanceof com.lowagie.text.List)) { stack.push(obj); return; } if (stack.empty()) document.add((Element) obj); else ((TextElementArray) stack.peek()).add(obj); return; } if (tag.equals("li")) { pendingLI = false; skipText = true; cprops.removeChain(tag); if (stack.empty()) return; Object obj = stack.pop(); if (!(obj instanceof ListItem)) { stack.push(obj); return; } if (stack.empty()) { document.add((Element) obj); return; } Object list = stack.pop(); if (!(list instanceof com.lowagie.text.List)) { stack.push(list); return; } ListItem item = (ListItem) obj; ((com.lowagie.text.List) list).add(item); ArrayList cks = item.getChunks(); if (!cks.isEmpty()) item.getListSymbol().setFont(((Chunk) cks.get(0)).getFont()); stack.push(list); return; } if (tag.equals("div") || tag.equals("body")) { cprops.removeChain(tag); return; } if (tag.equals("pre")) { cprops.removeChain(tag); isPRE = false; return; } if (tag.equals("p")) { cprops.removeChain(tag); return; } if (tag.equals("h1") || tag.equals("h2") || tag.equals("h3") || tag.equals("h4") || tag.equals("h5") || tag.equals("h6")) { cprops.removeChain(tag); return; } if (tag.equals("table")) { if (pendingTR) endElement("tr"); cprops.removeChain("table"); IncTable table = (IncTable) stack.pop(); PdfPTable tb = table.buildTable(); tb.setSplitRows(true); if (stack.empty()) document.add(tb); else ((TextElementArray) stack.peek()).add(tb); boolean state[] = (boolean[]) tableState.pop(); pendingTR = state[0]; pendingTD = state[1]; skipText = false; return; } if (tag.equals("tr")) { if (pendingTD) endElement("td"); pendingTR = false; cprops.removeChain("tr"); ArrayList cells = new ArrayList(); IncTable table = null; while (true) { Object obj = stack.pop(); if (obj instanceof IncCell) { cells.add(((IncCell) obj).getCell()); } if (obj instanceof IncTable) { table = (IncTable) obj; break; } } table.addCols(cells); table.endRow(); stack.push(table); skipText = true; return; } if (tag.equals("td") || tag.equals("th")) { pendingTD = false; cprops.removeChain("td"); skipText = true; return; } } catch (Exception e) { throw new ExceptionConverter(e); } }
From source file:org.sakaiproject.tool.assessment.pdf.itext.HTMLWorker.java
License:Mozilla Public License
public void endElement(String tag) { if (!tagsSupported.containsKey(tag)) return;/*from w ww . j ava 2 s .c o m*/ try { String follow = (String) FactoryProperties.followTags.get(tag); if (follow != null) { cprops.removeChain(follow); return; } if (tag.equals("font") || tag.equals("span")) { cprops.removeChain(tag); return; } if (tag.equals("a")) { if (currentParagraph == null) currentParagraph = new Paragraph(); ALink i = null; boolean skip = false; if (interfaceProps != null) { i = (ALink) interfaceProps.get("alink_interface"); if (i != null) skip = i.process(currentParagraph, cprops); } if (!skip) { String href = cprops.getProperty("href"); if (href != null) { ArrayList chunks = currentParagraph.getChunks(); for (int k = 0; k < chunks.size(); ++k) { Chunk ck = (Chunk) chunks.get(k); ck.setAnchor(href); } } } Paragraph tmp = (Paragraph) stack.pop(); Phrase tmp2 = new Phrase(); tmp2.add(currentParagraph); tmp.add(tmp2); currentParagraph = tmp; cprops.removeChain("a"); return; } if (tag.equals("blockquote")) { cprops.removeChain(tag); currentParagraph = new Paragraph(); currentParagraph.add(factoryProperties.createChunk("\n", cprops)); inBLOCK = false; return; } if (tag.equals("br")) { return; } if (tag.equals("hr")) { return; } if (currentParagraph != null) { if (stack.empty()) document.add(currentParagraph); else { Object obj = stack.pop(); if (obj instanceof TextElementArray) { TextElementArray current = (TextElementArray) obj; current.add(currentParagraph); } stack.push(obj); } } currentParagraph = null; if (tag.equals("ul") || tag.equals("ol")) { if (pendingLI) endElement("li"); skipText = false; cprops.removeChain(tag); if (stack.empty()) return; Object obj = stack.pop(); if (!(obj instanceof com.lowagie.text.List)) { stack.push(obj); return; } if (stack.empty()) document.add((Element) obj); else ((TextElementArray) stack.peek()).add(obj); return; } if (tag.equals("li")) { pendingLI = false; skipText = true; cprops.removeChain(tag); if (stack.empty()) return; Object obj = stack.pop(); if (!(obj instanceof ListItem)) { stack.push(obj); return; } if (stack.empty()) { document.add((Element) obj); return; } Object list = stack.pop(); if (!(list instanceof com.lowagie.text.List)) { stack.push(list); return; } ListItem item = (ListItem) obj; ((com.lowagie.text.List) list).add(item); ArrayList cks = item.getChunks(); if (!cks.isEmpty()) item.getListSymbol().setFont(((Chunk) cks.get(0)).getFont()); stack.push(list); return; } if (tag.equals("div") || tag.equals("body")) { cprops.removeChain(tag); return; } if (tag.equals("pre")) { cprops.removeChain(tag); isPRE = false; return; } if (tag.equals("p")) { cprops.removeChain(tag); return; } if (tag.equals("h1") || tag.equals("h2") || tag.equals("h3") || tag.equals("h4") || tag.equals("h5") || tag.equals("h6")) { cprops.removeChain(tag); return; } if (tag.equals("table")) { if (pendingTR) endElement("tr"); cprops.removeChain("table"); IncTable table = (IncTable) stack.pop(); if (table.getRows() == null || table.getRows().isEmpty()) { // we have an empty table skip it return; } PdfPTable tb = table.buildTable(); tb.setSplitRows(true); if (stack.empty()) document.add(tb); else ((TextElementArray) stack.peek()).add(tb); boolean state[] = (boolean[]) tableState.pop(); pendingTR = state[0]; pendingTD = state[1]; skipText = false; return; } if (tag.equals("tr")) { if (pendingTD) endElement("td"); pendingTR = false; cprops.removeChain("tr"); ArrayList cells = new ArrayList(); IncTable table = null; while (true) { Object obj = stack.pop(); if (obj instanceof IncCell) { cells.add(((IncCell) obj).getCell()); } if (obj instanceof IncTable) { table = (IncTable) obj; break; } } if (cells.size() > 0) { table.addCols(cells); table.endRow(); } stack.push(table); skipText = true; return; } if (tag.equals("td") || tag.equals("th")) { pendingTD = false; cprops.removeChain("td"); skipText = true; return; } } catch (Exception e) { throw new ExceptionConverter(e); } }
From source file:org.sigmah.server.endpoint.export.sigmah.exporter.ProjectReportExporter.java
License:Open Source License
/** * Adds the given section to the RTF document. * @param section Section to add.//from w w w. j ava2 s . c o m * @param prefix Current index (for example: 3.1.1). * @param index Local index. * @param parent Parent element. * @throws DocumentException */ private void addSection(ProjectReportSectionDTO section, StringBuilder prefix, int index, Object parent) throws DocumentException, IOException { // Adding the title to the document final TextElementArray thisSection; if (parent instanceof Document) { // Style final Paragraph paragraph = new Paragraph(section.getName()); paragraph.getFont().setSize(16); paragraph.getFont().setStyle(Font.BOLD); // New chapter final Chapter chapter = new Chapter(paragraph, index); thisSection = chapter; } else if (parent instanceof Chapter) { // Style final Paragraph paragraph = new Paragraph(section.getName()); paragraph.getFont().setSize(14); paragraph.getFont().setStyle(Font.BOLD); // New section final Section chapterSection = ((Chapter) parent).addSection(paragraph); thisSection = chapterSection; } else if (parent instanceof TextElementArray) { // Style final Paragraph paragraph = new Paragraph(prefix.toString() + ' ' + section.getName()); paragraph.getFont().setSize(12); paragraph.getFont().setStyle(Font.BOLD); // New paragraph ((TextElementArray) parent).add(paragraph); thisSection = (TextElementArray) parent; } else thisSection = null; // Adding the content of this section int subIndex = 1; final int prefixLength = prefix.length(); final StyleSheet stylesheet = new StyleSheet(); stylesheet.loadTagStyle(HtmlTags.PARAGRAPH, "margin", "0"); stylesheet.loadTagStyle(HtmlTags.PARAGRAPH, "padding", "0"); stylesheet.loadTagStyle(HtmlTags.DIV, "margin", "0"); stylesheet.loadTagStyle(HtmlTags.DIV, "padding", "0"); for (final ProjectReportContent child : section.getChildren()) { if (child instanceof ProjectReportSectionDTO) { prefix.append(index).append('.'); addSection((ProjectReportSectionDTO) child, prefix, subIndex, thisSection); subIndex++; prefix.setLength(prefixLength); } else if (child instanceof RichTextElementDTO) { final String value = ((RichTextElementDTO) child).getText(); if (value != null && !"".equals(value)) { // HTML parsing. final List<Element> elements = HTMLWorker.parseToList(new StringReader(value), stylesheet); for (final Element element : elements) thisSection.add(element); } } } // Adding the chapter to the document if (thisSection instanceof Chapter && parent instanceof Document) ((Document) parent).add((Chapter) thisSection); }