Inheritance: IElement, ISpaceable, IAccessibleElement
Example #1
0
        private void WriteAttributes(PdfDiv div)
        {
            if (div != null)
            {
                // Setting non-inheritable attributes
                if (div.BackgroundColor != null)
                {
                    SetColorAttribute(div.BackgroundColor, null, PdfName.BACKGROUNDCOLOR);
                }

                // Setting inheritable attributes
                SetTextAlignAttribute(div.TextAlignment);
            }
        }
Example #2
0
        public void CreateTaggedPdf18() {
            InitializeDocument("18");

            PdfDiv div = new PdfDiv();

            Paragraph paragraph = new Paragraph(text);
            paragraph.Font = new Font(Font.FontFamily.HELVETICA, 8, Font.NORMAL, BaseColor.RED);

            div.BackgroundColor = BaseColor.MAGENTA;
            div.TextAlignment = Element.ALIGN_CENTER;
            div.AddElement(paragraph);
            document.Add(div);
            document.Close();
            int[] nums = new int[] {32};
            CheckNums(nums);
            CompareResults("18");
        }
Example #3
0
        public void CreateTaggedPdf16() {
            InitializeDocument("16");

            Paragraph p = new Paragraph();
            Chunk chunk = new Chunk("Hello tagged world!");
            chunk.SetBackground(new BaseColor(255, 0, 255));
            chunk.Font = FontFactory.GetFont("TimesNewRoman", 20, BaseColor.ORANGE);
            chunk.SetUnderline(BaseColor.PINK, 1.2f, 1, 1, 1, 0);
            p.Add(chunk);
            PdfDiv div = new PdfDiv();
            div.AddElement(p);
            document.Add(div);

            document.Add(new Paragraph("This paragraph appears between 2 div blocks"));

            div = new PdfDiv();
            div.AddElement(new Paragraph(text));
            document.Add(div);


            document.Close();
            int[] nums = new int[] {48, 7};
            CheckNums(nums);
            CompareResults("16");
        }
Example #4
0
        private int floatingLayout(List <IElement> floatingElements, bool simulate)
        {
            int   status     = ColumnText.NO_MORE_TEXT;
            float minYLine   = yLine;
            float leftWidth  = 0;
            float rightWidth = 0;

            while (floatingElements.Count > 0)
            {
                if (floatingElements[0] is PdfDiv)
                {
                    PdfDiv floatingElement = (PdfDiv)floatingElements[0];
                    status = floatingElement.layout(compositeColumn, true, floatLeftX, minY, floatRightX, yLine);
                    if ((status & ColumnText.NO_MORE_TEXT) == 0)
                    {
                        yLine       = minYLine;
                        floatLeftX  = leftX;
                        floatRightX = rightX;
                        status      = floatingElement.layout(compositeColumn, true, floatLeftX, minY, floatRightX, yLine);
                        //if ((status & ColumnText.NO_MORE_TEXT) == 0) {
                        //    break;
                        //}
                    }
                    if (floatingElement.Float == PdfDiv.FloatType.LEFT)
                    {
                        status      = floatingElement.layout(compositeColumn, simulate, floatLeftX, minY, floatRightX, yLine);
                        floatLeftX += floatingElement.getActualWidth();
                        leftWidth  += floatingElement.getActualWidth();
                    }
                    else if (floatingElement.Float == PdfDiv.FloatType.RIGHT)
                    {
                        status       = floatingElement.layout(compositeColumn, simulate, floatRightX - floatingElement.getActualWidth() - 0.01f, minY, floatRightX, yLine);
                        floatRightX -= floatingElement.getActualWidth();
                        rightWidth  += floatingElement.getActualWidth();
                    }
                    minYLine = Math.Min(minYLine, yLine - floatingElement.getActualHeight());
                }
                else
                {
                    IElement firstElement = floatingElements[0];
                    if (firstElement is ISpaceable)
                    {
                        yLine -= ((ISpaceable)firstElement).SpacingBefore;
                    }
                    compositeColumn.AddElement(firstElement);
                    if (yLine > minYLine)
                    {
                        compositeColumn.SetSimpleColumn(floatLeftX, yLine, floatRightX, minYLine);
                    }
                    else
                    {
                        compositeColumn.SetSimpleColumn(floatLeftX, yLine, floatRightX, minY);
                    }

                    compositeColumn.FilledWidth = 0;

                    status = compositeColumn.Go(simulate);
                    if (yLine > minYLine && (floatLeftX > leftX || floatRightX < rightX) && (status & ColumnText.NO_MORE_TEXT) == 0)
                    {
                        yLine       = minYLine;
                        floatLeftX  = leftX;
                        floatRightX = rightX;
                        if (leftWidth != 0 && rightWidth != 0)
                        {
                            filledWidth = rightX - leftX;
                        }
                        else
                        {
                            if (leftWidth > filledWidth)
                            {
                                filledWidth = leftWidth;
                            }
                            if (rightWidth > filledWidth)
                            {
                                filledWidth = rightWidth;
                            }
                        }

                        leftWidth  = 0;
                        rightWidth = 0;
                        compositeColumn.SetSimpleColumn(floatLeftX, yLine, floatRightX, minY);
                        status   = compositeColumn.Go(simulate);
                        minYLine = compositeColumn.YLine + compositeColumn.Descender;
                        yLine    = minYLine;
                        if (compositeColumn.FilledWidth > filledWidth)
                        {
                            filledWidth = compositeColumn.FilledWidth;
                        }
                    }
                    else
                    {
                        if (rightWidth > 0)
                        {
                            rightWidth += compositeColumn.FilledWidth;
                        }
                        else if (leftWidth > 0)
                        {
                            leftWidth += compositeColumn.FilledWidth;
                        }
                        else if (compositeColumn.FilledWidth > filledWidth)
                        {
                            filledWidth = compositeColumn.FilledWidth;
                        }
                        minYLine = Math.Min(compositeColumn.YLine + compositeColumn.Descender, minYLine);
                        yLine    = compositeColumn.YLine + compositeColumn.Descender;
                    }

                    compositeColumn.CompositeElements.Clear();
                    //if ((status & ColumnText.NO_MORE_TEXT) == 0) {
                    //    break;
                    //}
                }

                floatingElements.RemoveAt(0);
            }


            if (leftWidth != 0 && rightWidth != 0)
            {
                filledWidth = rightX - leftX;
            }
            else
            {
                if (leftWidth > filledWidth)
                {
                    filledWidth = leftWidth;
                }
                if (rightWidth > filledWidth)
                {
                    filledWidth = rightWidth;
                }
            }

            yLine       = minYLine;
            floatLeftX  = leftX;
            floatRightX = rightX;

            return(status);
        }
Example #5
0
        public int layout(bool simulate)
        {
            int status = ColumnText.NO_MORE_TEXT;

            filledWidth = 0;

            List <IElement> floatingElements = new List <IElement>();

            while (content.Count > 0)
            {
                if (content[0] is PdfDiv)
                {
                    PdfDiv floatingElement = (PdfDiv)content[0];
                    if (floatingElement.Float == PdfDiv.FloatType.LEFT || floatingElement.Float == PdfDiv.FloatType.RIGHT)
                    {
                        floatingElements.Add(floatingElement);
                        content.RemoveAt(0);
                    }
                    else
                    {
                        if (floatingElements.Count > 0)
                        {
                            status = floatingLayout(floatingElements, simulate);
                            //if ((status & ColumnText.NO_MORE_TEXT) == 0) {
                            //    break;
                            //}
                        }

                        status = floatingElement.layout(compositeColumn, true, floatLeftX, minY, floatRightX, yLine);

                        if (!simulate)
                        {
                            status = floatingElement.layout(compositeColumn, simulate, floatLeftX, minY, floatRightX, yLine);
                        }

                        yLine -= floatingElement.getActualHeight();

                        if (floatingElement.getActualWidth() > filledWidth)
                        {
                            filledWidth = floatingElement.getActualWidth();
                        }
                        //if ((status & ColumnText.NO_MORE_TEXT) == 0) {
                        //    break;
                        //}
                        content.RemoveAt(0);
                    }
                }
                else
                {
                    floatingElements.Add(content[0]);
                    content.RemoveAt(0);
                }
            }

            if (/*(status & ColumnText.NO_MORE_TEXT) != 0 && */ floatingElements.Count > 0)
            {
                status = floatingLayout(floatingElements, simulate);
            }

            //foreach (IElement floatingElement in floatingElements) {
            //    content.Add(floatingElement);
            //}


            return(status);
        }
