Beispiel #1
0
        public virtual void WriteUtf8AltText()
        {
            String      RESOURCE = sourceFolder + "Space Odyssey.jpg";
            PdfDocument pdfDoc   = new PdfDocument(new PdfWriter(destinationFolder + "writeUtf8AltText.pdf"));

            pdfDoc.SetTagged();
            PdfPage        page       = pdfDoc.AddNewPage();
            PdfCanvas      canvas     = new PdfCanvas(page);
            TagTreePointer tagPointer = new TagTreePointer(pdfDoc);

            tagPointer.SetPageForTagging(page);
            tagPointer.AddTag(StandardRoles.DIV);
            tagPointer.AddTag(StandardRoles.SPAN);
            //  2001: A Space Odyssey (Космическая одиссея)
            tagPointer.GetContext().GetPointerStructElem(tagPointer).SetAlt(new PdfString("2001: A Space Odyssey (\u041A\u043E\u0441\u043C\u0438\u0447\u0435\u0441\u043A\u0430\u044F "
                                                                                          + "\u043E\u0434\u0438\u0441\u0441\u0435\u044F)", PdfEncodings.UTF8));
            ImageData img = ImageDataFactory.Create(RESOURCE);

            canvas.OpenTag(tagPointer.GetTagReference());
            canvas.AddImage(img, 36, 700, 100, false, false);
            canvas.CloseTag();
            canvas.EndText();
            pdfDoc.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + "writeUtf8AltText.pdf"
                                                                             , sourceFolder + "cmp_writeUtf8AltText.pdf", destinationFolder, "diffAltText_"));
        }
        public virtual void TagStructureRemovingTest02()
        {
            PdfReader reader = new PdfReader(sourceFolder + "taggedDocument.pdf");
            PdfWriter writer = new PdfWriter(destinationFolder + "tagStructureRemovingTest02.pdf");

            writer.SetCompressionLevel(CompressionConstants.NO_COMPRESSION);
            PdfDocument document   = new PdfDocument(reader, writer);
            PdfPage     firstPage  = document.GetPage(1);
            PdfPage     secondPage = document.GetPage(2);

            document.RemovePage(firstPage);
            document.RemovePage(secondPage);
            PdfPage        page       = document.AddNewPage();
            TagTreePointer tagPointer = new TagTreePointer(document);

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

            tagPointer.AddTag(StandardRoles.P);
            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();
            document.Close();
            CompareResult("tagStructureRemovingTest02.pdf", "cmp_tagStructureRemovingTest02.pdf", "diffRemoving02_");
        }
