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_"
                          );
        }
        public virtual void TagTreePointerTest02()
        {
            FileStream fos    = new FileStream(destinationFolder + "tagTreePointerTest02.pdf", FileMode.Create);
            PdfWriter  writer = new PdfWriter(fos);

            writer.SetCompressionLevel(CompressionConstants.NO_COMPRESSION);
            PdfDocument document = new PdfDocument(writer);

            document.SetTagged();
            PdfPage        page       = document.AddNewPage();
            TagTreePointer tagPointer = new TagTreePointer(document);

            tagPointer.SetPageForTagging(page);
            PdfCanvas canvas = new PdfCanvas(page);

            canvas.BeginText();
            PdfFont standardFont = PdfFontFactory.CreateFont(StandardFonts.COURIER);

            canvas.SetFontAndSize(standardFont, 24).SetTextMatrix(1, 0, 0, 1, 32, 512);
            PdfStructureAttributes attributes = new PdfStructureAttributes("random attributes");

            attributes.AddTextAttribute("hello", "world");
            tagPointer.AddTag(StandardRoles.P).AddTag(StandardRoles.SPAN).GetProperties().SetActualText("Actual text for span is: Hello World"
                                                                                                        ).SetLanguage("en-GB").AddAttributes(attributes);
            canvas.OpenTag(tagPointer.GetTagReference()).ShowText("Hello ").CloseTag();
            canvas.SetFontAndSize(standardFont, 30).OpenTag(tagPointer.GetTagReference()).ShowText("World").CloseTag();
            canvas.EndText().Release();
            page.Flush();
            document.Close();
            CompareResult("tagTreePointerTest02.pdf", "cmp_tagTreePointerTest02.pdf", "diff02_");
        }
Example #3
0
        public override AccessibilityProperties AddAttributes(int index, PdfStructureAttributes attributes)
        {
            if (attributes == null)
            {
                return(this);
            }
            PdfObject attributesObject   = GetBackingElem().GetAttributes(false);
            PdfObject combinedAttributes = AccessibilityPropertiesToStructElem.CombineAttributesList(attributesObject,
                                                                                                     index, JavaCollectionsUtil.SingletonList(attributes), GetBackingElem().GetPdfObject().GetAsNumber(PdfName
                                                                                                                                                                                                       .R));

            GetBackingElem().SetAttributes(combinedAttributes);
            return(this);
        }
 public override AccessibilityProperties AddAttributes(int index, PdfStructureAttributes attributes)
 {
     if (attributes != null)
     {
         if (index > 0)
         {
             attributesList.Add(index, attributes);
         }
         else
         {
             attributesList.Add(attributes);
         }
     }
     return(this);
 }
        public virtual void AccessibleAttributesInsertionTest02()
        {
            PdfReader              reader       = new PdfReader(sourceFolder + "taggedDocumentWithAttributes.pdf");
            PdfWriter              writer       = new PdfWriter(destinationFolder + "accessibleAttributesInsertionTest02.pdf");
            PdfDocument            document     = new PdfDocument(reader, writer);
            TagTreePointer         pointer      = new TagTreePointer(document);
            PdfStructureAttributes testAttrDict = new PdfStructureAttributes("test");

            // 1 attribute array
            pointer.MoveToKid(1).GetProperties().AddAttributes(testAttrDict);
            pointer.MoveToRoot();
            // 3 attributes
            pointer.MoveToKid(2).GetProperties().AddAttributes(testAttrDict);
            pointer.MoveToRoot();
            // 1 attribute dictionary
            pointer.MoveToKid(0).MoveToKid(StandardRoles.LI).MoveToKid(StandardRoles.LBODY).GetProperties().AddAttributes
                (testAttrDict);
            // no attributes
            pointer.MoveToKid(StandardRoles.P).MoveToKid(StandardRoles.SPAN).GetProperties().AddAttributes(testAttrDict
                                                                                                           );
            document.Close();
            CompareResult("accessibleAttributesInsertionTest02.pdf", "cmp_accessibleAttributesInsertionTest02.pdf", "diffAttributes02_"
                          );
        }
Example #6
0
 public override AccessibilityProperties AddAttributes(PdfStructureAttributes attributes)
 {
     return(AddAttributes(-1, attributes));
 }
