public static PdfStructureAttributes GetTableAttributes(AbstractRenderer renderer, TagTreePointer taggingPointer
                                                                )
        {
            IRoleMappingResolver resolvedMapping = ResolveMappingToStandard(taggingPointer);

            if (resolvedMapping == null || !StandardRoles.TD.Equals(resolvedMapping.GetRole()) && !StandardRoles.TH.Equals
                    (resolvedMapping.GetRole()))
            {
                return(null);
            }
            PdfDictionary attributes = new PdfDictionary();

            attributes.Put(PdfName.O, PdfName.Table);
            if (renderer.GetModelElement() is Cell)
            {
                Cell cell = (Cell)renderer.GetModelElement();
                if (cell.GetRowspan() != 1)
                {
                    attributes.Put(PdfName.RowSpan, new PdfNumber(cell.GetRowspan()));
                }
                if (cell.GetColspan() != 1)
                {
                    attributes.Put(PdfName.ColSpan, new PdfNumber(cell.GetColspan()));
                }
            }
            return(attributes.Size() > 1 ? new PdfStructureAttributes(attributes) : null);
        }
Пример #2
0
 public static void ApplyLayoutAttributes(PdfName role, AbstractRenderer renderer, PdfDocument doc) {
     if (!(renderer.GetModelElement() is IAccessibleElement)) {
         return;
     }
     int tagType = PdfStructElem.IdentifyType(doc, role);
     PdfDictionary attributes = new PdfDictionary();
     PdfName attributesType = PdfName.Layout;
     attributes.Put(PdfName.O, attributesType);
     PdfDictionary roleMap = doc.GetStructTreeRoot().GetRoleMap();
     if (roleMap.ContainsKey(role)) {
         role = roleMap.GetAsName(role);
     }
     //TODO WritingMode attribute applying when needed
     ApplyCommonLayoutAttributes(renderer, attributes);
     if (tagType == PdfStructElem.BlockLevel) {
         ApplyBlockLevelLayoutAttributes(role, renderer, attributes, doc);
     }
     if (tagType == PdfStructElem.InlineLevel) {
         ApplyInlineLevelLayoutAttributes(renderer, attributes);
     }
     if (tagType == PdfStructElem.Illustration) {
         ApplyIllustrationLayoutAttributes(renderer, attributes);
     }
     if (attributes.Size() > 1) {
         AccessibilityProperties properties = ((IAccessibleElement)renderer.GetModelElement()).GetAccessibilityProperties
             ();
         RemoveSameAttributesTypeIfPresent(properties, attributesType);
         properties.AddAttributes(attributes);
     }
 }
Пример #3
0
        public static void ApplyTableAttributes(AbstractRenderer renderer)
        {
            if (!(renderer.GetModelElement() is IAccessibleElement))
            {
                return;
            }
            IAccessibleElement accessibleElement = (IAccessibleElement)renderer.GetModelElement();
            PdfDictionary      attributes        = new PdfDictionary();
            PdfName            attributesType    = PdfName.Table;

            attributes.Put(PdfName.O, attributesType);
            if (accessibleElement is Cell)
            {
                Cell cell = (Cell)accessibleElement;
                if (cell.GetRowspan() != 1)
                {
                    attributes.Put(PdfName.RowSpan, new PdfNumber(cell.GetRowspan()));
                }
                if (cell.GetColspan() != 1)
                {
                    attributes.Put(PdfName.ColSpan, new PdfNumber(cell.GetColspan()));
                }
            }
            if (attributes.Size() > 1)
            {
                AccessibilityProperties properties = accessibleElement.GetAccessibilityProperties();
                RemoveSameAttributesTypeIfPresent(properties, attributesType);
                properties.AddAttributes(attributes);
            }
        }