Example #6
0
        public PdfDiv apply(PdfDiv div, Tag t, IMarginMemory memory, IPageSizeContainable psc) {
            IDictionary<String, String> css = t.CSS;
            float fontSize = FontSizeTranslator.GetInstance().TranslateFontSize(t);
            if (fontSize == Font.UNDEFINED) {
                fontSize =  FontSizeTranslator.DEFAULT_FONT_SIZE;
            }
            String align = null;
            if (t.Attributes.ContainsKey(HTML.Attribute.ALIGN)) {
                align = t.Attributes[HTML.Attribute.ALIGN];
            } else if (css.ContainsKey(CSS.Property.TEXT_ALIGN)) {
                align = css[CSS.Property.TEXT_ALIGN];
            }

            if (align != null) {
                if (Util.EqualsIgnoreCase(align,CSS.Value.CENTER)) {
                    div.TextAlignment = Element.ALIGN_CENTER;
                } else if (Util.EqualsIgnoreCase(align, CSS.Value.RIGHT)) {
                    div.TextAlignment = Element.ALIGN_RIGHT;
                } else if (Util.EqualsIgnoreCase(align, CSS.Value.JUSTIFY)) {
                    div.TextAlignment = Element.ALIGN_JUSTIFIED;
                }
            }


            String widthValue;
            if (!t.CSS.TryGetValue(HTML.Attribute.WIDTH, out widthValue)) {
                t.Attributes.TryGetValue(HTML.Attribute.WIDTH, out widthValue);
            }
            if (widthValue != null) {
                if (utils.IsNumericValue(widthValue) || utils.IsMetricValue(widthValue)) {
				    div.Width = utils.ParsePxInCmMmPcToPt(widthValue);
                } else if (utils.IsRelativeValue(widthValue)) {
                    if (widthValue.Contains(CSS.Value.PERCENTAGE)) {
                        div.PercentageWidth = utils.ParseRelativeValue(widthValue, 1f);
                    } else {
                        div.Width = utils.ParseRelativeValue(widthValue, fontSize);
                    }
                }
            }

            String heightValue;
            if (!t.CSS.TryGetValue(HTML.Attribute.HEIGHT, out heightValue)) {
                t.Attributes.TryGetValue(HTML.Attribute.HEIGHT, out heightValue);
            }
            if (heightValue != null) {
                if (utils.IsNumericValue(heightValue) || utils.IsMetricValue(heightValue)) {
                    div.Height = utils.ParsePxInCmMmPcToPt(heightValue);
                } else if (utils.IsRelativeValue(heightValue)) {
                    if (heightValue.Contains(CSS.Value.PERCENTAGE)) {
                        div.PercentageHeight = utils.ParseRelativeValue(heightValue, 1f);
                    } else {
                        div.Height = utils.ParseRelativeValue(heightValue, fontSize);
                    }
                }
            }

            float? marginTop = null;
            float? marginBottom = null;

            foreach (KeyValuePair<String, String> entry in css) {
                String key = entry.Key;
			    String value = entry.Value;
                if (Util.EqualsIgnoreCase(key, CSS.Property.LEFT)) {
                    div.Left = utils.ParseValueToPt(value, fontSize);
                } else if (Util.EqualsIgnoreCase(key, CSS.Property.RIGHT)) {
                    if (div.Width == null || div.Left == null) {
                        div.Right = utils.ParseValueToPt(value, fontSize);
                    }
                } else if (Util.EqualsIgnoreCase(key, CSS.Property.TOP)) {
                    div.Top = utils.ParseValueToPt(value, fontSize);
                } else if (Util.EqualsIgnoreCase(key, CSS.Property.BOTTOM)) {
                    if (div.Height == null || div.Top == null) {
                        div.Bottom = utils.ParseValueToPt(value, fontSize);
                    }
                } else if (Util.EqualsIgnoreCase(key, CSS.Property.BACKGROUND_COLOR)) {
				    div.BackgroundColor = HtmlUtilities.DecodeColor(value);
                } else if (Util.EqualsIgnoreCase(key, CSS.Property.PADDING_LEFT)) {
                    div.PaddingLeft = utils.ParseValueToPt(value, fontSize);
                } else if (Util.EqualsIgnoreCase(key, CSS.Property.PADDING_RIGHT)) {
                    div.PaddingRight = utils.ParseValueToPt(value, fontSize);
                } else if (Util.EqualsIgnoreCase(key, CSS.Property.PADDING_TOP)) {
                    div.PaddingTop = utils.ParseValueToPt(value, fontSize);
                } else if (Util.EqualsIgnoreCase(key, CSS.Property.PADDING_BOTTOM)) {
                    div.PaddingBottom = utils.ParseValueToPt(value, fontSize);
                } else if (Util.EqualsIgnoreCase(key, CSS.Property.MARGIN_TOP)) {
                    marginTop = utils.CalculateMarginTop(value, fontSize, memory);
                } else if (Util.EqualsIgnoreCase(key, CSS.Property.MARGIN_BOTTOM)) {
                    marginBottom = utils.ParseValueToPt(value, fontSize);
                } else if (Util.EqualsIgnoreCase(key, CSS.Property.FLOAT)) {
                    if (Util.EqualsIgnoreCase(value, CSS.Value.LEFT)) {
                        div.Float = PdfDiv.FloatType.LEFT;
                    } else if (Util.EqualsIgnoreCase(value, CSS.Value.RIGHT)) {
                        div.Float = PdfDiv.FloatType.RIGHT;
                    }
                } else if (Util.EqualsIgnoreCase(key, CSS.Property.POSITION)) {
                    if (Util.EqualsIgnoreCase(value, CSS.Value.ABSOLUTE)) {
                        div.Position = PdfDiv.PositionType.ABSOLUTE;
                    } else if (Util.EqualsIgnoreCase(value, CSS.Value.FIXED)) {
                        div.Position = PdfDiv.PositionType.FIXED;
                    } else if (Util.EqualsIgnoreCase(value, CSS.Value.RELATIVE)) {
                        div.Position = PdfDiv.PositionType.RELATIVE;
                    }
                }

                //TODO: border, background properties.
            }

            return div;
        }