Beispiel #3
0
        public virtual void WriteUtf8ActualText()
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(destinationFolder + "writeUtf8ActualText.pdf"));

            pdfDoc.SetTagged();
            PdfPage        page       = pdfDoc.AddNewPage();
            PdfCanvas      canvas     = new PdfCanvas(page);
            TagTreePointer tagPointer = new TagTreePointer(pdfDoc);

            tagPointer.SetPageForTagging(page);
            tagPointer.AddTag(StandardRoles.DIV);
            tagPointer.AddTag(StandardRoles.SPAN);
            tagPointer.GetContext().GetPointerStructElem(tagPointer).SetActualText(new PdfString("actual", PdfEncodings
                                                                                                 .UTF8));
            canvas.BeginText();
            canvas.MoveText(36, 788);
            canvas.SetFontAndSize(PdfFontFactory.CreateFont(StandardFonts.TIMES_ROMAN), 12);
            canvas.OpenTag(tagPointer.GetTagReference());
            canvas.ShowText("These piece of text has an actual text property. Can be viewed via properties of span in the tag tree."
                            );
            canvas.CloseTag();
            canvas.EndText();
            pdfDoc.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + "writeUtf8ActualText.pdf"
                                                                             , sourceFolder + "cmp_writeUtf8ActualText.pdf", destinationFolder, "diffActualText_"));
        }
        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(PdfName.P);
            IAccessibleElement paragraphElement = tagPointer.GetConnectedElement(true);
            PdfFont            standardFont     = PdfFontFactory.CreateFont(FontConstants.COURIER);

            canvas.BeginText().SetFontAndSize(standardFont, 24).SetTextMatrix(1, 0, 0, 1, 32, 512);
            tagPointer.AddTag(PdfName.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);
            tagPointer.MoveToTag(paragraphElement).AddTag(PdfName.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_");
        }
        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_");
        }
        protected void ManipulatePdf(String src, String dest)
        {
            PdfReader   reader      = new PdfReader(src);
            PdfWriter   writer      = new PdfWriter(dest);
            PdfDocument pdfDocument = new PdfDocument(reader, writer);

            PdfPage   firstPage = pdfDocument.GetFirstPage();
            PdfCanvas canvas    = new PdfCanvas(firstPage);

            // The blue star would be something like logo of our document.
            // So for example we don't want it to be read out loud on every page. To achieve it, we mark it as an Artifact.
            canvas.OpenTag(new CanvasArtifact());
            DrawStar(canvas, 30, 745, ColorConstants.BLUE);
            canvas.CloseTag();

            // The green star we want to be a part of actual content and logical structure of the document.
            // To modify tag structure manually we create TagTreePointer. After creation it points at the root tag.
            TagTreePointer tagPointer = new TagTreePointer(pdfDocument);

            tagPointer.AddTag(StandardRoles.FIGURE);
            tagPointer.GetProperties().SetAlternateDescription("The green star.");

            // It is important to set the page at which new content will be tagged
            tagPointer.SetPageForTagging(firstPage);

            canvas.OpenTag(tagPointer.GetTagReference());
            DrawStar(canvas, 450, 745, ColorConstants.GREEN);
            canvas.CloseTag();

            // We can change the position of the existing tags in the tag structure.
            tagPointer.MoveToParent();
            TagTreePointer newPositionOfStar = new TagTreePointer(pdfDocument);

            newPositionOfStar.MoveToKid(StandardRoles.SECT);
            int indexOfTheGreenStarTag = 2;

            // tagPointer points at the parent of the green star tag
            tagPointer.RelocateKid(indexOfTheGreenStarTag, newPositionOfStar);

            // Using the relocateKid method, we can even change the order of the same parent's kids.
            // This could be used to change for example reading order.

            // Make both tagPointer and newPositionOfStar to point at the same tag
            tagPointer.MoveToRoot().MoveToKid(StandardRoles.SECT);


            // Next added tag to this tag pointer will be added at the 0 position
            newPositionOfStar.SetNextNewKidIndex(0);
            indexOfTheGreenStarTag = 2;
            tagPointer.RelocateKid(indexOfTheGreenStarTag, newPositionOfStar);

            pdfDocument.Close();
        }
        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_");
        }
        public virtual void TagStructureFlushingTest06()
        {
            PdfWriter writer = new PdfWriter(destinationFolder + "tagStructureFlushingTest06.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);
            canvas.BeginText();
            PdfFont standardFont = PdfFontFactory.CreateFont(StandardFonts.COURIER);

            canvas.SetFontAndSize(standardFont, 24).SetTextMatrix(1, 0, 0, 1, 32, 512);
            tagPointer.AddTag(StandardRoles.SPAN);
            WaitingTagsManager waitingTagsManager = document.GetTagStructureContext().GetWaitingTagsManager();
            Object             associatedObj      = new Object();

            waitingTagsManager.AssignWaitingState(tagPointer, associatedObj);
            canvas.OpenTag(tagPointer.GetTagReference()).ShowText("Hello ").CloseTag();
            canvas.SetFontAndSize(standardFont, 30).OpenTag(tagPointer.GetTagReference()).ShowText("World").CloseTag();
            canvas.EndText().Release();
            page1.Flush();
            tagPointer.RelocateKid(0, new TagTreePointer(document).MoveToKid(StandardRoles.DIV).SetNextNewKidIndex(0).
                                   AddTag(StandardRoles.P));
            tagPointer.RemoveTag();
            waitingTagsManager.RemoveWaitingState(associatedObj);
            document.GetTagStructureContext().FlushPageTags(page1);
            document.GetStructTreeRoot().CreateParentTreeEntryForPage(page1);
            document.Close();
            CompareResult("tagStructureFlushingTest06.pdf", "cmp_tagStructureFlushingTest06.pdf", "diffFlushing06_");
        }
        public virtual void TagTreePointerTest01()
        {
            FileStream  fos      = new FileStream(destinationFolder + "tagTreePointerTest01.pdf", FileMode.Create);
            PdfWriter   writer   = new PdfWriter(fos).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);
            PdfFont   standardFont = PdfFontFactory.CreateFont(StandardFonts.COURIER);

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

            tagPointer.SetPageForTagging(page2);
            canvas = new PdfCanvas(page2);
            canvas.BeginText().SetFontAndSize(PdfFontFactory.CreateFont(StandardFonts.HELVETICA), 24).SetTextMatrix(1,
                                                                                                                    0, 0, 1, 32, 512);
            tagPointer.AddTag(StandardRoles.P).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("tagTreePointerTest01.pdf", "cmp_tagTreePointerTest01.pdf", "diff01_");
        }
        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_");
        }
