iTextSharp.text.pdf.PdfPRow.WriteCells C# (CSharp) Метод

WriteCells() публичный Метод

public WriteCells ( int colStart, int colEnd, float xPos, float yPos, PdfContentByte canvases, bool reusable ) : void
colStart int
colEnd int
xPos float
yPos float
canvases PdfContentByte
reusable bool
Результат void
        public void WriteCells(int colStart, int colEnd, float xPos, float yPos, PdfContentByte[] canvases, bool reusable)
        {
            if (!calculated)
                CalculateHeights();
            if (colEnd < 0)
                colEnd = cells.Length;
            else
                colEnd = Math.Min(colEnd, cells.Length);
            if (colStart < 0)
                colStart = 0;
            if (colStart >= colEnd)
                return;

            int newStart;
            for (newStart = colStart; newStart >= 0; --newStart) {
                if (cells[newStart] != null)
                    break;
                if (newStart > 0)
                    xPos -= widths[newStart - 1];
            }

            if (newStart < 0)
                newStart = 0;
            if (cells[newStart] != null)
                xPos -= cells[newStart].Left;

            for (int k = newStart; k < colEnd; ++k) {
                PdfPCell cell = cells[k];
                if (cell == null)
                    continue;
                float currentMaxHeight = maxHeight + extraHeights[k];

                WriteBorderAndBackground(xPos, yPos, currentMaxHeight, cell, canvases);

                Image img = cell.Image;

                float tly = cell.Top + yPos - cell.EffectivePaddingTop;
                if (cell.Height <= currentMaxHeight) {
                    switch (cell.VerticalAlignment) {
                    case Element.ALIGN_BOTTOM:
                        tly = cell.Top + yPos - currentMaxHeight + cell.Height
                                - cell.EffectivePaddingTop;
                        break;
                    case Element.ALIGN_MIDDLE:
                        tly = cell.Top + yPos + (cell.Height - currentMaxHeight) / 2
                                - cell.EffectivePaddingTop;
                        break;
                    default:
                        break;
                    }
                }
                if (img != null) {
                    if (cell.Rotation != 0) {
                        img = Image.GetInstance(img);
                        img.Rotation = img.GetImageRotation() + (float)(cell.Rotation * Math.PI / 180.0);
                    }
                    bool vf = false;
                    if (cell.Height > currentMaxHeight) {
                        if (!img.ScaleToFitLineWhenOverflow) {
                            continue;
                        }
                        img.ScalePercent(100);
                        float scale = (currentMaxHeight - cell.EffectivePaddingTop - cell
                                .EffectivePaddingBottom)
                                / img.ScaledHeight;
                        img.ScalePercent(scale * 100);
                        vf = true;
                    }
                    float left = cell.Left + xPos
                            + cell.EffectivePaddingLeft;
                    if (vf) {
                        switch (cell.HorizontalAlignment) {
                        case Element.ALIGN_CENTER:
                            left = xPos
                                    + (cell.Left + cell.EffectivePaddingLeft
                                            + cell.Right
                                            - cell.EffectivePaddingRight - img
                                            .ScaledWidth) / 2;
                            break;
                        case Element.ALIGN_RIGHT:
                            left = xPos + cell.Right
                                    - cell.EffectivePaddingRight
                                    - img.ScaledWidth;
                            break;
                        default:
                            break;
                        }
                        tly = cell.Top + yPos - cell.EffectivePaddingTop;
                    }
                    img.SetAbsolutePosition(left, tly - img.ScaledHeight);
                    canvases[PdfPTable.TEXTCANVAS].AddImage(img);
                } else {
                    // rotation sponsored by Connection GmbH
                    if (cell.Rotation == 90 || cell.Rotation == 270) {
                        float netWidth = currentMaxHeight - cell.EffectivePaddingTop - cell.EffectivePaddingBottom;
                        float netHeight = cell.Width - cell.EffectivePaddingLeft - cell.EffectivePaddingRight;
                        ColumnText ct = ColumnText.Duplicate(cell.Column);
                        ct.Canvases = canvases;
                        ct.SetSimpleColumn(0, 0, netWidth + 0.001f, -netHeight);
                        ct.Go(true);
                        float calcHeight = -ct.YLine;
                        if (netWidth <= 0 || netHeight <= 0)
                            calcHeight = 0;
                        if (calcHeight > 0) {
                            if (cell.UseDescender)
                                calcHeight -= ct.Descender;
                            if (reusable)
                                ct = ColumnText.Duplicate(cell.Column);
                            else
                                ct = cell.Column;
                            ct.Canvases = canvases;
                            ct.SetSimpleColumn(-0.003f, -0.001f, netWidth + 0.003f, calcHeight);
                            float pivotX;
                            float pivotY;
                            if (cell.Rotation == 90) {
                                pivotY = cell.Top + yPos - currentMaxHeight + cell.EffectivePaddingBottom;
                                switch (cell.VerticalAlignment) {
                                case Element.ALIGN_BOTTOM:
                                    pivotX = cell.Left + xPos + cell.Width - cell.EffectivePaddingRight;
                                    break;
                                case Element.ALIGN_MIDDLE:
                                    pivotX = cell.Left + xPos + (cell.Width + cell.EffectivePaddingLeft - cell.EffectivePaddingRight + calcHeight) / 2;
                                    break;
                                default: //top
                                    pivotX = cell.Left + xPos + cell.EffectivePaddingLeft + calcHeight;
                                    break;
                                }
                                SaveAndRotateCanvases(canvases, 0,1,-1,0,pivotX,pivotY);
                            }
                            else {
                                pivotY = cell.Top + yPos - cell.EffectivePaddingTop;
                                switch (cell.VerticalAlignment) {
                                case Element.ALIGN_BOTTOM:
                                    pivotX = cell.Left + xPos + cell.EffectivePaddingLeft;
                                    break;
                                case Element.ALIGN_MIDDLE:
                                    pivotX = cell.Left + xPos + (cell.Width + cell.EffectivePaddingLeft - cell.EffectivePaddingRight - calcHeight) / 2;
                                    break;
                                default: //top
                                    pivotX = cell.Left + xPos + cell.Width - cell.EffectivePaddingRight - calcHeight;
                                    break;
                                }
                                SaveAndRotateCanvases(canvases, 0,-1,1,0,pivotX,pivotY);
                            }
                            try {
                                ct.Go();
                            } finally {
                                RestoreCanvases(canvases);
                            }
                        }
                    }
                    else {
                        float fixedHeight = cell.FixedHeight;
                        float rightLimit = cell.Right + xPos
                                - cell.EffectivePaddingRight;
                        float leftLimit = cell.Left + xPos
                                + cell.EffectivePaddingLeft;
                        if (cell.NoWrap) {
                            switch (cell.HorizontalAlignment) {
                                case Element.ALIGN_CENTER:
                                    rightLimit += 10000;
                                    leftLimit -= 10000;
                                    break;
                                case Element.ALIGN_RIGHT:
                                    if (cell.Rotation == 180) {
                                        rightLimit += RIGHT_LIMIT;
                                    }
                                    else {
                                        leftLimit -= RIGHT_LIMIT;
                                    }
                                    break;
                                default:
                                    if (cell.Rotation == 180) {
                                        leftLimit -= RIGHT_LIMIT;
                                    }
                                    else {
                                        rightLimit += RIGHT_LIMIT;
                                    }
                                    break;
                            }
                        }
                        ColumnText ct;
                        if (reusable)
                            ct = ColumnText.Duplicate(cell.Column);
                        else
                            ct = cell.Column;
                        ct.Canvases = canvases;
                        float bry = tly
                                - (currentMaxHeight
                                - cell.EffectivePaddingTop - cell.EffectivePaddingBottom);
                        if (fixedHeight > 0) {
                            if (cell.Height > currentMaxHeight) {
                                tly = cell.Top + yPos - cell.EffectivePaddingTop;
                                bry = cell.Top + yPos - currentMaxHeight + cell.EffectivePaddingBottom;
                            }
                        }
                        if ((tly > bry || ct.ZeroHeightElement()) && leftLimit < rightLimit) {
                            ct.SetSimpleColumn(leftLimit, bry - 0.001f, rightLimit, tly);
                            if (cell.Rotation == 180) {
                                float shx = leftLimit + rightLimit;
                                float shy = yPos + yPos - currentMaxHeight + cell.EffectivePaddingBottom - cell.EffectivePaddingTop;
                                SaveAndRotateCanvases(canvases, -1,0,0,-1,shx,shy);
                            }
                            try {
                                ct.Go();
                            } finally {
                                if (cell.Rotation == 180) {
                                    RestoreCanvases(canvases);
                                }
                            }
                        }
                    }
                }
                IPdfPCellEvent evt = cell.CellEvent;
                if (evt != null) {
                    Rectangle rect = new Rectangle(cell.Left + xPos, cell.Top
                            + yPos - currentMaxHeight, cell.Right + xPos, cell.Top
                            + yPos);
                    evt.CellLayout(cell, rect, canvases);
                }
            }
        }

