public virtual void AddAssociatedFilesTest01()
        {
            PdfDocument pdfDocument = new PdfDocument(new PdfWriter(destinationFolder + "add_associated_files01.pdf",
                                                                    new WriterProperties().SetPdfVersion(PdfVersion.PDF_2_0)));

            pdfDocument.SetTagged();
            pdfDocument.AddAssociatedFile("af_1", PdfFileSpec.CreateEmbeddedFileSpec(pdfDocument, "Associated File 1".
                                                                                     GetBytes(), "af_1.txt", PdfName.Data));
            pdfDocument.AddNewPage();
            pdfDocument.GetFirstPage().AddAssociatedFile("af_2", PdfFileSpec.CreateEmbeddedFileSpec(pdfDocument, "Associated File 2"
                                                                                                    .GetBytes(), "af_2.txt", PdfName.Data));
            PdfStructTreeRoot root = pdfDocument.GetStructTreeRoot();

            root.AddAssociatedFile("af_3", PdfFileSpec.CreateEmbeddedFileSpec(pdfDocument, "Associated File 3".GetBytes
                                                                                  (), "af_3.txt", PdfName.Data));
            PdfFileSpec af5 = PdfFileSpec.CreateEmbeddedFileSpec(pdfDocument, "Associated File 5".GetBytes(), "af_5",
                                                                 "af_5.txt", PdfName.Data);
            PdfTextAnnotation textannot = new PdfTextAnnotation(new Rectangle(100, 600, 50, 40));

            textannot.SetText(new PdfString("Text Annotation 01")).SetContents(new PdfString("Some contents..."));
            textannot.AddAssociatedFile(af5);
            pdfDocument.GetFirstPage().AddAnnotation(textannot);
            pdfDocument.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + "add_associated_files01.pdf"
                                                                             , sourceFolder + "cmp_add_associated_files01.pdf", "d:/", "diff_"));
        }
        private PdfStructElem AddNewKid(String role)
        {
            PdfStructElem kid = new PdfStructElem(GetDocument(), PdfStructTreeRoot.ConvertRoleToPdfName(role));

            ProcessKidNamespace(kid);
            return(AddNewKid(kid));
        }
Exemple #3
0
        public virtual void StructElemTest08()
        {
            PdfWriter writer = new PdfWriter(destinationFolder + "structElemTest08.pdf");

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

            document.SetTagged();
            PdfStructTreeRoot doc       = document.GetStructTreeRoot();
            PdfPage           firstPage = document.AddNewPage();
            PdfCanvas         canvas    = new PdfCanvas(firstPage);

            canvas.BeginText();
            canvas.SetFontAndSize(PdfFontFactory.CreateFont(StandardFonts.COURIER), 24);
            canvas.SetTextMatrix(1, 0, 0, 1, 32, 512);
            PdfStructElem paragraph = doc.AddKid(new PdfStructElem(document, PdfName.P));
            PdfStructElem span1     = paragraph.AddKid(new PdfStructElem(document, PdfName.Span, firstPage));

            canvas.OpenTag(new CanvasTag(span1.AddKid(new PdfMcrNumber(firstPage, span1))));
            canvas.ShowText("Hello ");
            canvas.CloseTag();
            canvas.OpenTag(new CanvasTag(span1.AddKid(new PdfMcrDictionary(firstPage, span1))));
            canvas.ShowText("World");
            canvas.CloseTag();
            canvas.EndText();
            canvas.Release();
            PdfPage secondPage = document.AddNewPage();

            // on flushing, the Document tag is not added
            firstPage.Flush();
            secondPage.Flush();
            document.Close();
            CompareResult("structElemTest08.pdf", "cmp_structElemTest08.pdf", "diff_structElem_08_");
        }
        private PdfStructElem AddNewKid(AccessibilityProperties properties)
        {
            PdfStructElem kid = new PdfStructElem(GetDocument(), PdfStructTreeRoot.ConvertRoleToPdfName(properties.GetRole
                                                                                                            ()));

            AccessibilityPropertiesToStructElem.Apply(properties, kid);
            ProcessKidNamespace(kid);
            return(AddNewKid(kid));
        }