Example #7
0
        virtual public PdfDiv Apply(PdfDiv div, Tag t, IMarginMemory memory, IPageSizeContainable psc, HtmlPipelineContext context)
        {
            if (t.Attributes.ContainsKey("id"))
                div.Tag = t.Attributes["id"];

            IDictionary<String, String> css = t.CSS;
            float fontSize = FontSizeTranslator.GetInstance().TranslateFontSize(t);
            if (fontSize == Font.UNDEFINED)
            {
                fontSize = FontSizeTranslator.DEFAULT_FONT_SIZE;
            }
            String align = null;
            if (t.Attributes.ContainsKey(HTML.Attribute.ALIGN))
            {
                align = t.Attributes[HTML.Attribute.ALIGN];
            }
            else if (css.ContainsKey(CSS.Property.TEXT_ALIGN))
            {
                align = css[CSS.Property.TEXT_ALIGN];
            }

            if (align != null)
            {
                div.TextAlignment = CSS.GetElementAlignment(align);
            }


            String widthValue;
            if (!css.TryGetValue(HTML.Attribute.WIDTH, out widthValue))
            {
                t.Attributes.TryGetValue(HTML.Attribute.WIDTH, out widthValue);
            }
            if (widthValue != null)
            {
                float pageWidth = psc.PageSize.Width;
                if (utils.IsNumericValue(widthValue) || utils.IsMetricValue(widthValue))
                {
                    div.Width = Math.Min(pageWidth, utils.ParsePxInCmMmPcToPt(widthValue));
                }
                else if (utils.IsRelativeValue(widthValue))
                {
                    if (widthValue.Contains(CSS.Value.PERCENTAGE))
                    {
                        div.PercentageWidth = utils.ParseRelativeValue(widthValue, 1f);
                    }
                    else
                    {
                        div.Width = Math.Min(pageWidth, utils.ParseRelativeValue(widthValue, fontSize));
                    }
                }
            }

            String heightValue;
            if (!css.TryGetValue(HTML.Attribute.HEIGHT, out heightValue))
            {
                t.Attributes.TryGetValue(HTML.Attribute.HEIGHT, out heightValue);
            }
            if (heightValue != null)
            {
                if (utils.IsNumericValue(heightValue) || utils.IsMetricValue(heightValue))
                {
                    div.Height = utils.ParsePxInCmMmPcToPt(heightValue);
                }
                else if (utils.IsRelativeValue(heightValue))
                {
                    if (heightValue.Contains(CSS.Value.PERCENTAGE))
                    {
                        div.PercentageHeight = utils.ParseRelativeValue(heightValue, 1f);
                    }
                    else
                    {
                        div.Height = utils.ParseRelativeValue(heightValue, fontSize);
                    }
                }
            }

            float? marginTop = null;
            float? marginBottom = null;

            foreach (KeyValuePair<String, String> entry in css)
            {
                String key = entry.Key;
                String value = entry.Value;
                if (Util.EqualsIgnoreCase(key, CSS.Property.LEFT))
                {
                    div.Left = utils.ParseValueToPt(value, fontSize);
                }
                else if (Util.EqualsIgnoreCase(key, CSS.Property.RIGHT))
                {
                    if (div.Width == null || div.Left == null)
                    {
                        div.Right = utils.ParseValueToPt(value, fontSize);
                    }
                }
                else if (Util.EqualsIgnoreCase(key, CSS.Property.TOP))
                {
                    div.Top = utils.ParseValueToPt(value, fontSize);
                }
                else if (Util.EqualsIgnoreCase(key, CSS.Property.BOTTOM))
                {
                    if (div.Height == null || div.Top == null)
                    {
                        div.Bottom = utils.ParseValueToPt(value, fontSize);
                    }
                }
                else if (Util.EqualsIgnoreCase(key, CSS.Property.BACKGROUND_COLOR))
                {
                    div.BackgroundColor = HtmlUtilities.DecodeColor(value);
                }
                else if (Util.EqualsIgnoreCase(key, CSS.Property.BACKGROUND_IMAGE))
                {
                    string url = utils.ExtractUrl(value);
                    try
                    {
                        Image img =
                            new ImageRetrieve(context.ResourcePath, context.GetImageProvider()).RetrieveImage(url);
                        div.BackgroundImage = img;
                    }
                    catch (NoImageException e)
                    {
                        if (LOG.IsLogging(Level.ERROR))
                        {
                            LOG.Error(string.Format(LocaleMessages.GetInstance().GetMessage("html.tag.img.failed"), url), e);
                        }
                    }
                }
                else if (Util.EqualsIgnoreCase(key, CSS.Property.PADDING_LEFT))
                {
                    div.PaddingLeft = utils.ParseValueToPt(value, fontSize);
                }
                else if (Util.EqualsIgnoreCase(key, CSS.Property.PADDING_RIGHT))
                {
                    div.PaddingRight = utils.ParseValueToPt(value, fontSize);
                }
                else if (Util.EqualsIgnoreCase(key, CSS.Property.PADDING_TOP))
                {
                    div.PaddingTop = utils.ParseValueToPt(value, fontSize);
                }
                else if (Util.EqualsIgnoreCase(key, CSS.Property.PADDING_BOTTOM))
                {
                    div.PaddingBottom = utils.ParseValueToPt(value, fontSize);
                }
                else if (Util.EqualsIgnoreCase(key, CSS.Property.MARGIN_TOP))
                {
                    div.SpacingBefore = div.SpacingBefore + utils.CalculateMarginTop(value, fontSize, memory);
                    marginTop = utils.CalculateMarginTop(value, fontSize, memory);
                }
                else if (Util.EqualsIgnoreCase(key, CSS.Property.MARGIN_BOTTOM))
                {
                    div.SpacingAfter = div.SpacingAfter + utils.CalculateMarginTop(value, fontSize, memory);
                    marginBottom = utils.ParseValueToPt(value, fontSize);
                }
                else if (Util.EqualsIgnoreCase(key, CSS.Property.FLOAT))
                {
                    if (Util.EqualsIgnoreCase(value, CSS.Value.LEFT))
                    {
                        div.Float = PdfDiv.FloatType.LEFT;
                    }
                    else if (Util.EqualsIgnoreCase(value, CSS.Value.RIGHT))
                    {
                        div.Float = PdfDiv.FloatType.RIGHT;
                    }
                }
                else if (Util.EqualsIgnoreCase(key, CSS.Property.POSITION))
                {
                    if (Util.EqualsIgnoreCase(value, CSS.Value.ABSOLUTE))
                    {
                        div.Position = PdfDiv.PositionType.ABSOLUTE;
                    }
                    else if (Util.EqualsIgnoreCase(value, CSS.Value.FIXED))
                    {
                        div.Position = PdfDiv.PositionType.FIXED;
                    }
                    else if (Util.EqualsIgnoreCase(value, CSS.Value.RELATIVE))
                    {
                        div.Position = PdfDiv.PositionType.RELATIVE;
                    }
                }
                else if (Util.EqualsIgnoreCase(key, CSS.Property.DISPLAY))
                {
                    if (Util.EqualsIgnoreCase(value, CSS.Value.BLOCK))
                    {
                        div.Display = PdfDiv.DisplayType.BLOCK;
                    }
                    else if (Util.EqualsIgnoreCase(value, CSS.Value.INLINE))
                    {
                        div.Display = PdfDiv.DisplayType.INLINE;
                    }
                    else if (Util.EqualsIgnoreCase(value, CSS.Value.INLINE_BLOCK))
                    {
                        div.Display = PdfDiv.DisplayType.INLINE_BLOCK;
                    }
                    else if (Util.EqualsIgnoreCase(value, CSS.Value.INLINE_TABLE))
                    {
                        div.Display = PdfDiv.DisplayType.INLINE_TABLE;
                    }
                    else if (Util.EqualsIgnoreCase(value, CSS.Value.LIST_ITEM))
                    {
                        div.Display = PdfDiv.DisplayType.LIST_ITEM;
                    }
                    else if (Util.EqualsIgnoreCase(value, CSS.Value.NONE))
                    {
                        div.Display = PdfDiv.DisplayType.NONE;
                    }
                    else if (Util.EqualsIgnoreCase(value, CSS.Value.RUN_IN))
                    {
                        div.Display = PdfDiv.DisplayType.RUN_IN;
                    }
                    else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE))
                    {
                        div.Display = PdfDiv.DisplayType.TABLE;
                    }
                    else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_CAPTION))
                    {
                        div.Display = PdfDiv.DisplayType.TABLE_CAPTION;
                    }
                    else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_CELL))
                    {
                        div.Display = PdfDiv.DisplayType.TABLE_CELL;
                    }
                    else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_COLUMN_GROUP))
                    {
                        div.Display = PdfDiv.DisplayType.TABLE_COLUMN_GROUP;
                    }
                    else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_COLUMN))
                    {
                        div.Display = PdfDiv.DisplayType.TABLE_COLUMN;
                    }
                    else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_FOOTER_GROUP))
                    {
                        div.Display = PdfDiv.DisplayType.TABLE_FOOTER_GROUP;
                    }
                    else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_HEADER_GROUP))
                    {
                        div.Display = PdfDiv.DisplayType.TABLE_HEADER_GROUP;
                    }
                    else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_ROW))
                    {
                        div.Display = PdfDiv.DisplayType.TABLE_ROW;
                    }
                    else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_ROW_GROUP))
                    {
                        div.Display = PdfDiv.DisplayType.TABLE_ROW_GROUP;
                    }
                }
                else if (Util.EqualsIgnoreCase(CSS.Property.BORDER_TOP_STYLE, key) || Util.EqualsIgnoreCase(CSS.Property.BORDER_TOP, key))
                {
                    if (value.Contains(CSS.Value.DOTTED))
                    {
                        div.BorderStyleTop = PdfDiv.BorderStyle.DOTTED;
                    }
                    else if (value.Contains(CSS.Value.DASHED))
                    {
                        div.BorderStyleTop = PdfDiv.BorderStyle.DASHED;
                    }
                    else if (value.Contains(CSS.Value.SOLID))
                    {
                        div.BorderStyleTop = PdfDiv.BorderStyle.SOLID;
                    }
                    else if (value.Contains(CSS.Value.DOUBLE))
                    {
                        div.BorderStyleTop = PdfDiv.BorderStyle.DOUBLE;
                    }
                    else if (value.Contains(CSS.Value.GROOVE))
                    {
                        div.BorderStyleTop = PdfDiv.BorderStyle.GROOVE;
                    }
                    else if (value.Contains(CSS.Value.RIDGE))
                    {
                        div.BorderStyleTop = PdfDiv.BorderStyle.RIDGE;
                    }
                    else if (value.Contains(CSS.Value.INSET))
                    {
                        div.BorderStyleTop = PdfDiv.BorderStyle.INSET;
                    }
                    else if (value.Contains(CSS.Value.OUTSET))
                    {
                        div.BorderStyleTop = PdfDiv.BorderStyle.OUTSET;
                    }

                }
                else if (Util.EqualsIgnoreCase(CSS.Property.BORDER_BOTTOM_STYLE, key) || Util.EqualsIgnoreCase(CSS.Property.BORDER_BOTTOM, key))
                {
                    if (value.Contains(CSS.Value.DOTTED))
                    {
                        div.BorderStyleBottom = PdfDiv.BorderStyle.DOTTED;
                    }
                    else if (value.Contains(CSS.Value.DASHED))
                    {
                        div.BorderStyleBottom = PdfDiv.BorderStyle.DASHED;
                    }
                    else if (value.Contains(CSS.Value.SOLID))
                    {
                        div.BorderStyleBottom = PdfDiv.BorderStyle.SOLID;
                    }
                    else if (value.Contains(CSS.Value.DOUBLE))
                    {
                        div.BorderStyleBottom = PdfDiv.BorderStyle.DOUBLE;
                    }
                    else if (value.Contains(CSS.Value.GROOVE))
                    {
                        div.BorderStyleBottom = PdfDiv.BorderStyle.GROOVE;
                    }
                    else if (value.Contains(CSS.Value.RIDGE))
                    {
                        div.BorderStyleBottom = PdfDiv.BorderStyle.RIDGE;
                    }
                    else if (value.Contains(CSS.Value.INSET))
                    {
                        div.BorderStyleBottom = PdfDiv.BorderStyle.INSET;
                    }
                    else if (value.Contains(CSS.Value.OUTSET))
                    {
                        div.BorderStyleBottom = PdfDiv.BorderStyle.OUTSET;
                    }

                }
                else if (Util.EqualsIgnoreCase(key, CSS.Property.PAGE_BREAK_INSIDE))
                {
                    if (Util.EqualsIgnoreCase(value, CSS.Value.AVOID))
                    {
                        div.KeepTogether = true;
                    }
                }

                //TODO: border, background properties.
            }

            return div;
        }
        virtual public PdfDiv apply(PdfDiv div, Tag t, IMarginMemory memory, IPageSizeContainable psc) {
            IDictionary<String, String> css = t.CSS;
            float fontSize = FontSizeTranslator.GetInstance().TranslateFontSize(t);
            if (fontSize == Font.UNDEFINED) {
                fontSize =  FontSizeTranslator.DEFAULT_FONT_SIZE;
            }
            String align = null;
            if (t.Attributes.ContainsKey(HTML.Attribute.ALIGN)) {
                align = t.Attributes[HTML.Attribute.ALIGN];
            } else if (css.ContainsKey(CSS.Property.TEXT_ALIGN)) {
                align = css[CSS.Property.TEXT_ALIGN];
            }

            if (align != null) {
                if (Util.EqualsIgnoreCase(align,CSS.Value.CENTER)) {
                    div.TextAlignment = Element.ALIGN_CENTER;
                } else if (Util.EqualsIgnoreCase(align, CSS.Value.LEFT)) {
                    div.TextAlignment = Element.ALIGN_LEFT;
                } else if (Util.EqualsIgnoreCase(align, CSS.Value.RIGHT)) {
                    div.TextAlignment = Element.ALIGN_RIGHT;
                } else if (Util.EqualsIgnoreCase(align, CSS.Value.JUSTIFY)) {
                    div.TextAlignment = Element.ALIGN_JUSTIFIED;
                }
            }


            String widthValue;
            if (!t.CSS.TryGetValue(HTML.Attribute.WIDTH, out widthValue)) {
                t.Attributes.TryGetValue(HTML.Attribute.WIDTH, out widthValue);
            }
            if (widthValue != null) {
                float pageWidth = psc.PageSize.Width;
                if (utils.IsNumericValue(widthValue) || utils.IsMetricValue(widthValue)) {
				    div.Width = Math.Min(pageWidth, utils.ParsePxInCmMmPcToPt(widthValue));
                } else if (utils.IsRelativeValue(widthValue)) {
                    if (widthValue.Contains(CSS.Value.PERCENTAGE)) {
                        div.PercentageWidth = utils.ParseRelativeValue(widthValue, 1f);
                    } else {
                        div.Width = Math.Min(pageWidth, utils.ParseRelativeValue(widthValue, fontSize));
                    }
                }
            }

            String heightValue;
            if (!t.CSS.TryGetValue(HTML.Attribute.HEIGHT, out heightValue)) {
                t.Attributes.TryGetValue(HTML.Attribute.HEIGHT, out heightValue);
            }
            if (heightValue != null) {
                if (utils.IsNumericValue(heightValue) || utils.IsMetricValue(heightValue)) {
                    div.Height = utils.ParsePxInCmMmPcToPt(heightValue);
                } else if (utils.IsRelativeValue(heightValue)) {
                    if (heightValue.Contains(CSS.Value.PERCENTAGE)) {
                        div.PercentageHeight = utils.ParseRelativeValue(heightValue, 1f);
                    } else {
                        div.Height = utils.ParseRelativeValue(heightValue, fontSize);
                    }
                }
            }

            float? marginTop = null;
            float? marginBottom = null;

            foreach (KeyValuePair<String, String> entry in css) {
                String key = entry.Key;
			    String value = entry.Value;
                if (Util.EqualsIgnoreCase(key, CSS.Property.LEFT)) {
                    div.Left = utils.ParseValueToPt(value, fontSize);
                } else if (Util.EqualsIgnoreCase(key, CSS.Property.RIGHT)) {
                    if (div.Width == null || div.Left == null) {
                        div.Right = utils.ParseValueToPt(value, fontSize);
                    }
                } else if (Util.EqualsIgnoreCase(key, CSS.Property.TOP)) {
                    div.Top = utils.ParseValueToPt(value, fontSize);
                } else if (Util.EqualsIgnoreCase(key, CSS.Property.BOTTOM)) {
                    if (div.Height == null || div.Top == null) {
                        div.Bottom = utils.ParseValueToPt(value, fontSize);
                    }
                } else if (Util.EqualsIgnoreCase(key, CSS.Property.BACKGROUND_COLOR)) {
				    div.BackgroundColor = HtmlUtilities.DecodeColor(value);
                } else if (Util.EqualsIgnoreCase(key, CSS.Property.PADDING_LEFT)) {
                    div.PaddingLeft = utils.ParseValueToPt(value, fontSize);
                } else if (Util.EqualsIgnoreCase(key, CSS.Property.PADDING_RIGHT)) {
                    div.PaddingRight = utils.ParseValueToPt(value, fontSize);
                } else if (Util.EqualsIgnoreCase(key, CSS.Property.PADDING_TOP)) {
                    div.PaddingTop = utils.ParseValueToPt(value, fontSize);
                } else if (Util.EqualsIgnoreCase(key, CSS.Property.PADDING_BOTTOM)) {
                    div.PaddingBottom = utils.ParseValueToPt(value, fontSize);
                } else if (Util.EqualsIgnoreCase(key, CSS.Property.MARGIN_TOP)) {
                    marginTop = utils.CalculateMarginTop(value, fontSize, memory);
                } else if (Util.EqualsIgnoreCase(key, CSS.Property.MARGIN_BOTTOM)) {
                    marginBottom = utils.ParseValueToPt(value, fontSize);
                } else if (Util.EqualsIgnoreCase(key, CSS.Property.FLOAT)) {
                    if (Util.EqualsIgnoreCase(value, CSS.Value.LEFT)) {
                        div.Float = PdfDiv.FloatType.LEFT;
                    } else if (Util.EqualsIgnoreCase(value, CSS.Value.RIGHT)) {
                        div.Float = PdfDiv.FloatType.RIGHT;
                    }
                } else if (Util.EqualsIgnoreCase(key, CSS.Property.POSITION)) {
                    if (Util.EqualsIgnoreCase(value, CSS.Value.ABSOLUTE)) {
                        div.Position = PdfDiv.PositionType.ABSOLUTE;
                    } else if (Util.EqualsIgnoreCase(value, CSS.Value.FIXED)) {
                        div.Position = PdfDiv.PositionType.FIXED;
                    } else if (Util.EqualsIgnoreCase(value, CSS.Value.RELATIVE)) {
                        div.Position = PdfDiv.PositionType.RELATIVE;
                    }
                }
                else if (Util.EqualsIgnoreCase(key, CSS.Property.DISPLAY)) {
                    if (Util.EqualsIgnoreCase(value, CSS.Value.BLOCK)) {
                        div.Display = PdfDiv.DisplayType.BLOCK;
                    } else if (Util.EqualsIgnoreCase(value, CSS.Value.INLINE)) {
                        div.Display = PdfDiv.DisplayType.INLINE;
                    } else if (Util.EqualsIgnoreCase(value, CSS.Value.INLINE_BLOCK)) {
                        div.Display = PdfDiv.DisplayType.INLINE_BLOCK;
                    } else if (Util.EqualsIgnoreCase(value, CSS.Value.INLINE_TABLE)) {
                        div.Display = PdfDiv.DisplayType.INLINE_TABLE;
                    } else if (Util.EqualsIgnoreCase(value, CSS.Value.LIST_ITEM)) {
                        div.Display = PdfDiv.DisplayType.LIST_ITEM;
                    } else if (Util.EqualsIgnoreCase(value, CSS.Value.NONE)) {
                        div.Display = PdfDiv.DisplayType.NONE;
                    } else if (Util.EqualsIgnoreCase(value, CSS.Value.RUN_IN)) {
                        div.Display = PdfDiv.DisplayType.RUN_IN;
                    } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE)) {
                        div.Display = PdfDiv.DisplayType.TABLE;
                    } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_CAPTION)) {
                        div.Display = PdfDiv.DisplayType.TABLE_CAPTION;
                    } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_CELL)) {
                        div.Display = PdfDiv.DisplayType.TABLE_CELL;
                    } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_COLUMN_GROUP)) {
                        div.Display = PdfDiv.DisplayType.TABLE_COLUMN_GROUP;
                    } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_COLUMN)) {
                        div.Display = PdfDiv.DisplayType.TABLE_COLUMN;
                    } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_FOOTER_GROUP)) {
                        div.Display = PdfDiv.DisplayType.TABLE_FOOTER_GROUP;
                    } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_HEADER_GROUP)) {
                        div.Display = PdfDiv.DisplayType.TABLE_HEADER_GROUP;
                    } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_ROW)) {
                        div.Display = PdfDiv.DisplayType.TABLE_ROW;
                    } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_ROW_GROUP)) {
                        div.Display = PdfDiv.DisplayType.TABLE_ROW_GROUP;
                    }
                }
                else if (Util.EqualsIgnoreCase(CSS.Property.BORDER_TOP_STYLE, key))
                {
                    if (Util.EqualsIgnoreCase(CSS.Value.DOTTED, value))
                    {
                        div.BorderStyle = PdfDiv.BorderTopStyle.DOTTED;
                    }
                    else if (Util.EqualsIgnoreCase(CSS.Value.DASHED, value))
                    {
                        div.BorderStyle = PdfDiv.BorderTopStyle.DASHED;
                    }
                    else if (Util.EqualsIgnoreCase(CSS.Value.SOLID, value))
                    {
                        div.BorderStyle = PdfDiv.BorderTopStyle.SOLID;
                    }
                    else if (Util.EqualsIgnoreCase(CSS.Value.DOUBLE, value))
                    {
                        div.BorderStyle = PdfDiv.BorderTopStyle.DOUBLE;
                    }
                    else if (Util.EqualsIgnoreCase(CSS.Value.GROOVE, value))
                    {
                        div.BorderStyle = PdfDiv.BorderTopStyle.GROOVE;
                    }
                    else if (Util.EqualsIgnoreCase(CSS.Value.RIDGE, value))
                    {
                        div.BorderStyle = PdfDiv.BorderTopStyle.RIDGE;
                    }
                    else if (Util.EqualsIgnoreCase(value, CSS.Value.INSET))
                    {
                        div.BorderStyle = PdfDiv.BorderTopStyle.INSET;
                    }
                    else if (Util.EqualsIgnoreCase(value, CSS.Value.OUTSET))
                    {
                        div.BorderStyle = PdfDiv.BorderTopStyle.OUTSET;
                    }
                } 

                //TODO: border, background properties.
            }

            return div;
        }
        public int Layout(PdfContentByte canvas, bool simulate)
        {
            compositeColumn.Canvas = canvas;
            int status = ColumnText.NO_MORE_TEXT;

            List <IElement> floatingElements = new List <IElement>();
            List <IElement> content          = simulate ? new List <IElement>(this.content) : this.content;

            while (content.Count > 0)
            {
                if (content[0] is PdfDiv)
                {
                    PdfDiv floatingElement = (PdfDiv)content[0];
                    if (floatingElement.Float == PdfDiv.FloatType.LEFT || floatingElement.Float == PdfDiv.FloatType.RIGHT)
                    {
                        floatingElements.Add(floatingElement);
                        content.RemoveAt(0);
                    }
                    else
                    {
                        if (floatingElements.Count > 0)
                        {
                            status = FloatingLayout(floatingElements, simulate);
                            if ((status & ColumnText.NO_MORE_TEXT) == 0)
                            {
                                break;
                            }
                        }

                        content.RemoveAt(0);

                        status = floatingElement.Layout(canvas, useAscender, true, floatLeftX, minY, floatRightX, yLine);

                        if (!simulate)
                        {
                            canvas.OpenMCBlock(floatingElement);
                            status = floatingElement.Layout(canvas, useAscender, simulate, floatLeftX, minY, floatRightX, yLine);
                            canvas.CloseMCBlock(floatingElement);
                        }

                        if (floatingElement.getActualWidth() > filledWidth)
                        {
                            filledWidth = floatingElement.getActualWidth();
                        }
                        if ((status & ColumnText.NO_MORE_TEXT) == 0)
                        {
                            content.Insert(0, floatingElement);
                            yLine = floatingElement.YLine;
                            break;
                        }
                        else
                        {
                            yLine -= floatingElement.getActualHeight();
                        }
                    }
                }
                else
                {
                    floatingElements.Add(content[0]);
                    content.RemoveAt(0);
                }
            }

            if ((status & ColumnText.NO_MORE_TEXT) != 0 && floatingElements.Count > 0)
            {
                status = FloatingLayout(floatingElements, simulate);
            }

            content.InsertRange(0, floatingElements);

            return(status);
        }
        private int FloatingLayout(List <IElement> floatingElements, bool simulate)
        {
            int   status     = ColumnText.NO_MORE_TEXT;
            float minYLine   = yLine;
            float leftWidth  = 0;
            float rightWidth = 0;

            ColumnText currentCompositeColumn = compositeColumn;

            if (simulate)
            {
                currentCompositeColumn = ColumnText.Duplicate(compositeColumn);
            }

            while (floatingElements.Count > 0)
            {
                IElement nextElement = floatingElements[0];
                floatingElements.RemoveAt(0);
                if (nextElement is PdfDiv)
                {
                    PdfDiv floatingElement = (PdfDiv)nextElement;
                    status = floatingElement.Layout(compositeColumn.Canvas, useAscender, true, floatLeftX, minY, floatRightX, yLine);
                    if ((status & ColumnText.NO_MORE_TEXT) == 0)
                    {
                        yLine       = minYLine;
                        floatLeftX  = leftX;
                        floatRightX = rightX;
                        status      = floatingElement.Layout(compositeColumn.Canvas, useAscender, true, floatLeftX, minY, floatRightX, yLine);
                        if ((status & ColumnText.NO_MORE_TEXT) == 0)
                        {
                            floatingElements.Insert(0, floatingElement);
                            break;
                        }
                    }
                    if (floatingElement.Float == PdfDiv.FloatType.LEFT)
                    {
                        status      = floatingElement.Layout(compositeColumn.Canvas, useAscender, simulate, floatLeftX, minY, floatRightX, yLine);
                        floatLeftX += floatingElement.getActualWidth();
                        leftWidth  += floatingElement.getActualWidth();
                    }
                    else if (floatingElement.Float == PdfDiv.FloatType.RIGHT)
                    {
                        status       = floatingElement.Layout(compositeColumn.Canvas, useAscender, simulate, floatRightX - floatingElement.getActualWidth() - 0.01f, minY, floatRightX, yLine);
                        floatRightX -= floatingElement.getActualWidth();
                        rightWidth  += floatingElement.getActualWidth();
                    }
                    minYLine = Math.Min(minYLine, yLine - floatingElement.getActualHeight());
                }
                else
                {
                    if (nextElement is ISpaceable)
                    {
                        yLine -= ((ISpaceable)nextElement).SpacingBefore;
                    }
                    if (simulate)
                    {
                        if (nextElement is PdfPTable)
                        {
                            currentCompositeColumn.AddElement(new PdfPTable((PdfPTable)nextElement));
                        }
                        else
                        {
                            currentCompositeColumn.AddElement(nextElement);
                        }
                    }
                    else
                    {
                        currentCompositeColumn.AddElement(nextElement);
                    }
                    if (yLine > minYLine)
                    {
                        currentCompositeColumn.SetSimpleColumn(floatLeftX, yLine, floatRightX, minYLine);
                    }
                    else
                    {
                        currentCompositeColumn.SetSimpleColumn(floatLeftX, yLine, floatRightX, minY);
                    }

                    currentCompositeColumn.FilledWidth = 0;

                    status = currentCompositeColumn.Go(simulate);
                    if (yLine > minYLine && (floatLeftX > leftX || floatRightX < rightX) && (status & ColumnText.NO_MORE_TEXT) == 0)
                    {
                        yLine       = minYLine;
                        floatLeftX  = leftX;
                        floatRightX = rightX;
                        if (leftWidth != 0 && rightWidth != 0)
                        {
                            filledWidth = rightX - leftX;
                        }
                        else
                        {
                            if (leftWidth > filledWidth)
                            {
                                filledWidth = leftWidth;
                            }
                            if (rightWidth > filledWidth)
                            {
                                filledWidth = rightWidth;
                            }
                        }

                        leftWidth  = 0;
                        rightWidth = 0;
                        if (simulate && nextElement is PdfPTable)
                        {
                            currentCompositeColumn.AddElement(new PdfPTable((PdfPTable)nextElement));
                        }

                        currentCompositeColumn.SetSimpleColumn(floatLeftX, yLine, floatRightX, minY);
                        status   = currentCompositeColumn.Go(simulate);
                        minYLine = currentCompositeColumn.YLine + currentCompositeColumn.Descender;
                        yLine    = minYLine;
                        if (compositeColumn.FilledWidth > filledWidth)
                        {
                            filledWidth = compositeColumn.FilledWidth;
                        }
                    }
                    else
                    {
                        if (rightWidth > 0)
                        {
                            rightWidth += currentCompositeColumn.FilledWidth;
                        }
                        else if (leftWidth > 0)
                        {
                            leftWidth += currentCompositeColumn.FilledWidth;
                        }
                        else if (currentCompositeColumn.FilledWidth > filledWidth)
                        {
                            filledWidth = currentCompositeColumn.FilledWidth;
                        }
                        minYLine = Math.Min(currentCompositeColumn.YLine + currentCompositeColumn.Descender, minYLine);
                        yLine    = currentCompositeColumn.YLine + currentCompositeColumn.Descender;
                    }

                    if ((status & ColumnText.NO_MORE_TEXT) == 0)
                    {
                        if (!simulate)
                        {
                            floatingElements.InsertRange(0, currentCompositeColumn.CompositeElements);
                            currentCompositeColumn.CompositeElements.Clear();
                        }
                        else
                        {
                            floatingElements.Insert(0, nextElement);
                            currentCompositeColumn.SetText(null);
                        }
                        break;
                    }
                    else
                    {
                        currentCompositeColumn.SetText(null);
                    }
                }
            }


            if (leftWidth != 0 && rightWidth != 0)
            {
                filledWidth = rightX - leftX;
            }
            else
            {
                if (leftWidth > filledWidth)
                {
                    filledWidth = leftWidth;
                }
                if (rightWidth > filledWidth)
                {
                    filledWidth = rightWidth;
                }
            }

            yLine       = minYLine;
            floatLeftX  = leftX;
            floatRightX = rightX;

            return(status);
        }