Beispiel #11
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 TagTreePointerTest03()
        {
            PdfReader reader = new PdfReader(sourceFolder + "taggedDocument.pdf");
            PdfWriter writer = new PdfWriter(destinationFolder + "tagTreePointerTest03.pdf");

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

            tagPointer.MoveToKid(StandardRoles.TABLE).MoveToKid(2, StandardRoles.TR);
            TagTreePointer tagPointerCopy = new TagTreePointer(tagPointer);

            tagPointer.RemoveTag();
            // tagPointerCopy now points at removed tag
            String exceptionMessage = null;

            try {
                tagPointerCopy.AddTag(StandardRoles.SPAN);
            }
            catch (PdfException e) {
                exceptionMessage = e.Message;
            }
            NUnit.Framework.Assert.AreEqual(PdfException.TagTreePointerIsInInvalidStateItPointsAtRemovedElementUseMoveToRoot
                                            , exceptionMessage);
            tagPointerCopy.MoveToRoot().MoveToKid(StandardRoles.TABLE);
            tagPointerCopy.MoveToKid(StandardRoles.TR);
            TagTreePointer tagPointerCopyCopy = new TagTreePointer(tagPointerCopy);

            tagPointerCopy.FlushTag();
            // tagPointerCopyCopy now points at flushed tag
            try {
                tagPointerCopyCopy.AddTag(StandardRoles.SPAN);
            }
            catch (PdfException e) {
                exceptionMessage = e.Message;
            }
            NUnit.Framework.Assert.AreEqual(PdfException.TagTreePointerIsInInvalidStateItPointsAtFlushedElementUseMoveToRoot
                                            , exceptionMessage);
            try {
                tagPointerCopy.MoveToKid(0);
            }
            catch (PdfException e) {
                exceptionMessage = e.Message;
            }
            NUnit.Framework.Assert.AreEqual(PdfException.CannotMoveToFlushedKid, exceptionMessage);
            document.Close();
        }
        public virtual void TagTreePointerTest07()
        {
            PdfWriter writer = new PdfWriter(destinationFolder + "tagTreePointerTest07.pdf").SetCompressionLevel(CompressionConstants
                                                                                                                 .NO_COMPRESSION);
            PdfDocument document = new PdfDocument(writer);

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

            tagPointer.AddTag(StandardRoles.SPAN);
            PdfCanvas canvas       = new PdfCanvas(page);
            PdfFont   standardFont = PdfFontFactory.CreateFont(StandardFonts.COURIER);

            canvas.BeginText().SetFontAndSize(standardFont, 24).SetTextMatrix(1, 0, 0, 1, 32, 512);
            canvas.OpenTag(tagPointer.GetTagReference()).ShowText("Hello ").CloseTag();
            canvas.OpenTag(tagPointer.GetTagReference().AddProperty(PdfName.E, new PdfString("Big Mister"))).ShowText(
                " BMr. ").CloseTag();
            canvas.SetFontAndSize(standardFont, 30).OpenTag(tagPointer.GetTagReference()).ShowText("World").CloseTag();
            canvas.EndText();
            document.Close();
            CompareResult("tagTreePointerTest07.pdf", "cmp_tagTreePointerTest07.pdf", "diff07_");
        }
