Search code examples
pdfpdf-generation

Add hyperlink to PDF


It seems to be an easy thing, but I continuously failing to create a link for a text in a PDF file. I have already checked the specifications, also checked previous answers here. This is what I created so far: DOWNLOAD

  1. An object - numbered as 6 - is created holding the hyperlink as : <</Type /Annot /Subtype /Link /Rect[ 99.21 754 109 800] /BS <</W 2>> /F 4 /A << /Type /Action /S /URI /URI (http://google.com/) >>
  2. In the page object I made a reference to this annotation: <</Type /Pages /Count 1 /MediaBox [0 0 595.3 841.9] /CropBox [0 0 595.3 841.9] /Kids [ 9 0 R ] /Resources 4 0 R /Annots [6 0 R]>>

But in the result the link box is not shown. Can somebody help me, why?

Solution

  • You have placed the Annots in an intermediate node of the pages tree, not a leaf node. Annots don't inherit down the tree hierarchy, so you need to put the Annots array in the page leaf node, not the tree.

    What you have is:

    3 0 obj
    <<
      /Type /Pages 
      /Count 1 
      /MediaBox [0 0 595.3 841.9]
      /CropBox [0 0 595.3 841.9]
      /Kids [ 9 0 R ]
      /Resources 4 0 R
      /Annots [6 0 R]
    >>
    

    That's the Pages tree, note that it has a /Kids key, so it's an intermediate node (in this case the origin), not a leaf. The Resources, CropBox and MediaBox are inherited, so any /Page leaf node which doesn't have those set will inherit the ones from preceding intermediate nodes. But Annots don't inherit that way.

    Your Page currently looks like this:

    9 0 obj
    <<
      /Type /Page
      /Parent 3 0 R
      /Contents 10 0 R
      /Rotate 0
    >>
    endobj
    

    Try editing it like this:

    9 0 obj
    <<
      /Type /Page
      /Parent 3 0 R
      /Contents 10 0 R
      /Rotate 0
      /Annots [6 0 R]
    >>
    endobj
    

    The next problem is that your Annotation is incorrect, what you have is:

    6 0 obj
    <<
      /Type /Annot
      /Subtype /Link
      /Rect[ 99.21 754 109 800]
      /BS <</W 2>>
      /F 4
      /A << 
        /Type /Action
        /S /URI
        /URI (http://google.com/)
      >>
    

    The first problem is that the number of dictionary open marks '<<' doesn't match the number of closing marks '>>'. So you've left a dictionary open. In addition you don't have an endobj to terminate the object, which is also invalid. You'll need to correct both of those:

    6 0 obj
    <<
      /Type /Annot
      /Subtype /Link
      /Rect[ 99.21 754 109 800]
      /BS <</W 2>>
      /F 4
      /A << 
        /Type /Action
        /S /URI
        /URI (http://google.com/)
      >>
    >>
    endobj
    

    before a PDF consumer can do anything with it.

    On top of those problems, the length of the page content stream (object 10) is incorrect. It's given as a reference to an indirect object, object 11, and that has a value of 0:

    10 0 obj
    << /Length 11 0 R >>
    stream
    BT
    /_Calibri 16 Tf
    99.21 754.02 Td
    (TEST LINK) Tj
    ET
    endstream
    endobj
    11 0 obj
    0
    endobj
    

    ... ...

    11 0 obj
    0
    endobj
    

    Also the number of entries in the Widths array for the font is wrong, there are 192 entries, but the font declares FirstChar as 32 and LastChar as 255, so there should be 223 entries.