Example #1
0
 private static void ApplyInlineLevelLayoutAttributes(AbstractRenderer renderer, PdfDictionary attributes) {
     float? textRise = renderer.GetPropertyAsFloat(Property.TEXT_RISE);
     if (textRise != null && textRise != 0) {
         attributes.Put(PdfName.BaselineShift, new PdfNumber((float)textRise));
     }
     Object underlines = renderer.GetProperty<Object>(Property.UNDERLINE);
     if (underlines != null) {
         float? fontSize = renderer.GetPropertyAsFloat(Property.FONT_SIZE);
         Underline underline = null;
         if (underlines is IList && !((IList<Object>)underlines).IsEmpty() && ((IList)underlines)[0] is Underline) {
             // in standard attributes only one text decoration could be described for an element. That's why we take only the first underline from the list.
             underline = (Underline)((IList)underlines)[0];
         }
         else {
             if (underlines is Underline) {
                 underline = (Underline)underlines;
             }
         }
         if (underline != null) {
             attributes.Put(PdfName.TextDecorationType, underline.GetYPosition((float)fontSize) > 0 ? PdfName.LineThrough
                  : PdfName.Underline);
             if (underline.GetColor() is DeviceRgb) {
                 attributes.Put(PdfName.TextDecorationColor, new PdfArray(underline.GetColor().GetColorValue()));
             }
             attributes.Put(PdfName.TextDecorationThickness, new PdfNumber(underline.GetThickness((float)fontSize)));
         }
     }
 }
Example #2
0
        private static void ApplyPaddingAttribute(AbstractRenderer renderer, PdfDictionary attributes)
        {
            float[] paddings = new float[] { (float)renderer.GetPropertyAsFloat(Property.PADDING_TOP), (float)renderer
                                             .GetPropertyAsFloat(Property.PADDING_RIGHT), (float)renderer.GetPropertyAsFloat(Property.PADDING_BOTTOM
                                                                                                                             ), (float)renderer.GetPropertyAsFloat(Property.PADDING_LEFT) };
            PdfObject padding = null;

            if (paddings[0] == paddings[1] && paddings[0] == paddings[2] && paddings[0] == paddings[3])
            {
                if (paddings[0] != 0)
                {
                    padding = new PdfNumber(paddings[0]);
                }
            }
            else
            {
                PdfArray paddingArray  = new PdfArray();
                int[]    paddingsOrder = new int[] { 0, 1, 2, 3 };
                //TODO set depending on writing direction
                foreach (int i in paddingsOrder)
                {
                    paddingArray.Add(new PdfNumber(paddings[i]));
                }
                padding = paddingArray;
            }
            if (padding != null)
            {
                attributes.Put(PdfName.Padding, padding);
            }
        }
Example #3
0
        private static void ApplyIllustrationLayoutAttributes(AbstractRenderer renderer, PdfDictionary attributes)
        {
            Rectangle bbox = renderer.GetOccupiedArea().GetBBox();

            attributes.Put(PdfName.BBox, new PdfArray(bbox));
            UnitValue width = renderer.GetProperty <UnitValue>(Property.WIDTH);

            if (width != null && width.IsPointValue())
            {
                attributes.Put(PdfName.Width, new PdfNumber(width.GetValue()));
            }
            else
            {
                attributes.Put(PdfName.Width, new PdfNumber(bbox.GetWidth()));
            }
            float?height = renderer.GetPropertyAsFloat(Property.HEIGHT);

            if (height != null)
            {
                attributes.Put(PdfName.Height, new PdfNumber((float)height));
            }
            else
            {
                attributes.Put(PdfName.Height, new PdfNumber(bbox.GetHeight()));
            }
        }
        private static void ApplyInlineLevelLayoutAttributes(AbstractRenderer renderer, PdfDictionary attributes)
        {
            float?textRise = renderer.GetPropertyAsFloat(Property.TEXT_RISE);

            if (textRise != null && textRise != 0)
            {
                attributes.Put(PdfName.BaselineShift, new PdfNumber((float)textRise));
            }
            Object underlines = renderer.GetProperty <Object>(Property.UNDERLINE);

            if (underlines != null)
            {
                UnitValue fontSize = renderer.GetPropertyAsUnitValue(Property.FONT_SIZE);
                if (!fontSize.IsPointValue())
                {
                    ILog logger = LogManager.GetLogger(typeof(AccessibleAttributesApplier));
                    logger.Error(MessageFormatUtil.Format(iText.IO.LogMessageConstant.PROPERTY_IN_PERCENTS_NOT_SUPPORTED, Property
                                                          .FONT_SIZE));
                }
                Underline underline = null;
                if (underlines is IList && ((IList)underlines).Count > 0 && ((IList)underlines)[0] is Underline)
                {
                    // in standard attributes only one text decoration could be described for an element. That's why we take only the first underline from the list.
                    underline = (Underline)((IList)underlines)[0];
                }
                else
                {
                    if (underlines is Underline)
                    {
                        underline = (Underline)underlines;
                    }
                }
                if (underline != null)
                {
                    attributes.Put(PdfName.TextDecorationType, underline.GetYPosition(fontSize.GetValue()) > 0 ? PdfName.LineThrough
                         : PdfName.Underline);
                    if (underline.GetColor() is DeviceRgb)
                    {
                        attributes.Put(PdfName.TextDecorationColor, new PdfArray(underline.GetColor().GetColorValue()));
                    }
                    attributes.Put(PdfName.TextDecorationThickness, new PdfNumber(underline.GetThickness(fontSize.GetValue()))
                                   );
                }
            }
        }
        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));
                }
            }
        }
Example #6
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));
         }
     }
 }