Beispiel #14
0
        public override void Draw(DrawContext drawContext)
        {
            PdfDocument document = drawContext.GetDocument();

            ApplyDestination(document);
            ApplyAction(document);
            bool               isTagged          = drawContext.IsTaggingEnabled() && GetModelElement() is IAccessibleElement;
            TagTreePointer     tagPointer        = null;
            IAccessibleElement accessibleElement = null;

            if (isTagged)
            {
                accessibleElement = (IAccessibleElement)GetModelElement();
                PdfName role = accessibleElement.GetRole();
                if (role != null && !PdfName.Artifact.Equals(role))
                {
                    tagPointer = document.GetTagStructureContext().GetAutoTaggingPointer();
                    if (!tagPointer.IsElementConnectedToTag(accessibleElement))
                    {
                        AccessibleAttributesApplier.ApplyLayoutAttributes(role, this, document);
                        if (role.Equals(PdfName.TD))
                        {
                            AccessibleAttributesApplier.ApplyTableAttributes(this);
                        }
                        if (role.Equals(PdfName.List))
                        {
                            AccessibleAttributesApplier.ApplyListAttributes(this);
                        }
                    }
                    tagPointer.AddTag(accessibleElement, true);
                }
                else
                {
                    isTagged = false;
                }
            }
            bool isRelativePosition = IsRelativePosition();

            if (isRelativePosition)
            {
                ApplyAbsolutePositioningTranslation(false);
            }
            BeginRotationIfApplied(drawContext.GetCanvas());
            DrawBackground(drawContext);
            DrawBorder(drawContext);
            DrawChildren(drawContext);
            EndRotationIfApplied(drawContext.GetCanvas());
            if (isRelativePosition)
            {
                ApplyAbsolutePositioningTranslation(true);
            }
            if (isTagged)
            {
                tagPointer.MoveToParent();
                if (isLastRendererForModelElement)
                {
                    document.GetTagStructureContext().RemoveElementConnectionToTag(accessibleElement);
                }
            }
            flushed = true;
        }
