Adding hyperlink to PDF (pdf specefication)

So, I want to generate PDF file without using any libraries. I able to create some text, but how can I add hyperlink to it? For example, I have this block:

14 0 obj
<<
/Length 15 0 R
>>
stream
/DeviceRGB CS
/DeviceRGB cs
q
1.0 1.0 1.0 scn
1.0 1.0 1.0 SCN
10.000000 974.900000 38.440000 11.160000 re B
0.0 0.0 0.0 scn
BT
10.000000 977.690000 Td
/Res0 10.000000 Tf
(asdfas) Tj
ET
Q
endstream
endobj

Res0 - name of font resource. What should I add to create hyperlink?

Upvotes: 1

Views: 261

Answers (1)

eazimmerman
eazimmerman

Reputation: 609

The following is taken from the link I provided in my comment.

The following line (written as html)

Here is some text <a href="http://www.w3.org/WAI/"> with a link </a> inside.

can be represented in a PDF as

 /P <</MCID 0>>                                                %Marked Content Sequence 0 (paragraph)
  BDC                                                          %Begin marked content sequence
   BT                                                          %Begin text object
    /F1 11.04 Tf                                               %set text font and size
    1 0 0 1 72.024 709.54 Tm                                   %set text matrix
    0 g                                                        %set non stroking color to black
    0 G                                                        %set stroke color to black
   [(H)3(ere )-4(is s)10(o)5(m)-4(e)9( t)-3(e)9(xt)-3( )] TJ   %Show text preceding the link" Here is some text"
   ET                                                          %end text object
  EMC                                                          %end marked content sequence

 /Span <</MCID 1>>                                             %Marked Content Sequence 1 (underlined link text)
  BDC                                                          %Begin marked content sequence
   BT                                                          %Begin text object
    1 0 0 1 152.42 709.54 Tm                                   %set text matrix
    0 0 1 rg                                                   %set non-stroking color to blue
    0 0 1 RG                                                   %set stroke color to blue
    [(with a )-2(li)3(n)14(k)] TJ                              %Show link text " with a link"
   ET                                                          %end text object
    0 0 1 rg                                                   %set stroke color to blue
    152.42 707.62 45.984 0.72 re                               %rectangle operator - target area for the link
    f*                                                         %fill the path using the even-odd rule
  EMC                                                          %end marked content sequence

 /P <</MCID 2>>                                                %Marked Content Sequence 2 (paragraph)
  BDC                                                          %Begin marked content sequence
   BT                                                          %begin text object
    1 0 0 1 198.41 709.54 Tm                                   %set text matrix                                            
    0 g                                                        %set non stroking color to black
    0 G                                                        %set stroke color to black
    [( )] TJ                                                   %empty text string showing white space
   ET                                                          %end text object
   BT                                                          %begin text object
    1 0 0 1 200.93 709.54 Tm                                   %set text matrix
    [(in)5(sid)5(e.)] TJ                                       %show text following the link "inside."
   ET                                                          %end text
   BT                                                          %begin text object
    1 0 0 1 229.97 709.54 Tm                                   %set text matrix
    [( )] TJ                                                   %empty text string showing white space
   ET                                                          %end text object
  EMC                                                          %end marked content sequence 

and to add the logic to the link use

 11 0 obj                                              %Object ID 11, generation   0, obj keyword
  <</K[1                                               %immediate child of the structure tree root
   <<
    /Obj 26 0 R                                        %reference to Object 26
    /Type/OBJR                                         %this object describes an indirect object reference
   >>]
    /P 12 0 R
    /Pg 17 0 R
    /S/Link
  >>
 endobj

 26 0 obj                                              %object ID 26 which is referenced by the OBJR in Object 11
  <</A 31 0 R
   /BS
   <</S/S
     /Type/Border
     /W 0
   >>
   /Border[0 0 0]                                      %a colorless border
   /H/I
   /Rect[150.128 694.558 200.551 720.0]                %the boundaries defining target area where link annotation is active
   /StructParent 1
   /Type/Annot                                         %Structure element is an annotation
   /Subtype/Link
  >>                                                   %It is a link annotation                                                 
 endobj     
 31 0 obj                                              %Object 31, gen 0, obj
  <</S/URI                                             %Object type is URI action
    /URI(http://www.w3.org/WAI)                        %The Uniform resource identifier to resolve
  >>   
 endobj

Upvotes: 1

Related Questions