Esempio n. 1
0
 /** Constructs a deep copy of a <CODE>PdfPCell</CODE>.
 * @param cell the <CODE>PdfPCell</CODE> to duplicate
 */
 public PdfPCell(PdfPCell cell)
     : base(cell.llx, cell.lly, cell.urx, cell.ury)
 {
     CloneNonPositionParameters(cell);
     verticalAlignment = cell.verticalAlignment;
     paddingLeft = cell.paddingLeft;
     paddingRight = cell.paddingRight;
     paddingTop = cell.paddingTop;
     paddingBottom = cell.paddingBottom;
     phrase = cell.phrase;
     fixedHeight = cell.fixedHeight;
     minimumHeight = cell.minimumHeight;
     noWrap = cell.noWrap;
     colspan = cell.colspan;
     rowspan = cell.rowspan;
     if (cell.table != null)
         table = new PdfPTable(cell.table);
     image = Image.GetInstance(cell.image);
     cellEvent = cell.cellEvent;
     useDescender = cell.useDescender;
     column = ColumnText.Duplicate(cell.column);
     useBorderPadding = cell.useBorderPadding;
     rotation = cell.rotation;
 }
Esempio n. 2
0
 /** Constructs a <CODE>PdfPCell</CODE> with a <CODE>PdfPtable</CODE>.
 * This constructor allows nested tables.
 * The default padding is 0.
 * @param table The <CODE>PdfPTable</CODE>
 */
 public PdfPCell(PdfPTable table)
     : this(table, null)
 {
 }
Esempio n. 3
0
 /** Constructs a <CODE>PdfPCell</CODE> with a <CODE>PdfPtable</CODE>.
 * This constructor allows nested tables.
 *
 * @param table The <CODE>PdfPTable</CODE>
 * @param style  The style to apply to the cell (you could use getDefaultCell())
 * @since 2.1.0
 */
 public PdfPCell(PdfPTable table, PdfPCell style)
     : base(0, 0, 0, 0)
 {
     borderWidth = 0.5f;
     border = BOX;
     column.SetLeading(0, 1);
     this.table = table;
     table.WidthPercentage = 100;
     table.ExtendLastRow = true;
     column.AddElement(table);
     if (style != null) {
         CloneNonPositionParameters(style);
         verticalAlignment = style.verticalAlignment;
         paddingLeft = style.paddingLeft;
         paddingRight = style.paddingRight;
         paddingTop = style.paddingTop;
         paddingBottom = style.paddingBottom;
         colspan = style.colspan;
         rowspan = style.rowspan;
         cellEvent = style.cellEvent;
         useDescender = style.useDescender;
         useBorderPadding = style.useBorderPadding;
         rotation = style.rotation;
     }
     else {
         Padding = 0;
     }
 }
Esempio n. 4
0
 /**
 * Creates a PdfPTable object based on this TableAttributes object.
 * @return a com.lowagie.text.pdf.PdfPTable object
 * @throws DocumentException
 */
 public PdfPTable CreatePdfPTable()
 {
     if (content.Count == 0) throw new BadElementException("Trying to create a table without rows.");
     SimpleCell rowx = (SimpleCell)content[0];
     int columns = 0;
     foreach (SimpleCell cell in rowx.Content) {
         columns += cell.Colspan;
     }
     float[] widths = new float[columns];
     float[] widthpercentages = new float[columns];
     PdfPTable table = new PdfPTable(columns);
     table.TableEvent = this;
     table.HorizontalAlignment = alignment;
     int pos;
     foreach (SimpleCell row in content) {
         pos = 0;
         foreach (SimpleCell cell in row.Content) {
             if (float.IsNaN(cell.Spacing_left))    {
                 cell.Spacing_left = cellspacing / 2f;
             }
             if (float.IsNaN(cell.Spacing_right))   {
                 cell.Spacing_right = cellspacing / 2f;
             }
             if (float.IsNaN(cell.Spacing_top)) {
                 cell.Spacing_top = cellspacing / 2f;
             }
             if (float.IsNaN(cell.Spacing_bottom))  {
                 cell.Spacing_bottom = cellspacing / 2f;
             }
             cell.Padding = cellpadding;
             table.AddCell(cell.CreatePdfPCell(row));
             if (cell.Colspan == 1) {
                 if (cell.Width > 0) widths[pos] = cell.Width;
                 if (cell.Widthpercentage > 0) widthpercentages[pos] = cell.Widthpercentage;
             }
             pos += cell.Colspan;
         }
     }
     float sumWidths = 0f;
     for (int i = 0; i < columns; i++) {
         if (widths[i] == 0) {
             sumWidths = 0;
             break;
         }
         sumWidths += widths[i];
     }
     if (sumWidths > 0) {
         table.TotalWidth = sumWidths;
         table.SetWidths(widths);
     }
     else {
         for (int i = 0; i < columns; i++) {
             if (widthpercentages[i] == 0) {
                 sumWidths = 0;
                 break;
             }
             sumWidths += widthpercentages[i];
         }
         if (sumWidths > 0) {
             table.SetWidths(widthpercentages);
         }
     }
     if (width > 0) {
         table.TotalWidth = width;
     }
     if (widthpercentage > 0) {
         table.WidthPercentage = widthpercentage;
     }
     return table;
 }
Esempio n. 5
0
 /**
 * @see com.lowagie.text.pdf.PdfPTableEvent#tableLayout(com.lowagie.text.pdf.PdfPTable, float[][], float[], int, int, com.lowagie.text.pdf.PdfContentByte[])
  */
 public void TableLayout(PdfPTable table, float[][] widths, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases)
 {
     float[] width = widths[0];
     Rectangle rect = new Rectangle(width[0], heights[heights.Length - 1], width[width.Length - 1], heights[0]);
     rect.CloneNonPositionParameters(this);
     int bd = rect.Border;
     rect.Border = Rectangle.NO_BORDER;
     canvases[PdfPTable.BACKGROUNDCANVAS].Rectangle(rect);
     rect.Border = bd;
     rect.BackgroundColor = null;
     canvases[PdfPTable.LINECANVAS].Rectangle(rect);
 }