Beispiel #15
0
        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDocument = new PdfDocument(new PdfWriter(dest));

            // Mark that this document is tagged
            pdfDocument.SetTagged();

            Document document = new Document(pdfDocument);

            // Save some space at the beginning of pages for future additions
            document.SetTopMargin(100);

            // Here we obtain a TagTreePointer which is used internally for auto tagging.
            // In the beginning, this instance points at the root of the tag structure tree.
            // We can move it and as a result all new content will be under the current position of the auto tagging pointer.
            // Auto tagging pointer is also used for tagging annotations and forms, so the same approach could be used there.
            TagTreePointer autoTaggingPointer = pdfDocument.GetTagStructureContext().GetAutoTaggingPointer();

            // Create a new tag, which will be a kid of the root element
            autoTaggingPointer.AddTag(StandardRoles.SECT);

            // Add some content to the page
            Paragraph p = new Paragraph().Add(TEXT1).Add(TEXT2).Add(TEXT3);

            document.Add(p);

            // Create bold font to the header text
            PdfFont bold = PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD);

            Table table = new Table(UnitValue.CreatePercentArray(2));

            table
            .SetWidth(350)
            .SetHorizontalAlignment(HorizontalAlignment.CENTER)
            .SetTextAlignment(TextAlignment.CENTER);
            table.AddHeaderCell(new Paragraph("Nominations").SetFont(bold));
            table.AddHeaderCell(new Paragraph("Film").SetFont(bold));
            table.AddCell("12").AddCell("The Revenant");
            table.AddCell("10").AddCell("Mad Max: Fury Road");
            table.AddCell("7").AddCell("The Martian");
            table.AddCell("6").AddCell("Spotlight");
            table.AddCell("5").AddCell("Star Wars: The Force Awakens");
            table.AddCell("4").AddCell("Room");
            table.AddCell("3").AddCell("The Hateful Eight");
            table.AddCell("2").AddCell("Ex Machina");
            document.Add(table);

            // From here we want to create another section of the document.
            // Move the root tag and create a new 'Section' tag
            autoTaggingPointer
            .MoveToParent()
            .AddTag(StandardRoles.SECT);

            p = new Paragraph(TEXT4);
            p.Add(TEXT5);
            document.Add(p);

            table = new Table(UnitValue.CreatePercentArray(2));
            table
            .SetWidth(350)
            .SetHorizontalAlignment(HorizontalAlignment.CENTER)
            .SetTextAlignment(TextAlignment.CENTER);
            table.AddHeaderCell(new Paragraph("Awards").SetFont(bold));
            table.AddHeaderCell(new Paragraph("Film").SetFont(bold));
            table.AddCell("6").AddCell("Mad Max: Fury Road");
            table.AddCell("3").AddCell("The Revenant");
            table.AddCell("2").AddCell("Spotlight");
            document.Add(table);

            // Layout element roles could be changed, or set to null if you want to omit it in the tag structure.
            // But be aware that if you set null to the role of the tag which immediate kids are page content items,
            // then this content won't be tagged at all (for example if you set role to null for Text element, then the
            // text on the page won't be tagged too).
            Paragraph caption = new Paragraph().SetTextAlignment(TextAlignment.CENTER);

            caption.GetAccessibilityProperties().SetRole(null);
            Text captionText = new Text("Table 2, winners");

            captionText.GetAccessibilityProperties().SetRole(StandardRoles.CAPTION);
            caption.Add(captionText);
            document.Add(caption);

            // By default, root tag has role of 'Document'. Let's change it to 'Part'.
            // Move to the root tag (here we also could have used moveToParent method)
            // and change the role of the tag the pointer points
            autoTaggingPointer
            .MoveToRoot()
            .SetRole(StandardRoles.PART);

            document.Close();
        }
