Search in sources :

Example 6 with TextElementArray

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

the class SAXiTextHandler method handleEndingTags.

/**
 * This method deals with the starting tags.
 *
 * @param name the name of the tag
 */
public void handleEndingTags(String name) {
    if (ElementTags.IGNORE.equals(name)) {
        ignore = false;
        return;
    }
    if (ignore)
        return;
    // tags that don't have any content
    if (isNewpage(name) || ElementTags.ANNOTATION.equals(name) || ElementTags.IMAGE.equals(name) || isNewline(name)) {
        return;
    }
    try {
        // titles of sections and chapters
        if (ElementTags.TITLE.equals(name)) {
            Paragraph current = (Paragraph) stack.pop();
            if (currentChunk != null) {
                current.add(currentChunk);
                currentChunk = null;
            }
            Section previous = (Section) stack.pop();
            previous.setTitle(current);
            stack.push(previous);
            return;
        }
        // all other endtags
        if (currentChunk != null) {
            TextElementArray current;
            try {
                current = (TextElementArray) stack.pop();
            } catch (EmptyStackException ese) {
                current = new Paragraph();
            }
            current.add(currentChunk);
            stack.push(current);
            currentChunk = null;
        }
        // chunks
        if (ElementTags.CHUNK.equals(name)) {
            return;
        }
        // phrases, anchors, lists, tables
        if (ElementTags.PHRASE.equals(name) || ElementTags.ANCHOR.equals(name) || ElementTags.LIST.equals(name) || ElementTags.PARAGRAPH.equals(name)) {
            Element current = stack.pop();
            try {
                TextElementArray previous = (TextElementArray) stack.pop();
                previous.add(current);
                stack.push(previous);
            } catch (EmptyStackException ese) {
                document.add(current);
            }
            return;
        }
        // listitems
        if (ElementTags.LISTITEM.equals(name)) {
            ListItem listItem = (ListItem) stack.pop();
            List list = (List) stack.pop();
            list.add(listItem);
            stack.push(list);
        }
        // tables
        if (ElementTags.TABLE.equals(name)) {
            Table table = (Table) stack.pop();
            try {
                TextElementArray previous = (TextElementArray) stack.pop();
                previous.add(table);
                stack.push(previous);
            } catch (EmptyStackException ese) {
                document.add(table);
            }
            return;
        }
        // rows
        if (ElementTags.ROW.equals(name)) {
            java.util.List<Cell> cells = new ArrayList<>();
            int columns = 0;
            Table table;
            Cell cell;
            while (true) {
                Element element = stack.pop();
                if (element.type() == Element.CELL) {
                    cell = (Cell) element;
                    columns += cell.getColspan();
                    cells.add(cell);
                } else {
                    table = (Table) element;
                    break;
                }
            }
            if (table.getColumns() < columns) {
                table.addColumns(columns - table.getColumns());
            }
            Collections.reverse(cells);
            String width;
            float[] cellWidths = new float[columns];
            boolean[] cellNulls = new boolean[columns];
            for (int i = 0; i < columns; i++) {
                cellWidths[i] = 0;
                cellNulls[i] = true;
            }
            float total = 0.0f;
            int j = 0;
            for (Cell value : cells) {
                cell = value;
                width = cell.getWidthAsString();
                if (cell.getWidth() == 0) {
                    if (cell.getColspan() == 1 && cellWidths[j] == 0) {
                        try {
                            cellWidths[j] = 100.0f / columns;
                            total += cellWidths[j];
                        } catch (Exception e) {
                        // empty on purpose
                        }
                    } else if (cell.getColspan() == 1) {
                        cellNulls[j] = false;
                    }
                } else if (cell.getColspan() == 1 && width.endsWith("%")) {
                    try {
                        cellWidths[j] = Float.parseFloat(width.substring(0, width.length() - 1) + "f");
                        total += cellWidths[j];
                        cellNulls[j] = false;
                    } catch (Exception e) {
                    // empty on purpose
                    }
                }
                j += cell.getColspan();
                table.addCell(cell);
            }
            float[] widths = table.getProportionalWidths();
            if (widths.length == columns) {
                float left = 0.0f;
                for (int i = 0; i < columns; i++) {
                    if (cellNulls[i] && widths[i] != 0) {
                        left += widths[i];
                        cellWidths[i] = widths[i];
                    }
                }
                if (100.0 >= total) {
                    for (int i = 0; i < widths.length; i++) {
                        if (cellWidths[i] == 0 && widths[i] != 0) {
                            cellWidths[i] = (widths[i] / left) * (100.0f - total);
                        }
                    }
                }
                table.setWidths(cellWidths);
            }
            stack.push(table);
        }
        // cells
        if (ElementTags.CELL.equals(name)) {
            return;
        }
        // sections
        if (ElementTags.SECTION.equals(name)) {
            stack.pop();
            return;
        }
        // chapters
        if (ElementTags.CHAPTER.equals(name)) {
            document.add(stack.pop());
            return;
        }
        // the documentroot
        if (isDocumentRoot(name)) {
            try {
                while (true) {
                    Element element = stack.pop();
                    try {
                        TextElementArray previous = (TextElementArray) stack.pop();
                        previous.add(element);
                        stack.push(previous);
                    } catch (EmptyStackException es) {
                        document.add(element);
                    }
                }
            } catch (EmptyStackException ese) {
            // empty on purpose
            }
            if (controlOpenClose) {
                document.close();
            }
        }
    } catch (DocumentException de) {
        throw new ExceptionConverter(de);
    }
}
Also used : Table(com.lowagie.text.Table) Element(com.lowagie.text.Element) ArrayList(java.util.ArrayList) Section(com.lowagie.text.Section) BadElementException(com.lowagie.text.BadElementException) EmptyStackException(java.util.EmptyStackException) DocumentException(com.lowagie.text.DocumentException) Paragraph(com.lowagie.text.Paragraph) EmptyStackException(java.util.EmptyStackException) ExceptionConverter(com.lowagie.text.ExceptionConverter) TextElementArray(com.lowagie.text.TextElementArray) DocumentException(com.lowagie.text.DocumentException) ArrayList(java.util.ArrayList) List(com.lowagie.text.List) ListItem(com.lowagie.text.ListItem) Cell(com.lowagie.text.Cell)