Example #11
0
        private void WriteAttributes(PdfDiv div)
        {
            if (div != null)
            {
                // Setting non-inheritable attributes
                if (div.BackgroundColor != null)
                    SetColorAttribute(div.BackgroundColor, null, PdfName.BACKGROUNDCOLOR);

                // Setting inheritable attributes
                SetTextAlignAttribute(div.TextAlignment);
            }
        }
Example #12
0
        public int layout(bool simulate)
        {
            int status = ColumnText.NO_MORE_TEXT;

            filledWidth = 0;

            List <IElement> floatingElements = new List <IElement>();
            List <IElement> content          = simulate ? new List <IElement>(this.content) : this.content;
            ColumnText      compositeColumn  = simulate ? ColumnText.Duplicate(this.compositeColumn) : this.compositeColumn;

            while (content.Count > 0)
            {
                if (content[0] is PdfDiv)
                {
                    PdfDiv floatingElement = (PdfDiv)content[0];
                    if (floatingElement.Float == PdfDiv.FloatType.LEFT || floatingElement.Float == PdfDiv.FloatType.RIGHT)
                    {
                        floatingElements.Add(floatingElement);
                        content.RemoveAt(0);
                    }
                    else
                    {
                        if (floatingElements.Count > 0)
                        {
                            status = floatingLayout(compositeColumn, floatingElements, simulate);
                            if ((status & ColumnText.NO_MORE_TEXT) == 0)
                            {
                                break;
                            }
                        }

                        content.RemoveAt(0);

                        status = floatingElement.layout(compositeColumn, true, floatLeftX, minY, floatRightX, yLine);

                        if (!simulate)
                        {
                            status = floatingElement.layout(compositeColumn, simulate, floatLeftX, minY, floatRightX, yLine);
                        }

                        yLine -= floatingElement.getActualHeight();

                        if (floatingElement.getActualWidth() > filledWidth)
                        {
                            filledWidth = floatingElement.getActualWidth();
                        }
                        if ((status & ColumnText.NO_MORE_TEXT) == 0)
                        {
                            content.Insert(0, floatingElement);
                            break;
                        }
                    }
                }
                else
                {
                    floatingElements.Add(content[0]);
                    content.RemoveAt(0);
                }
            }

            if ((status & ColumnText.NO_MORE_TEXT) != 0 && floatingElements.Count > 0)
            {
                status = floatingLayout(compositeColumn, floatingElements, simulate);
            }

            foreach (IElement floatingElement in floatingElements)
            {
                content.Insert(0, floatingElement);
            }

            return(status);
        }
