Search in sources :

Example 16 with Element

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

the class PdfTextExtractorTest method createSimpleDocumentWithElements.

static byte[] createSimpleDocumentWithElements(Element... elements) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Document document = new Document(PageSize.A4);
    PdfWriter.getInstance(document, baos);
    document.open();
    for (Element element : elements) {
        document.add(element);
    }
    document.close();
    return baos.toByteArray();
}
Also used : Element(com.lowagie.text.Element) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(com.lowagie.text.Document)

Example 17 with Element

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

the class ColumnTextMultiTable 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)

Example 18 with Element

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

the class ColumnTextMultiTable method addFullBockToPage.

static float addFullBockToPage(float actualY, ColumnText ct, Element... elements) {
    int result;
    for (Element element : elements) {
        ct.addElement(element);
    }
    ct.setYLine(actualY);
    result = ct.go(true);
    if (!ColumnText.hasMoreText(result)) {
        ct.setYLine(actualY);
        ct.setText(null);
        for (Element element : elements) {
            ct.addElement(element);
        }
        ct.go(false);
        actualY = ct.getYLine();
    } else {
        ct.setText(null);
        for (Element element : elements) {
            ct.addElement(element);
        }
        result = 0;
        while (ColumnText.hasMoreText(result)) {
            document.newPage();
            ct.setYLine(A4_HEIGHT_BODY + A4_MARGIN_BOTTOM);
            result = ct.go(false);
        }
        actualY = ct.getYLine();
    }
    return actualY;
}
Also used : Element(com.lowagie.text.Element)

Example 19 with Element

use of com.lowagie.text.Element in project gdmatrix by gdmatrix.

the class P7MTransformer method addSignatureInfo.

private void addSignatureInfo(com.lowagie.text.Document pdfDoc, String signatureInfo) throws Exception {
    pdfDoc.newPage();
    StyleSheet sh = new StyleSheet();
    List<Element> objects = HTMLWorker.parseToList(new StringReader(signatureInfo), sh);
    // top margin
    Paragraph par = new Paragraph();
    par.add(new Paragraph());
    par.add(new Paragraph());
    for (Object object : objects) {
        Element e = (Element) object;
        par.add(e);
    }
    pdfDoc.add(par);
}
Also used : StyleSheet(com.lowagie.text.html.simpleparser.StyleSheet) Element(com.lowagie.text.Element) StringReader(java.io.StringReader) PdfObject(com.lowagie.text.pdf.PdfObject) Paragraph(com.lowagie.text.Paragraph)

Example 20 with Element

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

the class ColumnText method goComposite.