Esempio n. 6
0
        protected int GoComposite(bool simulate)
        {
            if (!rectangularMode)
            throw new DocumentException("Irregular columns are not supported in composite mode.");
            linesWritten = 0;
            descender = 0;
            bool firstPass = adjustFirstLine;
            main_loop:
            while (true) {
            if (compositeElements.Count == 0)
                return NO_MORE_TEXT;
            IElement element = (IElement)compositeElements[0];
            if (element.Type == Element.PARAGRAPH) {
                Paragraph para = (Paragraph)element;
                int status = 0;
                for (int keep = 0; keep < 2; ++keep) {
                    float lastY = yLine;
                    bool createHere = false;
                    if (compositeColumn == null) {
                        compositeColumn = new ColumnText(canvas);
                        compositeColumn.UseAscender = (firstPass ? useAscender : false);
                        compositeColumn.Alignment = para.Alignment;
                        compositeColumn.Indent = para.IndentationLeft + para.FirstLineIndent;
                        compositeColumn.ExtraParagraphSpace = para.ExtraParagraphSpace;
                        compositeColumn.FollowingIndent = para.IndentationLeft;
                        compositeColumn.RightIndent = para.IndentationRight;
                        compositeColumn.SetLeading(para.Leading, para.MultipliedLeading);
                        compositeColumn.RunDirection = runDirection;
                        compositeColumn.ArabicOptions = arabicOptions;
                        compositeColumn.SpaceCharRatio = spaceCharRatio;
                        compositeColumn.AddText(para);
                        if (!firstPass) {
                            yLine -= para.SpacingBefore;
                        }
                        createHere = true;
                    }
                    compositeColumn.leftX = leftX;
                    compositeColumn.rightX = rightX;
                    compositeColumn.yLine = yLine;
                    compositeColumn.rectangularWidth = rectangularWidth;
                    compositeColumn.rectangularMode = rectangularMode;
                    compositeColumn.minY = minY;
                    compositeColumn.maxY = maxY;
                    bool keepCandidate = (para.KeepTogether && 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.RemoveAt(0);
                    yLine -= para.SpacingAfter;
                }
                if ((status & NO_MORE_COLUMN) != 0) {
                    return NO_MORE_COLUMN;
                }
            }
            else if (element.Type == Element.LIST) {
                List list = (List)element;
                ArrayList items = list.Items;
                ListItem item = null;
                float listIndentation = list.IndentationLeft;
                int count = 0;
                Stack stack = new Stack();
                for (int k = 0; k < items.Count; ++k) {
                    Object obj = items[k];
                    if (obj is ListItem) {
                        if (count == listIdx) {
                            item = (ListItem)obj;
                            break;
                        }
                        else ++count;
                    }
                    else if (obj is List) {
                        stack.Push(new Object[]{list, k, listIndentation});
                        list = (List)obj;
                        items = list.Items;
                        listIndentation += list.IndentationLeft;
                        k = -1;
                        continue;
                    }
                    if (k == items.Count - 1) {
                        if (stack.Count > 0) {
                            Object[] objs = (Object[])stack.Pop();
                            list = (List)objs[0];
                            items = list.Items;
                            k = (int)objs[1];
                            listIndentation = (float)objs[2];
                        }
                    }
                }
                int status = 0;
                for (int keep = 0; keep < 2; ++keep) {
                    float lastY = yLine;
                    bool createHere = false;
                    if (compositeColumn == null) {
                        if (item == null) {
                            listIdx = 0;
                            compositeElements.RemoveAt(0);
                            goto main_loop;
                        }
                        compositeColumn = new ColumnText(canvas);

                        compositeColumn.UseAscender = (firstPass ? useAscender : false);
                        compositeColumn.Alignment = item.Alignment;
                        compositeColumn.Indent = item.IndentationLeft + listIndentation + item.FirstLineIndent;
                        compositeColumn.ExtraParagraphSpace = item.ExtraParagraphSpace;
                        compositeColumn.FollowingIndent = compositeColumn.Indent;
                        compositeColumn.RightIndent = item.IndentationRight + list.IndentationRight;
                        compositeColumn.SetLeading(item.Leading, item.MultipliedLeading);
                        compositeColumn.RunDirection = runDirection;
                        compositeColumn.ArabicOptions = arabicOptions;
                        compositeColumn.SpaceCharRatio = spaceCharRatio;
                        compositeColumn.AddText(item);
                        if (!firstPass) {
                            yLine -= item.SpacingBefore;
                        }
                        createHere = true;
                    }
                    compositeColumn.leftX = leftX;
                    compositeColumn.rightX = rightX;
                    compositeColumn.yLine = yLine;
                    compositeColumn.rectangularWidth = rectangularWidth;
                    compositeColumn.rectangularMode = rectangularMode;
                    compositeColumn.minY = minY;
                    compositeColumn.maxY = maxY;
                    bool keepCandidate = (item.KeepTogether && 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.ListSymbol), compositeColumn.leftX + listIndentation, compositeColumn.firstLineY, 0);
                    compositeColumn.firstLineYDone = true;
                }
                if ((status & NO_MORE_TEXT) != 0) {
                    compositeColumn = null;
                    ++listIdx;
                    yLine -= item.SpacingAfter;
                }
                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.HeaderRows) {
                    compositeElements.RemoveAt(0);
                    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.LockedWidth) {
                    tableWidth = table.TotalWidth;
                    UpdateFilledWidth(tableWidth);
                }
                else {
                    tableWidth = rectangularWidth * table.WidthPercentage / 100f;
                    table.TotalWidth = tableWidth;
                }

                // how many header rows are real header rows; how many are footer rows?
                int headerRows = table.HeaderRows;
                int footerRows = table.FooterRows;
                if (footerRows > headerRows)
                    footerRows = headerRows;
                int realHeaderRows = headerRows - footerRows;
                float headerHeight = table.HeaderHeight;
                float footerHeight = table.FooterHeight;

                // make sure the header and footer fit on the page
                bool skipHeader = (!firstPass && table.SkipFirstHeader && listIdx <= headerRows);
                if (!skipHeader) {
                    yTemp -= headerHeight;
                    if (yTemp < minY || yTemp > maxY) {
                        if (firstPass) {
                            compositeElements.RemoveAt(0);
                            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.ElementComplete) {
                    yTemp -= footerHeight;
                }
                for (k = listIdx; k < table.Size; ++k) {
                    float rowHeight = table.GetRowHeight(k);
                    if (yTemp - rowHeight < minY)
                        break;
                    yTemp -= rowHeight;
                }
                if (!table.ElementComplete) {
                    yTemp += footerHeight;
                }
                // either k is the first row that doesn't fit on the page (break);
                if (k < table.Size) {
                    if (table.SplitRows && (!table.SplitLate || (k == listIdx && firstPass))) {
                        if (!splittedRow) {
                            splittedRow = true;
                            table = new PdfPTable(table);
                            compositeElements[0] = table;
                            ArrayList rows = table.Rows;
                            for (int i = headerRows; i < listIdx; ++i)
                                rows[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.Rows.Insert(++k, newRow);
                        }
                    }
                    else if (!table.SplitRows && k == listIdx && firstPass) {
                        compositeElements.RemoveAt(0);
                        splittedRow = false;
                        continue;
                    }
                    else if (k == listIdx && !firstPass && (!table.SplitRows || table.SplitLate) && (table.FooterRows == 0 || table.ElementComplete)) {
                        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.HorizontalAlignment) {
                        case Element.ALIGN_LEFT:
                            break;
                        case Element.ALIGN_RIGHT:
                            x1 += rectangularWidth - tableWidth;
                            break;
                        default:
                            x1 += (rectangularWidth - tableWidth) / 2f;
                            break;
                    }
                    // copy the rows that fit on the page in a new table nt
                    PdfPTable nt = PdfPTable.ShallowCopy(table);
                    ArrayList sub = nt.Rows;

                    // first we add the real header rows (if necessary)
                    if (!skipHeader) {
                        for (int j = 0; j < realHeaderRows; ++j) {
                            PdfPRow headerRow = table.GetRow(j);
                            sub.Add(headerRow);
                        }
                    }
                    else {
                        nt.HeaderRows = footerRows;
                    }
                    // then we add the real content
                    sub.AddRange(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)
                    bool showFooter = !table.SkipLastFooter;
                    if (k < table.Size) {
                        nt.ElementComplete = true;
                        showFooter = true;
                    }
                    // we add the footer rows if necessary (not for incomplete tables)
                    for (int j = 0; j < footerRows && nt.ElementComplete && 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[sub.Count - 1 - footerRows];
                    if (table.ExtendLastRow) {
                        rowHeight = last.MaxHeights;
                        last.MaxHeights = 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.ExtendLastRow) {
                        last.MaxHeights = rowHeight;
                    }
                }
                else if (table.ExtendLastRow && minY > PdfPRow.BOTTOM_LIMIT) {
                    yTemp = minY;
                }
                yLine = yTemp;
                if (!(skipHeader || table.ElementComplete)) {
                    yLine += footerHeight;
                }
                if (k >= table.Size) {
                    yLine -= table.SpacingAfter;
                    compositeElements.RemoveAt(0);
                    splittedRow = false;
                    listIdx = 0;
                }
                else {
                    if (splittedRow) {
                        ArrayList rows = table.Rows;
                        for (int i = listIdx; i < k; ++i)
                            rows[i] = null;
                    }
                    listIdx = k;
                    return NO_MORE_COLUMN;
                }
            }
            else if (element.Type == Element.YMARK) {
                if (!simulate) {
                    IDrawInterface zh = (IDrawInterface)element;
                    zh.Draw(canvas, leftX, minY, rightX, maxY, yLine);
                }
                compositeElements.RemoveAt(0);
            }
            else
                compositeElements.RemoveAt(0);
            }
        }
Esempio n. 7
0
 internal bool FitsPage(PdfPTable table, float margin)
 {
     if (!table.LockedWidth) {
         float totalWidth = (IndentRight - IndentLeft) * table.WidthPercentage / 100;
         table.TotalWidth = totalWidth;
     }
     // ensuring that a new line has been started.
     EnsureNewLine();
     return table.TotalHeight + ((currentHeight > 0) ? table.SpacingBefore : 0f)
         <= IndentTop - currentHeight - IndentBottom - margin;
 }
Esempio n. 8
0
 /**
 * Copies the format of the sourceTable without copying the content.
 * @param sourceTable
 */
 protected internal void CopyFormat(PdfPTable sourceTable)
 {
     relativeWidths = new float[sourceTable.NumberOfColumns];
     absoluteWidths = new float[sourceTable.NumberOfColumns];
     Array.Copy(sourceTable.relativeWidths, 0, relativeWidths, 0, NumberOfColumns);
     Array.Copy(sourceTable.absoluteWidths, 0, absoluteWidths, 0, NumberOfColumns);
     totalWidth = sourceTable.totalWidth;
     totalHeight = sourceTable.totalHeight;
     currentRowIdx = 0;
     tableEvent = sourceTable.tableEvent;
     runDirection = sourceTable.runDirection;
     defaultCell = new PdfPCell(sourceTable.defaultCell);
     currentRow = new PdfPCell[sourceTable.currentRow.Length];
     isColspan = sourceTable.isColspan;
     splitRows = sourceTable.splitRows;
     spacingAfter = sourceTable.spacingAfter;
     spacingBefore = sourceTable.spacingBefore;
     headerRows = sourceTable.headerRows;
     footerRows = sourceTable.footerRows;
     lockedWidth = sourceTable.lockedWidth;
     extendLastRow = sourceTable.extendLastRow;
     headersInEvent = sourceTable.headersInEvent;
     widthPercentage = sourceTable.widthPercentage;
     splitLate = sourceTable.splitLate;
     skipFirstHeader = sourceTable.skipFirstHeader;
     skipLastFooter = sourceTable.skipLastFooter;
     horizontalAlignment = sourceTable.horizontalAlignment;
     keepTogether = sourceTable.keepTogether;
     complete = sourceTable.complete;
 }
Esempio n. 9
0
 /** Constructs a copy of a <CODE>PdfPTable</CODE>.
 * @param table the <CODE>PdfPTable</CODE> to be copied
 */
 public PdfPTable(PdfPTable table)
 {
     CopyFormat(table);
     for (int k = 0; k < currentRow.Length; ++k) {
         if (table.currentRow[k] == null)
             break;
         currentRow[k] = new PdfPCell(table.currentRow[k]);
     }
     for (int k = 0; k < table.rows.Count; ++k) {
         PdfPRow row = (PdfPRow)(table.rows[k]);
         if (row != null)
             row = new PdfPRow(row);
         rows.Add(row);
     }
 }
Esempio n. 10
0
 /**
 * Create a PdfPTable based on this Table object.
 * @return a PdfPTable object
 * @throws BadElementException
 */
 public PdfPTable CreatePdfPTable()
 {
     if (!convert2pdfptable) {
         throw new BadElementException("No error, just an old style table");
     }
     AutoFillEmptyCells = true;
     Complete();
     PdfPTable pdfptable = new PdfPTable(widths);
     pdfptable.ElementComplete = complete;
     if (NotAddedYet)
         pdfptable.SkipFirstHeader = true;
     SimpleTable t_evt = new SimpleTable();
     t_evt.CloneNonPositionParameters(this);
     t_evt.Cellspacing = cellspacing;
     pdfptable.TableEvent = t_evt;
     pdfptable.HeaderRows = lastHeaderRow + 1;
     pdfptable.SplitLate = cellsFitPage;
     pdfptable.KeepTogether = tableFitsPage;
     if (!float.IsNaN(offset)) {
         pdfptable.SpacingBefore = offset;
     }
     pdfptable.HorizontalAlignment = alignment;
     if (locked) {
         pdfptable.TotalWidth = width;
         pdfptable.LockedWidth = true;
     }
     else {
         pdfptable.WidthPercentage = width;
     }
     foreach (Row row in this) {
         IElement cell;
         PdfPCell pcell;
         for (int i = 0; i < row.Columns; i++) {
             if ((cell = (IElement)row.GetCell(i)) != null) {
                 if (cell is Table) {
                     pcell = new PdfPCell(((Table)cell).CreatePdfPTable());
                 }
                 else if (cell is Cell) {
                     pcell = ((Cell)cell).CreatePdfPCell();
                     pcell.Padding = cellpadding + cellspacing / 2f;
                     SimpleCell c_evt = new SimpleCell(SimpleCell.CELL);
                     c_evt.CloneNonPositionParameters((Cell)cell);
                     c_evt.Spacing = cellspacing * 2f;
                     pcell.CellEvent = c_evt;
                 }
                 else {
                     pcell = new PdfPCell();
                 }
                 pdfptable.AddCell(pcell);
             }
         }
     }
     return pdfptable;
 }
 /**
 * @see com.lowagie.text.pdf.PdfPTableEvent#tableLayout(com.lowagie.text.pdf.PdfPTable, float[][], float[], int, int, com.lowagie.text.pdf.PdfContentByte[])
 */
 public void TableLayout(PdfPTable table, float[][] widths, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases)
 {
     foreach (IPdfPTableEvent eventa in events) {
         eventa.TableLayout(table, widths, heights, headerRows, rowStart, canvases);
     }
 }
Esempio n. 12
0
 public PdfPTable BuildTable()
 {
     if (rows.Count == 0)
         return new PdfPTable(1);
     int ncol = 0;
     ArrayList c0 = (ArrayList)rows[0];
     for (int k = 0; k < c0.Count; ++k) {
         ncol += ((PdfPCell)c0[k]).Colspan;
     }
     PdfPTable table = new PdfPTable(ncol);
     String width = (String)props["width"];
     if (width == null)
         table.WidthPercentage = 100;
     else {
         if (width.EndsWith("%"))
             table.WidthPercentage = float.Parse(width.Substring(0, width.Length - 1), NumberFormatInfo.InvariantInfo);
         else {
             table.TotalWidth = float.Parse(width, NumberFormatInfo.InvariantInfo);
             table.LockedWidth = true;
         }
     }
     for (int row = 0; row < rows.Count; ++row) {
         ArrayList col = (ArrayList)rows[row];
         for (int k = 0; k < col.Count; ++k) {
             table.AddCell((PdfPCell)col[k]);
         }
     }
     return table;
 }
Esempio n. 13
0
 /**
 * Splits a row to newHeight.
 * The returned row is the remainder. It will return null if the newHeight
 * was so small that only an empty row would result.
 *
 * @param new_height the new height
 * @return the remainder row or null if the newHeight was so small that only
 * an empty row would result
 */
 public PdfPRow SplitRow(PdfPTable table, int rowIndex, float new_height)
 {
     PdfPCell[] newCells = new PdfPCell[cells.Length];
     float[] fixHs = new float[cells.Length];
     float[] minHs = new float[cells.Length];
     bool allEmpty = true;
     for (int k = 0; k < cells.Length; ++k) {
         float newHeight = new_height;
         PdfPCell cell = cells[k];
         if (cell == null) {
             int index = rowIndex;
             if (table.RowSpanAbove(index, k)) {
                 newHeight += table.GetRowHeight(index);
                 while (table.RowSpanAbove(--index, k)) {
                     newHeight += table.GetRowHeight(index);
                 }
                 PdfPRow row = table.GetRow(index);
                 if (row != null && row.GetCells()[k] != null) {
                     newCells[k] = new PdfPCell(row.GetCells()[k]);
                     newCells[k].ConsumeHeight(newHeight);
                     newCells[k].Rowspan = row.GetCells()[k].Rowspan - rowIndex + index;
                     allEmpty = false;
                 }
             }
             continue;
         }
         fixHs[k] = cell.FixedHeight;
         minHs[k] = cell.MinimumHeight;
         Image img = cell.Image;
         PdfPCell newCell = new PdfPCell(cell);
         if (img != null) {
             if (newHeight > cell.EffectivePaddingBottom + cell.EffectivePaddingTop + 2) {
                 newCell.Phrase = null;
                 allEmpty = false;
             }
         }
         else {
             float y;
             ColumnText ct = ColumnText.Duplicate(cell.Column);
             float left = cell.Left + cell.EffectivePaddingLeft;
             float bottom = cell.Top + cell.EffectivePaddingBottom - newHeight;
             float right = cell.Right - cell.EffectivePaddingRight;
             float top = cell.Top - cell.EffectivePaddingTop;
             switch (cell.Rotation) {
                 case 90:
                 case 270:
                     y = SetColumn(ct, bottom, left, top, right);
                     break;
                 default:
                     y = SetColumn(ct, left, bottom, cell.NoWrap ? RIGHT_LIMIT : right, top);
                     break;
             }
             int status;
             status = ct.Go(true);
             bool thisEmpty = (ct.YLine == y);
             if (thisEmpty) {
                 newCell.Column = ColumnText.Duplicate(cell.Column);
                 ct.FilledWidth = 0;
             }
             else if ((status & ColumnText.NO_MORE_TEXT) == 0) {
                 newCell.Column = ct;
                 ct.FilledWidth = 0;
             }
             else
                 newCell.Phrase = null;
             allEmpty = (allEmpty && thisEmpty);
         }
         newCells[k] = newCell;
         cell.FixedHeight = newHeight;
     }
     if (allEmpty) {
         for (int k = 0; k < cells.Length; ++k) {
             PdfPCell cell = cells[k];
             if (cell == null)
                 continue;
             if (fixHs[k] > 0)
                 cell.FixedHeight = fixHs[k];
             else
                 cell.MinimumHeight = minHs[k];
         }
         return null;
     }
     CalculateHeights();
     PdfPRow split = new PdfPRow(newCells);
     split.widths = (float[]) widths.Clone();
     split.CalculateHeights();
     return split;
 }
Esempio n. 14
0
        /**
        * Signals that an <CODE>Element</CODE> was added to the <CODE>Document</CODE>.
        *
        * @param element the element to add
        * @return <CODE>true</CODE> if the element was added, <CODE>false</CODE> if not.
        * @throws DocumentException when a document isn't open yet, or has been closed
        */
        public override bool Add(IElement element)
        {
            if (writer != null && writer.IsPaused()) {
                return false;
            }
            switch (element.Type) {

                // Information (headers)
                case Element.HEADER:
                    info.Addkey(((Meta)element).Name, ((Meta)element).Content);
                    break;
                case Element.TITLE:
                    info.AddTitle(((Meta)element).Content);
                    break;
                case Element.SUBJECT:
                    info.AddSubject(((Meta)element).Content);
                    break;
                case Element.KEYWORDS:
                    info.AddKeywords(((Meta)element).Content);
                    break;
                case Element.AUTHOR:
                    info.AddAuthor(((Meta)element).Content);
                    break;
                case Element.CREATOR:
                    info.AddCreator(((Meta)element).Content);
                    break;
                case Element.PRODUCER:
                    // you can not change the name of the producer
                    info.AddProducer();
                    break;
                case Element.CREATIONDATE:
                    // you can not set the creation date, only reset it
                    info.AddCreationDate();
                    break;

                    // content (text)
                case Element.CHUNK: {
                    // if there isn't a current line available, we make one
                    if (line == null) {
                        CarriageReturn();
                    }

                    // we cast the element to a chunk
                    PdfChunk chunk = new PdfChunk((Chunk) element, anchorAction);
                    // we try to add the chunk to the line, until we succeed
                    {
                        PdfChunk overflow;
                        while ((overflow = line.Add(chunk)) != null) {
                            CarriageReturn();
                            chunk = overflow;
                            chunk.TrimFirstSpace();
                        }
                    }
                    pageEmpty = false;
                    if (chunk.IsAttribute(Chunk.NEWPAGE)) {
                        NewPage();
                    }
                    break;
                }
                case Element.ANCHOR: {
                    leadingCount++;
                    Anchor anchor = (Anchor) element;
                    String url = anchor.Reference;
                    leading = anchor.Leading;
                    if (url != null) {
                        anchorAction = new PdfAction(url);
                    }

                    // we process the element
                    element.Process(this);
                    anchorAction = null;
                    leadingCount--;
                    break;
                }
                case Element.ANNOTATION: {
                    if (line == null) {
                        CarriageReturn();
                    }
                    Annotation annot = (Annotation) element;
                    Rectangle rect = new Rectangle(0, 0);
                    if (line != null)
                        rect = new Rectangle(annot.GetLlx(IndentRight - line.WidthLeft), annot.GetLly(IndentTop - currentHeight), annot.GetUrx(IndentRight - line.WidthLeft + 20), annot.GetUry(IndentTop - currentHeight - 20));
                    PdfAnnotation an = PdfAnnotationsImp.ConvertAnnotation(writer, annot, rect);
                    annotationsImp.AddPlainAnnotation(an);
                    pageEmpty = false;
                    break;
                }
                case Element.PHRASE: {
                    leadingCount++;
                    // we cast the element to a phrase and set the leading of the document
                    leading = ((Phrase) element).Leading;
                    // we process the element
                    element.Process(this);
                    leadingCount--;
                    break;
                }
                case Element.PARAGRAPH: {
                    leadingCount++;
                    // we cast the element to a paragraph
                    Paragraph paragraph = (Paragraph) element;

                    AddSpacing(paragraph.SpacingBefore, leading, paragraph.Font);

                    // we adjust the parameters of the document
                    alignment = paragraph.Alignment;
                    leading = paragraph.TotalLeading;

                    CarriageReturn();
                    // we don't want to make orphans/widows
                    if (currentHeight + line.Height + leading > IndentTop - IndentBottom) {
                        NewPage();
                    }

                    indentation.indentLeft += paragraph.IndentationLeft;
                    indentation.indentRight += paragraph.IndentationRight;

                    CarriageReturn();

                    IPdfPageEvent pageEvent = writer.PageEvent;
                    if (pageEvent != null && !isSectionTitle)
                        pageEvent.OnParagraph(writer, this, IndentTop - currentHeight);

                    // if a paragraph has to be kept together, we wrap it in a table object
                    if (paragraph.KeepTogether) {
                        CarriageReturn();
                        PdfPTable table = new PdfPTable(1);
                        table.WidthPercentage = 100f;
                        PdfPCell cell = new PdfPCell();
                        cell.AddElement(paragraph);
                        cell.Border = Rectangle.NO_BORDER;
                        cell.Padding = 0;
                        table.AddCell(cell);
                        indentation.indentLeft -= paragraph.IndentationLeft;
                        indentation.indentRight -= paragraph.IndentationRight;
                        this.Add(table);
                        indentation.indentLeft += paragraph.IndentationLeft;
                        indentation.indentRight += paragraph.IndentationRight;
                    }
                    else {
                        line.SetExtraIndent(paragraph.FirstLineIndent);
                        element.Process(this);
                        CarriageReturn();
                        AddSpacing(paragraph.SpacingAfter, paragraph.TotalLeading, paragraph.Font);
                    }

                    if (pageEvent != null && !isSectionTitle)
                        pageEvent.OnParagraphEnd(writer, this, IndentTop - currentHeight);

                    alignment = Element.ALIGN_LEFT;
                    indentation.indentLeft -= paragraph.IndentationLeft;
                    indentation.indentRight -= paragraph.IndentationRight;
                    CarriageReturn();
                    leadingCount--;
                    break;
                }
                case Element.SECTION:
                case Element.CHAPTER: {
                    // Chapters and Sections only differ in their constructor
                    // so we cast both to a Section
                    Section section = (Section) element;
                    IPdfPageEvent pageEvent = writer.PageEvent;

                    bool hasTitle = section.NotAddedYet && section.Title != null;

                    // if the section is a chapter, we begin a new page
                    if (section.TriggerNewPage) {
                        NewPage();
                    }

                    if (hasTitle) {
                        float fith = IndentTop - currentHeight;
                        int rotation = pageSize.Rotation;
                        if (rotation == 90 || rotation == 180)
                            fith = pageSize.Height - fith;
                        PdfDestination destination = new PdfDestination(PdfDestination.FITH, fith);
                        while (currentOutline.Level >= section.Depth) {
                            currentOutline = currentOutline.Parent;
                        }
                        PdfOutline outline = new PdfOutline(currentOutline, destination, section.GetBookmarkTitle(), section.BookmarkOpen);
                        currentOutline = outline;
                    }

                    // some values are set
                    CarriageReturn();
                    indentation.sectionIndentLeft += section.IndentationLeft;
                    indentation.sectionIndentRight += section.IndentationRight;
                    if (section.NotAddedYet && pageEvent != null)
                        if (element.Type == Element.CHAPTER)
                            pageEvent.OnChapter(writer, this, IndentTop - currentHeight, section.Title);
                        else
                            pageEvent.OnSection(writer, this, IndentTop - currentHeight, section.Depth, section.Title);

                    // the title of the section (if any has to be printed)
                    if (hasTitle) {
                        isSectionTitle = true;
                        Add(section.Title);
                        isSectionTitle = false;
                    }
                    indentation.sectionIndentLeft += section.Indentation;
                    // we process the section
                    element.Process(this);
                    // some parameters are set back to normal again
                    indentation.sectionIndentLeft -= (section.IndentationLeft + section.Indentation);
                    indentation.sectionIndentRight -= section.IndentationRight;

                    if (section.ElementComplete && pageEvent != null)
                        if (element.Type == Element.CHAPTER)
                            pageEvent.OnChapterEnd(writer, this, IndentTop - currentHeight);
                        else
                            pageEvent.OnSectionEnd(writer, this, IndentTop - currentHeight);

                    break;
                }
                case Element.LIST: {
                    // we cast the element to a List
                    List list = (List) element;
                    if (list.Alignindent) {
                        list.NormalizeIndentation();
                    }
                    // we adjust the document
                    indentation.listIndentLeft += list.IndentationLeft;
                    indentation.indentRight += list.IndentationRight;
                    // we process the items in the list
                    element.Process(this);
                    // some parameters are set back to normal again
                    indentation.listIndentLeft -= list.IndentationLeft;
                    indentation.indentRight -= list.IndentationRight;
                    CarriageReturn();
                    break;
                }
                case Element.LISTITEM: {
                    leadingCount++;
                    // we cast the element to a ListItem
                    ListItem listItem = (ListItem) element;

                    AddSpacing(listItem.SpacingBefore, leading, listItem.Font);

                    // we adjust the document
                    alignment = listItem.Alignment;
                    indentation.listIndentLeft += listItem.IndentationLeft;
                    indentation.indentRight += listItem.IndentationRight;
                    leading = listItem.TotalLeading;
                    CarriageReturn();
                    // we prepare the current line to be able to show us the listsymbol
                    line.ListItem = listItem;
                    // we process the item
                    element.Process(this);

                    AddSpacing(listItem.SpacingAfter, listItem.TotalLeading, listItem.Font);

                    // if the last line is justified, it should be aligned to the left
                    if (line.HasToBeJustified()) {
                        line.ResetAlignment();
                    }
                    // some parameters are set back to normal again
                    CarriageReturn();
                    indentation.listIndentLeft -= listItem.IndentationLeft;
                    indentation.indentRight -= listItem.IndentationRight;
                    leadingCount--;
                    break;
                }
                case Element.RECTANGLE: {
                    Rectangle rectangle = (Rectangle) element;
                    graphics.Rectangle(rectangle);
                    pageEmpty = false;
                    break;
                }
                case Element.PTABLE: {
                    PdfPTable ptable = (PdfPTable)element;
                    if (ptable.Size <= ptable.HeaderRows)
                        break; //nothing to do

                    // before every table, we add a new line and flush all lines
                    EnsureNewLine();
                    FlushLines();

                    AddPTable(ptable);
                    pageEmpty = false;
                    NewLine();
                    break;
                }
                case Element.MULTI_COLUMN_TEXT: {
                    EnsureNewLine();
                    FlushLines();
                    MultiColumnText multiText = (MultiColumnText) element;
                    float height = multiText.Write(writer.DirectContent, this, IndentTop - currentHeight);
                    currentHeight += height;
                    text.MoveText(0, -1f* height);
                    pageEmpty = false;
                    break;
                }
                case Element.TABLE : {
                    if (element is SimpleTable) {
                        PdfPTable ptable = ((SimpleTable)element).CreatePdfPTable();
                        if (ptable.Size <= ptable.HeaderRows)
                            break; //nothing to do

                        // before every table, we add a new line and flush all lines
                        EnsureNewLine();
                        FlushLines();
                        AddPTable(ptable);
                        pageEmpty = false;
                        break;
                    } else if (element is Table) {

                        try {
                            PdfPTable ptable = ((Table)element).CreatePdfPTable();
                            if (ptable.Size <= ptable.HeaderRows)
                                break; //nothing to do

                            // before every table, we add a new line and flush all lines
                            EnsureNewLine();
                            FlushLines();
                            AddPTable(ptable);
                            pageEmpty = false;
                            break;
                        }
                        catch (BadElementException) {
                            // constructing the PdfTable
                            // Before the table, add a blank line using offset or default leading
                            float offset = ((Table)element).Offset;
                            if (float.IsNaN(offset))
                                offset = leading;
                            CarriageReturn();
                            lines.Add(new PdfLine(IndentLeft, IndentRight, alignment, offset));
                            currentHeight += offset;
                            AddPdfTable((Table)element);
                        }
                    } else {
                        return false;
                    }
                    break;
                }
                case Element.JPEG:
                case Element.JPEG2000:
                case Element.JBIG2:
                case Element.IMGRAW:
                case Element.IMGTEMPLATE: {
                    //carriageReturn(); suggestion by Marc Campforts
                    Add((Image) element);
                    break;
                }
                case Element.YMARK: {
                    IDrawInterface zh = (IDrawInterface)element;
                    zh.Draw(graphics, IndentLeft, IndentBottom, IndentRight, IndentTop, IndentTop - currentHeight - (leadingCount > 0 ? leading : 0));
                    pageEmpty = false;
                    break;
                }
                case Element.MARKED: {
                    MarkedObject mo;
                    if (element is MarkedSection) {
                        mo = ((MarkedSection)element).Title;
                        if (mo != null) {
                            mo.Process(this);
                        }
                    }
                    mo = (MarkedObject)element;
                    mo.Process(this);
                    break;
                }
                default:
                    return false;
            }
            lastElementType = element.Type;
            return true;
        }
Esempio n. 15
0
 /**
 * Adds an iText element to the cell.
 * @param element
 */
 public void AddElement(IElement element)
 {
     if (table != null) {
         table = null;
         column.SetText(null);
     }
     column.AddElement(element);
 }
Esempio n. 16
0
 /**
 * Makes a shallow copy of a table (format without content).
 * @param table
 * @return a shallow copy of the table
 */
 public static PdfPTable ShallowCopy(PdfPTable table)
 {
     PdfPTable nt = new PdfPTable();
     nt.CopyFormat(table);
     return nt;
 }
Esempio n. 17
0
 protected internal void SetSimpleVars(ColumnText org)
 {
     maxY = org.maxY;
     minY = org.minY;
     alignment = org.alignment;
     leftWall = null;
     if (org.leftWall != null)
     leftWall = new ArrayList(org.leftWall);
     rightWall = null;
     if (org.rightWall != null)
     rightWall = new ArrayList(org.rightWall);
     yLine = org.yLine;
     currentLeading = org.currentLeading;
     fixedLeading = org.fixedLeading;
     multipliedLeading = org.multipliedLeading;
     canvas = org.canvas;
     canvases = org.canvases;
     lineStatus = org.lineStatus;
     indent = org.indent;
     followingIndent = org.followingIndent;
     rightIndent = org.rightIndent;
     extraParagraphSpace = org.extraParagraphSpace;
     rectangularWidth = org.rectangularWidth;
     rectangularMode = org.rectangularMode;
     spaceCharRatio = org.spaceCharRatio;
     lastWasNewline = org.lastWasNewline;
     linesWritten = org.linesWritten;
     arabicOptions = org.arabicOptions;
     runDirection = org.runDirection;
     descender = org.descender;
     composite = org.composite;
     splittedRow = org.splittedRow;
     if (org.composite) {
     compositeElements = new ArrayList(org.compositeElements);
     if (splittedRow) {
         PdfPTable table = (PdfPTable)compositeElements[0];
         compositeElements[0] = new PdfPTable(table);
     }
     if (org.compositeColumn != null)
         compositeColumn = Duplicate(org.compositeColumn);
     }
     listIdx = org.listIdx;
     firstLineY = org.firstLineY;
     leftX = org.leftX;
     rightX = org.rightX;
     firstLineYDone = org.firstLineYDone;
     waitPhrase = org.waitPhrase;
     useAscender = org.useAscender;
     filledWidth = org.filledWidth;
     adjustFirstLine = org.adjustFirstLine;
 }
Esempio n. 18
0
 /**
 * Adds a nested table.
 * @param table the table to be added to the cell
 */
 public void AddCell(PdfPTable table)
 {
     defaultCell.Table = table;
     AddCell(defaultCell);
     defaultCell.Table = null;
 }
Esempio n. 19
0
 /**
  * Adds an element. Elements supported are <CODE>Paragraph</CODE>,
  * <CODE>List</CODE>, <CODE>PdfPTable</CODE>, <CODE>Image</CODE> and
  * <CODE>Graphic</CODE>.
  * <p>
  * It removes all the text placed with <CODE>addText()</CODE>.
  * @param element the <CODE>Element</CODE>
  */
 public void AddElement(IElement element)
 {
     if (element == null)
     return;
     if (element is Image) {
     Image img = (Image)element;
     PdfPTable t = new PdfPTable(1);
     float w = img.WidthPercentage;
     if (w == 0) {
         t.TotalWidth = img.ScaledWidth;
         t.LockedWidth = true;
     }
     else
         t.WidthPercentage = w;
     t.SpacingAfter = img.SpacingAfter;
     t.SpacingBefore = img.SpacingBefore;
     switch (img.Alignment) {
         case Image.LEFT_ALIGN:
             t.HorizontalAlignment = Element.ALIGN_LEFT;
             break;
         case Image.RIGHT_ALIGN:
             t.HorizontalAlignment = Element.ALIGN_RIGHT;
             break;
         default:
             t.HorizontalAlignment = Element.ALIGN_CENTER;
             break;
     }
     PdfPCell c = new PdfPCell(img, true);
     c.Padding = 0;
     c.Border = img.Border;
     c.BorderColor = img.BorderColor;
     c.BorderWidth = img.BorderWidth;
     c.BackgroundColor = img.BackgroundColor;
     t.AddCell(c);
     element = t;
     }
     if (element.Type == Element.CHUNK) {
     element = new Paragraph((Chunk)element);
     }
     else if (element.Type == Element.PHRASE) {
     element = new Paragraph((Phrase)element);
     }
     if (element is SimpleTable) {
     try {
         element = ((SimpleTable)element).CreatePdfPTable();
     } catch (DocumentException) {
         throw new ArgumentException("Element not allowed.");
     }
     }
     else if (element.Type != Element.PARAGRAPH && element.Type != Element.LIST && element.Type != Element.PTABLE && element.Type != Element.YMARK)
     throw new ArgumentException("Element not allowed.");
     if (!composite) {
     composite = true;
     compositeElements = new ArrayList();
     bidiLine = null;
     waitPhrase = null;
     }
     compositeElements.Add(element);
 }
Esempio n. 20
0
 //    [M4] Adding a PdfPTable
 /** Adds a <CODE>PdfPTable</CODE> to the document.
 * @param ptable the <CODE>PdfPTable</CODE> to be added to the document.
 * @throws DocumentException on error
 */
 internal void AddPTable(PdfPTable ptable)
 {
     ColumnText ct = new ColumnText(writer.DirectContent);
     // if the table prefers to be on a single page, and it wouldn't
     //fit on the current page, start a new page.
     if (ptable.KeepTogether && !FitsPage(ptable, 0f) && currentHeight > 0)  {
         NewPage();
     }
     // add dummy paragraph if we aren't at the top of a page, so that
     // spacingBefore will be taken into account by ColumnText
     if (currentHeight > 0) {
         Paragraph p = new Paragraph();
         p.Leading = 0;
         ct.AddElement(p);
     }
     ct.AddElement(ptable);
     bool he = ptable.HeadersInEvent;
     ptable.HeadersInEvent = true;
     int loop = 0;
     while (true) {
         ct.SetSimpleColumn(IndentLeft, IndentBottom, IndentRight, IndentTop - currentHeight);
         int status = ct.Go();
         if ((status & ColumnText.NO_MORE_TEXT) != 0) {
             text.MoveText(0, ct.YLine - IndentTop + currentHeight);
             currentHeight = IndentTop - ct.YLine;
             break;
         }
         if (IndentTop - currentHeight == ct.YLine)
             ++loop;
         else
             loop = 0;
         if (loop == 3) {
             Add(new Paragraph("ERROR: Infinite table loop"));
             break;
         }
         NewPage();
     }
     ptable.HeadersInEvent = he;
 }