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.

Fri Apr 09, 2021 8:08 pm

Hi

I'm trying to add a hyperlink in the footer of a pdf.

I used PdfTextWebLink and i get an error object not set to an instance of an object - see CODE1

I used PdfHTMLTextElement but the hyperlink doesn't show - see CODE2

I tied PdfFileLinkAnnotation but itn't not working with PdfPageTemplateElement

Please advise
Thank you

CODE 1
Dim brush As PdfBrush = PdfBrushes.Black
Dim pen As PdfPen = PdfPens.Black
Dim text As String = "Download the Carbon Footprint Calculator"
Dim link2 As New PdfTextWebLink()
link2 = New PdfTextWebLink()
link2.Text = text
link2.Url = "https://www.ail.ca/wp-content/uploads/2021/04/CSP-vs-RCP-Carbon-Footprint-Calculator-2.xlsx"
link2.Font = font
link2.Pen = pen
link2.Brush = brush
link2.StringFormat = New PdfStringFormat
link2.StringFormat = FormatL
pageLeftTop = New PointF(50, y)

link2.DrawTextWebLink(bottomSpace.Graphics, pageLeftTop)

CODE 2
hText = "Corrugated Steel Pipe is <b><font color='#32CD32'>greener</font></b> than Reinforced Concrete Pipe.<a href='https://www.ail.ca/wp-content/uploads/2021/04/CSP-vs-RCP-Carbon-Footprint-Calculator-2.xlsx'> Download the Carbon Footprint Calculator </a> to see the difference. It's easy!"
Dim richTextElement As New PdfHTMLTextElement(hText, font, PdfBrushes.Black)
richTextElement.TextAlign = TextAlign.Left
pageLeftTop = New PointF(45, y)

richTextElement.Draw(bottomSpace.Graphics, pageLeftTop, pageSize.Width, y)

cmarin
 
Posts: 9
Joined: Fri Jun 05, 2015 12:46 pm

Mon Apr 12, 2021 7:12 am

Hello,

Thanks for your inquiry!

For the issue of code1 throws error “object not set to an instance of an object”: I have reproduced your issue and logged it in our issue tracking system with the ticket SPIREPDF-4176 for further investigation. Sorry for the inconvenience caused.

For the issue of code2 doesn't show the hyperlink: Kindly note that the PdfHTMLTextElement cannot add the HTML string contains the URL to a PDF.

For the issue of PdfFileLinkAnnotation: Sorry that PdfFileLinkAnnotation just supports to link to an existing file, and it cannot link to a URL.

Hope you can understand. If there is any update about SPIREPDF-4176, we will keep you informed.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Mon Apr 12, 2021 11:49 am

I'm trying to open an existing file that's on our file server.
Is there anyway I can achieve that? the Pdfs are sent to people outside of the company.

Much appreciated

cmarin
 
Posts: 9
Joined: Fri Jun 05, 2015 12:46 pm

Tue Apr 13, 2021 10:16 am

Hello,

Thanks for your feedback!

As one method, please refer to the following code to draw the PdfTextWebLink to each page first, and then add the FooterTemplate to the PDF file. In this way, you can download the file through the link of the PdfTextWebLink, and then open it.

Code: Select all
        Dim pdf As PdfDocument = New PdfDocument()
        pdf.Pages.Add()
        pdf.Pages.Add()
        pdf.Pages.Add()
        pdf.Pages.Add()

        Dim margins As New PdfMargins(60, 60, 60, 60)
        For Each page As PdfPageBase In pdf.Pages
            Dim ponit As PointF = New PointF(80, page.Size.Height - 100)
            CreateLink(page, ponit)
        Next
        pdf.Template.Bottom = CreateFooterTemplate(pdf, margins)
        pdf.SaveToStream(ms, FileFormat.PDF)

    Private Function CreateFooterTemplate(doc As PdfDocument, margins As PdfMargins) As PdfPageTemplateElement
        'get page size
        Dim pageSize As SizeF = doc.PageSettings.Size

        'create a PdfPageTemplateElement object which works as footer space
        Dim footerSpace As New PdfPageTemplateElement(pageSize.Width, margins.Bottom)
        footerSpace.Foreground = False

        'declare two float variables
        Dim x As Single = margins.Left
        Dim y As Single = 0

        'draw line in footer space
        Dim pen As New PdfPen(PdfBrushes.Gray, 1)
        footerSpace.Graphics.DrawLine(pen, x, y, pageSize.Width - x, y)

        'draw text in footer space
        y = y + 5
        Dim font As New PdfTrueTypeFont(New Font("Impact", 10.0F), True)
        Dim format As New PdfStringFormat(PdfTextAlignment.Left)

        'draw dynamic field in footer space
        Dim number As New PdfPageNumberField()
        Dim count As New PdfPageCountField()
        Dim compositeField As New PdfCompositeField(font, PdfBrushes.Gray, "Page {0} of {1}", number, count)
        compositeField.StringFormat = New PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Top)
        Dim size As SizeF = font.MeasureString(compositeField.Text)
        compositeField.Bounds = New RectangleF(pageSize.Width - x, y, size.Width, size.Height)
        compositeField.Draw(footerSpace.Graphics)

        'return footerSpace
        Return footerSpace
    End Function

    Private Sub CreateLink(page As PdfPageBase, point As PointF)

        Dim font2 As New PdfTrueTypeFont(New Font("Verdana", 10, System.Drawing.FontStyle.Bold))
        Dim text As String = "Download the Carbon Footprint Calculator"
        Dim link2 As New PdfTextWebLink()
        link2.Text = text
        link2.Url = "https://www.ail.ca/wp-content/uploads/2021/04/CSP-vs-RCP-Carbon-Footprint-Calculator-2.xlsx"
        link2.Font = font2
        link2.Brush = PdfBrushes.DarkSeaGreen
        link2.DrawTextWebLink(page.Canvas, point)
    End Sub

