Search in sources :

Example 1 with Element

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

the class PdfCell method addList.

private void addList(List list, float left, float right, int alignment) {
    PdfChunk chunk;
    PdfChunk overflow;
    ArrayList allActions = new ArrayList();
    processActions(list, null, allActions);
    int aCounter = 0;
    for (Iterator it = list.getItems().iterator(); it.hasNext(); ) {
        Element ele = (Element) it.next();
        switch(ele.type()) {
            case Element.LISTITEM:
                ListItem item = (ListItem) ele;
                line = new PdfLine(left + item.getIndentationLeft(), right, alignment, item.getLeading());
                line.setListItem(item);
                for (Iterator j = item.getChunks().iterator(); j.hasNext(); ) {
                    chunk = new PdfChunk((Chunk) j.next(), (PdfAction) (allActions.get(aCounter++)));
                    while ((overflow = line.add(chunk)) != null) {
                        addLine(line);
                        line = new PdfLine(left + item.getIndentationLeft(), right, alignment, item.getLeading());
                        chunk = overflow;
                    }
                    line.resetAlignment();
                    addLine(line);
                    line = new PdfLine(left + item.getIndentationLeft(), right, alignment, leading);
                }
                break;
            case Element.LIST:
                List sublist = (List) ele;
                addList(sublist, left + sublist.getIndentationLeft(), right, alignment);
                break;
        }
    }
}
Also used : Element(com.lowagie.text.Element) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) List(com.lowagie.text.List) ArrayList(java.util.ArrayList) ListItem(com.lowagie.text.ListItem) Chunk(com.lowagie.text.Chunk)

Example 2 with Element

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

the class UnicodePdfTest method testSimplePdf.

@Test
public void testSimplePdf() throws FileNotFoundException, DocumentException {
    BaseFont font = null;
    try {
        font = BaseFont.createFont("LiberationSerif-Regular.ttf", BaseFont.IDENTITY_H, false);
    } catch (IOException ioe) {
    // nop
    }
    Document document = null;
    try {
        document = PdfTestBase.createPdf("unicode.pdf");
        // new page with a rectangle
        document.open();
        Element unicodeParagraph = new Paragraph(INPUT, new Font(font, 12));
        document.add(unicodeParagraph);
    } finally {
        // close document
        if (document != null)
            document.close();
    }
}
Also used : Element(com.lowagie.text.Element) IOException(java.io.IOException) Document(com.lowagie.text.Document) Font(com.lowagie.text.Font) Paragraph(com.lowagie.text.Paragraph) Test(org.junit.Test)

Example 3 with Element

use of com.lowagie.text.Element 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 4 with Element

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

the class RtfCell method importCell.

/**
 * Imports the Cell properties into the RtfCell
 *
 * @param cell The Cell to import
 */
