예제 #1
0
        // methods to set the membervariables
        /**
         * Adds an element to this Cell.
         * <P>
         * Remark: you can't add ListItems, Rows, Cells,
         * JPEGs, GIFs or PNGs to a Cell.
         *
         * @param element The Element to add
         * @throws BadElementException if the method was called with a ListItem, Row or Cell
         */
        /// <summary>
        /// Adds an element to this Cell.
        /// </summary>
        /// <remarks>
        /// You can't add ListItems, Rows, Cells,
        /// JPEGs, GIFs or PNGs to a Cell.
        /// </remarks>
        /// <param name="element">the Element to add</param>
        public void AddElement(IElement element)
        {
            if (IsTable()) {
                Table table = (Table) arrayList[0];
                Cell tmp = new Cell(element);
                tmp.Border = NO_BORDER;
                tmp.Colspan = table.Columns;
                table.AddCell(tmp);
                return;
            }
            switch (element.Type) {
                case Element.LISTITEM:
                case Element.ROW:
                case Element.CELL:
                    throw new BadElementException("You can't add listitems, rows or cells to a cell.");
                case Element.JPEG:
                case Element.IMGRAW:
                case Element.IMGTEMPLATE:
                    arrayList.Add(element);
                    break;
                case Element.LIST:
                    if (float.IsNaN(this.Leading)) {
                        leading = ((List) element).TotalLeading;
                    }
                    if (((List) element).IsEmpty()) return;
                    arrayList.Add(element);
                    return;
                case Element.ANCHOR:
                case Element.PARAGRAPH:
                case Element.PHRASE:
                    if (float.IsNaN(leading)) {
                        leading = ((Phrase) element).Leading;
                    }
                    if (((Phrase) element).IsEmpty()) return;
                    arrayList.Add(element);
                    return;
                case Element.CHUNK:
                    if (((Chunk) element).IsEmpty()) return;
                    arrayList.Add(element);
                    return;
                case Element.TABLE:
                    Table table = new Table(3);
                    float[] widths = new float[3];
                    widths[1] = ((Table)element).Width;

                    switch (((Table)element).Alignment) {
                        case Element.ALIGN_LEFT:
                            widths[0] = 0f;
                            widths[2] = 100f - widths[1];
                            break;
                        case Element.ALIGN_CENTER:
                            widths[0] = (100f - widths[1]) / 2f;
                            widths[2] = widths[0];
                            break;
                        case Element.ALIGN_RIGHT:
                            widths[0] = 100f - widths[1];
                            widths[2] = 0f;
                            break;
                    }
                    table.Widths = widths;
                    Cell tmp;
                    if (arrayList.Count == 0) {
                        table.AddCell(Cell.DummyCell);
                    }
                    else {
                        tmp = new Cell();
                        tmp.Border = NO_BORDER;
                        tmp.Colspan = 3;
                        foreach (IElement ele in arrayList) {
                            tmp.Add(ele);
                        }
                        table.AddCell(tmp);
                    }
                    tmp = new Cell();
                    tmp.Border = NO_BORDER;
                    table.AddCell(tmp);
                    table.InsertTable((Table)element);
                    tmp = new Cell();
                    tmp.Border = NO_BORDER;
                    table.AddCell(tmp);
                    table.AddCell(Cell.DummyCell);
                    Clear();
                    arrayList.Add(table);
                    return;
                default:
                    arrayList.Add(element);
                    break;
            }
        }
예제 #2
0
        /// <summary>
        /// To put a table within the existing table at the given position
        /// generateTable will of course re-arrange the widths of the columns.
        /// </summary>
        /// <param name="aTable">the table you want to insert</param>
        /// <param name="aLocation">a Point</param>
        public void InsertTable(Table aTable, Point p)
        {
            if (aTable == null) throw new Exception("insertTable - table has null-value");

            mTableInserted = true;
            aTable.Complete();
            if (p.Y > columns)
                throw new ArgumentException("insertTable -- wrong columnposition("+ p.Y + ") of location; max =" + columns);
            int rowCount = p.X + 1 - rows.Count;
            int i = 0;
            if ( rowCount > 0 ) {   //create new rows ?
                for (; i < rowCount; i++) {
                    rows.Add(new Row(columns));
                }
            }

            ((Row) rows[p.X]).SetElement(aTable,p.Y);

            CurrentLocationToNextValidPosition = p;
        }
