Exemple #1
0
        private bool CreateSingleTag(TaggingHintKey hintKey, TagTreePointer tagPointer)
        {
            if (hintKey.IsFinished())
            {
                ILog logger = LogManager.GetLogger(typeof(iText.Layout.Tagging.LayoutTaggingHelper));
                logger.Error(iText.IO.LogMessageConstant.ATTEMPT_TO_CREATE_A_TAG_FOR_FINISHED_HINT);
                return(false);
            }
            if (IsNonAccessibleHint(hintKey))
            {
                // try move pointer to the nearest accessible parent in case any direct content will be
                // tagged with this tagPointer
                TaggingHintKey parentTagHint = GetAccessibleParentHint(hintKey);
                context.GetWaitingTagsManager().TryMovePointerToWaitingTag(tagPointer, parentTagHint);
                return(false);
            }
            WaitingTagsManager waitingTagsManager = context.GetWaitingTagsManager();

            if (!waitingTagsManager.TryMovePointerToWaitingTag(tagPointer, hintKey))
            {
                IAccessibleElement modelElement = hintKey.GetAccessibleElement();
                TaggingHintKey     parentHint   = GetAccessibleParentHint(hintKey);
                int ind = -1;
                if (parentHint != null)
                {
                    // if parent tag hasn't been created yet - it's ok, kid tags will be moved on it's creation
                    if (waitingTagsManager.TryMovePointerToWaitingTag(tagPointer, parentHint))
                    {
                        IList <TaggingHintKey> siblingsHint = GetAccessibleKidsHint(parentHint);
                        int i = siblingsHint.IndexOf(hintKey);
                        ind = GetNearestNextSiblingTagIndex(waitingTagsManager, tagPointer, siblingsHint, i);
                    }
                }
                tagPointer.AddTag(ind, modelElement.GetAccessibilityProperties());
                if (hintKey.GetOverriddenRole() != null)
                {
                    tagPointer.SetRole(hintKey.GetOverriddenRole());
                }
                waitingTagsManager.AssignWaitingState(tagPointer, hintKey);
                IList <TaggingHintKey> kidsHint = GetAccessibleKidsHint(hintKey);
                foreach (TaggingHintKey kidKey in kidsHint)
                {
                    MoveKidTagIfCreated(hintKey, kidKey);
                }
                return(true);
            }
            return(false);
        }
        public virtual void TagTreePointerTest06()
        {
            PdfReader reader = new PdfReader(sourceFolder + "taggedDocument.pdf");
            PdfWriter writer = new PdfWriter(destinationFolder + "tagTreePointerTest06.pdf");

            writer.SetCompressionLevel(CompressionConstants.NO_COMPRESSION);
            PdfDocument    document   = new PdfDocument(reader, writer);
            TagTreePointer tagPointer = new TagTreePointer(document);

            tagPointer.SetRole(StandardRoles.PART);
            NUnit.Framework.Assert.AreEqual(tagPointer.GetRole(), "Part");
            tagPointer.MoveToKid(StandardRoles.TABLE).GetProperties().SetLanguage("en-US");
            tagPointer.MoveToKid(StandardRoles.P);
            String actualText1 = "Some looong latin text";

            tagPointer.GetProperties().SetActualText(actualText1);
            WaitingTagsManager waitingTagsManager = document.GetTagStructureContext().GetWaitingTagsManager();
            //        assertNull(waitingTagsManager.getAssociatedObject(tagPointer));
            Object associatedObj = new Object();

            waitingTagsManager.AssignWaitingState(tagPointer, associatedObj);
            tagPointer.MoveToRoot().MoveToKid(StandardRoles.TABLE).MoveToKid(1, StandardRoles.TR).GetProperties().SetActualText
                ("More latin text");
            waitingTagsManager.TryMovePointerToWaitingTag(tagPointer, associatedObj);
            tagPointer.SetRole(StandardRoles.DIV);
            tagPointer.GetProperties().SetLanguage("en-Us");
            NUnit.Framework.Assert.AreEqual(tagPointer.GetProperties().GetActualText(), actualText1);
            document.Close();
            CompareResult("tagTreePointerTest06.pdf", "cmp_tagTreePointerTest06.pdf", "diff06_");
        }
        public virtual void TagStructureFlushingTest05()
        {
            PdfWriter writer = new PdfWriter(destinationFolder + "tagStructureFlushingTest05.pdf");

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

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

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

            tagPointer.AddTag(StandardRoles.DIV);
            tagPointer.AddTag(StandardRoles.P);
            WaitingTagsManager waitingTagsManager = tagPointer.GetContext().GetWaitingTagsManager();
            Object             pWaitingTagObj     = new Object();

            waitingTagsManager.AssignWaitingState(tagPointer, pWaitingTagObj);
            canvas.BeginText();
            PdfFont standardFont = PdfFontFactory.CreateFont(StandardFonts.COURIER);

            canvas.SetFontAndSize(standardFont, 24).SetTextMatrix(1, 0, 0, 1, 32, 512);
            tagPointer.AddTag(StandardRoles.SPAN);
            canvas.OpenTag(tagPointer.GetTagReference()).ShowText("Hello ").CloseTag();
            canvas.SetFontAndSize(standardFont, 30).OpenTag(tagPointer.GetTagReference()).ShowText("World").CloseTag();
            tagPointer.MoveToParent().MoveToParent();
            // Flushing /Div tag and it's children. /P tag shall not be flushed, as it is has connected paragraphElement
            // object. On removing connection between paragraphElement and /P tag, /P tag shall be flushed.
            // When tag is flushed, tagPointer begins to point to tag's parent. If parent is also flushed - to the root.
            tagPointer.FlushTag();
            waitingTagsManager.TryMovePointerToWaitingTag(tagPointer, pWaitingTagObj);
            tagPointer.AddTag(StandardRoles.SPAN);
            canvas.OpenTag(tagPointer.GetTagReference()).ShowText("Hello ").CloseTag();
            canvas.SetFontAndSize(standardFont, 30).OpenTag(tagPointer.GetTagReference()).ShowText("again").CloseTag();
            waitingTagsManager.RemoveWaitingState(pWaitingTagObj);
            tagPointer.MoveToRoot();
            canvas.EndText().Release();
            PdfPage page2 = document.AddNewPage();

            tagPointer.SetPageForTagging(page2);
            canvas = new PdfCanvas(page2);
            tagPointer.AddTag(StandardRoles.P);
            canvas.BeginText().SetFontAndSize(PdfFontFactory.CreateFont(StandardFonts.HELVETICA), 24).SetTextMatrix(1,
                                                                                                                    0, 0, 1, 32, 512);
            tagPointer.AddTag(StandardRoles.SPAN);
            canvas.OpenTag(tagPointer.GetTagReference()).ShowText("Hello ").CloseTag();
            tagPointer.MoveToParent().AddTag(StandardRoles.SPAN);
            canvas.OpenTag(tagPointer.GetTagReference()).ShowText("World").CloseTag();
            canvas.EndText().Release();
            page1.Flush();
            page2.Flush();
            document.Close();
            CompareResult("tagStructureFlushingTest05.pdf", "cmp_tagStructureFlushingTest05.pdf", "diffFlushing05_");
        }