Else, you can consider referring to the following code to add the file which from your URL as an attachment to the PDF file, and then open this file.
Code: Select all
        Dim url As String = "https://www.ail.ca/wp-content/uploads/2021/04/CSP-vs-RCP-Carbon-Footprint-Calculator-2.xlsx"
        Dim myWebClient As New WebClient()
        Dim fileBytes As Byte() = myWebClient.DownloadData(url)

        Dim pdf As PdfDocument = New PdfDocument()
        Dim attachment As New PdfAttachment("customName.xlsx", fileBytes)
        attachment.Description = "the downloadfile"
        pdf.Attachments.Add(attachment)
        pdf.SaveToFile("attach.pdf", FileFormat.PDF)


Anyway, if there is any progress with SPIREPDF-4176, we will keep you informed.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Thu Apr 15, 2021 8:12 pm

Hi
Is it possible to add hyperlink in PdfGrid?
Thank you

cmarin
 
Posts: 9
Joined: Fri Jun 05, 2015 12:46 pm

Fri Apr 16, 2021 8:01 am

Hello,

Thanks for your inquiry!

Kindly note that the PDF document specification did not have the concept of the Grid, so if you want to add a hyperlink to the grid, you need to fill in the target text to the grid first, and then add the hyperlink to the target text area.

Please refer to the following code to achieve this. If you encounter any question, please feel free to contact us.

Code: Select all
        Dim rectangle As RectangleF = New RectangleF
        Dim res() As PdfTextFind = Nothing
        For Each pages As PdfPageBase In pdf.Pages
            'Find text
            res = pages.FindText("E-iceblue home", TextFindParameter.WholeWord).Finds
        Next
        For Each find As PdfTextFind In res
            rectangle = find.Bounds
            Dim link3 As PdfUriAnnotation = New PdfUriAnnotation(rectangle)
            link3.Border = New PdfAnnotationBorder(0)
            link3.Uri = "http://www.e-iceblue.com"
            CType(find.SearchPage, PdfNewPage).AnnotationsWidget.Add(link3)
        Next


Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Fri Apr 16, 2021 3:52 pm

It didn't work for me.
It finds the text but it doesn't put the hyperlink in.

cmarin
 
Posts: 9
Joined: Fri Jun 05, 2015 12:46 pm

Mon Apr 19, 2021 6:47 am

Hello,

Thanks for your feedback!

To help us investigate your issue quickly and efficiently, please provide us with your input file (if any), testing code for reference. Thanks in advance.

Besides, the issue SPIREPDF-4176 is in the testing phase now, once tests pass, we will prepare a hotfix version for you asap.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Tue Apr 27, 2021 9:54 am

Hello,

Thanks for your patience!

Glad to inform you that we just released Spire.PDF Pack(Hot Fix) Version:7.4.13 which fixes the issue of the application threw the error "Object reference not set to an instance of an object" when drawing web link on PDF page template element (SPIREPDF-4176).

Please download the fix version from the following links and test.

Website link: https://www.e-iceblue.com/Download/download-pdf-for-net-now.html
Nuget link:
https://www.nuget.org/packages/Spire.PDF/7.4.13
https://www.nuget.org/packages/Spire.PDF.NETCore/7.4.13

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Return to Spire.PDF