Beispiel #1
0
 /// <summary>
 /// Creates a new
 /// <see cref="LiTagWorker"/>
 /// instance.
 /// </summary>
 /// <param name="element">the element</param>
 /// <param name="context">the context</param>
 public LiTagWorker(IElementNode element, ProcessorContext context)
 {
     listItem = new ListItem();
     if (element.GetAttribute(AttributeConstants.VALUE) != null)
     {
         int?indexValue = (int?)CssUtils.ParseInteger(element.GetAttribute(AttributeConstants.VALUE));
         if (indexValue != null)
         {
             listItem.SetListSymbolOrdinalValue(indexValue.Value);
         }
     }
     if (!(context.GetState().Top() is UlOlTagWorker))
     {
         listItem.SetProperty(Property.LIST_SYMBOL_POSITION, ListSymbolPosition.INSIDE);
         float em = CssUtils.ParseAbsoluteLength(element.GetStyles().Get(CssConstants.FONT_SIZE));
         if (TagConstants.LI.Equals(element.Name()))
         {
             ListStyleApplierUtil.SetDiscStyle(listItem, em);
         }
         else
         {
             listItem.SetProperty(Property.LIST_SYMBOL, null);
         }
         list = new List();
         list.Add(listItem);
     }
     inlineHelper = new WaitingInlineElementsHelper(element.GetStyles().Get(CssConstants.WHITE_SPACE), element.
                                                    GetStyles().Get(CssConstants.TEXT_TRANSFORM));
     AccessiblePropHelper.TrySetLangAttribute(listItem, element);
 }
Beispiel #2
0
        /// <summary>
        /// Creates a new
        /// <see cref="ColgroupTagWorker"/>
        /// instance.
        /// </summary>
        /// <param name="element">the element</param>
        /// <param name="context">the context</param>
        public ColgroupTagWorker(IElementNode element, ProcessorContext context)
        {
            int?span = CssUtils.ParseInteger(element.GetAttribute(AttributeConstants.SPAN));

            colgroup = new ColgroupWrapper(span != null ? (int)span : 1);
            colgroup.SetLang(element.GetAttribute(AttributeConstants.LANG));
        }
        /// <summary>Resolves a normalized form name.</summary>
        /// <param name="name">the proposed name</param>
        /// <returns>the resolved name</returns>
        private String ResolveNormalizedFormName(String name)
        {
            int separatorIndex = name.LastIndexOf(NAME_COUNT_SEPARATOR);
            int?nameIndex      = null;

            if (separatorIndex != -1 && separatorIndex < name.Length)
            {
                String numberString = name.Substring(separatorIndex + 1);
                nameIndex = CssUtils.ParseInteger(numberString);
                //Treat number as index only in case it is positive
                if (nameIndex != null && nameIndex > 0)
                {
                    name = name.JSubstring(0, separatorIndex);
                }
            }
            int?savedIndex  = names.Get(name);
            int indexToSave = savedIndex != null ? savedIndex.Value + 1 : 0;

            if (nameIndex != null && indexToSave < nameIndex.Value)
            {
                indexToSave = nameIndex.Value;
            }
            names.Put(name, indexToSave);
            return(indexToSave == 0 ? name : name + NAME_COUNT_SEPARATOR + indexToSave.ToString());
        }