protected int goComposite(boolean simulate) throws DocumentException {
    if (!rectangularMode)
        throw new DocumentException("Irregular columns are not supported in composite mode.");
    linesWritten = 0;
    descender = 0;
    boolean firstPass = adjustFirstLine;
    main_loop: while (true) {
        if (compositeElements.isEmpty())
            return NO_MORE_TEXT;
        Element element = (Element) compositeElements.getFirst();
        if (element.type() == Element.PARAGRAPH) {
            Paragraph para = (Paragraph) element;
            int status = 0;
            for (int keep = 0; keep < 2; ++keep) {
                float lastY = yLine;
                boolean createHere = false;
                if (compositeColumn == null) {
                    compositeColumn = new ColumnText(canvas);
                    compositeColumn.setUseAscender(firstPass ? useAscender : false);
                    compositeColumn.setAlignment(para.getAlignment());
                    compositeColumn.setIndent(para.getIndentationLeft() + para.getFirstLineIndent());
                    compositeColumn.setExtraParagraphSpace(para.getExtraParagraphSpace());
                    compositeColumn.setFollowingIndent(para.getIndentationLeft());
                    compositeColumn.setRightIndent(para.getIndentationRight());
                    compositeColumn.setLeading(para.getLeading(), para.getMultipliedLeading());
                    compositeColumn.setRunDirection(runDirection);
                    compositeColumn.setArabicOptions(arabicOptions);
                    compositeColumn.setSpaceCharRatio(spaceCharRatio);
                    compositeColumn.addText(para);
                    if (!firstPass) {
                        yLine -= para.getSpacingBefore();
                    }
                    createHere = true;
                }
                compositeColumn.leftX = leftX;
                compositeColumn.rightX = rightX;
                compositeColumn.yLine = yLine;
                compositeColumn.rectangularWidth = rectangularWidth;
                compositeColumn.rectangularMode = rectangularMode;
                compositeColumn.minY = minY;
                compositeColumn.maxY = maxY;
                boolean keepCandidate = (para.getKeepTogether() && createHere && !firstPass);
                status = compositeColumn.go(simulate || (keepCandidate && keep == 0));
                updateFilledWidth(compositeColumn.filledWidth);
                if ((status & NO_MORE_TEXT) == 0 && keepCandidate) {
                    compositeColumn = null;
                    yLine = lastY;
                    return NO_MORE_COLUMN;
                }
                if (simulate || !keepCandidate)
                    break;
                if (keep == 0) {
                    compositeColumn = null;
                    yLine = lastY;
                }
            }
            firstPass = false;
            yLine = compositeColumn.yLine;
            linesWritten += compositeColumn.linesWritten;
            descender = compositeColumn.descender;
            if ((status & NO_MORE_TEXT) != 0) {
                compositeColumn = null;
                compositeElements.removeFirst();
                yLine -= para.getSpacingAfter();
            }
            if ((status & NO_MORE_COLUMN) != 0) {
                return NO_MORE_COLUMN;
            }
        } else if (element.type() == Element.LIST) {
            com.lowagie.text.List list = (com.lowagie.text.List) element;
            ArrayList items = list.getItems();
            ListItem item = null;
            float listIndentation = list.getIndentationLeft();
            int count = 0;
            Stack stack = new Stack();
            for (int k = 0; k < items.size(); ++k) {
                Object obj = items.get(k);
                if (obj instanceof ListItem) {
                    if (count == listIdx) {
                        item = (ListItem) obj;
                        break;
                    } else
                        ++count;
                } else if (obj instanceof com.lowagie.text.List) {
                    stack.push(new Object[] { list, Integer.valueOf(k), new Float(listIndentation) });
                    list = (com.lowagie.text.List) obj;
                    items = list.getItems();
                    listIndentation += list.getIndentationLeft();
                    k = -1;
                    continue;
                }
                if (k == items.size() - 1) {
                    if (!stack.isEmpty()) {
                        Object[] objs = (Object[]) stack.pop();
                        list = (com.lowagie.text.List) objs[0];
                        items = list.getItems();
                        k = ((Integer) objs[1]).intValue();
                        listIndentation = ((Float) objs[2]).floatValue();
                    }
                }
            }
            int status = 0;
            for (int keep = 0; keep < 2; ++keep) {
                float lastY = yLine;
                boolean createHere = false;
                if (compositeColumn == null) {
                    if (item == null) {
                        listIdx = 0;
                        compositeElements.removeFirst();
                        continue main_loop;
                    }
                    compositeColumn = new ColumnText(canvas);
                    compositeColumn.setUseAscender(firstPass ? useAscender : false);
                    compositeColumn.setAlignment(item.getAlignment());
                    compositeColumn.setIndent(item.getIndentationLeft() + listIndentation + item.getFirstLineIndent());
                    compositeColumn.setExtraParagraphSpace(item.getExtraParagraphSpace());
                    compositeColumn.setFollowingIndent(compositeColumn.getIndent());
                    compositeColumn.setRightIndent(item.getIndentationRight() + list.getIndentationRight());
                    compositeColumn.setLeading(item.getLeading(), item.getMultipliedLeading());
                    compositeColumn.setRunDirection(runDirection);
                    compositeColumn.setArabicOptions(arabicOptions);
                    compositeColumn.setSpaceCharRatio(spaceCharRatio);
                    compositeColumn.addText(item);
                    if (!firstPass) {
                        yLine -= item.getSpacingBefore();
                    }
                    createHere = true;
                }
                compositeColumn.leftX = leftX;
                compositeColumn.rightX = rightX;
                compositeColumn.yLine = yLine;
                compositeColumn.rectangularWidth = rectangularWidth;
                compositeColumn.rectangularMode = rectangularMode;
                compositeColumn.minY = minY;
                compositeColumn.maxY = maxY;
                boolean keepCandidate = (item.getKeepTogether() && createHere && !firstPass);
                status = compositeColumn.go(simulate || (keepCandidate && keep == 0));
                updateFilledWidth(compositeColumn.filledWidth);
                if ((status & NO_MORE_TEXT) == 0 && keepCandidate) {
                    compositeColumn = null;
                    yLine = lastY;
                    return NO_MORE_COLUMN;
                }
                if (simulate || !keepCandidate)
                    break;
                if (keep == 0) {
                    compositeColumn = null;
                    yLine = lastY;
                }
            }
            firstPass = false;
            yLine = compositeColumn.yLine;
            linesWritten += compositeColumn.linesWritten;
            descender = compositeColumn.descender;
            if (!Float.isNaN(compositeColumn.firstLineY) && !compositeColumn.firstLineYDone) {
                if (!simulate)
                    showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(item.getListSymbol()), compositeColumn.leftX + listIndentation, compositeColumn.firstLineY, 0);
                compositeColumn.firstLineYDone = true;
            }
            if ((status & NO_MORE_TEXT) != 0) {
                compositeColumn = null;
                ++listIdx;
                yLine -= item.getSpacingAfter();
            }
            if ((status & NO_MORE_COLUMN) != 0)
                return NO_MORE_COLUMN;
        } else if (element.type() == Element.PTABLE) {
            // don't write anything in the current column if there's no more space available
            if (yLine < minY || yLine > maxY)
                return NO_MORE_COLUMN;
            // get the PdfPTable element
            PdfPTable table = (PdfPTable) element;
            // we ignore tables without a body
            if (table.size() <= table.getHeaderRows()) {
                compositeElements.removeFirst();
                continue;
            }
            // offsets
            float yTemp = yLine;
            if (!firstPass && listIdx == 0)
                yTemp -= table.spacingBefore();
            float yLineWrite = yTemp;
            // don't write anything in the current column if there's no more space available
            if (yTemp < minY || yTemp > maxY)
                return NO_MORE_COLUMN;
            // coordinates
            currentLeading = 0;
            float x1 = leftX;
            float tableWidth;
            if (table.isLockedWidth()) {
                tableWidth = table.getTotalWidth();
                updateFilledWidth(tableWidth);
            } else {
                tableWidth = rectangularWidth * table.getWidthPercentage() / 100f;
                table.setTotalWidth(tableWidth);
            }
            // how many header rows are real header rows; how many are footer rows?
            int headerRows = table.getHeaderRows();
            int footerRows = table.getFooterRows();
            if (footerRows > headerRows)
                footerRows = headerRows;
            int realHeaderRows = headerRows - footerRows;
            float headerHeight = table.getHeaderHeight();
            float footerHeight = table.getFooterHeight();
            // make sure the header and footer fit on the page
            boolean skipHeader = (!firstPass && table.isSkipFirstHeader() && listIdx <= headerRows);
            if (!skipHeader) {
                yTemp -= headerHeight;
                if (yTemp < minY || yTemp > maxY) {
                    if (firstPass) {
                        compositeElements.removeFirst();
                        continue;
                    }
                    return NO_MORE_COLUMN;
                }
            }
            // how many real rows (not header or footer rows) fit on a page?
            int k;
            if (listIdx < headerRows)
                listIdx = headerRows;
            if (!table.isComplete())
                yTemp -= footerHeight;
            for (k = listIdx; k < table.size(); ++k) {
                float rowHeight = table.getRowHeight(k);
                if (yTemp - rowHeight < minY)
                    break;
                yTemp -= rowHeight;
            }
            if (!table.isComplete())
                yTemp += footerHeight;
            // either k is the first row that doesn't fit on the page (break);
            if (k < table.size()) {
                if (table.isSplitRows() && (!table.isSplitLate() || (k == listIdx && firstPass))) {
                    if (!splittedRow) {
                        splittedRow = true;
                        table = new PdfPTable(table);
                        compositeElements.set(0, table);
                        ArrayList rows = table.getRows();
                        for (int i = headerRows; i < listIdx; ++i) rows.set(i, null);
                    }
                    float h = yTemp - minY;
                    PdfPRow newRow = table.getRow(k).splitRow(table, k, h);
                    if (newRow == null) {
                        if (k == listIdx)
                            return NO_MORE_COLUMN;
                    } else {
                        yTemp = minY;
                        table.getRows().add(++k, newRow);
                    }
                } else if (!table.isSplitRows() && k == listIdx && firstPass) {
                    compositeElements.removeFirst();
                    splittedRow = false;
                    continue;
                } else if (k == listIdx && !firstPass && (!table.isSplitRows() || table.isSplitLate()) && (table.getFooterRows() == 0 || table.isComplete()))
                    return NO_MORE_COLUMN;
            }
            // or k is the number of rows in the table (for loop was done).
            firstPass = false;
            // we draw the table (for real now)
            if (!simulate) {
                // set the alignment
                switch(table.getHorizontalAlignment()) {
                    case Element.ALIGN_LEFT:
                        break;
                    case Element.ALIGN_RIGHT:
                        x1 += rectangularWidth - tableWidth;
                        break;
                    default:
                        x1 += (rectangularWidth - tableWidth) / 2f;
                }
                // copy the rows that fit on the page in a new table nt
                PdfPTable nt = PdfPTable.shallowCopy(table);
                ArrayList sub = nt.getRows();
                // first we add the real header rows (if necessary)
                if (!skipHeader && realHeaderRows > 0) {
                    sub.addAll(table.getRows(0, realHeaderRows));
                } else
                    nt.setHeaderRows(footerRows);
                // then we add the real content
                sub.addAll(table.getRows(listIdx, k));
                // if k < table.size(), we must indicate that the new table is complete;
                // otherwise no footers will be added (because iText thinks the table continues on the same page)
                boolean showFooter = !table.isSkipLastFooter();
                boolean newPageFollows = false;
                if (k < table.size()) {
                    nt.setComplete(true);
                    showFooter = true;
                    newPageFollows = true;
                }
                // we add the footer rows if necessary (not for incomplete tables)
                for (int j = 0; j < footerRows && nt.isComplete() && showFooter; ++j) sub.add(table.getRow(j + realHeaderRows));
                // we need a correction if the last row needs to be extended
                float rowHeight = 0;
                PdfPRow last = (PdfPRow) sub.get(sub.size() - 1 - footerRows);
                if (table.isExtendLastRow(newPageFollows)) {
                    rowHeight = last.getMaxHeights();
                    last.setMaxHeights(yTemp - minY + rowHeight);
                    yTemp = minY;
                }
                // now we render the rows of the new table
                if (canvases != null)
                    nt.writeSelectedRows(0, -1, x1, yLineWrite, canvases);
                else
                    nt.writeSelectedRows(0, -1, x1, yLineWrite, canvas);
                if (table.isExtendLastRow(newPageFollows)) {
                    last.setMaxHeights(rowHeight);
                }
            } else if (table.isExtendLastRow() && minY > PdfPRow.BOTTOM_LIMIT)
                yTemp = minY;
            yLine = yTemp;
            if (!(skipHeader || table.isComplete()))
                yLine += footerHeight;
            if (k >= table.size()) {
                yLine -= table.spacingAfter();
                compositeElements.removeFirst();
                splittedRow = false;
                listIdx = 0;
            } else {
                if (splittedRow) {
                    ArrayList rows = table.getRows();
                    for (int i = listIdx; i < k; ++i) rows.set(i, null);
                }
                listIdx = k;
                return NO_MORE_COLUMN;
            }
        } else if (element.type() == Element.YMARK) {
            if (!simulate) {
                DrawInterface zh = (DrawInterface) element;
                zh.draw(canvas, leftX, minY, rightX, maxY, yLine);
            }
            compositeElements.removeFirst();
        } else
            compositeElements.removeFirst();
    }
}
Also used : Element(com.lowagie.text.Element) ArrayList(java.util.ArrayList) DrawInterface(com.lowagie.text.pdf.draw.DrawInterface) Phrase(com.lowagie.text.Phrase) Paragraph(com.lowagie.text.Paragraph) Stack(java.util.Stack) DocumentException(com.lowagie.text.DocumentException) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ListItem(com.lowagie.text.ListItem)

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