Exemple #5
0
        private void InitRegisteredNamespaces()
        {
            PdfStructTreeRoot structTreeRoot = document.GetStructTreeRoot();

            foreach (PdfNamespace @namespace in structTreeRoot.GetNamespaces())
            {
                namespaces.Add(@namespace.GetPdfObject());
                nameToNamespace.Put(@namespace.GetNamespaceName(), @namespace);
            }
        }
Exemple #6
0
 /// <summary>Calculates and returns next available MCID reference.</summary>
 /// <returns>calculated MCID reference.</returns>
 /// <exception cref="iText.Kernel.PdfException"/>
 public virtual int GetNextMcid()
 {
     if (!GetDocument().IsTagged())
     {
         throw new PdfException(PdfException.MustBeATaggedDocument);
     }
     if (mcid == -1)
     {
         PdfStructTreeRoot structTreeRoot = GetDocument().GetStructTreeRoot();
         mcid = structTreeRoot.GetNextMcidForPage(this);
     }
     return(mcid++);
 }
        internal RoleMappingResolverPdf2(String role, PdfNamespace @namespace, PdfDocument document)
        {
            this.currRole      = PdfStructTreeRoot.ConvertRoleToPdfName(role);
            this.currNamespace = @namespace;
            String        defaultNsName    = StandardNamespaces.GetDefault();
            PdfDictionary defaultNsRoleMap = document.GetStructTreeRoot().GetRoleMap();

            this.defaultNamespace = new PdfNamespace(defaultNsName).SetNamespaceRoleMap(defaultNsRoleMap);
            if (currNamespace == null)
            {
                currNamespace = defaultNamespace;
            }
        }
Exemple #8
0
        /// <summary>Flushes the tags which are considered to belong to the given page.</summary>
        /// <remarks>
        /// Flushes the tags which are considered to belong to the given page.
        /// The logic that defines if the given tag (structure element) belongs to the page is the following:
        /// if all the marked content references (dictionary or number references), that are the
        /// descendants of the given structure element, belong to the current page - the tag is considered
        /// to belong to the page. If tag has descendants from several pages - it is flushed, if all other pages except the
        /// current one are flushed.
        /// <br /><br />
        /// If some of the page's tags have waiting state (see
        /// <see cref="WaitingTagsManager"/>
        /// these tags are considered
        /// as not yet finished ones, and they and their children won't be flushed.
        /// </remarks>
        /// <param name="page">a page which tags will be flushed</param>
        /// <returns>
        /// current
        /// <see cref="TagStructureContext"/>
        /// instance
        /// </returns>
        public virtual iText.Kernel.Pdf.Tagutils.TagStructureContext FlushPageTags(PdfPage page)
        {
            PdfStructTreeRoot    structTreeRoot = document.GetStructTreeRoot();
            ICollection <PdfMcr> pageMcrs       = structTreeRoot.GetPageMarkedContentReferences(page);

            if (pageMcrs != null)
            {
                foreach (PdfMcr mcr in pageMcrs)
                {
                    PdfStructElem parent = (PdfStructElem)mcr.GetParent();
                    FlushParentIfBelongsToPage(parent, page);
                }
            }
            return(this);
        }
Exemple #9
0
        /// <summary>Removes all tags that belong only to this page.</summary>
        /// <remarks>
        /// Removes all tags that belong only to this page. The logic which defines if tag belongs to the page is described
        /// at
        /// <see cref="FlushPageTags(iText.Kernel.Pdf.PdfPage)"/>.
        /// </remarks>
        /// <param name="page">page that defines which tags are to be removed</param>
        /// <returns>
        /// current
        /// <see cref="TagStructureContext"/>
        /// instance
        /// </returns>
        public virtual iText.Kernel.Pdf.Tagutils.TagStructureContext RemovePageTags(PdfPage page)
        {
            PdfStructTreeRoot    structTreeRoot = document.GetStructTreeRoot();
            ICollection <PdfMcr> pageMcrs       = structTreeRoot.GetPageMarkedContentReferences(page);

            if (pageMcrs != null)
            {
                // We create a copy here, because pageMcrs is backed by the internal collection which is changed when mcrs are removed.
                IList <PdfMcr> mcrsList = new List <PdfMcr>(pageMcrs);
                foreach (PdfMcr mcr in mcrsList)
                {
                    RemovePageTagFromParent(mcr, mcr.GetParent());
                }
            }
            return(this);
        }