Beispiel #4
0
        /// <summary>Applies column styles.</summary>
        /// <param name="node">the node</param>
        /// <param name="rowColHelper">the helper class to keep track of the position inside the table</param>
        private void ApplyColStyles(INode node, RowColHelper rowColHelper)
        {
            int          col;
            IElementNode element;

            foreach (INode child in node.ChildNodes())
            {
                if (child is IElementNode)
                {
                    element = (IElementNode)child;
                    if (TagConstants.TR.Equals(element.Name()))
                    {
                        ApplyColStyles(element, rowColHelper);
                        rowColHelper.NewRow();
                    }
                    else
                    {
                        if (TagConstants.TH.Equals(element.Name()) || TagConstants.TD.Equals(element.Name()))
                        {
                            int?colspan = CssUtils.ParseInteger(element.GetAttribute(AttributeConstants.COLSPAN));
                            int?rowspan = CssUtils.ParseInteger(element.GetAttribute(AttributeConstants.ROWSPAN));
                            colspan = colspan != null ? colspan : 1;
                            rowspan = rowspan != null ? rowspan : 1;
                            col     = rowColHelper.MoveToNextEmptyCol();
                            if (GetColWrapper(col) != null)
                            {
                                ColWrapper colWrapper = GetColWrapper(col);
                                if (colWrapper.GetCellCssProps() != null)
                                {
                                    element.AddAdditionalHtmlStyles(colWrapper.GetCellCssProps());
                                }
                                String elemLang = element.GetAttribute(AttributeConstants.LANG);
                                String trLang   = null;
                                if (node is IElementNode)
                                {
                                    trLang = ((IElementNode)node).GetAttribute(AttributeConstants.LANG);
                                }
                                if (trLang == null && colWrapper.GetLang() != null && elemLang == null)
                                {
                                    element.GetAttributes().SetAttribute(AttributeConstants.LANG, colWrapper.GetLang());
                                }
                            }
                            rowColHelper.UpdateCurrentPosition((int)colspan, (int)rowspan);
                        }
                        else
                        {
                            ApplyColStyles(child, rowColHelper);
                        }
                    }
                }
            }
        }
Beispiel #5
0
        /// <summary>
        /// Creates a new
        /// <see cref="TdTagWorker"/>
        /// instance.
        /// </summary>
        /// <param name="element">the element</param>
        /// <param name="context">the context</param>
        public TdTagWorker(IElementNode element, ProcessorContext context)
        {
            int?colspan = CssUtils.ParseInteger(element.GetAttribute(AttributeConstants.COLSPAN));
            int?rowspan = CssUtils.ParseInteger(element.GetAttribute(AttributeConstants.ROWSPAN));

            colspan = colspan != null ? colspan : 1;
            rowspan = rowspan != null ? rowspan : 1;
            cell    = new Cell((int)rowspan, (int)colspan);
            cell.SetPadding(0);
            inlineHelper = new WaitingInlineElementsHelper(element.GetStyles().Get(CssConstants.WHITE_SPACE), element.
                                                           GetStyles().Get(CssConstants.TEXT_TRANSFORM));
            display = element.GetStyles() != null?element.GetStyles().Get(CssConstants.DISPLAY) : null;
        }
Beispiel #6
0
 /// <summary>
 /// Creates a new
 /// <see cref="UlOlTagWorker"/>
 /// instance.
 /// </summary>
 /// <param name="element">the element</param>
 /// <param name="context">the context</param>
 public UlOlTagWorker(IElementNode element, ProcessorContext context)
 {
     list = new List().SetListSymbol("");
     //In the case of an ordered list, see if the start attribute can be found
     if (element.GetAttribute(AttributeConstants.START) != null)
     {
         int?startValue = CssUtils.ParseInteger(element.GetAttribute(AttributeConstants.START));
         if (startValue != null)
         {
             list.SetItemStartIndex((int)startValue);
         }
     }
     inlineHelper = new WaitingInlineElementsHelper(element.GetStyles().Get(CssConstants.WHITE_SPACE), element.
                                                    GetStyles().Get(CssConstants.TEXT_TRANSFORM));
 }
 /// <summary>
 /// Creates a new
 /// <see cref="SelectTagWorker"/>
 /// instance.
 /// </summary>
 /// <param name="element">the element</param>
 /// <param name="context">the context</param>
 public SelectTagWorker(IElementNode element, ProcessorContext context) {
     String name = context.GetFormFieldNameResolver().ResolveFormName(element.GetAttribute(AttributeConstants.NAME
         ));
     bool multipleAttr = element.GetAttribute(AttributeConstants.MULTIPLE) != null;
     int? sizeAttr = CssUtils.ParseInteger(element.GetAttribute(AttributeConstants.SIZE));
     int size = GetSelectSize(sizeAttr, multipleAttr);
     if (size > 1 || multipleAttr) {
         selectElement = new ListBoxField(name, size, multipleAttr);
     }
     else {
         selectElement = new ComboBoxField(name);
     }
     String lang = element.GetAttribute(AttributeConstants.LANG);
     selectElement.SetProperty(Html2PdfProperty.FORM_ACCESSIBILITY_LANGUAGE, lang);
     selectElement.SetProperty(Html2PdfProperty.FORM_FIELD_FLATTEN, !context.IsCreateAcroForm());
     display = element.GetStyles() != null ? element.GetStyles().Get(CssConstants.DISPLAY) : null;
 }
