Search in sources :

Example 1 with TextElementArray

use of com.lowagie.text.TextElementArray in project itext2 by albfernandez.

the class HTMLWorker method endElement.

public void endElement(String tag) {
    if (!tagsSupported.containsKey(tag))
        return;
    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();
            }
            boolean skip = false;
            if (interfaceProps != null) {
                ALink 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();
                    int size = chunks.size();
                    for (int k = 0; k < 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(HtmlTags.UNORDEREDLIST) || tag.equals(HtmlTags.ORDEREDLIST)) {
            if (pendingLI)
                endElement(HtmlTags.LISTITEM);
            skipText = false;
            cprops.removeChain(tag);
            if (stack.empty())
                return;
            Object obj = stack.pop();
            if (!(obj instanceof List)) {
                stack.push(obj);
                return;
            }
            if (stack.empty())
                document.add((Element) obj);
            else
                ((TextElementArray) stack.peek()).add(obj);
            return;
        }
        if (tag.equals(HtmlTags.LISTITEM)) {
            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 List)) {
                stack.push(list);
                return;
            }
            ListItem item = (ListItem) obj;
            ((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(HtmlTags.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);
    }
}
Also used : Element(com.lowagie.text.Element) ArrayList(java.util.ArrayList) Phrase(com.lowagie.text.Phrase) Chunk(com.lowagie.text.Chunk) IOException(java.io.IOException) DocumentException(com.lowagie.text.DocumentException) Paragraph(com.lowagie.text.Paragraph) ExceptionConverter(com.lowagie.text.ExceptionConverter) PdfPTable(com.lowagie.text.pdf.PdfPTable) TextElementArray(com.lowagie.text.TextElementArray) ArrayList(java.util.ArrayList) List(com.lowagie.text.List) ListItem(com.lowagie.text.ListItem)

Example 2 with TextElementArray

use of com.lowagie.text.TextElementArray in project itext2 by albfernandez.

the class SAXiTextHandler method addImage.

protected void addImage(Image img) throws EmptyStackException {
    // if there is an element on the stack...
    Object current = stack.pop();
    // added directly
    if (current instanceof Chapter || current instanceof Section || current instanceof Cell) {
        ((TextElementArray) current).add(img);
        stack.push(current);
        return;
    } else // ...if not, we need to to a lot of stuff
    {
        Stack newStack = new Stack();
        while (!(current instanceof Chapter || current instanceof Section || current instanceof Cell)) {
            newStack.push(current);
            if (current instanceof Anchor) {
                img.setAnnotation(new Annotation(0, 0, 0, 0, ((Anchor) current).getReference()));
            }
            current = stack.pop();
        }
        ((TextElementArray) current).add(img);
        stack.push(current);
        while (!newStack.empty()) {
            stack.push(newStack.pop());
        }
        return;
    }
}
Also used : Anchor(com.lowagie.text.Anchor) TextElementArray(com.lowagie.text.TextElementArray) Chapter(com.lowagie.text.Chapter) Section(com.lowagie.text.Section) Cell(com.lowagie.text.Cell) Annotation(com.lowagie.text.Annotation) Stack(java.util.Stack)

Example 3 with TextElementArray

use of com.lowagie.text.TextElementArray in project OpenPDF by LibrePDF.

the class HTMLWorker method endElement.