Beispiel #16
0
        public override void Draw(DrawContext drawContext)
        {
            base.Draw(drawContext);
            PdfDocument    document   = drawContext.GetDocument();
            bool           isTagged   = drawContext.IsTaggingEnabled() && GetModelElement() is IAccessibleElement;
            bool           isArtifact = false;
            TagTreePointer tagPointer = null;

            if (isTagged)
            {
                tagPointer = document.GetTagStructureContext().GetAutoTaggingPointer();
                IAccessibleElement accessibleElement = (IAccessibleElement)GetModelElement();
                PdfName            role = accessibleElement.GetRole();
                if (role != null && !PdfName.Artifact.Equals(role))
                {
                    AccessibleAttributesApplier.ApplyLayoutAttributes(accessibleElement.GetRole(), this, document);
                    tagPointer.AddTag(accessibleElement);
                }
                else
                {
                    isTagged = false;
                    if (PdfName.Artifact.Equals(role))
                    {
                        isArtifact = true;
                    }
                }
            }
            ApplyMargins(occupiedArea.GetBBox(), false);
            bool isRelativePosition = IsRelativePosition();

            if (isRelativePosition)
            {
                ApplyAbsolutePositioningTranslation(false);
            }
            if (fixedYPosition == null)
            {
                fixedYPosition = occupiedArea.GetBBox().GetY() + pivotY;
            }
            if (fixedXPosition == null)
            {
                fixedXPosition = occupiedArea.GetBBox().GetX();
            }
            PdfCanvas canvas = drawContext.GetCanvas();

            if (isTagged)
            {
                canvas.OpenTag(tagPointer.GetTagReference());
            }
            else
            {
                if (isArtifact)
                {
                    canvas.OpenTag(new CanvasArtifact());
                }
            }
            PdfXObject xObject = ((Image)(GetModelElement())).GetXObject();

            canvas.AddXObject(xObject, matrix[0], matrix[1], matrix[2], matrix[3], (float)fixedXPosition + deltaX, (float
                                                                                                                    )fixedYPosition);
            if (true.Equals(GetPropertyAsBoolean(Property.FLUSH_ON_DRAW)))
            {
                xObject.Flush();
            }
            if (isTagged || isArtifact)
            {
                canvas.CloseTag();
            }
            if (isRelativePosition)
            {
                ApplyAbsolutePositioningTranslation(true);
            }
            ApplyMargins(occupiedArea.GetBBox(), true);
            if (isTagged)
            {
                tagPointer.MoveToParent();
            }
        }
        public override void Draw(DrawContext drawContext)
        {
            bool           isTagged   = drawContext.IsTaggingEnabled() && GetModelElement() is IAccessibleElement;
            TagTreePointer tagPointer = null;

            if (isTagged)
            {
                tagPointer = drawContext.GetDocument().GetTagStructureContext().GetAutoTaggingPointer();
                IAccessibleElement modelElement = (IAccessibleElement)GetModelElement();
                PdfName            role         = modelElement.GetRole();
                if (role != null && !PdfName.Artifact.Equals(role))
                {
                    bool lBodyTagIsCreated = tagPointer.IsElementConnectedToTag(modelElement);
                    if (!lBodyTagIsCreated)
                    {
                        tagPointer.AddTag(PdfName.LI);
                    }
                    else
                    {
                        tagPointer.MoveToTag(modelElement).MoveToParent();
                    }
                }
                else
                {
                    isTagged = false;
                }
            }
            base.Draw(drawContext);
            // It will be null in case of overflow (only the "split" part will contain symbol renderer.
            if (symbolRenderer != null)
            {
                symbolRenderer.SetParent(parent);
                float x = occupiedArea.GetBBox().GetX();
                if (childRenderers.Count > 0)
                {
                    float?yLine = ((AbstractRenderer)childRenderers[0]).GetFirstYLineRecursively();
                    if (yLine != null)
                    {
                        if (symbolRenderer is TextRenderer)
                        {
                            ((TextRenderer)symbolRenderer).MoveYLineTo((float)yLine);
                        }
                        else
                        {
                            symbolRenderer.Move(0, (float)yLine - symbolRenderer.GetOccupiedArea().GetBBox().GetY());
                        }
                    }
                    else
                    {
                        symbolRenderer.Move(0, occupiedArea.GetBBox().GetY() + occupiedArea.GetBBox().GetHeight() - (symbolRenderer
                                                                                                                     .GetOccupiedArea().GetBBox().GetY() + symbolRenderer.GetOccupiedArea().GetBBox().GetHeight()));
                    }
                }
                else
                {
                    symbolRenderer.Move(0, occupiedArea.GetBBox().GetY() + occupiedArea.GetBBox().GetHeight() - symbolRenderer
                                        .GetOccupiedArea().GetBBox().GetHeight() - symbolRenderer.GetOccupiedArea().GetBBox().GetY());
                }
                ListSymbolAlignment listSymbolAlignment = (ListSymbolAlignment)parent.GetProperty <ListSymbolAlignment?>(Property
                                                                                                                         .LIST_SYMBOL_ALIGNMENT, ListSymbolAlignment.RIGHT);
                float xPosition = x - symbolRenderer.GetOccupiedArea().GetBBox().GetX();
                if (listSymbolAlignment == ListSymbolAlignment.RIGHT)
                {
                    xPosition += symbolAreaWidth - symbolRenderer.GetOccupiedArea().GetBBox().GetWidth();
                }
                symbolRenderer.Move(xPosition, 0);
                if (isTagged)
                {
                    tagPointer.AddTag(0, PdfName.Lbl);
                }
                symbolRenderer.Draw(drawContext);
                if (isTagged)
                {
                    tagPointer.MoveToParent();
                }
            }
            if (isTagged)
            {
                tagPointer.MoveToParent();
            }
        }