Пример #4
0
        public static void ApplyListAttributes(AbstractRenderer renderer)
        {
            if (!(renderer.GetModelElement() is List))
            {
                return;
            }
            PdfDictionary attributes     = new PdfDictionary();
            PdfName       attributesType = PdfName.List;

            attributes.Put(PdfName.O, attributesType);
            Object listSymbol = renderer.GetProperty <Object>(Property.LIST_SYMBOL);

            if (listSymbol is ListNumberingType)
            {
                ListNumberingType numberingType = (ListNumberingType)listSymbol;
                attributes.Put(PdfName.ListNumbering, TransformNumberingTypeToName(numberingType));
            }
            if (attributes.Size() > 1)
            {
                AccessibilityProperties properties = ((IAccessibleElement)renderer.GetModelElement()).GetAccessibilityProperties
                                                         ();
                RemoveSameAttributesTypeIfPresent(properties, attributesType);
                properties.AddAttributes(attributes);
            }
        }
Пример #5
0
 private static void ApplyCommonLayoutAttributes(AbstractRenderer renderer, PdfDictionary attributes) {
     Background background = renderer.GetProperty<Background>(Property.BACKGROUND);
     if (background != null && background.GetColor() is DeviceRgb) {
         attributes.Put(PdfName.BackgroundColor, new PdfArray(background.GetColor().GetColorValue()));
     }
     //TODO NOTE: applying border attributes for cells is temporarily turned off on purpose. Remove this 'if' in future.
     // The reason is that currently, we can't distinguish if all cells have same border style or not.
     // Therefore for every cell in every table we have to write the same border attributes, which creates lots of clutter.
     if (!(renderer.GetModelElement() is Cell)) {
         ApplyBorderAttributes(renderer, attributes);
     }
     ApplyPaddingAttribute(renderer, attributes);
     Color color = renderer.GetPropertyAsColor(Property.FONT_COLOR);
     if (color != null && color is DeviceRgb) {
         attributes.Put(PdfName.Color, new PdfArray(color.GetColorValue()));
     }
 }
        private static void ApplyBlockLevelLayoutAttributes(String role, AbstractRenderer renderer, PdfDictionary
                                                            attributes)
        {
            UnitValue[] margins = new UnitValue[] { renderer.GetPropertyAsUnitValue(Property.MARGIN_TOP), renderer.GetPropertyAsUnitValue
                                                        (Property.MARGIN_BOTTOM), renderer.GetPropertyAsUnitValue(Property.MARGIN_LEFT), renderer.GetPropertyAsUnitValue
                                                        (Property.MARGIN_RIGHT) };
            int[] marginsOrder = new int[] { 0, 1, 2, 3 };
            //TODO set depending on writing direction
            UnitValue spaceBefore = margins[marginsOrder[0]];

            if (spaceBefore != null)
            {
                if (!spaceBefore.IsPointValue())
                {
                    ILog logger = LogManager.GetLogger(typeof(AccessibleAttributesApplier));
                    logger.Error(MessageFormatUtil.Format(iText.IO.LogMessageConstant.PROPERTY_IN_PERCENTS_NOT_SUPPORTED, Property
                                                          .MARGIN_TOP));
                }
                if (0 != spaceBefore.GetValue())
                {
                    attributes.Put(PdfName.SpaceBefore, new PdfNumber(spaceBefore.GetValue()));
                }
            }
            UnitValue spaceAfter = margins[marginsOrder[1]];

            if (spaceAfter != null)
            {
                if (!spaceAfter.IsPointValue())
                {
                    ILog logger = LogManager.GetLogger(typeof(AccessibleAttributesApplier));
                    logger.Error(MessageFormatUtil.Format(iText.IO.LogMessageConstant.PROPERTY_IN_PERCENTS_NOT_SUPPORTED, Property
                                                          .MARGIN_BOTTOM));
                }
                if (0 != spaceAfter.GetValue())
                {
                    attributes.Put(PdfName.SpaceAfter, new PdfNumber(spaceAfter.GetValue()));
                }
            }
            UnitValue startIndent = margins[marginsOrder[2]];

            if (startIndent != null)
            {
                if (!startIndent.IsPointValue())
                {
                    ILog logger = LogManager.GetLogger(typeof(AccessibleAttributesApplier));
                    logger.Error(MessageFormatUtil.Format(iText.IO.LogMessageConstant.PROPERTY_IN_PERCENTS_NOT_SUPPORTED, Property
                                                          .MARGIN_LEFT));
                }
                if (0 != startIndent.GetValue())
                {
                    attributes.Put(PdfName.StartIndent, new PdfNumber(startIndent.GetValue()));
                }
            }
            UnitValue endIndent = margins[marginsOrder[3]];

            if (endIndent != null)
            {
                if (!endIndent.IsPointValue())
                {
                    ILog logger = LogManager.GetLogger(typeof(AccessibleAttributesApplier));
                    logger.Error(MessageFormatUtil.Format(iText.IO.LogMessageConstant.PROPERTY_IN_PERCENTS_NOT_SUPPORTED, Property
                                                          .MARGIN_RIGHT));
                }
                if (0 != endIndent.GetValue())
                {
                    attributes.Put(PdfName.EndIndent, new PdfNumber(endIndent.GetValue()));
                }
            }
            float?firstLineIndent = renderer.GetPropertyAsFloat(Property.FIRST_LINE_INDENT);

            if (firstLineIndent != null && firstLineIndent != 0)
            {
                attributes.Put(PdfName.TextIndent, new PdfNumber((float)firstLineIndent));
            }
            TextAlignment?textAlignment = renderer.GetProperty <TextAlignment?>(Property.TEXT_ALIGNMENT);

            if (textAlignment != null && (!role.Equals(StandardRoles.TH) && !role.Equals(StandardRoles.TD)))
            {
                //for table cells there is an InlineAlign attribute (see below)
                attributes.Put(PdfName.TextAlign, TransformTextAlignmentValueToName(textAlignment));
            }
            // attributes are applied only on the first renderer
            if (renderer.isLastRendererForModelElement)
            {
                Rectangle bbox = renderer.GetOccupiedArea().GetBBox();
                attributes.Put(PdfName.BBox, new PdfArray(bbox));
            }
            if (role.Equals(StandardRoles.TH) || role.Equals(StandardRoles.TD) || role.Equals(StandardRoles.TABLE))
            {
                // For large tables the width can be changed from flush to flush so the Width attribute shouldn't be applied.
                // There are also technical issues with large tables widths being explicitly set as property on element during layouting
                // (even if user didn't explcitly specfied it). This is required due to specificity of large elements implementation,
                // however in this case we cannot distinguish layout-specific and user-specified width properties.
                if (!(renderer is TableRenderer) || ((Table)renderer.GetModelElement()).IsComplete())
                {
                    UnitValue width = renderer.GetProperty <UnitValue>(Property.WIDTH);
                    if (width != null && width.IsPointValue())
                    {
                        attributes.Put(PdfName.Width, new PdfNumber(width.GetValue()));
                    }
                }
                UnitValue height = renderer.GetProperty <UnitValue>(Property.HEIGHT);
                if (height != null && height.IsPointValue())
                {
                    attributes.Put(PdfName.Height, new PdfNumber(height.GetValue()));
                }
            }
            if (role.Equals(StandardRoles.TH) || role.Equals(StandardRoles.TD))
            {
                HorizontalAlignment?horizontalAlignment = renderer.GetProperty <HorizontalAlignment?>(Property.HORIZONTAL_ALIGNMENT
                                                                                                      );
                if (horizontalAlignment != null)
                {
                    attributes.Put(PdfName.BlockAlign, TransformBlockAlignToName(horizontalAlignment));
                }
                if (textAlignment != null && (textAlignment != TextAlignment.JUSTIFIED && textAlignment != TextAlignment.JUSTIFIED_ALL
                                              ))
                {
                    //there is no justified alignment for InlineAlign attribute
                    attributes.Put(PdfName.InlineAlign, TransformTextAlignmentValueToName(textAlignment));
                }
            }
        }
