Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please check

C#
dynamic AnnotationReferencedPage = PdfReader.GetPdfObject((PRIndirectReference)AnnotationAction.GetAsArray(PdfName.D).ArrayList(1));


Run Time Error


Non-invocable member 'iTextSharp.text.pdf.PdfArray.ArrayList' cannot be used like a method



C#
PdfReader R = default(PdfReader);
            int PageCount = 0;
            PdfDictionary PageDictionary = default(PdfDictionary);
            PdfArray Annots = default(PdfArray);

            //'//Open our reader
            R = new PdfReader(BaseFile);
            //'//Get the page cont
            PageCount = R.NumberOfPages;

            //'//Loop through each page
            for (int I = 1; I <= PageCount; I++)
            {
                //'//Get the current page
                PageDictionary = R.GetPageN(I);

                //'//Get all of the annotations for the current page
                Annots = PageDictionary.GetAsArray(PdfName.ANNOTS);

                //'//Make sure we have something
                if ((Annots == null) || (Annots.Length == 0))
                    continue;

                //'//Loop through each annotation

                foreach (object A_loopVariable in Annots.ArrayList)
                {
                    PdfObject A = (PdfObject)A_loopVariable;
                    
                    //'//I do not completely understand this but I think this turns an Indirect Reference into an actual object, but I could be wrong
                    //'//Anyway, convert the itext-specific object as a generic PDF object
                    dynamic AnnotationDictionary = (PdfDictionary)PdfReader.GetPdfObject(A);

                    //'//Make sure this annotation has a link
                    if (!AnnotationDictionary.Get(PdfName.SUBTYPE).Equals(PdfName.LINK))
                        continue;

                    //'//Make sure this annotation has an ACTION
                    if (AnnotationDictionary.Get(PdfName.A) == null)
                        continue;

                    //'//Get the ACTION for the current annotation
                    dynamic AnnotationAction = AnnotationDictionary.GetAsDict(PdfName.A);

                    //'//Test if it is a named actions such as /FIRST, /LAST, etc
                    if (AnnotationAction.Get(PdfName.S).Equals(PdfName.NAMED))
                    {
                        Trace.Write("GOTO:");
                        if (AnnotationAction.Get(PdfName.N).Equals(PdfName.FIRSTPAGE))
                        {
                            Trace.WriteLine(1);
                        }
                        else if (AnnotationAction.Get(PdfName.N).Equals(PdfName.NEXTPAGE))
                        {
                            Trace.WriteLine(Math.Min(I + 1, PageCount));
                            //'//Any links that go past the end of the document should just go to the last page
                        }
                        else if (AnnotationAction.Get(PdfName.N).Equals(PdfName.LASTPAGE))
                        {
                            Trace.WriteLine(PageCount);
                        }
                        else if (AnnotationAction.Get(PdfName.N).Equals(PdfName.PREVPAGE))
                        {
                            Trace.WriteLine(Math.Max(I - 1, 1));
                            //'//Any links the go before the first page should just go to the first page
                        }


                        //'//Otherwise see if its a GOTO page action

                    }
// Here I am finding that has some action 
                    else if (AnnotationAction.Get(PdfName.S).Equals(PdfName.GOTO))
                    {
                        //'//Make sure that it has a destination
                        if (AnnotationAction.GetAsArray(PdfName.D) == null)
                            continue;




                        //'//AnnotationAction.GetAsArray(PdfName.D) gets the destination
                        //'//AnnotationAction.GetAsArray(PdfName.D).ArrayList(0) get the indirect reference part of the destination (.ArrayList(1) has fitting options)
                        //'//DirectCast(AnnotationAction.GetAsArray(PdfName.D).ArrayList(0), PRIndirectReference) turns it into a PRIndirectReference
                        //'//The full line gets us an actual page object (actually I think it could be any type of pdf object but I have not tested that).
                        //'//BIG NOTE: This line really should have a bunch more sanity checks in place
                        
                        dynamic AnnotationReferencedPage = PdfReader.GetPdfObject((PRIndirectReference)AnnotationAction.GetAsArray(PdfName.D).ArrayList(0));
                        Trace.Write("GOTO:");
                        //'//Re-loop through all of the pages in the main document comparing them to this page
                        for (int J = 1; J <= PageCount; J++)
                        {
                            if (AnnotationReferencedPage.Equals(R.GetPageN(J)))
                            {
                                
                                Trace.WriteLine(J);
                                break; // TODO: might not be correct. Was : Exit For
                            }
                        }
                    }
Posted
Updated 26-Feb-14 2:59am
v4
Comments
CHill60 26-Feb-14 9:37am    
If the problem is with ArrayList(1) as you suggest, does ArrayList[1] work?

1 solution

C#
public List<PdfObject> ArrayList {
       get {
           return arrayList;
       }
}

From the source code of iTextSharp it's clear that ArratList is a List of PdfObjects and not really an array - for sure not a method.
To learn about List<t></t>, read here - http://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx[^]
To loop over all the references try this
C#
List<PdfObject> ArrayList = AnnotationAction.GetAsArray(PdfName.D).ArrayList;
foreach(PdfObject oAnnot in ArrayList)
{
  // find the page in original document...or whatever you want...
}
 
Share this answer
 
v3
Comments
Ashwani Gusain 26-Feb-14 6:27am    
Hi Peter ok, so can u please suggest me where i need change my code.
Kornfeld Eliyahu Peter 26-Feb-14 6:31am    
For that first I have to know what you trying to do!
Ashwani Gusain 26-Feb-14 7:07am    
Peter, i am trying collect all internal links and their destination in pdf file using ItextSharp.If u have better solution please update.

I am updating my code.
Kornfeld Eliyahu Peter 26-Feb-14 7:14am    
Did the same to my answer...
Ashwani Gusain 26-Feb-14 7:50am    
peter it showing some error please check

"
Cannot implicitly convert type 'iTextSharp.text.pdf.PdfObject' to 'System.Collections.Generic.List<itextsharp.text.pdf.pdfobject>"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900