Unable get textbox annotation

I want to get the newest annotation that i have create and insert text to it programatically, but page.Annotations.Count always return 0, can anybody help me with this?              

Below is my example code:      

var currentPage = pdfViewer.CurrentPage;

PdfLoadedDocument loadedDoc = pdfViewer.LoadedDocument;

PdfPageBase page = loadedDoc.Pages[currentPage];

page.Annotations[page.Annotations.Count - 1].Text = string.Format("«{0}»", x.Code);


5 Replies

MS Manigandan Shanmugam Syncfusion Team November 3, 2023 10:51 AM UTC

Hi Nguyen,

After reviewing the information provided, we have determined that you are currently obtaining the page number instead of the page index, and retrieving the annotation based on this page number. As a result, the annotation count is showing as 0. To resolve this issue, subtract 1 from the current page number to obtain the accurate page index. We have attached the sample for your reference.

Please refer the below code snippet.

            var currentPage = pdfViewer.CurrentPage-1;

            PdfLoadedDocument loadedDoc = pdfViewer.LoadedDocument;

            PdfPageBase page = loadedDoc.Pages[currentPage];

            page.Annotations[page.Annotations.Count - 1].Text = string.Format("Hello");


Attachment: sample_4a871a02.zip


NT Nguyên Tru?ng replied to Manigandan Shanmugam November 6, 2023 03:53 AM UTC

I try your way and it working, but another problem appear that is 
page.Annotations[page.Annotations.Count - 1].Text = string.Format("Hello");

but display text on .pdf file didn't change, after save it always empty and next time call text it always retu



MS Manigandan Shanmugam Syncfusion Team November 6, 2023 12:19 PM UTC

Based on the provided information, it seems that your intention is to incorporate text into the free text annotation. The text property of the annotation is used to include text notes in the annotation. To display the text within the text box of the free text annotation, we utilize the markup text property. We have attached the sample for your reference.

Please find the below code snippet for your reference.

        private void Button_Click(object sender, RoutedEventArgs e)

        {

            var currentPage = pdfViewer.CurrentPage-1;

            PdfLoadedDocument loadedDoc = pdfViewer.LoadedDocument;

            PdfPageBase page = loadedDoc.Pages[currentPage];

 

            if(page.Annotations[page.Annotations.Count-1] is PdfFreeTextAnnotation)

            {

                PdfFreeTextAnnotation pdfFreeTextAnnotation = page.Annotations[page.Annotations.Count - 1] as PdfFreeTextAnnotation;

                pdfFreeTextAnnotation.MarkupText = "Free text applied";

            }

        }


Attachment: sample_bc72f3b4.zip


NT Nguyên Tru?ng replied to Manigandan Shanmugam November 29, 2023 10:56 AM UTC

Hi Shan, I was follow your code and then i have met a problem, that is after i change the MarkUpText of PdfLoadedDocument, i save file but it not change the MarkUpText of the original file, can you help me solve the problem? Thank you

                    foreach (var dic in displayableDic)

                    {

                        for (int i = 0; i < pdf.Pages.Count; i++)

                        {

                            var pdfPage = pdf.Pages[i];

                            for (int j = 0; j < pdfPage.Annotations.Count; j++)

                            {

                                if (pdfPage.Annotations[j] is PdfLoadedFreeTextAnnotation)

                                {

                                    PdfLoadedFreeTextAnnotation annotation = pdfPage.Annotations[j] as PdfLoadedFreeTextAnnotation;

                                    if (annotation.MarkUpText != null && annotation.MarkUpText.Length > 0)

                                    {

                                        annotation.MarkUpText = dic.Value;

                                    }

                                }

                            }

                        }

                    }





MS Manigandan Shanmugam Syncfusion Team November 30, 2023 02:37 PM UTC

Upon reviewing the provided information, we have concluded that you are currently acquiring the PdfLoadedFreeTextAnnotation and setting the markup text. In our PDFviewer, the annotations on rendered pages are stored as PdfFreeTextAnnotation, while the annotations on non-rendered pages are stored as PdfLoadedFreeTextAnnotation. Therefore, the markup text for certain free text annotations is not preserved. To resolve this problem, we need to evaluate the PdfLoadedFreeTextAnnotation and PdfFreeTextAnnotation, and incorporate the markup text appropriately. We have attached a sample for your reference.


Please refer the below code snippet.


            PdfLoadedDocument loadedDoc = pdfViewer.LoadedDocument;

            for (int i = 0; i < loadedDoc.Pages.Count; i++)

            {

                var pdfPage = loadedDoc.Pages[i];

                for (int j = 0; j < pdfPage.Annotations.Count; j++)

                {

                    if (pdfPage.Annotations[j] is PdfFreeTextAnnotation)

                    {

                        PdfFreeTextAnnotation annotation = pdfPage.Annotations[j] as PdfFreeTextAnnotation;

                        annotation.MarkupText = "Free text applied";

                    }

                    else if (pdfPage.Annotations[j] is PdfLoadedFreeTextAnnotation)

                    {

                        PdfLoadedFreeTextAnnotation annotation = pdfPage.Annotations[j] as PdfLoadedFreeTextAnnotation;

                        annotation.MarkUpText = "Free text applied";

                    }

                }

            }


Attachment: sample_e204b168.zip

Loader.
Up arrow icon