Пример #7
0
 private static void ApplyBlockLevelLayoutAttributes(PdfName role, AbstractRenderer renderer, PdfDictionary
      attributes, PdfDocument doc) {
     float?[] margins = new float?[] { renderer.GetPropertyAsFloat(Property.MARGIN_TOP), renderer.GetPropertyAsFloat
         (Property.MARGIN_BOTTOM), renderer.GetPropertyAsFloat(Property.MARGIN_LEFT), renderer.GetPropertyAsFloat
         (Property.MARGIN_RIGHT) };
     int[] marginsOrder = new int[] { 0, 1, 2, 3 };
     //TODO set depending on writing direction
     float? spaceBefore = margins[marginsOrder[0]];
     if (spaceBefore != null && spaceBefore != 0) {
         attributes.Put(PdfName.SpaceBefore, new PdfNumber((float)spaceBefore));
     }
     float? spaceAfter = margins[marginsOrder[1]];
     if (spaceAfter != null && spaceAfter != 0) {
         attributes.Put(PdfName.SpaceAfter, new PdfNumber((float)spaceAfter));
     }
     float? startIndent = margins[marginsOrder[2]];
     if (startIndent != null && startIndent != 0) {
         attributes.Put(PdfName.StartIndent, new PdfNumber((float)startIndent));
     }
     float? endIndent = margins[marginsOrder[3]];
     if (endIndent != null && endIndent != 0) {
         attributes.Put(PdfName.EndIndent, new PdfNumber((float)endIndent));
     }
     float? firstLineIndent = renderer.GetProperty<float?>(Property.FIRST_LINE_INDENT);
     if (firstLineIndent != null && firstLineIndent != 0) {
         attributes.Put(PdfName.TextIndent, new PdfNumber((float)firstLineIndent));
     }
     TextAlignment? textAlignment = renderer.GetProperty<TextAlignment?>(Property.TEXT_ALIGNMENT);
     if (textAlignment != null && (!role.Equals(PdfName.TH) && !role.Equals(PdfName.TD))) {
         //for table cells there is an InlineAlign attribute (see below)
         attributes.Put(PdfName.TextAlign, TransformTextAlignmentValueToName(textAlignment));
     }
     bool connectedToTag = doc.GetTagStructureContext().IsElementConnectedToTag((IAccessibleElement)renderer.GetModelElement
         ());
     bool elementIsOnSinglePage = !connectedToTag && renderer.isLastRendererForModelElement;
     if (elementIsOnSinglePage) {
         Rectangle bbox = renderer.GetOccupiedArea().GetBBox();
         attributes.Put(PdfName.BBox, new PdfArray(bbox));
     }
     if (role.Equals(PdfName.TH) || role.Equals(PdfName.TD) || role.Equals(PdfName.Table)) {
         UnitValue width = renderer.GetProperty<UnitValue>(Property.WIDTH);
         if (width != null && width.IsPointValue()) {
             attributes.Put(PdfName.Width, new PdfNumber(width.GetValue()));
         }
         float? height = renderer.GetPropertyAsFloat(Property.HEIGHT);
         if (height != null) {
             attributes.Put(PdfName.Height, new PdfNumber((float)height));
         }
     }
     if (role.Equals(PdfName.TH) || role.Equals(PdfName.TD)) {
         HorizontalAlignment? horizontalAlignment = renderer.GetProperty<HorizontalAlignment?>(Property.HORIZONTAL_ALIGNMENT
             );
         if (horizontalAlignment != null) {
             attributes.Put(PdfName.BlockAlign, TransformBlockAlignToName(horizontalAlignment));
         }
         if (textAlignment != null && (textAlignment != TextAlignment.JUSTIFIED && textAlignment != TextAlignment.JUSTIFIED_ALL
             )) {
             //there is no justified alignment for InlineAlign attribute
             attributes.Put(PdfName.InlineAlign, TransformTextAlignmentValueToName(textAlignment));
         }
     }
 }