Example #13
0
        private int FloatingLayout(List <IElement> floatingElements, bool simulate)
        {
            int   status     = ColumnText.NO_MORE_TEXT;
            float minYLine   = yLine;
            float leftWidth  = 0;
            float rightWidth = 0;

            ColumnText currentCompositeColumn = compositeColumn;

            if (simulate)
            {
                currentCompositeColumn = ColumnText.Duplicate(compositeColumn);
            }

            bool ignoreSpacingBefore = maxY == yLine;

            while (floatingElements.Count > 0)
            {
                IElement nextElement = floatingElements[0];
                floatingElements.RemoveAt(0);
                if (nextElement is PdfDiv)
                {
                    PdfDiv floatingElement = (PdfDiv)nextElement;
                    status = floatingElement.Layout(compositeColumn.Canvas, useAscender, true, floatLeftX, minY, floatRightX, yLine);
                    if ((status & ColumnText.NO_MORE_TEXT) == 0)
                    {
                        yLine       = minYLine;
                        floatLeftX  = leftX;
                        floatRightX = rightX;
                        status      = floatingElement.Layout(compositeColumn.Canvas, useAscender, true, floatLeftX, minY, floatRightX, yLine);
                        if ((status & ColumnText.NO_MORE_TEXT) == 0)
                        {
                            floatingElements.Insert(0, floatingElement);
                            break;
                        }
                    }
                    if (floatingElement.Float == PdfDiv.FloatType.LEFT)
                    {
                        status      = floatingElement.Layout(compositeColumn.Canvas, useAscender, simulate, floatLeftX, minY, floatRightX, yLine);
                        floatLeftX += floatingElement.getActualWidth();
                        leftWidth  += floatingElement.getActualWidth();
                    }
                    else if (floatingElement.Float == PdfDiv.FloatType.RIGHT)
                    {
                        status       = floatingElement.Layout(compositeColumn.Canvas, useAscender, simulate, floatRightX - floatingElement.getActualWidth() - 0.01f, minY, floatRightX, yLine);
                        floatRightX -= floatingElement.getActualWidth();
                        rightWidth  += floatingElement.getActualWidth();
                    }
                    minYLine = Math.Min(minYLine, yLine - floatingElement.getActualHeight());
                }
                else
                {
                    if (minY > minYLine)
                    {
                        status = ColumnText.NO_MORE_COLUMN;
                        floatingElements.Insert(0, nextElement);
                        if (currentCompositeColumn != null)
                        {
                            currentCompositeColumn.SetText(null);
                        }
                        break;
                    }
                    else
                    {
                        if (nextElement is ISpaceable && (!ignoreSpacingBefore || !currentCompositeColumn.IgnoreSpacingBefore || ((ISpaceable)nextElement).PaddingTop != 0))
                        {
                            yLine -= ((ISpaceable)nextElement).SpacingBefore;
                        }
                        if (simulate)
                        {
                            if (nextElement is PdfPTable)
                            {
                                currentCompositeColumn.AddElement(new PdfPTable((PdfPTable)nextElement));
                            }
                            else
                            {
                                currentCompositeColumn.AddElement(nextElement);
                            }
                        }
                        else
                        {
                            currentCompositeColumn.AddElement(nextElement);
                        }

                        if (yLine > minYLine)
                        {
                            currentCompositeColumn.SetSimpleColumn(floatLeftX, yLine, floatRightX, minYLine);
                        }
                        else
                        {
                            currentCompositeColumn.SetSimpleColumn(floatLeftX, yLine, floatRightX, minY);
                        }

                        currentCompositeColumn.FilledWidth = 0;

                        status = currentCompositeColumn.Go(simulate);
                        if (yLine > minYLine && (floatLeftX > leftX || floatRightX < rightX) &&
                            (status & ColumnText.NO_MORE_TEXT) == 0)
                        {
                            yLine       = minYLine;
                            floatLeftX  = leftX;
                            floatRightX = rightX;
                            if (leftWidth != 0 && rightWidth != 0)
                            {
                                filledWidth = rightX - leftX;
                            }
                            else
                            {
                                if (leftWidth > filledWidth)
                                {
                                    filledWidth = leftWidth;
                                }
                                if (rightWidth > filledWidth)
                                {
                                    filledWidth = rightWidth;
                                }
                            }

                            leftWidth  = 0;
                            rightWidth = 0;
                            if (simulate && nextElement is PdfPTable)
                            {
                                currentCompositeColumn.AddElement(new PdfPTable((PdfPTable)nextElement));
                            }

                            currentCompositeColumn.SetSimpleColumn(floatLeftX, yLine, floatRightX, minY);
                            status   = currentCompositeColumn.Go(simulate);
                            minYLine = currentCompositeColumn.YLine + currentCompositeColumn.Descender;
                            yLine    = minYLine;
                            if (currentCompositeColumn.FilledWidth > filledWidth)
                            {
                                filledWidth = currentCompositeColumn.FilledWidth;
                            }
                        }
                        else
                        {
                            if (rightWidth > 0)
                            {
                                rightWidth += currentCompositeColumn.FilledWidth;
                            }
                            else if (leftWidth > 0)
                            {
                                leftWidth += currentCompositeColumn.FilledWidth;
                            }
                            else if (currentCompositeColumn.FilledWidth > filledWidth)
                            {
                                filledWidth = currentCompositeColumn.FilledWidth;
                            }
                            minYLine = Math.Min(currentCompositeColumn.YLine + currentCompositeColumn.Descender, minYLine);
                            yLine    = currentCompositeColumn.YLine + currentCompositeColumn.Descender;
                        }

                        if ((status & ColumnText.NO_MORE_TEXT) == 0)
                        {
                            if (!simulate)
                            {
                                floatingElements.InsertRange(0, currentCompositeColumn.CompositeElements);
                                currentCompositeColumn.CompositeElements.Clear();
                            }
                            else
                            {
                                floatingElements.Insert(0, nextElement);
                                currentCompositeColumn.SetText(null);
                            }
                            break;
                        }
                        else
                        {
                            currentCompositeColumn.SetText(null);
                        }
                    }
                }
                if (nextElement is Paragraph)
                {
                    Paragraph p = (Paragraph)nextElement;
                    foreach (IElement e in p)
                    {
                        if (e is WritableDirectElement)
                        {
                            WritableDirectElement writableElement = (WritableDirectElement)e;
                            if (writableElement.DirectElemenType == WritableDirectElement.DIRECT_ELEMENT_TYPE_HEADER && !simulate)
                            {
                                PdfWriter   writer = compositeColumn.Canvas.PdfWriter;
                                PdfDocument doc    = compositeColumn.Canvas.PdfDocument;

                                // here is used a little hack:
                                // writableElement.write() method implementation uses PdfWriter.getVerticalPosition() to create PdfDestination (see com.itextpdf.tool.xml.html.Header),
                                // so here we are adjusting document's currentHeight in order to make getVerticalPosition() return value corresponding to real current position
                                float savedHeight = doc.currentHeight;
                                doc.currentHeight = doc.Top - yLine - doc.indentation.indentTop;
                                writableElement.Write(writer, doc);
                                doc.currentHeight = savedHeight;
                            }
                        }
                    }
                }
                if (ignoreSpacingBefore && nextElement.Chunks.Count == 0)
                {
                    if (nextElement is Paragraph)
                    {
                        Paragraph p = (Paragraph)nextElement;
                        IElement  e = p[0];
                        if (e is WritableDirectElement)
                        {
                            WritableDirectElement writableDirectElement = (WritableDirectElement)e;
                            if (writableDirectElement.DirectElemenType != WritableDirectElement.DIRECT_ELEMENT_TYPE_HEADER)
                            {
                                ignoreSpacingBefore = false;
                            }
                        }
                    }
                    else if (nextElement is ISpaceable)
                    {
                        ignoreSpacingBefore = false;
                    }
                }
                else
                {
                    ignoreSpacingBefore = false;
                }
            }

            if (leftWidth != 0 && rightWidth != 0)
            {
                filledWidth = rightX - leftX;
            }
            else
            {
                if (leftWidth > filledWidth)
                {
                    filledWidth = leftWidth;
                }
                if (rightWidth > filledWidth)
                {
                    filledWidth = rightWidth;
                }
            }

            yLine       = minYLine;
            floatLeftX  = leftX;
            floatRightX = rightX;

            return(status);
        }