Exemple #10
0
 private void ActualizeNamespacesInStructTreeRoot()
 {
     if (namespaces.Count > 0)
     {
         PdfStructTreeRoot           structTreeRoot = GetDocument().GetStructTreeRoot();
         PdfArray                    rootNamespaces = structTreeRoot.GetNamespacesObject();
         ICollection <PdfDictionary> newNamespaces  = new LinkedHashSet <PdfDictionary>(namespaces);
         for (int i = 0; i < rootNamespaces.Size(); ++i)
         {
             newNamespaces.Remove(rootNamespaces.GetAsDictionary(i));
         }
         foreach (PdfDictionary newNs in newNamespaces)
         {
             rootNamespaces.Add(newNs);
         }
         if (!newNamespaces.IsEmpty())
         {
             structTreeRoot.SetModified();
         }
     }
 }
Exemple #11
0
        /// <summary>Converts the current tag structure into an XML file with provided encoding.</summary>
        /// <param name="os">the output stream to save XML file to</param>
        /// <param name="charset">the charset of the resultant XML file</param>
        public virtual void ConvertToXml(Stream os, String charset)
        {
            @out = new StreamWriter(os, EncodingUtil.GetEncoding(charset));
            if (rootTag != null)
            {
                @out.Write("<" + rootTag + ">" + Environment.NewLine);
            }
            // get the StructTreeRoot from the document
            PdfStructTreeRoot structTreeRoot = document.GetStructTreeRoot();

            if (structTreeRoot == null)
            {
                throw new PdfException(PdfException.DocumentDoesntContainStructTreeRoot);
            }
            // Inspect the child or children of the StructTreeRoot
            InspectKids(structTreeRoot.GetKids());
            if (rootTag != null)
            {
                @out.Write("</" + rootTag + ">");
            }
            @out.Flush();
            @out.Dispose();
        }
        public virtual void SeveralSameElementsInStructTreeRootTest()
        {
            String                inFile                   = sourceFolder + "severalSameElementsInStructTreeRoot.pdf";
            PdfDocument           doc                      = new PdfDocument(new PdfReader(inFile), new PdfWriter(new MemoryStream()));
            PdfStructTreeRoot     structTreeRoot           = doc.GetStructTreeRoot();
            IList <PdfStructElem> kidsOfStructTreeRootKids = new List <PdfStructElem>();

            foreach (IStructureNode kid in structTreeRoot.GetKids())
            {
                foreach (IStructureNode kidOfKid in kid.GetKids())
                {
                    if (kidOfKid is PdfStructElem)
                    {
                        kidsOfStructTreeRootKids.Add((PdfStructElem)kidOfKid);
                    }
                }
            }
            structTreeRoot.Flush();
            foreach (PdfStructElem kidsOfStructTreeRootKid in kidsOfStructTreeRootKids)
            {
                NUnit.Framework.Assert.IsTrue(kidsOfStructTreeRootKid.IsFlushed());
            }
        }
Exemple #13
0
 public override AccessibilityProperties SetRole(String role)
 {
     GetBackingElem().SetRole(PdfStructTreeRoot.ConvertRoleToPdfName(role));
     return(this);
 }
Exemple #14
0
 public override AccessibilityProperties SetPhoneticAlphabet(String phoneticAlphabet)
 {
     GetBackingElem().SetPhoneticAlphabet(PdfStructTreeRoot.ConvertRoleToPdfName(phoneticAlphabet));
     return(this);
 }
 /// <summary>Sets new role to the current tag.</summary>
 /// <param name="role">new role to be set.</param>
 /// <returns>
 /// this
 /// <see cref="TagTreePointer"/>
 /// instance.
 /// </returns>
 public virtual iText.Kernel.Pdf.Tagutils.TagTreePointer SetRole(String role)
 {
     GetCurrentStructElem().SetRole(PdfStructTreeRoot.ConvertRoleToPdfName(role));
     return(this);
 }
 internal RoleMappingResolver(String role, PdfDocument document)
 {
     this.currRole = PdfStructTreeRoot.ConvertRoleToPdfName(role);
     this.roleMap  = document.GetStructTreeRoot().GetRoleMap();
 }