Beispiel #18
0
        public virtual void HandleEvent(Event pdfEvent)
        {
            {
                PdfDocumentEvent docEvent = (PdfDocumentEvent)pdfEvent;
                PdfDocument      pdfDoc   = docEvent.GetDocument();
                Document         document = new Document(pdfDoc);
                PdfPage          page     = docEvent.GetPage();
                int       pageNumber      = pdfDoc.GetPageNumber(page);
                Rectangle pageSize        = page.GetPageSize();

                PdfCanvas pdfCanvas = new PdfCanvas(page.NewContentStreamBefore(), page.GetResources(), pdfDoc);

                //Add header and footer
                PdfFont font = PdfFontFactory.CreateRegisteredFont("calibri-bold");

                if (pageNumber == 1)
                {
                    if (_docOptions.HasFlag(DocConverter.DocOptions.AddHeaderPageOne))
                    {
                        pdfCanvas.SaveState();
                        PdfExtGState state = new PdfExtGState();
                        state.SetFillOpacity(1.0f);
                        pdfCanvas.SetExtGState(state);

                        var imageHeight = _idHeaderFirst.GetHeight() * 72 / 96; //convert pixels to points
                        var imageToAdd  = new Image(_idHeaderFirst, 0, pageSize.GetTop() - imageHeight, pageSize.GetWidth());

                        imageToAdd.GetAccessibilityProperties()
                        .SetAlternateDescription("Background header image");
                        imageToAdd.GetAccessibilityProperties().SetRole(StandardRoles.ARTIFACT);

                        PdfLayer pdflayer = new PdfLayer("main layer", pdfDoc);

                        pdflayer.SetOn(true);
                        Canvas canvas = new Canvas(pdfCanvas, pdfDoc,
                                                   document.GetPageEffectiveArea(pdfDoc.GetDefaultPageSize()));
                        pdflayer.SetPageElement("L");
                        pdfCanvas.BeginLayer(pdflayer);
                        canvas.EnableAutoTagging(page);
                        canvas.Add(imageToAdd);
                        pdfCanvas.EndLayer();
                    }

                    if (_docOptions.HasFlag(DocConverter.DocOptions.DisplayTitle))
                    {
                        //Add Title
                        Color          purpleColor = new DeviceRgb(85, 60, 116);
                        TagTreePointer tagPointer  = new TagTreePointer(pdfDoc);
                        tagPointer.SetPageForTagging(page);
                        tagPointer.AddTag(StandardRoles.TITLE);
                        pdfCanvas.BeginText().SetColor(purpleColor, true).SetFontAndSize(font, 28)
                        .MoveText(42, pageSize.GetTop() - 150).OpenTag(tagPointer.GetTagReference()).ShowText(_title).CloseTag().Stroke();
                    }
                }
                else if (_docOptions.HasFlag(DocConverter.DocOptions.AddHeaderAllPages))
                {
                    pdfCanvas.SaveState();
                    PdfExtGState state = new PdfExtGState();
                    state.SetFillOpacity(1.0f);
                    pdfCanvas.SetExtGState(state);
                    var imageHeight = _idHeaderAll.GetHeight() * 72 / 96; //convert pixels to points
                    var imageToAdd  = new Image(_idHeaderAll, 0, pageSize.GetTop() - imageHeight, pageSize.GetWidth());
                    imageToAdd.GetAccessibilityProperties().SetRole(StandardRoles.ARTIFACT);
                    imageToAdd.GetAccessibilityProperties()
                    .SetAlternateDescription("Background header image");
                    PdfLayer pdflayer = new PdfLayer("main layer", pdfDoc);

                    pdflayer.SetOn(true);

                    Canvas canvas = new Canvas(pdfCanvas, pdfDoc,
                                               document.GetPageEffectiveArea(pdfDoc.GetDefaultPageSize()));

                    pdfCanvas.BeginLayer(pdflayer);
                    canvas.EnableAutoTagging(page);
                    canvas.Add(imageToAdd);
                    pdfCanvas.EndLayer();
                    pdfCanvas.RestoreState();
                }

                if (_docOptions.HasFlag(DocConverter.DocOptions.AddLineBottomEachPage))
                {
                    //Add line to the bottom
                    Color blueColor = new DeviceCmyk(100, 25, 0, 39);

                    pdfCanvas.SetStrokeColor(blueColor)
                    .MoveTo(36, 36)
                    .LineTo(559, 36)
                    .ClosePathStroke();
                }


                pdfCanvas.Release();
            }
        }