private void importCell(Cell cell) {
    this.content = new ArrayList();
    if (cell == null) {
        this.borders = new RtfBorderGroup(this.document, RtfBorder.CELL_BORDER, this.parentRow.getParentTable().getBorders());
        return;
    }
    this.colspan = cell.getColspan();
    this.rowspan = cell.getRowspan();
    if (cell.getRowspan() > 1) {
        this.mergeType = MERGE_VERT_PARENT;
    }
    if (cell instanceof RtfCell) {
        this.borders = new RtfBorderGroup(this.document, RtfBorder.CELL_BORDER, ((RtfCell) cell).getBorders());
    } else {
        this.borders = new RtfBorderGroup(this.document, RtfBorder.CELL_BORDER, cell.getBorder(), cell.getBorderWidth(), cell.getBorderColor());
    }
    this.verticalAlignment = cell.getVerticalAlignment();
    if (cell.getBackgroundColor() == null) {
        this.backgroundColor = new RtfColor(this.document, 255, 255, 255);
    } else {
        this.backgroundColor = new RtfColor(this.document, cell.getBackgroundColor());
    }
    this.cellPadding = (int) this.parentRow.getParentTable().getCellPadding();
    Iterator cellIterator = cell.getElements();
    Paragraph container = null;
    while (cellIterator.hasNext()) {
        try {
            Element element = (Element) cellIterator.next();
            // should we wrap it in a paragraph
            if (!(element instanceof Paragraph) && !(element instanceof List)) {
                if (container != null) {
                    container.add(element);
                } else {
                    container = new Paragraph();
                    container.setAlignment(cell.getHorizontalAlignment());
                    container.add(element);
                }
            } else {
                if (container != null) {
                    RtfBasicElement[] rtfElements = this.document.getMapper().mapElement(container);
                    for (int i = 0; i < rtfElements.length; i++) {
                        rtfElements[i].setInTable(true);
                        this.content.add(rtfElements[i]);
                    }
                    container = null;
                }
                // with that of enclosing cell
                if (element instanceof Paragraph && ((Paragraph) element).getAlignment() == Element.ALIGN_UNDEFINED) {
                    ((Paragraph) element).setAlignment(cell.getHorizontalAlignment());
                }
                RtfBasicElement[] rtfElements = this.document.getMapper().mapElement(element);
                for (int i = 0; i < rtfElements.length; i++) {
                    rtfElements[i].setInTable(true);
                    this.content.add(rtfElements[i]);
                }
            }
        } catch (DocumentException de) {
            de.printStackTrace();
        }
    }
    if (container != null) {
        try {
            RtfBasicElement[] rtfElements = this.document.getMapper().mapElement(container);
            for (int i = 0; i < rtfElements.length; i++) {
                rtfElements[i].setInTable(true);
                this.content.add(rtfElements[i]);
            }
        } catch (DocumentException de) {
            de.printStackTrace();
        }
    }
}
Also used : Element(com.lowagie.text.Element) RtfBasicElement(com.lowagie.text.rtf.RtfBasicElement) RtfExtendedElement(com.lowagie.text.rtf.RtfExtendedElement) DocumentException(com.lowagie.text.DocumentException) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) RtfColor(com.lowagie.text.rtf.style.RtfColor) ArrayList(java.util.ArrayList) List(com.lowagie.text.List) RtfBasicElement(com.lowagie.text.rtf.RtfBasicElement) Paragraph(com.lowagie.text.Paragraph) RtfParagraph(com.lowagie.text.rtf.text.RtfParagraph)

Example 5 with Element

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

the class ColumnTextTable method getHeightOfBlock.

/**
 * Get the height of the input block.
 * @param elements The input elements which will be used to calculate the height.
 * @return height, which is the height of the input block.
 */
float getHeightOfBlock(Element... elements) {
    ColumnText ct = new ColumnText(pdfWriter.getDirectContent());
    float startY = A4_HEIGHT_BODY + A4_MARGIN_BOTTOM;
    float height;
    ct.setSimpleColumn(A4_MARGIN_LEFT, A4_MARGIN_BOTTOM, A4_WIDTH_BODY + A4_MARGIN_LEFT, A4_HEIGHT_BODY + A4_MARGIN_BOTTOM);
    ct.setYLine(startY);
    for (Element element : elements) {
        ct.addElement(element);
    }
    int result = ct.go(true);
    if (ColumnText.hasMoreText(result)) {
        return -1;
    } else {
        height = startY - ct.getYLine();
        return height;
    }
}
Also used : ColumnText(com.lowagie.text.pdf.ColumnText) Element(com.lowagie.text.Element)

Aggregations

Element (com.lowagie.text.Element)24 DocumentException (com.lowagie.text.DocumentException)13 Paragraph (com.lowagie.text.Paragraph)13 ArrayList (java.util.ArrayList)12 Chunk (com.lowagie.text.Chunk)9 ListItem (com.lowagie.text.ListItem)9 List (com.lowagie.text.List)8 ExceptionConverter (com.lowagie.text.ExceptionConverter)7 Phrase (com.lowagie.text.Phrase)7 TextElementArray (com.lowagie.text.TextElementArray)7 Section (com.lowagie.text.Section)6 IOException (java.io.IOException)6 Iterator (java.util.Iterator)6 BadElementException (com.lowagie.text.BadElementException)5 Table (com.lowagie.text.Table)5 Annotation (com.lowagie.text.Annotation)4 Image (com.lowagie.text.Image)4 EmptyStackException (java.util.EmptyStackException)4 Cell (com.lowagie.text.Cell)3 Font (com.lowagie.text.Font)3