Beispiel #8
0
        /// <summary>Processes counters.</summary>
        /// <param name="cssProps">the CSS properties</param>
        /// <param name="context">the processor context</param>
        /// <param name="scope">the scope</param>
        public static void ProcessCounters(IDictionary <String, String> cssProps, CssContext context, INode scope)
        {
            String counterReset = cssProps.Get(CssConstants.COUNTER_RESET);

            if (counterReset != null)
            {
                CssCounterManager counterManager = context.GetCounterManager();
                String[]          @params        = iText.IO.Util.StringUtil.Split(counterReset, " ");
                for (int i = 0; i < @params.Length; i++)
                {
                    String counterName = @params[i];
                    int?   possibleCounterValue;
                    if (i + 1 < @params.Length && (possibleCounterValue = CssUtils.ParseInteger(@params[i + 1])) != null)
                    {
                        counterManager.ResetCounter(counterName, (int)possibleCounterValue, scope);
                        i++;
                    }
                    else
                    {
                        counterManager.ResetCounter(counterName, scope);
                    }
                }
            }
            String counterIncrement = cssProps.Get(CssConstants.COUNTER_INCREMENT);

            if (counterIncrement != null)
            {
                CssCounterManager counterManager = context.GetCounterManager();
                String[]          @params        = iText.IO.Util.StringUtil.Split(counterIncrement, " ");
                for (int i = 0; i < @params.Length; i++)
                {
                    String counterName = @params[i];
                    int?   possibleIncrementValue;
                    if (i + 1 < @params.Length && (possibleIncrementValue = CssUtils.ParseInteger(@params[i + 1])) != null)
                    {
                        counterManager.IncrementCounter(counterName, (int)possibleIncrementValue, scope);
                        i++;
                    }
                    else
                    {
                        counterManager.IncrementCounter(counterName, scope);
                    }
                }
            }
        }
        /// <summary>
        /// Creates a new
        /// <see cref="TextAreaTagWorker"/>
        /// instance.
        /// </summary>
        /// <param name="element">the element</param>
        /// <param name="context">the context</param>
        public TextAreaTagWorker(IElementNode element, ProcessorContext context)
        {
            String name = element.GetAttribute(AttributeConstants.ID);

            if (name == null)
            {
                name = DEFAULT_TEXTAREA_NAME;
            }
            name     = context.GetFormFieldNameResolver().ResolveFormName(name);
            textArea = new TextArea(name);
            int?rows = CssUtils.ParseInteger(element.GetAttribute(AttributeConstants.ROWS));
            int?cols = CssUtils.ParseInteger(element.GetAttribute(AttributeConstants.COLS));

            textArea.SetProperty(Html2PdfProperty.FORM_FIELD_ROWS, rows);
            textArea.SetProperty(Html2PdfProperty.FORM_FIELD_COLS, cols);
            textArea.SetProperty(Html2PdfProperty.FORM_FIELD_FLATTEN, !context.IsCreateAcroForm());
            textArea.SetProperty(Html2PdfProperty.FORM_ACCESSIBILITY_LANGUAGE, element.GetAttribute(AttributeConstants
                                                                                                    .LANG));
            String placeholder = element.GetAttribute(AttributeConstants.PLACEHOLDER);

            if (null != placeholder)
            {
                Paragraph paragraph;
                if (String.IsNullOrEmpty(placeholder))
                {
                    paragraph = new Paragraph();
                }
                else
                {
                    if (String.IsNullOrEmpty(placeholder.Trim()))
                    {
                        paragraph = new Paragraph("\u00A0");
                    }
                    else
                    {
                        paragraph = new Paragraph(placeholder);
                    }
                }
                textArea.SetPlaceholder(paragraph.SetMargin(0));
            }
            display = element.GetStyles() != null?element.GetStyles().Get(CssConstants.DISPLAY) : null;
        }
        /// <summary>Applies column styles.</summary>
        /// <param name="node">the node</param>
        /// <param name="rowColHelper">the helper class to keep track of the position inside the table</param>
        private void ApplyColStyles(INode node, RowColHelper rowColHelper)
        {
            int          col;
            IElementNode element;

            foreach (INode child in node.ChildNodes())
            {
                if (child is IElementNode)
                {
                    element = (IElementNode)child;
                    if (element.Name().Equals(TagConstants.TR))
                    {
                        ApplyColStyles(element, rowColHelper);
                        rowColHelper.NewRow();
                    }
                    else
                    {
                        if (element.Name().Equals(TagConstants.TH) || element.Name().Equals(TagConstants.TD))
                        {
                            int?colspan = CssUtils.ParseInteger(element.GetAttribute(AttributeConstants.COLSPAN));
                            int?rowspan = CssUtils.ParseInteger(element.GetAttribute(AttributeConstants.ROWSPAN));
                            colspan = colspan != null ? colspan : 1;
                            rowspan = rowspan != null ? rowspan : 1;
                            col     = rowColHelper.MoveToNextEmptyCol();
                            if (GetColWrapper(col) != null && GetColWrapper(col).GetCellCssProps() != null)
                            {
                                element.AddAdditionalHtmlStyles(GetColWrapper(col).GetCellCssProps());
                            }
                            rowColHelper.UpdateCurrentPosition((int)colspan, (int)rowspan);
                        }
                        else
                        {
                            ApplyColStyles(child, rowColHelper);
                        }
                    }
                }
            }
        }
 /// <summary>Applies orphans and widows properties to an element.</summary>
 /// <param name="cssProps">the CSS properties</param>
 /// <param name="element">to which the property will be applied.</param>
 public static void ApplyOrphansAndWidows(IDictionary <String, String> cssProps, IPropertyContainer element)
 {
     if (cssProps != null)
     {
         if (cssProps.ContainsKey(CssConstants.WIDOWS))
         {
             int?minWidows = CssUtils.ParseInteger(cssProps.Get(CssConstants.WIDOWS));
             if (minWidows != null && minWidows > 0)
             {
                 element.SetProperty(Property.WIDOWS_CONTROL, new ParagraphWidowsControl(minWidows.Value, MAX_LINES_TO_MOVE
                                                                                         , OVERFLOW_PARAGRAPH_ON_VIOLATION));
             }
         }
         if (cssProps.ContainsKey(CssConstants.ORPHANS))
         {
             int?minOrphans = CssUtils.ParseInteger(cssProps.Get(CssConstants.ORPHANS));
             if (minOrphans != null && minOrphans > 0)
             {
                 element.SetProperty(Property.ORPHANS_CONTROL, new ParagraphOrphansControl(minOrphans.Value));
             }
         }
     }
 }