Example #14
0
        public void CreateTaggedPdf19() {
            InitializeDocument("19");

            PdfDiv div = new PdfDiv();
            writer.DirectContent.OpenMCBlock(div);

            PdfArtifact artifact = new PdfArtifact();
            artifact.Type = new PdfString("Rectangle");
            writer.DirectContent.OpenMCBlock(artifact);
            writer.DirectContent.SetColorFill(BaseColor.RED);
            writer.DirectContent.Rectangle(100, 100, 400, 400);
            writer.DirectContent.Fill();
            writer.DirectContent.CloseMCBlock(artifact);

            writer.DirectContent.CloseMCBlock(div);

            document.Close();
            CompareResults("19");
        }
    public void btnpdf_Click(object sende, EventArgs e)
    {
        divck.Visible = false;
        using (var pdfDoc = new Document(PageSize.A4))
        {
            pdfDoc.SetMargins(15f, 15f, 30f, 0f);
            var pdfWriter = PdfWriter.GetInstance(pdfDoc, new FileStream(Server.MapPath("~/PDFS/TestM10.pdf"), FileMode.Create));
            pdfDoc.Open();
            var table1 = new PdfPTable(4);

            var table3 = new PdfPTable(3);
            var table2 = new PdfPTable(2);
            var table0 = new PdfPTable(1);
            PdfPCell cell = new PdfPCell();

            table1.WidthPercentage = 100;
            table1.HorizontalAlignment = 0;
            table1.SpacingAfter = 10;
            float[] sglTblHdWidths = new float[3];
            sglTblHdWidths[0] = 150f;
            sglTblHdWidths[1] = 200f;
            sglTblHdWidths[2] = 385f;

            DataSet ds = getData();

            int a = ds.Tables[0].Rows.Count % 20;
            int b = 20 - a;

            for (int j = 0; j < b; j++)
            {
                ds.Tables[0].Rows.Add();

            }

            var cou = ds.Tables[0].Rows.Count;
            int pgnum = 0;

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                iTextSharp.text.Font fntTableFont1 = FontFactory.GetFont("Calibri", 12, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
                iTextSharp.text.Font fntTableFont = FontFactory.GetFont("Calibri", 8, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
                iTextSharp.text.Font fntTableFont2 = FontFactory.GetFont("Calibri", 8, iTextSharp.text.Font.BOLD, BaseColor.BLACK);

                //PdfPCell CellZero = new PdfPCell(new Phrase((ds.Tables[0].Rows[i]["LeadId"].ToString()), fntTableFont));
                PdfPTable stab = new PdfPTable(1);
                //stab.DefaultCell.Border = Rectangle.NO_BORDER;
                //stab.DefaultCell.BorderColor = iTextSharp.text.BaseColor.WHITE;

                PdfPCell c1 = new PdfPCell();
                if (ds.Tables[0].Rows[i]["LeadId"].ToString() != "")
                {
                    c1 = new PdfPCell(new Phrase(("#" + ds.Tables[0].Rows[i]["LeadId"].ToString()), fntTableFont));
                    c1.HorizontalAlignment = Element.ALIGN_LEFT;
                    c1.Border = Rectangle.NO_BORDER;
                    stab.AddCell(c1);
                }
                else
                {
                    c1 = new PdfPCell(new Phrase((""), fntTableFont));
                    c1.HorizontalAlignment = Element.ALIGN_LEFT;
                    c1.Border = Rectangle.NO_BORDER;
                    stab.AddCell(c1);
                    table1.DefaultCell.Border = Rectangle.NO_BORDER;
                }

                PdfPCell c2 = new PdfPCell();
                if (ds.Tables[0].Rows[i]["Model"].ToString() != "")
                {
                    c2 = new PdfPCell(new Phrase((ds.Tables[0].Rows[i]["Model"].ToString().Substring(0, ds.Tables[0].Rows[i]["Model"].ToString().Length - 1)), fntTableFont));
                    c2.HorizontalAlignment = Element.ALIGN_CENTER;
                    c2.Border = Rectangle.NO_BORDER;
                    stab.AddCell(c2);
                }
                else
                {
                    c2 = new PdfPCell(new Phrase((""), fntTableFont));
                    c2.HorizontalAlignment = Element.ALIGN_CENTER;
                    c2.Border = Rectangle.NO_BORDER;
                    stab.AddCell(c2);
                    table1.DefaultCell.Border = Rectangle.NO_BORDER;
                }

                PdfPCell c3 = new PdfPCell();
                if (ds.Tables[0].Rows[i]["LeadId"].ToString() != "")
                {
                    c3 = new PdfPCell(new Phrase((ds.Tables[0].Rows[i]["PhoneNumer"].ToString()), fntTableFont1));
                    c3.HorizontalAlignment = Element.ALIGN_CENTER;
                    c3.Border = Rectangle.NO_BORDER;
                    stab.AddCell(c3);
                }
                else
                {

                    c3 = new PdfPCell(new Phrase((""), fntTableFont1));
                    c3.HorizontalAlignment = Element.ALIGN_CENTER;
                    c3.Border = Rectangle.NO_BORDER;
                    stab.AddCell(c3);
                    table1.DefaultCell.Border = Rectangle.NO_BORDER;
                }

                PdfPTable c4 = new PdfPTable(2);
                c4.DefaultCell.Border = Rectangle.NO_BORDER;
                if (ds.Tables[0].Rows[i]["LeadId"].ToString() != "")
                {

                    string str1 = ds.Tables[0].Rows[i]["State"].ToString();
                    string str2 = "$" + ds.Tables[0].Rows[i]["price"].ToString();
                    if (str2.Contains('.'))
                        str2 = str2.Substring(0, str2.LastIndexOf('.'));
                    // str1=str1+str2;
                    PdfPCell pce1 = new PdfPCell(new Phrase(str1, fntTableFont));
                    PdfPCell pce2 = new PdfPCell(new Phrase(str2, fntTableFont));
                    pce1.HorizontalAlignment = Element.ALIGN_LEFT;
                    pce2.HorizontalAlignment = Element.ALIGN_RIGHT;
                    pce1.Border = Rectangle.NO_BORDER;
                    pce2.Border = Rectangle.NO_BORDER;
                    c4.AddCell(pce1);
                    c4.AddCell(pce2);
                    stab.DefaultCell.Border = Rectangle.NO_BORDER;
                    stab.AddCell(c4);
                }
                else
                {

                    string str1 = "";
                    string str2 = "";

                    PdfPCell pce1 = new PdfPCell(new Phrase(str1, fntTableFont));
                    PdfPCell pce2 = new PdfPCell(new Phrase(str2, fntTableFont));
                    pce1.HorizontalAlignment = Element.ALIGN_LEFT;
                    pce2.HorizontalAlignment = Element.ALIGN_RIGHT;
                    pce1.Border = Rectangle.NO_BORDER;
                    pce2.Border = Rectangle.NO_BORDER;
                    stab.DefaultCell.Border = Rectangle.NO_BORDER;
                    c4.AddCell(pce1);
                    c4.AddCell(pce2);
                    stab.AddCell(c4);
                    table1.DefaultCell.Border = Rectangle.NO_BORDER;
                }

                PdfPCell c5 = new PdfPCell();
                if (ds.Tables[0].Rows[i]["LeadId"].ToString() != "")
                {
                    c5 = new PdfPCell(new Phrase(((ds.Tables[0].Rows[i]["Descriptions"].ToString() + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n")), fntTableFont));
                    c5.HorizontalAlignment = Element.ALIGN_CENTER;
                    c5.Border = Rectangle.NO_BORDER;
                    stab.AddCell(c5);
                }
                else
                {
                    c5 = new PdfPCell(new Phrase((("")), fntTableFont));
                    c5.HorizontalAlignment = Element.ALIGN_CENTER;
                    c5.Border = Rectangle.NO_BORDER;
                    stab.AddCell(c5);
                    table1.DefaultCell.Border = Rectangle.NO_BORDER;
                }

                table1.AddCell(stab);

                if (i > 0 && table1.Rows.Count % 5 == 0 && table1.Rows.Count > 0)
                {
                    //if (k == 0)
                    //{
                    //    pdfDoc.Add(new Phrase("LEADS FOR WINNERS                          " + ddlstates.SelectedValue.ToString() + "                                           " + DateTime.Now.ToString("dd/MM/yyyy")));
                    //    k = k + 1;
                    //}

                    PdfPTable ptab = new PdfPTable(3);

                    ptab.WidthPercentage = 100;
                    PdfPCell pcell1 = new PdfPCell(new Phrase("LEADS FOR WINNERS"));
                    PdfPCell pcell2 = new PdfPCell(new Phrase(ddlstates.SelectedValue.ToString()));
                    PdfPCell pcell3 = new PdfPCell(new Phrase(DateTime.Now.ToString("dd/MM/yyyy") + "   " + "Page :" + (++pgnum).ToString()));
                    pcell1.HorizontalAlignment = Rectangle.ALIGN_LEFT;
                    pcell1.HorizontalAlignment = Rectangle.ALIGN_MIDDLE;
                    pcell2.HorizontalAlignment = Rectangle.ALIGN_CENTER;
                    pcell3.HorizontalAlignment = Rectangle.ALIGN_RIGHT;
                    pcell1.Border = Rectangle.NO_BORDER;
                    pcell2.Border = Rectangle.NO_BORDER;
                    pcell3.Border = Rectangle.NO_BORDER;
                    ptab.DefaultCell.Border = Rectangle.NO_BORDER;
                    ptab.AddCell(pcell1);
                    ptab.AddCell(pcell2);
                    ptab.AddCell(pcell3);

                    PdfDiv pdiv = new PdfDiv();
                    pdiv.ContentHeight = 100;
                    pdiv.Content.Add(new Chunk("      "));
                    pdfDoc.Add(ptab);
                    pdfDoc.Add(pdiv);
                    pdfDoc.Add(table1);

                    pdfDoc.NewPage();
                    table1.Rows.Clear();

                    c = c + 20;

                }

                table1.WidthPercentage = 100;
            }
        }
        Session["Type"] = "PDF";

        Response.Redirect("DownloadPage1Test.aspx");
    }
 internal void AddDiv(PdfDiv div) {
     if (floatingElements == null) {
         floatingElements = new List<IElement>();
     }
     floatingElements.Add(div);
 }
Example #17
0
        private int FloatingLayout(List <IElement> floatingElements, bool simulate)
        {
            int   status     = ColumnText.NO_MORE_TEXT;
            float minYLine   = yLine;
            float leftWidth  = 0;
            float rightWidth = 0;

            ColumnText currentCompositeColumn = compositeColumn;

            if (simulate)
            {
                currentCompositeColumn = ColumnText.Duplicate(compositeColumn);
            }

            bool ignoreSpacingBefore = maxY == yLine;

            while (floatingElements.Count > 0)
            {
                IElement nextElement = floatingElements[0];
                floatingElements.RemoveAt(0);
                if (nextElement is PdfDiv)
                {
                    PdfDiv floatingElement = (PdfDiv)nextElement;
                    status = floatingElement.Layout(compositeColumn.Canvas, useAscender, true, floatLeftX, minY, floatRightX, yLine);
                    if ((status & ColumnText.NO_MORE_TEXT) == 0)
                    {
                        yLine       = minYLine;
                        floatLeftX  = leftX;
                        floatRightX = rightX;
                        status      = floatingElement.Layout(compositeColumn.Canvas, useAscender, true, floatLeftX, minY, floatRightX, yLine);
                        if ((status & ColumnText.NO_MORE_TEXT) == 0)
                        {
                            floatingElements.Insert(0, floatingElement);
                            break;
                        }
                    }
                    if (floatingElement.Float == PdfDiv.FloatType.LEFT)
                    {
                        status      = floatingElement.Layout(compositeColumn.Canvas, useAscender, simulate, floatLeftX, minY, floatRightX, yLine);
                        floatLeftX += floatingElement.getActualWidth();
                        leftWidth  += floatingElement.getActualWidth();
                    }
                    else if (floatingElement.Float == PdfDiv.FloatType.RIGHT)
                    {
                        status       = floatingElement.Layout(compositeColumn.Canvas, useAscender, simulate, floatRightX - floatingElement.getActualWidth() - 0.01f, minY, floatRightX, yLine);
                        floatRightX -= floatingElement.getActualWidth();
                        rightWidth  += floatingElement.getActualWidth();
                    }
                    minYLine = Math.Min(minYLine, yLine - floatingElement.getActualHeight());
                }
                else
                {
                    if (minY > minYLine)
                    {
                        status = ColumnText.NO_MORE_COLUMN;
                        floatingElements.Insert(0, nextElement);
                        if (currentCompositeColumn != null)
                        {
                            currentCompositeColumn.SetText(null);
                        }
                        break;
                    }
                    else
                    {
                        if (nextElement is ISpaceable && (!ignoreSpacingBefore || !currentCompositeColumn.IgnoreSpacingBefore || ((ISpaceable)nextElement).PaddingTop != 0))
                        {
                            yLine -= ((ISpaceable)nextElement).SpacingBefore;
                        }
                        if (simulate)
                        {
                            if (nextElement is PdfPTable)
                            {
                                currentCompositeColumn.AddElement(new PdfPTable((PdfPTable)nextElement));
                            }
                            else
                            {
                                currentCompositeColumn.AddElement(nextElement);
                            }
                        }
                        else
                        {
                            currentCompositeColumn.AddElement(nextElement);
                        }

                        if (yLine > minYLine)
                        {
                            currentCompositeColumn.SetSimpleColumn(floatLeftX, yLine, floatRightX, minYLine);
                        }
                        else
                        {
                            currentCompositeColumn.SetSimpleColumn(floatLeftX, yLine, floatRightX, minY);
                        }

                        currentCompositeColumn.FilledWidth = 0;

                        status = currentCompositeColumn.Go(simulate);
                        if (yLine > minYLine && (floatLeftX > leftX || floatRightX < rightX) &&
                            (status & ColumnText.NO_MORE_TEXT) == 0)
                        {
                            yLine       = minYLine;
                            floatLeftX  = leftX;
                            floatRightX = rightX;
                            if (leftWidth != 0 && rightWidth != 0)
                            {
                                filledWidth = rightX - leftX;
                            }
                            else
                            {
                                if (leftWidth > filledWidth)
                                {
                                    filledWidth = leftWidth;
                                }
                                if (rightWidth > filledWidth)
                                {
                                    filledWidth = rightWidth;
                                }
                            }

                            leftWidth  = 0;
                            rightWidth = 0;
                            if (simulate && nextElement is PdfPTable)
                            {
                                currentCompositeColumn.AddElement(new PdfPTable((PdfPTable)nextElement));
                            }

                            currentCompositeColumn.SetSimpleColumn(floatLeftX, yLine, floatRightX, minY);
                            status   = currentCompositeColumn.Go(simulate);
                            minYLine = currentCompositeColumn.YLine + currentCompositeColumn.Descender;
                            yLine    = minYLine;
                            if (currentCompositeColumn.FilledWidth > filledWidth)
                            {
                                filledWidth = currentCompositeColumn.FilledWidth;
                            }
                        }
                        else
                        {
                            if (rightWidth > 0)
                            {
                                rightWidth += currentCompositeColumn.FilledWidth;
                            }
                            else if (leftWidth > 0)
                            {
                                leftWidth += currentCompositeColumn.FilledWidth;
                            }
                            else if (currentCompositeColumn.FilledWidth > filledWidth)
                            {
                                filledWidth = currentCompositeColumn.FilledWidth;
                            }
                            minYLine = Math.Min(currentCompositeColumn.YLine + currentCompositeColumn.Descender, minYLine);
                            yLine    = currentCompositeColumn.YLine + currentCompositeColumn.Descender;
                        }

                        if ((status & ColumnText.NO_MORE_TEXT) == 0)
                        {
                            if (!simulate)
                            {
                                floatingElements.InsertRange(0, currentCompositeColumn.CompositeElements);
                                currentCompositeColumn.CompositeElements.Clear();
                            }
                            else
                            {
                                floatingElements.Insert(0, nextElement);
                                currentCompositeColumn.SetText(null);
                            }
                            break;
                        }
                        else
                        {
                            currentCompositeColumn.SetText(null);
                        }
                    }
                }
                if (ignoreSpacingBefore && nextElement.Chunks.Count == 0)
                {
                    if (nextElement is Paragraph)
                    {
                        Paragraph p = (Paragraph)nextElement;
                        IElement  e = p[0];
                        if (e is WritableDirectElement)
                        {
                            WritableDirectElement writableDirectElement = (WritableDirectElement)e;
                            if (writableDirectElement.DirectElemenType != WritableDirectElement.DIRECT_ELEMENT_TYPE_HEADER)
                            {
                                ignoreSpacingBefore = false;
                            }
                        }
                    }
                    else if (nextElement is ISpaceable)
                    {
                        ignoreSpacingBefore = false;
                    }
                }
                else
                {
                    ignoreSpacingBefore = false;
                }
            }

            if (leftWidth != 0 && rightWidth != 0)
            {
                filledWidth = rightX - leftX;
            }
            else
            {
                if (leftWidth > filledWidth)
                {
                    filledWidth = leftWidth;
                }
                if (rightWidth > filledWidth)
                {
                    filledWidth = rightWidth;
                }
            }

            yLine       = minYLine;
            floatLeftX  = leftX;
            floatRightX = rightX;

            return(status);
        }