iTextSharp.text.pdf.PdfChunk.Truncate C# (CSharp) Метод

Truncate() приватный Метод

private Truncate ( float width ) : PdfChunk
width float
Результат PdfChunk
        internal PdfChunk Truncate(float width)
        {
            if (image != null) {
                if (image.ScaledWidth > width) {
                    // Image does not fit the line, resize if requested
                    if (image.ScaleToFitLineWhenOverflow) {
                        float scalePercent = width / image.Width * 100;
                        image.ScalePercent(scalePercent);
                        return null;
                    }
                    PdfChunk pc = new PdfChunk("", this);
                    value = "";
                    attributes.Remove(Chunk.IMAGE);
                    image = null;
                    font = PdfFont.DefaultFont;
                    return pc;
                }
                else
                    return null;
            }

            int currentPosition = 0;
            float currentWidth = 0;

            // it's no use trying to split if there isn't even enough place for a space
            if (width < font.Width()) {
                string returnValue = value.Substring(1);
                value = value.Substring(0, 1);
                PdfChunk pc = new PdfChunk(returnValue, this);
                return pc;
            }

            // loop over all the characters of a string
            // or until the totalWidth is reached
            int length = value.Length;
            bool surrogate = false;
            while (currentPosition < length) {
                // the width of every character is added to the currentWidth
                surrogate = Utilities.IsSurrogatePair(value, currentPosition);
                if (surrogate)
                    currentWidth += GetCharWidth(Utilities.ConvertToUtf32(value, currentPosition));
                else
                    currentWidth += GetCharWidth(value[currentPosition]);
                if (currentWidth > width)
                    break;
                if (surrogate)
                    currentPosition++;
                currentPosition++;
            }

            // if all the characters fit in the total width, null is returned (there is no overflow)
            if (currentPosition == length) {
                return null;
            }

            // otherwise, the string has to be truncated
            //currentPosition -= 2;
            // we have to chop off minimum 1 character from the chunk
            if (currentPosition == 0) {
                currentPosition = 1;
                if (surrogate)
                    ++currentPosition;
            }
            string retVal = value.Substring(currentPosition);
            value = value.Substring(0, currentPosition);
            PdfChunk tmp = new PdfChunk(retVal, this);
            return tmp;
        }

Usage Example

Пример #1
0
        // methods

        /**
         * Adds a <CODE>PdfChunk</CODE> to the <CODE>PdfLine</CODE>.
         *
         * @param        chunk        the <CODE>PdfChunk</CODE> to add
         * @return        <CODE>null</CODE> if the chunk could be added completely; if not
         *                a <CODE>PdfChunk</CODE> containing the part of the chunk that could
         *                not be added is returned
         */

        internal PdfChunk Add(PdfChunk chunk)
        {
            // nothing happens if the chunk is null.
            if (chunk == null || chunk.ToString().Equals(""))
            {
                return(null);
            }

            // we split the chunk to be added
            PdfChunk overflow = chunk.Split(width);

            newlineSplit = (chunk.IsNewlineSplit() || overflow == null);
            //        if (chunk.IsNewlineSplit() && alignment == Element.ALIGN_JUSTIFIED)
            //            alignment = Element.ALIGN_LEFT;


            // if the length of the chunk > 0 we add it to the line
            if (chunk.Length > 0)
            {
                if (overflow != null)
                {
                    chunk.TrimLastSpace();
                }
                width -= chunk.Width;
                line.Add(chunk);
            }

            // if the length == 0 and there were no other chunks added to the line yet,
            // we risk to end up in an endless loop trying endlessly to add the same chunk
            else if (line.Count < 1)
            {
                chunk    = overflow;
                overflow = chunk.Truncate(width);
                width   -= chunk.Width;
                if (chunk.Length > 0)
                {
                    line.Add(chunk);
                    return(overflow);
                }
                // if the chunck couldn't even be truncated, we add everything, so be it
                else
                {
                    if (overflow != null)
                    {
                        line.Add(overflow);
                    }
                    return(null);
                }
            }
            else
            {
                width += ((PdfChunk)(line[line.Count - 1])).TrimLastSpace();
            }
            return(overflow);
        }
All Usage Examples Of iTextSharp.text.pdf.PdfChunk::Truncate