Beispiel #1
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);
     }
 }
        public virtual void AccessibleAttributesInsertionTest01()
        {
            PdfReader      reader   = new PdfReader(sourceFolder + "taggedDocumentWithAttributes.pdf");
            PdfWriter      writer   = new PdfWriter(destinationFolder + "accessibleAttributesInsertionTest01.pdf");
            PdfDocument    document = new PdfDocument(reader, writer);
            TagTreePointer pointer  = new TagTreePointer(document);
            // 2 attributes
            AccessibilityProperties properties = pointer.MoveToKid(0).GetProperties();
            PdfStructureAttributes  testAttr   = new PdfStructureAttributes("test");

            testAttr.AddIntAttribute("N", 4);
            properties.AddAttributes(testAttr);
            testAttr = new PdfStructureAttributes("test");
            testAttr.AddIntAttribute("N", 0);
            properties.AddAttributes(0, testAttr);
            testAttr = new PdfStructureAttributes("test");
            testAttr.AddIntAttribute("N", 5);
            properties.AddAttributes(4, testAttr);
            testAttr = new PdfStructureAttributes("test");
            testAttr.AddIntAttribute("N", 2);
            properties.AddAttributes(2, testAttr);
            try {
                properties.AddAttributes(10, testAttr);
                NUnit.Framework.Assert.Fail();
            }
            catch (Exception e) {
                NUnit.Framework.Assert.IsTrue(ExceptionUtil.IsOutOfRange(e));
            }
            document.Close();
            CompareResult("accessibleAttributesInsertionTest01.pdf", "cmp_accessibleAttributesInsertionTest01.pdf", "diffAttributes01_"
                          );
        }
Beispiel #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);
            }
        }
Beispiel #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);
            }
        }
Beispiel #5
0
 public virtual AccessibilityProperties GetAccessibilityProperties()
 {
     if (tagProperties == null)
     {
         tagProperties = new AccessibilityProperties();
     }
     return(tagProperties);
 }
Beispiel #6
0
 public override AccessibilityProperties GetAccessibilityProperties()
 {
     if (tagProperties == null)
     {
         tagProperties = new AccessibilityProperties();
     }
     return(tagProperties);
 }
Beispiel #7
0
 public static void TrySetLangAttribute(IAccessibleElement accessibleElement, String lang)
 {
     if (lang != null)
     {
         AccessibilityProperties properties = accessibleElement.GetAccessibilityProperties();
         if (properties.GetLanguage() == null)
         {
             properties.SetLanguage(lang);
         }
     }
 }
Beispiel #8
0
 /// <summary>The same layout element instance can be added several times to the document.</summary>
 /// <remarks>
 /// The same layout element instance can be added several times to the document.
 /// In that case it will already have attributes which belong to the previous positioning on the page, and because of
 /// that we want to remove those old irrelevant attributes.
 /// </remarks>
 private static void RemoveSameAttributesTypeIfPresent(AccessibilityProperties properties, PdfName attributesType
     ) {
     IList<PdfDictionary> attributesList = properties.GetAttributesList();
     int i;
     for (i = 0; i < attributesList.Count; i++) {
         PdfDictionary attr = attributesList[i];
         if (attributesType.Equals(attr.Get(PdfName.O))) {
             break;
         }
     }
     if (i < attributesList.Count) {
         attributesList.JRemoveAt(i);
     }
 }
        public virtual void TagTreePointerTest08()
        {
            PdfWriter writer = new PdfWriter(destinationFolder + "tagTreePointerTest08.pdf").SetCompressionLevel(CompressionConstants
                                                                                                                 .NO_COMPRESSION);
            PdfDocument             document   = new PdfDocument(new PdfReader(sourceFolder + "taggedDocument2.pdf"), writer);
            TagTreePointer          pointer    = new TagTreePointer(document);
            AccessibilityProperties properties = pointer.MoveToKid(StandardRoles.DIV).GetProperties();
            String language = properties.GetLanguage();

            NUnit.Framework.Assert.AreEqual("en-Us", language);
            properties.SetLanguage("EN-GB");
            pointer.MoveToRoot().MoveToKid(2, StandardRoles.P).GetProperties().SetRole(StandardRoles.H6);
            document.Close();
            CompareResult("tagTreePointerTest08.pdf", "cmp_tagTreePointerTest08.pdf", "diff08_");
        }
Beispiel #10
0
        public override void ProcessEnd(IElementNode element, ProcessorContext context)
        {
            base.ProcessEnd(element, context);
            IPropertyContainer elementResult = base.GetElementResult();

            if (elementResult is IAccessibleElement)
            {
                ((IAccessibleElement)elementResult).GetAccessibilityProperties().SetRole(StandardRoles.TH);
                if (context.GetPdfDocument() == null || context.GetPdfDocument().IsTagged())
                {
                    String scope = element.GetAttribute(AttributeConstants.SCOPE);
                    AccessibilityProperties properties = ((IAccessibleElement)elementResult).GetAccessibilityProperties();
                    PdfDictionary           attributes = new PdfDictionary();
                    attributes.Put(PdfName.O, PdfName.Table);
                    if (scope != null && (AttributeConstants.ROW.EqualsIgnoreCase(scope) || AttributeConstants.ROWGROUP.EqualsIgnoreCase
                                              (scope)))
                    {
                        attributes.Put(PdfName.Scope, PdfName.Row);
                        properties.AddAttributes(new PdfStructureAttributes(attributes));
                    }
                    else
                    {
                        if (scope != null && (AttributeConstants.COL.EqualsIgnoreCase(scope) || AttributeConstants.COLGROUP.EqualsIgnoreCase
                                                  (scope)))
                        {
                            attributes.Put(PdfName.Scope, PdfName.Column);
                            properties.AddAttributes(new PdfStructureAttributes(attributes));
                        }
                        else
                        {
                            ILog logger = LogManager.GetLogger(typeof(iText.Html2pdf.Attach.Impl.Tags.ThTagWorker));
                            logger.Warn(MessageFormatUtil.Format(iText.Html2pdf.LogMessageConstant.NOT_SUPPORTED_TH_SCOPE_TYPE, scope)
                                        );
                        }
                    }
                }
            }
        }