Example 7 with TextElementArray

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

the class SAXiTextHandler method handleEndingTags.

/**
 * This method deals with the starting tags.
 *
 * @param name
 *            the name of the tag
 */
public void handleEndingTags(String name) {
    if (ElementTags.IGNORE.equals(name)) {
        ignore = false;
        return;
    }
    if (ignore)
        return;
    // tags that don't have any content
    if (isNewpage(name) || ElementTags.ANNOTATION.equals(name) || ElementTags.IMAGE.equals(name) || isNewline(name)) {
        return;
    }
    try {
        // titles of sections and chapters
        if (ElementTags.TITLE.equals(name)) {
            Paragraph current = (Paragraph) stack.pop();
            if (currentChunk != null) {
                current.add(currentChunk);
                currentChunk = null;
            }
            Section previous = (Section) stack.pop();
            previous.setTitle(current);
            stack.push(previous);
            return;
        }
        // all other endtags
        if (currentChunk != null) {
            TextElementArray current;
            try {
                current = (TextElementArray) stack.pop();
            } catch (EmptyStackException ese) {
                current = new Paragraph();
            }
            current.add(currentChunk);
            stack.push(current);
            currentChunk = null;
        }
        // chunks
        if (ElementTags.CHUNK.equals(name)) {
            return;
        }
        // phrases, anchors, lists, tables
        if (ElementTags.PHRASE.equals(name) || ElementTags.ANCHOR.equals(name) || ElementTags.LIST.equals(name) || ElementTags.PARAGRAPH.equals(name)) {
            Element current = (Element) stack.pop();
            try {
                TextElementArray previous = (TextElementArray) stack.pop();
                previous.add(current);
                stack.push(previous);
            } catch (EmptyStackException ese) {
                document.add(current);
            }
            return;
        }
        // listitems
        if (ElementTags.LISTITEM.equals(name)) {
            ListItem listItem = (ListItem) stack.pop();
            List list = (List) stack.pop();
            list.add(listItem);
            stack.push(list);
        }
        // tables
        if (ElementTags.TABLE.equals(name)) {
            Table table = (Table) stack.pop();
            try {
                TextElementArray previous = (TextElementArray) stack.pop();
                previous.add(table);
                stack.push(previous);
            } catch (EmptyStackException ese) {
                document.add(table);
            }
            return;
        }
        // rows
        if (ElementTags.ROW.equals(name)) {
            ArrayList cells = new ArrayList();
            int columns = 0;
            Table table;
            Cell cell;
            while (true) {
                Element element = (Element) stack.pop();
                if (element.type() == Element.CELL) {
                    cell = (Cell) element;
                    columns += cell.getColspan();
                    cells.add(cell);
                } else {
                    table = (Table) element;
                    break;
                }
            }
            if (table.getColumns() < columns) {
                table.addColumns(columns - table.getColumns());
            }
            Collections.reverse(cells);
            String width;
            float[] cellWidths = new float[columns];
            boolean[] cellNulls = new boolean[columns];
            for (int i = 0; i < columns; i++) {
                cellWidths[i] = 0;
                cellNulls[i] = true;
            }
            float total = 0;
            int j = 0;
            for (Iterator i = cells.iterator(); i.hasNext(); ) {
                cell = (Cell) i.next();
                width = cell.getWidthAsString();
                if (cell.getWidth() == 0) {
                    if (cell.getColspan() == 1 && cellWidths[j] == 0) {
                        try {
                            cellWidths[j] = 100f / columns;
                            total += cellWidths[j];
                        } catch (Exception e) {
                        // empty on purpose
                        }
                    } else if (cell.getColspan() == 1) {
                        cellNulls[j] = false;
                    }
                } else if (cell.getColspan() == 1 && width.endsWith("%")) {
                    try {
                        cellWidths[j] = Float.parseFloat(width.substring(0, width.length() - 1) + "f");
                        total += cellWidths[j];
                    } catch (Exception e) {
                    // empty on purpose
                    }
                }
                j += cell.getColspan();
                table.addCell(cell);
            }
            float[] widths = table.getProportionalWidths();
            if (widths.length == columns) {
                float left = 0.0f;
                for (int i = 0; i < columns; i++) {
                    if (cellNulls[i] && widths[i] != 0) {
                        left += widths[i];
                        cellWidths[i] = widths[i];
                    }
                }
                if (100.0 >= total) {
                    for (int i = 0; i < widths.length; i++) {
                        if (cellWidths[i] == 0 && widths[i] != 0) {
                            cellWidths[i] = (widths[i] / left) * (100.0f - total);
                        }
                    }
                }
                table.setWidths(cellWidths);
            }
            stack.push(table);
        }
        // cells
        if (ElementTags.CELL.equals(name)) {
            return;
        }
        // sections
        if (ElementTags.SECTION.equals(name)) {
            stack.pop();
            return;
        }
        // chapters
        if (ElementTags.CHAPTER.equals(name)) {
            document.add((Element) stack.pop());
            return;
        }
        // the documentroot
        if (isDocumentRoot(name)) {
            try {
                while (true) {
                    Element element = (Element) stack.pop();
                    try {
                        TextElementArray previous = (TextElementArray) stack.pop();
                        previous.add(element);
                        stack.push(previous);
                    } catch (EmptyStackException es) {
                        document.add(element);
                    }
                }
            } catch (EmptyStackException ese) {
            // empty on purpose
            }
            if (controlOpenClose)
                document.close();
            return;
        }
    } catch (DocumentException de) {
        throw new ExceptionConverter(de);
    }
}
Also used : Table(com.lowagie.text.Table) Element(com.lowagie.text.Element) ArrayList(java.util.ArrayList) Section(com.lowagie.text.Section) BadElementException(com.lowagie.text.BadElementException) EmptyStackException(java.util.EmptyStackException) DocumentException(com.lowagie.text.DocumentException) Paragraph(com.lowagie.text.Paragraph) EmptyStackException(java.util.EmptyStackException) ExceptionConverter(com.lowagie.text.ExceptionConverter) TextElementArray(com.lowagie.text.TextElementArray) DocumentException(com.lowagie.text.DocumentException) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(com.lowagie.text.List) ListItem(com.lowagie.text.ListItem) Cell(com.lowagie.text.Cell)

Example 8 with TextElementArray

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

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) {
    // System.err.println("Start: " + name);
    if (ignore || ElementTags.IGNORE.equals(name)) {
        ignore = true;
        return;
    }
    // maybe there is some meaningful data that wasn't between tags
    if (currentChunk != null) {
        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 = (Element) 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 (Iterator i = attributes.keySet().iterator(); i.hasNext(); ) {
            key = (String) i.next();
            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 {
                    String pageSizeName = value;
                    Field pageSizeField = PageSize.class.getField(pageSizeName);
                    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) Iterator(java.util.Iterator)

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