Beispiel #12
0
        /// <summary>
        /// Tries to match a
        /// <see cref="MediaDeviceDescription"/>.
        /// </summary>
        /// <param name="deviceDescription">the device description</param>
        /// <returns>true, if successful</returns>
        public virtual bool Matches(MediaDeviceDescription deviceDescription)
        {
            switch (feature)
            {
            case MediaFeature.COLOR: {
                int?val = CssUtils.ParseInteger(value);
                if (minPrefix)
                {
                    return(val != null && deviceDescription.GetBitsPerComponent() >= val);
                }
                else
                {
                    if (maxPrefix)
                    {
                        return(val != null && deviceDescription.GetBitsPerComponent() <= val);
                    }
                    else
                    {
                        return(val == null?deviceDescription.GetBitsPerComponent() != 0 : val == deviceDescription.GetBitsPerComponent
                                   ());
                    }
                }
                goto case MediaFeature.COLOR_INDEX;
            }

            case MediaFeature.COLOR_INDEX: {
                int?val = CssUtils.ParseInteger(value);
                if (minPrefix)
                {
                    return(val != null && deviceDescription.GetColorIndex() >= val);
                }
                else
                {
                    if (maxPrefix)
                    {
                        return(val != null && deviceDescription.GetColorIndex() <= val);
                    }
                    else
                    {
                        return(val == null?deviceDescription.GetColorIndex() != 0 : val == deviceDescription.GetColorIndex());
                    }
                }
                goto case MediaFeature.ASPECT_RATIO;
            }

            case MediaFeature.ASPECT_RATIO: {
                int[] aspectRatio = CssUtils.ParseAspectRatio(value);
                if (minPrefix)
                {
                    return(aspectRatio != null && aspectRatio[0] * deviceDescription.GetHeight() >= aspectRatio[1] * deviceDescription
                           .GetWidth());
                }
                else
                {
                    if (maxPrefix)
                    {
                        return(aspectRatio != null && aspectRatio[0] * deviceDescription.GetHeight() <= aspectRatio[1] * deviceDescription
                               .GetWidth());
                    }
                    else
                    {
                        return(aspectRatio != null && CssUtils.CompareFloats(aspectRatio[0] * deviceDescription.GetHeight(), aspectRatio
                                                                             [1] * deviceDescription.GetWidth()));
                    }
                }
                goto case MediaFeature.GRID;
            }

            case MediaFeature.GRID: {
                int?val = CssUtils.ParseInteger(value);
                return(val != null && val == 0 && !deviceDescription.IsGrid() || deviceDescription.IsGrid());
            }

            case MediaFeature.SCAN: {
                return(Object.Equals(value, deviceDescription.GetScan()));
            }

            case MediaFeature.ORIENTATION: {
                return(Object.Equals(value, deviceDescription.GetOrientation()));
            }

            case MediaFeature.MONOCHROME: {
                int?val = CssUtils.ParseInteger(value);
                if (minPrefix)
                {
                    return(val != null && deviceDescription.GetMonochrome() >= val);
                }
                else
                {
                    if (maxPrefix)
                    {
                        return(val != null && deviceDescription.GetMonochrome() <= val);
                    }
                    else
                    {
                        return(val == null?deviceDescription.GetMonochrome() > 0 : val == deviceDescription.GetMonochrome());
                    }
                }
                goto case MediaFeature.HEIGHT;
            }

            case MediaFeature.HEIGHT: {
                float val = ParseAbsoluteLength(value);
                if (minPrefix)
                {
                    return(deviceDescription.GetHeight() >= val);
                }
                else
                {
                    if (maxPrefix)
                    {
                        return(deviceDescription.GetHeight() <= val);
                    }
                    else
                    {
                        return(deviceDescription.GetHeight() > 0);
                    }
                }
                goto case MediaFeature.WIDTH;
            }

            case MediaFeature.WIDTH: {
                float val = ParseAbsoluteLength(value);
                if (minPrefix)
                {
                    return(deviceDescription.GetWidth() >= val);
                }
                else
                {
                    if (maxPrefix)
                    {
                        return(deviceDescription.GetWidth() <= val);
                    }
                    else
                    {
                        return(deviceDescription.GetWidth() > 0);
                    }
                }
                goto case MediaFeature.RESOLUTION;
            }

            case MediaFeature.RESOLUTION: {
                float val = CssUtils.ParseResolution(value);
                if (minPrefix)
                {
                    return(deviceDescription.GetResolution() >= val);
                }
                else
                {
                    if (maxPrefix)
                    {
                        return(deviceDescription.GetResolution() <= val);
                    }
                    else
                    {
                        return(deviceDescription.GetResolution() > 0);
                    }
                }
                goto default;
            }

            default: {
                return(false);
            }
            }
        }