Usage Example

Пример #1
0
        /** Writes the selected rows and columns to the document.
         * This method does not clip the columns; this is only important
         * if there are columns with colspan at boundaries.
         * <P>
         * <CODE>canvases</CODE> is obtained from <CODE>beginWritingRows()</CODE>.
         * <P>
         * The table event is only fired for complete rows.
         * @param colStart the first column to be written, zero index
         * @param colEnd the last column to be written + 1. If it is -1 all the
         * columns to the end are written
         * @param rowStart the first row to be written, zero index
         * @param rowEnd the last row to be written + 1. If it is -1 all the
         * rows to the end are written
         * @param xPos the x write coodinate
         * @param yPos the y write coodinate
         * @param canvases an array of 4 <CODE>PdfContentByte</CODE> obtained from
         * <CODE>beginWrittingRows()</CODE>
         * @return the y coordinate position of the bottom of the last row
         * @see #beginWritingRows(com.lowagie.text.pdf.PdfContentByte)
         */
        public float WriteSelectedRows(int colStart, int colEnd, int rowStart, int rowEnd, float xPos, float yPos, PdfContentByte[] canvases)
        {
            if (totalWidth <= 0)
            {
                throw new ArgumentException("The table width must be greater than zero.");
            }
            int size = rows.Count;

            if (rowEnd < 0)
            {
                rowEnd = size;
            }
            rowEnd = Math.Min(rowEnd, size);
            if (rowStart < 0)
            {
                rowStart = 0;
            }
            if (rowStart >= rowEnd)
            {
                return(yPos);
            }
            if (colEnd < 0)
            {
                colEnd = absoluteWidths.Length;
            }
            colEnd = Math.Min(colEnd, absoluteWidths.Length);
            if (colStart < 0)
            {
                colStart = 0;
            }
            colStart = Math.Min(colStart, absoluteWidths.Length);
            float yPosStart = yPos;

            for (int k = rowStart; k < rowEnd; ++k)
            {
                PdfPRow row = (PdfPRow)rows[k];
                if (row != null)
                {
                    row.WriteCells(colStart, colEnd, xPos, yPos, canvases);
                    yPos -= row.MaxHeights;
                }
            }
            if (tableEvent != null && colStart == 0 && colEnd == absoluteWidths.Length)
            {
                float[] heights = new float[rowEnd - rowStart + 1];
                heights[0] = yPosStart;
                for (int k = rowStart; k < rowEnd; ++k)
                {
                    PdfPRow row = (PdfPRow)rows[k];
                    float   hr  = 0;
                    if (row != null)
                    {
                        hr = row.MaxHeights;
                    }
                    heights[k - rowStart + 1] = heights[k - rowStart] - hr;
                }
                tableEvent.TableLayout(this, GetEventWidths(xPos, rowStart, rowEnd, headersInEvent), heights, headersInEvent ? headerRows : 0, rowStart, canvases);
            }
            return(yPos);
        }