Example #7
0
 public virtual AccessibilityProperties AddAttributes(int index, PdfStructureAttributes attributes)
 {
     return(this);
 }
Example #8
0
        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDocument = new PdfDocument(new PdfWriter(dest));

            pdfDocument.SetTagged();

            Document doc = new Document(pdfDocument);

            Table table = new Table(UnitValue.CreatePercentArray(3)).UseAllAvailableWidth();

            // Initialize ID strings beforehand. Every ID should be unique across the document
            PdfString[] headersId =
            {
                // Since '/ID' is a `byte string` according to specification we are not passing
                // encoding to the constructor of the PdfString
                new PdfString("header_id_0"), new PdfString("header_id_1"), new PdfString("header_id_2")
            };

            PdfName     idTreeName = new PdfName("IDTree");
            PdfNameTree idTree     = new PdfNameTree(pdfDocument.GetCatalog(), idTreeName);

            for (int i = 0; i < 3; ++i)
            {
                Cell c = new Cell().Add(new Paragraph("Header " + (i + 1)));
                c.GetAccessibilityProperties().SetRole(StandardRoles.TH);
                table.AddHeaderCell(c);
                PdfString headerId = headersId[i];

                // Use custom renderer for cell header element in order to add ID to its tag
                CellRenderer renderer = new StructIdCellRenderer(c, doc, headerId, idTree);
                c.SetNextRenderer(renderer);
            }

            List <TaggingHintKey> colSpanHints = new List <TaggingHintKey>();

            for (int i = 0; i < 4; i++)
            {
                Cell c;
                if (i < 3)
                {
                    c = new Cell().Add(new Paragraph((i + 1).ToString()));
                }
                else
                {
                    // Colspan creation
                    c = new Cell(1, 3).Add(new Paragraph((i + 1).ToString()));
                }

                if (i < 3)
                {
                    // Correct table tagging requires marking which headers correspond to the given cell.
                    // The correspondence is defined by header cells tags IDs. For table cells without
                    // col/row spans it's easy to reference a header: just add proper
                    // PdfStructureAttributes to it. Table cells with col spans are processed below.
                    PdfStructureAttributes tableAttributes = new PdfStructureAttributes("Table");
                    PdfArray headers;
                    headers = new PdfArray(headersId[i % headersId.Length]);
                    tableAttributes.GetPdfObject().Put(PdfName.Headers, headers);
                    c.GetAccessibilityProperties().AddAttributes(tableAttributes);
                }
                else
                {
                    // When we add PdfStructureAttributes to the element these attributes override any
                    // attributes generated for it automatically. E.g. cells with colspan require properly
                    // generated attributes which describe the colspan (e.g. which columns this cell spans).
                    // So here we will use a different approach: fetch the tag which will be created for
                    // the cell and modify attributes object directly.
                    TaggingHintKey colSpanCellHint = LayoutTaggingHelper.GetOrCreateHintKey(c);
                    colSpanHints.Add(colSpanCellHint);
                }

                table.AddCell(c);
            }

            doc.Add(table);

            // After table has been drawn on the page, we can modify the colspan cells tags
            foreach (TaggingHintKey colSpanHint in colSpanHints)
            {
                WaitingTagsManager waitingTagsManager = pdfDocument.GetTagStructureContext().GetWaitingTagsManager();
                TagTreePointer     p = new TagTreePointer(pdfDocument);

                // Move tag pointer to the colspan cell using its hint
                if (!waitingTagsManager.TryMovePointerToWaitingTag(p, colSpanHint))
                {
                    // It is not expected to happen ever if immediate-flush is enabled (which is by default),
                    // otherwise this should be done after the flush
                    throw new InvalidOperationException("A work-around does not work. A tag for the cell is " +
                                                        "not created or cannot be found.");
                }

                foreach (PdfStructureAttributes attr in p.GetProperties().GetAttributesList())
                {
                    if ("Table".Equals(attr.GetAttributeAsEnum("O")))
                    {
                        // Specify all the headers for the column spanning (all of 3)
                        PdfArray headers = new PdfArray(headersId);
                        attr.GetPdfObject().Put(PdfName.Headers, headers);
                        break;
                    }
                }
            }

            pdfDocument.GetStructTreeRoot().GetPdfObject().Put(idTreeName, idTree.BuildTree()
                                                               .MakeIndirect(pdfDocument));

            doc.Close();
        }