Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Mon Apr 12, 2021 11:56 am

According to docs, several examples and forum posts I added the following code to my project:

Code: Select all
            using var doc = new PdfDocument();

            doc.LoadFromFile(docPath);

            foreach (PdfPageBase page in doc.Pages)
            {
                foreach(PdfTextWebLinkAnnotationWidget link in page.AnnotationsWidget)
                {
                    var linkUrl = link.Url;

                    linkUrl.AddQueryParam("refId", model.Meta.ReferrerId ?? refId)
                           .SetQueryParam("utm_campaign", "referral")
                           .SetQueryParam("utm_source", "handout01")
                           .SetQueryParam("utm_medium", "pdf");

                    link.Url = linkUrl;
                }
            }


On line with getting current value of link.Url I get a System.NullReferenceException. I can not read the property. If I try only to set the link.Url to "google_com" all links in the PDF will get updated to that value.

Why can't I read the link.Url property? How to alter the current link? I habe to take current value and add some query parameters to it.

Thanks and regards,
Sven
Regards

Sven

DarthSonic
 
Posts: 4
Joined: Mon Apr 12, 2021 11:50 am

Tue Apr 13, 2021 10:50 am

Hello Sven,

Thanks for your inquiry.
Please refer to the following code to get the link and alter the current link. If there are still any issues, please provide your PDF file for further investigation.
Code: Select all
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile("LinkAnnotation.pdf");

            List<string> links = new List<string>();
            foreach (PdfPageBase page in doc.Pages)
            {
                PdfAnnotationCollection pdfAnnotations = page.AnnotationsWidget;

                foreach (PdfAnnotation pdfAnnotation in pdfAnnotations)
                {
                    if (pdfAnnotation is PdfTextWebLinkAnnotationWidget)
                    {
                        PdfTextWebLinkAnnotationWidget link = pdfAnnotation as PdfTextWebLinkAnnotationWidget;
                        links.Add(link.Url);
                        link.Url = "http://www.e-iceblue.com";// alter the current link
                    }
                    else if (pdfAnnotation is PdfUriAnnotationWidget)
                    {
                        PdfUriAnnotationWidget link = pdfAnnotation as PdfUriAnnotationWidget;
                        links.Add(link.Uri);
                        link.Uri = "http://www.e-iceblue.com";// alter the current link
                    }
                }
            }
            doc.SaveToFile("result.pdf");



Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Return to Spire.PDF

cron