public void endElement(String tag) {
    if (!tagsSupported.containsKey(tag))
        return;
    try {
        String follow = 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();
            }
            boolean skip = false;
            if (interfaceProps != null) {
                ALink i = (ALink) interfaceProps.get("alink_interface");
                if (i != null)
                    skip = i.process(currentParagraph, cprops);
            }
            if (!skip) {
                cprops.findProperty("href").ifPresent(href -> {
                    List<Element> chunks = currentParagraph.getChunks();
                    for (Element chunk : chunks) {
                        Chunk ck = (Chunk) chunk;
                        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(HtmlTags.UNORDEREDLIST) || tag.equals(HtmlTags.ORDEREDLIST)) {
            if (pendingLI)
                endElement(HtmlTags.LISTITEM);
            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 {
                Object peek = stack.peek();
                if (peek instanceof com.lowagie.text.List) {
                    ((com.lowagie.text.List) peek).add((com.lowagie.text.List) obj);
                } else {
                    ((TextElementArray) peek).add((Element) obj);
                }
            }
            return;
        }
        if (tag.equals(HtmlTags.LISTITEM)) {
            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);
            List<Element> 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(HtmlTags.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");
            List<PdfPCell> cells = new ArrayList<>();
            IncTable table;
            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);
    }
}
Also used : PdfPCell(com.lowagie.text.pdf.PdfPCell) Element(com.lowagie.text.Element) ArrayList(java.util.ArrayList) Phrase(com.lowagie.text.Phrase) Chunk(com.lowagie.text.Chunk) IOException(java.io.IOException) DocumentException(com.lowagie.text.DocumentException) Paragraph(com.lowagie.text.Paragraph) ExceptionConverter(com.lowagie.text.ExceptionConverter) PdfPTable(com.lowagie.text.pdf.PdfPTable) TextElementArray(com.lowagie.text.TextElementArray) ArrayList(java.util.ArrayList) List(java.util.List) ListItem(com.lowagie.text.ListItem)

Example 4 with TextElementArray

use of com.lowagie.text.TextElementArray in project OpenPDF by LibrePDF.

the class SAXiTextHandler method addImage.

protected void addImage(Image img) throws EmptyStackException {
    // if there is an element on the stack...
    Element current = stack.pop();
    // added directly
    if (current instanceof Section || current instanceof Cell) {
        ((TextElementArray) current).add(img);
        stack.push(current);
    } else // ... if it is a Phrase, we have to wrap the Image in a new Chunk
    if (current instanceof Phrase) {
        ((TextElementArray) current).add(new Chunk(img, 0, 0));
        stack.push(current);
    } else // ...if not, we need to to a lot of stuff
    {
        Stack<Element> newStack = new Stack<>();
        while (!(current instanceof Section || current instanceof Cell)) {
            newStack.push(current);
            if (current instanceof Anchor) {
                img.setAnnotation(new Annotation(0, 0, 0, 0, ((Anchor) current).getReference()));
            }
            current = stack.pop();
        }
        ((TextElementArray) current).add(img);
        stack.push(current);
        while (!newStack.empty()) {
            stack.push(newStack.pop());
        }
    }
}
Also used : Anchor(com.lowagie.text.Anchor) TextElementArray(com.lowagie.text.TextElementArray) Element(com.lowagie.text.Element) Phrase(com.lowagie.text.Phrase) Chunk(com.lowagie.text.Chunk) Section(com.lowagie.text.Section) Cell(com.lowagie.text.Cell) Annotation(com.lowagie.text.Annotation) Stack(java.util.Stack)

Example 5 with TextElementArray

use of com.lowagie.text.TextElementArray in project OpenPDF by LibrePDF.

the class SAXiTextHandler method handleStartingTags.

/**
 * This method deals with the starting tags.
 *
 * @param name       the name of the tag
 * @param attributes the list of attributes
 */
public void handleStartingTags(String name, Properties attributes) {
    if (ignore || ElementTags.IGNORE.equals(name)) {
        ignore = true;
        return;
    }
    // maybe there is some meaningful data that wasn't between tags
    if (currentChunk != null && isNotBlank(currentChunk.getContent())) {
        TextElementArray current;
        try {
            current = (TextElementArray) stack.pop();
        } catch (EmptyStackException ese) {
            if (bf == null) {
                current = new Paragraph("", new Font());
            } else {
                current = new Paragraph("", new Font(this.bf));
            }
        }
        current.add(currentChunk);
        stack.push(current);
        currentChunk = null;
    }
    // chunks
    if (ElementTags.CHUNK.equals(name)) {
        currentChunk = ElementFactory.getChunk(attributes);
        if (bf != null) {
            currentChunk.setFont(new Font(this.bf));
        }
        return;
    }
    // symbols
    if (ElementTags.ENTITY.equals(name)) {
        Font f = new Font();
        if (currentChunk != null) {
            handleEndingTags(ElementTags.CHUNK);
            f = currentChunk.getFont();
        }
        currentChunk = EntitiesToSymbol.get(attributes.getProperty(ElementTags.ID), f);
        return;
    }
    // phrases
    if (ElementTags.PHRASE.equals(name)) {
        stack.push(ElementFactory.getPhrase(attributes));
        return;
    }
    // anchors
    if (ElementTags.ANCHOR.equals(name)) {
        stack.push(ElementFactory.getAnchor(attributes));
        return;
    }
    // paragraphs and titles
    if (ElementTags.PARAGRAPH.equals(name) || ElementTags.TITLE.equals(name)) {
        stack.push(ElementFactory.getParagraph(attributes));
        return;
    }
    // lists
    if (ElementTags.LIST.equals(name)) {
        stack.push(ElementFactory.getList(attributes));
        return;
    }
    // listitems
    if (ElementTags.LISTITEM.equals(name)) {
        stack.push(ElementFactory.getListItem(attributes));
        return;
    }
    // cells
    if (ElementTags.CELL.equals(name)) {
        stack.push(ElementFactory.getCell(attributes));
        return;
    }
    // tables
    if (ElementTags.TABLE.equals(name)) {
        Table table = ElementFactory.getTable(attributes);
        float[] widths = table.getProportionalWidths();
        for (int i = 0; i < widths.length; i++) {
            if (widths[i] == 0) {
                widths[i] = 100.0f / widths.length;
            }
        }
        try {
            table.setWidths(widths);
        } catch (BadElementException bee) {
            // this shouldn't happen
            throw new ExceptionConverter(bee);
        }
        stack.push(table);
        return;
    }
    // sections
    if (ElementTags.SECTION.equals(name)) {
        Element previous = stack.pop();
        Section section;
        try {
            section = ElementFactory.getSection((Section) previous, attributes);
        } catch (ClassCastException cce) {
            throw new ExceptionConverter(cce);
        }
        stack.push(previous);
        stack.push(section);
        return;
    }
    // chapters
    if (ElementTags.CHAPTER.equals(name)) {
        stack.push(ElementFactory.getChapter(attributes));
        return;
    }
    // images
    if (ElementTags.IMAGE.equals(name)) {
        try {
            Image img = ElementFactory.getImage(attributes);
            try {
                addImage(img);
                return;
            } catch (EmptyStackException ese) {
                // to the document
                try {
                    document.add(img);
                } catch (DocumentException de) {
                    throw new ExceptionConverter(de);
                }
                return;
            }
        } catch (Exception e) {
            throw new ExceptionConverter(e);
        }
    }
    // annotations
    if (ElementTags.ANNOTATION.equals(name)) {
        Annotation annotation = ElementFactory.getAnnotation(attributes);
        TextElementArray current;
        try {
            try {
                current = (TextElementArray) stack.pop();
                try {
                    current.add(annotation);
                } catch (Exception e) {
                    document.add(annotation);
                }
                stack.push(current);
            } catch (EmptyStackException ese) {
                document.add(annotation);
            }
            return;
        } catch (DocumentException de) {
            throw new ExceptionConverter(de);
        }
    }
    // newlines
    if (isNewline(name)) {
        TextElementArray current;
        try {
            current = (TextElementArray) stack.pop();
            current.add(Chunk.NEWLINE);
            stack.push(current);
        } catch (EmptyStackException ese) {
            if (currentChunk == null) {
                try {
                    document.add(Chunk.NEWLINE);
                } catch (DocumentException de) {
                    throw new ExceptionConverter(de);
                }
            } else {
                currentChunk.append("\n");
            }
        }
        return;
    }
    // newpage
    if (isNewpage(name)) {
        TextElementArray current;
        try {
            current = (TextElementArray) stack.pop();
            Chunk newPage = new Chunk("");
            newPage.setNewPage();
            if (bf != null) {
                newPage.setFont(new Font(this.bf));
            }
            current.add(newPage);
            stack.push(current);
        } catch (EmptyStackException ese) {
            document.newPage();
        }
        return;
    }
    if (ElementTags.HORIZONTALRULE.equals(name)) {
        TextElementArray current;
        LineSeparator hr = new LineSeparator(1.0f, 100.0f, null, Element.ALIGN_CENTER, 0);
        try {
            current = (TextElementArray) stack.pop();
            current.add(hr);
            stack.push(current);
        } catch (EmptyStackException ese) {
            try {
                document.add(hr);
            } catch (DocumentException de) {
                throw new ExceptionConverter(de);
            }
        }
        return;
    }
    // documentroot
    if (isDocumentRoot(name)) {
        String key;
        String value;
        // pagesize and orientation specific code suggested by Samuel Gabriel
        // Updated by Ricardo Coutinho. Only use if set in html!
        Rectangle pageSize = null;
        String orientation = null;
        for (Object o : attributes.keySet()) {
            key = (String) o;
            value = attributes.getProperty(key);
            try {
                // margin specific code suggested by Reza Nasiri
                if (ElementTags.LEFT.equalsIgnoreCase(key))
                    leftMargin = Float.parseFloat(value + "f");
                if (ElementTags.RIGHT.equalsIgnoreCase(key))
                    rightMargin = Float.parseFloat(value + "f");
                if (ElementTags.TOP.equalsIgnoreCase(key))
                    topMargin = Float.parseFloat(value + "f");
                if (ElementTags.BOTTOM.equalsIgnoreCase(key))
                    bottomMargin = Float.parseFloat(value + "f");
            } catch (Exception ex) {
                throw new ExceptionConverter(ex);
            }
            if (ElementTags.PAGE_SIZE.equals(key)) {
                try {
                    Field pageSizeField = PageSize.class.getField(value);
                    pageSize = (Rectangle) pageSizeField.get(null);
                } catch (Exception ex) {
                    throw new ExceptionConverter(ex);
                }
            } else if (ElementTags.ORIENTATION.equals(key)) {
                try {
                    if ("landscape".equals(value)) {
                        orientation = "landscape";
                    }
                } catch (Exception ex) {
                    throw new ExceptionConverter(ex);
                }
            } else {
                try {
                    document.add(new Meta(key, value));
                } catch (DocumentException de) {
                    throw new ExceptionConverter(de);
                }
            }
        }
        if (pageSize != null) {
            if ("landscape".equals(orientation)) {
                pageSize = pageSize.rotate();
            }
            document.setPageSize(pageSize);
        }
        document.setMargins(leftMargin, rightMargin, topMargin, bottomMargin);
        if (controlOpenClose)
            document.open();
    }
}
Also used : Meta(com.lowagie.text.Meta) Table(com.lowagie.text.Table) BadElementException(com.lowagie.text.BadElementException) Element(com.lowagie.text.Element) Rectangle(com.lowagie.text.Rectangle) Image(com.lowagie.text.Image) Chunk(com.lowagie.text.Chunk) Section(com.lowagie.text.Section) LineSeparator(com.lowagie.text.pdf.draw.LineSeparator) BaseFont(com.lowagie.text.pdf.BaseFont) Font(com.lowagie.text.Font) BadElementException(com.lowagie.text.BadElementException) EmptyStackException(java.util.EmptyStackException) DocumentException(com.lowagie.text.DocumentException) Annotation(com.lowagie.text.Annotation) Paragraph(com.lowagie.text.Paragraph) EmptyStackException(java.util.EmptyStackException) ExceptionConverter(com.lowagie.text.ExceptionConverter) Field(java.lang.reflect.Field) TextElementArray(com.lowagie.text.TextElementArray) DocumentException(com.lowagie.text.DocumentException)

Aggregations

TextElementArray (com.lowagie.text.TextElementArray)8 Element (com.lowagie.text.Element)7 DocumentException (com.lowagie.text.DocumentException)6 ExceptionConverter (com.lowagie.text.ExceptionConverter)6 Paragraph (com.lowagie.text.Paragraph)6 Section (com.lowagie.text.Section)6 Chunk (com.lowagie.text.Chunk)5 Annotation (com.lowagie.text.Annotation)4 BadElementException (com.lowagie.text.BadElementException)4 Cell (com.lowagie.text.Cell)4 ListItem (com.lowagie.text.ListItem)4 Table (com.lowagie.text.Table)4 ArrayList (java.util.ArrayList)4 EmptyStackException (java.util.EmptyStackException)4 List (com.lowagie.text.List)3 Phrase (com.lowagie.text.Phrase)3 Anchor (com.lowagie.text.Anchor)2 Font (com.lowagie.text.Font)2 Image (com.lowagie.text.Image)2 Meta (com.lowagie.text.Meta)2