Exemple #4
0
        private void MoveKidTagIfCreated(TaggingHintKey parentKey, TaggingHintKey kidKey)
        {
            // both arguments shall be accessible, non-accessible are not handled inside this method
            TagTreePointer     kidPointer         = new TagTreePointer(document);
            WaitingTagsManager waitingTagsManager = context.GetWaitingTagsManager();

            if (!waitingTagsManager.TryMovePointerToWaitingTag(kidPointer, kidKey))
            {
                return;
            }
            TagTreePointer parentPointer = new TagTreePointer(document);

            if (!waitingTagsManager.TryMovePointerToWaitingTag(parentPointer, parentKey))
            {
                return;
            }
            int kidIndInParentKidsHint = GetAccessibleKidsHint(parentKey).IndexOf(kidKey);
            int ind = GetNearestNextSiblingTagIndex(waitingTagsManager, parentPointer, GetAccessibleKidsHint(parentKey
                                                                                                             ), kidIndInParentKidsHint);

            parentPointer.SetNextNewKidIndex(ind);
            kidPointer.Relocate(parentPointer);
        }
Exemple #5
0
        private int GetNearestNextSiblingTagIndex(WaitingTagsManager waitingTagsManager, TagTreePointer parentPointer
                                                  , IList <TaggingHintKey> siblingsHint, int start)
        {
            int            ind = -1;
            TagTreePointer nextSiblingPointer = new TagTreePointer(document);

            while (++start < siblingsHint.Count)
            {
                if (waitingTagsManager.TryMovePointerToWaitingTag(nextSiblingPointer, siblingsHint[start]) && parentPointer
                    .IsPointingToSameTag(new TagTreePointer(nextSiblingPointer).MoveToParent()))
                {
                    ind = nextSiblingPointer.GetIndexInParentKidsList();
                    break;
                }
            }
            return(ind);
        }
        private void RemoveTagUnavailableInPriorToOneDotFivePdf(TaggingHintKey taggingHintKey, LayoutTaggingHelper
                                                                taggingHelper)
        {
            taggingHelper.ReplaceKidHint(taggingHintKey, taggingHelper.GetAccessibleKidsHint(taggingHintKey));
            PdfDocument        pdfDocument        = taggingHelper.GetPdfDocument();
            WaitingTagsManager waitingTagsManager = pdfDocument.GetTagStructureContext().GetWaitingTagsManager();
            TagTreePointer     tagPointer         = new TagTreePointer(pdfDocument);

            if (waitingTagsManager.TryMovePointerToWaitingTag(tagPointer, taggingHintKey))
            {
                waitingTagsManager.RemoveWaitingState(taggingHintKey);
                tagPointer.RemoveTag();
            }
            if (finishForbidden.Remove(taggingHintKey))
            {
                taggingHintKey.SetFinished();
            }
        }
        public virtual void TagStructureRemovingTest03()
        {
            PdfWriter writer = new PdfWriter(destinationFolder + "tagStructureRemovingTest03.pdf");

            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);

            tagPointer.AddTag(StandardRoles.P);
            WaitingTagsManager waitingTagsManager = tagPointer.GetContext().GetWaitingTagsManager();
            Object             pWaitingTagObj     = new Object();

            waitingTagsManager.AssignWaitingState(tagPointer, pWaitingTagObj);
            PdfFont standardFont = PdfFontFactory.CreateFont(StandardFonts.COURIER);

            canvas.BeginText().SetFontAndSize(standardFont, 24).SetTextMatrix(1, 0, 0, 1, 32, 512);
            tagPointer.AddTag(StandardRoles.SPAN);
            canvas.OpenTag(tagPointer.GetTagReference()).ShowText("Hello ").CloseTag();
            canvas.SetFontAndSize(standardFont, 30).OpenTag(tagPointer.GetTagReference()).ShowText("World").CloseTag()
            .EndText();
            tagPointer.MoveToParent().MoveToParent();
            document.RemovePage(1);
            PdfPage newPage = document.AddNewPage();

            canvas = new PdfCanvas(newPage);
            tagPointer.SetPageForTagging(newPage);
            waitingTagsManager.TryMovePointerToWaitingTag(tagPointer, pWaitingTagObj);
            tagPointer.AddTag(StandardRoles.SPAN);
            canvas.OpenTag(tagPointer.GetTagReference()).BeginText().SetFontAndSize(standardFont, 24).SetTextMatrix(1,
                                                                                                                    0, 0, 1, 32, 512).ShowText("Hello.").EndText().CloseTag();
            document.Close();
            CompareResult("tagStructureRemovingTest03.pdf", "cmp_tagStructureRemovingTest03.pdf", "diffRemoving03_");
        }
Exemple #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();
        }