How to highlight and make text selection in PDF using PDFTron Library in Xamarin IOS

952 views Asked by At

I had done code for Text Search. Then I had make highlight and Text Selection. But It wont' Worked. I am using PDF Net Library. I am using PDFDOC to open online pdf url. I had used TextSearch Method Search the document and returns upon the following circumstances: Reached the end of the document Reached the end of a page (if set to return by specifying mode 'e_page_stop' ) Found an instance matching the search pattern

. Here is the Code.

    public async void GetAnnotation()
    {// Sample code showing how to use high-level text extraction APIs.
        try
        {

            docToOpen.InitSecurityHandler();

            Int32 page_num = 0;
            String result_str = "", ambient_string = "";
            Highlights hlts = new Highlights();

            TextSearch txt_search = new TextSearch();
            Int32 mode = (Int32)(TextSearch.SearchMode.e_whole_word | TextSearch.SearchMode.e_page_stop | TextSearch.SearchMode.e_highlight);
            String pattern = "is";
            string contents;
            string Url = String.Format("https://whitecoatguru.azurewebsites.net/UserNote.asmx/GetUserNote?user_id=" + pv.user_id + "&resource_version_id=" + pv.resource_version_id + "");
            HttpClient hc = new HttpClient();
            contents = await hc.GetStringAsync(Url);
            List<UserNotes> values = JsonConvert.DeserializeObject<List<UserNotes>>(contents);
            pv.values = values;
            if (values.Count != 0)
            {

                for (int i = 0; i < values.Count; i++)
                {

                    //call Begin() method to initialize the text search.
                    txt_search.Begin(docToOpen, pattern, mode, 1, 1);

                    int step = 0;

                    //call Run() method iteratively to find all matching instances.
                    while (true)
                    {
                        TextSearch.ResultCode code = txt_search.Run(ref page_num, ref result_str, ref ambient_string, hlts);

                        if (code == TextSearch.ResultCode.e_found)
                        {


                            hlts.Begin(docToOpen);
                            while (hlts.HasNext())
                            {
                                pdftron.PDF.Page cur_page = docToOpen.GetPage(hlts.GetCurrentPageNumber());
                                double[] quads = hlts.GetCurrentQuads();



                                ///////////////////////////////////////////////
                                pv.x1cord = quads[0];
                                pv.y1cord = quads[1];
                                pv.x2cord = quads[4];
                                pv.y2cord = quads[5];






                                Obj annots = cur_page.GetAnnots();
                                if (annots == null)
                                {
                                    // If there are no annotations, create a new annotation 
                                    // array for the page.
                                    annots = docToOpen.CreateIndirectArray();
                                    cur_page.GetSDFObj().Put("Annots", annots);
                                }

                                // Create the Text annotation
                                Obj text_annot = docToOpen.CreateIndirectDict();
                                text_annot.PutName("Subtype", "Text");
                                text_annot.PutBool("Open", true);
                                text_annot.PutString("Contents", "The quick brown fox ate the lazy mouse.");
                                text_annot.PutRect("Rect", 10, 10, 10, 10);

                                // Insert the annotation in the page annotation array
                                annots.PushBack(text_annot);
                                EventHandler<AnnotationModificationEventArgs> annotationAdded = this.annotationAdded;
                                if (annotationAdded != null)
                                {
                                    IntPtr pageIntPtr = unchecked((IntPtr)(long)pageNumber);
                                    Int64 ptr_objc = annotation.GetHandleInternal();
                                    IntPtr ptr_cs = new IntPtr(ptr_objc);
                                    AnnotationModificationEventArgs args = new AnnotationModificationEventArgs(pageIntPtr.ToInt32());
                                    annotationAdded(Annot.CreateInternal(ptr_cs), args);
                                }
                                hlts.Next();
                            }


                            break;

                        }
                        else if (code == TextSearch.ResultCode.e_page)
                        {
                            //you can update your UI here, if needed
                        }
                        else
                        {
                            break;
                        }

                    }
                }
            }
        }

        catch (PDFNetException e)
        {
            Console.WriteLine(e.Message);
        }
     }
0

There are 0 answers