Beispiel #13
0
        /// <summary>
        /// Creates a new
        /// <see cref="InputTagWorker"/>
        /// instance.
        /// </summary>
        /// <param name="element">the element</param>
        /// <param name="context">the context</param>
        public InputTagWorker(IElementNode element, ProcessorContext context)
        {
            String lang      = element.GetAttribute(AttributeConstants.LANG);
            String inputType = element.GetAttribute(AttributeConstants.TYPE);

            if (!AttributeConstants.INPUT_TYPE_VALUES.Contains(inputType))
            {
                if (null != inputType && 0 != inputType.Length)
                {
                    ILog logger = LogManager.GetLogger(typeof(iText.Html2pdf.Attach.Impl.Tags.InputTagWorker));
                    logger.Warn(MessageFormatUtil.Format(iText.Html2pdf.LogMessageConstant.INPUT_TYPE_IS_INVALID, inputType));
                }
                inputType = AttributeConstants.TEXT;
            }
            String value = element.GetAttribute(AttributeConstants.VALUE);
            String name  = context.GetFormFieldNameResolver().ResolveFormName(element.GetAttribute(AttributeConstants.NAME
                                                                                                   ));

            // Default input type is text
            if (inputType == null || AttributeConstants.TEXT.Equals(inputType) || AttributeConstants.EMAIL.Equals(inputType
                                                                                                                  ) || AttributeConstants.PASSWORD.Equals(inputType) || AttributeConstants.NUMBER.Equals(inputType))
            {
                int?size = CssUtils.ParseInteger(element.GetAttribute(AttributeConstants.SIZE));
                formElement = new InputField(name);
                value       = PreprocessInputValue(value, inputType);
                // process placeholder instead
                String placeholder = element.GetAttribute(AttributeConstants.PLACEHOLDER);
                if (null != placeholder)
                {
                    Paragraph paragraph;
                    if (String.IsNullOrEmpty(placeholder))
                    {
                        paragraph = new Paragraph();
                    }
                    else
                    {
                        if (String.IsNullOrEmpty(placeholder.Trim()))
                        {
                            paragraph = new Paragraph("\u00A0");
                        }
                        else
                        {
                            paragraph = new Paragraph(placeholder);
                        }
                    }
                    ((InputField)formElement).SetPlaceholder(paragraph.SetMargin(0));
                }
                formElement.SetProperty(Html2PdfProperty.FORM_FIELD_VALUE, value);
                formElement.SetProperty(Html2PdfProperty.FORM_FIELD_SIZE, size);
                if (AttributeConstants.PASSWORD.Equals(inputType))
                {
                    formElement.SetProperty(Html2PdfProperty.FORM_FIELD_PASSWORD_FLAG, true);
                }
            }
            else
            {
                if (AttributeConstants.SUBMIT.Equals(inputType) || AttributeConstants.BUTTON.Equals(inputType))
                {
                    formElement = new InputButton(name);
                    formElement.SetProperty(Html2PdfProperty.FORM_FIELD_VALUE, value);
                }
                else
                {
                    if (AttributeConstants.CHECKBOX.Equals(inputType))
                    {
                        formElement = new CheckBox(name);
                        String @checked = element.GetAttribute(AttributeConstants.CHECKED);
                        if (null != @checked)
                        {
                            formElement.SetProperty(Html2PdfProperty.FORM_FIELD_CHECKED, @checked);
                        }
                    }
                    else
                    {
                        // has attribute == is checked
                        if (AttributeConstants.RADIO.Equals(inputType))
                        {
                            formElement = new Radio(name);
                            String radioGroupName = element.GetAttribute(AttributeConstants.NAME);
                            formElement.SetProperty(Html2PdfProperty.FORM_FIELD_VALUE, radioGroupName);
                            String @checked = element.GetAttribute(AttributeConstants.CHECKED);
                            if (null != @checked)
                            {
                                context.GetRadioCheckResolver().CheckField(radioGroupName, (Radio)formElement);
                                formElement.SetProperty(Html2PdfProperty.FORM_FIELD_CHECKED, @checked);
                            }
                        }
                        else
                        {
                            // has attribute == is checked
                            ILog logger = LogManager.GetLogger(typeof(iText.Html2pdf.Attach.Impl.Tags.InputTagWorker));
                            logger.Error(MessageFormatUtil.Format(iText.Html2pdf.LogMessageConstant.INPUT_TYPE_IS_NOT_SUPPORTED, inputType
                                                                  ));
                        }
                    }
                }
            }
            if (formElement != null)
            {
                formElement.SetProperty(Html2PdfProperty.FORM_FIELD_FLATTEN, !context.IsCreateAcroForm());
                formElement.SetProperty(Html2PdfProperty.FORM_ACCESSIBILITY_LANGUAGE, lang);
            }
            display = element.GetStyles() != null?element.GetStyles().Get(CssConstants.DISPLAY) : null;
        }