pdf-generationpdftk

How to reuse a path in PDF generation?


I am learning the PDF "syntax" and try to create various PDF documents manually (on Windows 7, Notepad++ to write the 1st unreferenced, broken, file, then run them through pdftk to produce a valid file with updated references, as explained here...).

My learning materials:

My question : I would like to create a path only once in the document, then possibly reuse it many times in other parts of the same document. E.g. I could define a logo once then reuse it in different pages, maybe several times at different positions in the same page, maybe with different scaling factors... What is the best way to achieve this?

To give a better idea, I could define a logo (here a cross) that way :

3 0 obj
<< /Length 32>>
stream
10 0 10 30 re f
0 10 30 10 re S
endstream
endobj

And would like to reuse the same "shape" (possibly with different scalings) in other places in the document, without having to specify the path again.

I am not looking for a software to do the job (eg. Acrobat...). I want to learn how to write this manually (then ask pdftk to fix the file).


Solution

  • Kurt Pfeifle's answer seems to work only for images. For path objects you have to use an XObject of subtype Form. Here is a working example:

    %PDF-1.0
    %¥±ë
    1 0 obj
    <<
    /Type /Catalog
    /Pages 2 0 R
    >>
    endobj
    2 0 obj
    <<
    /Type /Pages
    /Kids [ 3 0 R ]
    /Count 1
    >>
    endobj
    3 0 obj
    <<
    /Type /Page
    /Parent 2 0 R
    /MediaBox [0 0 297.6377952756 389.7637795276]
    /Contents 4 0 R
    /Resources << /XObject << /Mypath 5 0 R >> >>
    >>
    endobj
    4 0 obj
    <<
    /Length 37
    >>
    stream
    /Mypath Do
    1 0 0 1 30 0 cm
    /Mypath Do
    endstream
    endobj
    5 0 obj
    <<
    /Type /XObject
    /Subtype /Form
    /BBox [20 0 50 50]
    /Length 18
    >>
    stream
    20 0 m 50 50 l h S
    endstream
    endobj
    xref
    0 6
    0000000000 65535 n 
    0000000017 00000 n 
    0000000066 00000 n 
    0000000125 00000 n 
    0000000280 00000 n 
    0000000367 00000 n 
    trailer
    <<
    /Root 1 0 R
    /Size 6
    >>
    startxref
    484
    %%EOF
    

    The Page object defines a resource Mypath (object 5). This objects defines a line from (20, 0) to (50, 50). The page content (object 4) uses the defined path with its current coordinates (/Mypath Do), then translates the graphic state by 30 units to the right (1 0 0 1 30 0 cm), than again draws the path (this time at (50, 0) to (80, 50)).