예제 #3
0
 /**
 * Creates a Table object based on this TableAttributes object.
 * @return a com.lowagie.text.Table object
 * @throws BadElementException
 */
 public Table CreateTable()
 {
     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];
     Table table = new Table(columns);
     table.Alignment = alignment;
     table.Spacing = cellspacing;
     table.Padding = cellpadding;
     table.CloneNonPositionParameters(this);
     int pos;
     foreach (SimpleCell row in content) {
         pos = 0;
         foreach (SimpleCell cell in row.Content) {
             table.AddCell(cell.CreateCell(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.Width = sumWidths;
         table.Locked = true;
         table.Widths = widths;
     }
     else {
         for (int i = 0; i < columns; i++) {
             if (widthpercentages[i] == 0) {
                 sumWidths = 0;
                 break;
             }
             sumWidths += widthpercentages[i];
         }
         if (sumWidths > 0) {
             table.Widths = widthpercentages;
         }
     }
     if (width > 0) {
         table.Width = width;
         table.Locked = true;
     }
     else if (widthpercentage > 0) {
         table.Width = widthpercentage;
     }
     return table;
 }
예제 #4
0
 /// <summary>
 /// To put a table within the existing table at the given position
 /// generateTable will of course re-arrange the widths of the columns.
 /// </summary>
 /// <param name="aTable">The Table to add</param>
 /// <param name="row">The row where the Cell will be added</param>
 /// <param name="column">The column where the Cell will be added</param>
 public void InsertTable(Table aTable, int row, int column)
 {
     if (aTable == null) throw new Exception("insertTable - table has null-value");
     InsertTable(aTable, new Point(row, column));
 }
예제 #5
0
 /// <summary>
 /// To put a table within the existing table at the current position
 /// generateTable will of course re-arrange the widths of the columns.
 /// </summary>
 /// <param name="aTable">the table you want to insert</param>
 public void InsertTable(Table aTable)
 {
     if (aTable == null) throw new Exception("insertTable - table has null-value");
     InsertTable(aTable, curPosition);
 }
예제 #6
0
        // constructors
        /**
         * Constructs a <CODE>PdfTable</CODE>-object.
         *
         * @param   table   a <CODE>Table</CODE>
         * @param   left    the left border on the page
         * @param   right   the right border on the page
         * @param   top     the start position of the top of the table
         */
        internal PdfTable(Table table, float left, float right, float top)
            : base(left, top, right, top)
        {
            // constructs a Rectangle (the bottomvalue will be changed afterwards)
            this.table = table;
            table.Complete();

            // copying the attributes from class Table
            CloneNonPositionParameters(table);

            this.columns = table.Columns;
            positions = table.GetWidths(left, right - left);

            // initialisation of some parameters
            Left = positions[0];
            Right = positions[positions.Length - 1];

            headercells = new ArrayList();
            cells = new ArrayList();

            UpdateRowAdditionsInternal();
        }
예제 #7
0
        /**
        * Adds a new table to
        * @param table              Table to add.  Rendered rows will be deleted after processing.
        * @param onlyFirstPage      Render only the first full page
        * @throws DocumentException
        */
        private void AddPdfTable(Table t)
        {
            // before every table, we flush all lines
            FlushLines();

            PdfTable table = new PdfTable(t, IndentLeft, IndentRight, IndentTop - currentHeight);
            RenderingContext ctx = new RenderingContext();
            ctx.pagetop = IndentTop;
            ctx.oldHeight = currentHeight;
            ctx.cellGraphics = new PdfContentByte(writer);
            ctx.rowspanMap = new Hashtable();
            ctx.table = table;

            // initialisation of parameters
            PdfCell cell;

            // drawing the table
            ArrayList headercells = table.HeaderCells;
            ArrayList cells = table.Cells;
            ArrayList rows = ExtractRows(cells, ctx);
            bool isContinue = false;
            while (cells.Count != 0) {
                // initialisation of some extra parameters;
                ctx.lostTableBottom = 0;

                // loop over the cells
                bool cellsShown = false;

                // draw the cells (line by line)
                ListIterator iterator = new ListIterator(rows);

                bool atLeastOneFits = false;
                while (iterator.HasNext()) {
                    ArrayList row = (ArrayList) iterator.Next();
                    AnalyzeRow(rows, ctx);
                    RenderCells(ctx, row, table.HasToFitPageCells() & atLeastOneFits);

                    if (!MayBeRemoved(row)) {
                        break;
                    }

                    ConsumeRowspan(row, ctx);
                    iterator.Remove();
                    atLeastOneFits = true;
                }

            //          compose cells array list for subsequent code
                cells.Clear();
                Hashtable opt = new Hashtable();
                foreach (ArrayList row in rows) {
                    foreach (PdfCell cellp in row) {
                        if (!opt.ContainsKey(cellp)) {
                            cells.Add(cellp);
                            opt[cellp] = null;
                        }
                    }
                }
                // we paint the graphics of the table after looping through all the cells
                Rectangle tablerec = new Rectangle(table);
                tablerec.Border = table.Border;
                tablerec.BorderWidth = table.BorderWidth;
                tablerec.BorderColor = table.BorderColor;
                tablerec.BackgroundColor = table.BackgroundColor;
                PdfContentByte under = writer.DirectContentUnder;
                under.Rectangle(tablerec.GetRectangle(Top, IndentBottom));
                under.Add(ctx.cellGraphics);
                // bugfix by Gerald Fehringer: now again add the border for the table
                // since it might have been covered by cell backgrounds
                tablerec.BackgroundColor = null;
                tablerec = tablerec.GetRectangle(Top, IndentBottom);
                tablerec.Border = table.Border;
                under.Rectangle(tablerec);
                // end bugfix
                ctx.cellGraphics = new PdfContentByte(null);
                // if the table continues on the next page
                if (rows.Count != 0) {
                    isContinue = true;
                    graphics.SetLineWidth(table.BorderWidth);
                    if (cellsShown && (table.Border & Rectangle.BOTTOM_BORDER) == Rectangle.BOTTOM_BORDER) {
                        // Draw the bottom line

                        // the color is set to the color of the element
                        Color tColor = table.BorderColor;
                        if (tColor != null) {
                            graphics.SetColorStroke(tColor);
                        }
                        graphics.MoveTo(table.Left, Math.Max(table.Bottom, IndentBottom));
                        graphics.LineTo(table.Right, Math.Max(table.Bottom, IndentBottom));
                        graphics.Stroke();
                        if (tColor != null) {
                            graphics.ResetRGBColorStroke();
                        }
                    }

                    // old page
                    pageEmpty = false;
                    float difference = ctx.lostTableBottom;

                    // new page
                    NewPage();
                    // G.F.: if something added in page event i.e. currentHeight > 0
                    float heightCorrection = 0;
                    bool somethingAdded = false;
                    if (currentHeight > 0) {
                        heightCorrection = 6;
                        currentHeight += heightCorrection;
                        somethingAdded = true;
                        NewLine();
                        FlushLines();
                        indentation.indentTop = currentHeight - leading;
                        currentHeight = 0;
                    }
                    else {
                        FlushLines();
                    }

                    // this part repeats the table headers (if any)
                    int size = headercells.Count;
                    if (size > 0) {
                        // this is the top of the headersection
                        cell = (PdfCell) headercells[0];
                        float oldTop = cell.GetTop(0);
                        // loop over all the cells of the table header
                        for (int ii = 0; ii < size; ii++) {
                            cell = (PdfCell) headercells[ii];
                            // calculation of the new cellpositions
                            cell.Top = IndentTop - oldTop + cell.GetTop(0);
                            cell.Bottom = IndentTop - oldTop + cell.GetBottom(0);
                            ctx.pagetop = cell.Bottom;
                            // we paint the borders of the cell
                            ctx.cellGraphics.Rectangle(cell.Rectangle(IndentTop, IndentBottom));
                            // we write the text of the cell
                            ArrayList images = cell.GetImages(IndentTop, IndentBottom);
                            foreach (Image image in images) {
                                cellsShown = true;
                                graphics.AddImage(image);
                            }
                            lines = cell.GetLines(IndentTop, IndentBottom);
                            float cellTop = cell.GetTop(IndentTop);
                            text.MoveText(0, cellTop-heightCorrection);
                            float cellDisplacement = FlushLines() - cellTop+heightCorrection;
                            text.MoveText(0, cellDisplacement);
                        }
                        currentHeight = IndentTop - ctx.pagetop + table.Cellspacing;
                        text.MoveText(0, ctx.pagetop - IndentTop - currentHeight);
                    }
                    else {
                        if (somethingAdded) {
                            ctx.pagetop = IndentTop;
                            text.MoveText(0, -table.Cellspacing);
                        }
                    }
                    ctx.oldHeight = currentHeight - heightCorrection;
                    // calculating the new positions of the table and the cells
                    size = Math.Min(cells.Count, table.Columns);
                    int i = 0;
                    while (i < size) {
                        cell = (PdfCell) cells[i];
                        if (cell.GetTop(-table.Cellspacing) > ctx.lostTableBottom) {
                            float newBottom = ctx.pagetop - difference + cell.Bottom;
                            float neededHeight = cell.RemainingHeight;
                            if (newBottom > ctx.pagetop - neededHeight) {
                                difference += newBottom - (ctx.pagetop - neededHeight);
                            }
                        }
                        i++;
                    }
                    size = cells.Count;
                    table.Top = IndentTop;
                    table.Bottom = ctx.pagetop - difference + table.GetBottom(table.Cellspacing);
                    for (i = 0; i < size; i++) {
                        cell = (PdfCell) cells[i];
                        float newBottom = ctx.pagetop - difference + cell.Bottom;
                        float newTop = ctx.pagetop - difference + cell.GetTop(-table.Cellspacing);
                        if (newTop > IndentTop - currentHeight) {
                            newTop = IndentTop - currentHeight;
                        }
                        cell.Top = newTop ;
                        cell.Bottom = newBottom ;
                    }
                }
            }

            float tableHeight = table.Top - table.Bottom;
            // bugfix by Adauto Martins when have more than two tables and more than one page
            // If continuation of table in other page (bug report #1460051)
            if (isContinue) {
                currentHeight = tableHeight;
                text.MoveText(0, -(tableHeight - (ctx.oldHeight * 2)));
            }
            else {
                currentHeight = ctx.oldHeight + tableHeight;
                text.MoveText(0, -tableHeight);
            }
            pageEmpty = false;
        }
예제 #8
0
 /**
 * Returns the bottomvalue of a <CODE>Table</CODE> if it were added to this document.
 *
 * @param    table   the table that may or may not be added to this document
 * @return   a bottom value
 */
 internal float GetBottom(Table table)
 {
     // constructing a PdfTable
     PdfTable tmp = new PdfTable(table, IndentLeft, IndentRight, IndentTop - currentHeight);
     return tmp.Bottom;
 }