Let's suppose that we have an attached file:

\immediate\pdfobj stream file {\jobname.tex}

\pdfannot width 10pt height 10pt depth 0pt {
  /Subtype /FileAttachment
  /FS << /F (file)
  /Type /Filespec
  /EF << /F \the\pdflastobj\space 0 R >> >>}

It is attached via two operations - first is a kind of file resource, the second is a visible image, by clicking on which we open the file.

Now suppose that we need to put the image at current position. The file which must be attached to it is formed dynamically by something like

\immediate\write\fileout{some dynamic content}%

while the text which is written to the file line by line goes to the document also. This means that it becomes available at a later stage, than the image is inserted.

If I do something like this, the document is compiled correctly, but the pdf viewer crashes:


\newcount\fileno \fileno=0

\pdfannot width 8pt height 8pt depth 0pt {
/Subtype /FileAttachment
/FS <</Type /Filespec /F (test.txt)
/EF <</F \the\fileno\space 0 R>> >> /Name /PushPin}


\immediate\pdfobj stream file {test.tmp}%


Is it possible to point the annotation to a yet-non-existing file resource, which is guaranteed to exist before the end of the whole document?

It is desirable that this feature will work with LuaTeX.

Is it possible to find the answer in this resource?


If I put the file resource before the annotation, it complains:

! LuaTeX error (ext5): cannot open file for embedding.
l.6 \immediate\pdfobj stream file {test.tmp}

because LuaTeX is attaching the file contents in-place. An alternative solution would be to delay this operation until the end of the document, if this is at all possible.


Some useful info from http://tug.ctan.org/systems/doc/pdftex/manual/samplepdf/samplepdf.tex

User-defined object can be inserted into the pdf output by \pdfobj. The
object is written out as specified, apart from case when \pdfobj is used with
"stream" option. An object created by \pdfobj is held in memory and will
not be written to the pdf output, unless 1) the object is referenced by
saying \pdfrefobj <object number>; or 2) \pdfobj is preceded by
\immediate. The object number of the last object created by \pdfobj is
accessible via \pdflastobj
  • OT: \immediate\openout\fileout=file.tmp% better as \immediate\openout\fileout=file.tmp % or \immediate\openout\fileout=file.tmp\relax so that the file name is properly terminated (rather than relying on the following tokens).
    – Joseph Wright
    Dec 29, 2015 at 9:35

1 Answer 1


pdfTeX/LuaTeX provide \pdfobj reserveobjnum and \pdfobj useobjnum:


\pdfobj reserveobjnum

\pdfannot width 8pt height 8pt depth 0pt {
/Subtype /FileAttachment
/FS <</Type /Filespec /F (test.txt)
/EF <</F \fileno\space 0 R>> >> /Name /PushPin}


\immediate\pdfobj useobjnum \fileno stream file {test.tmp}


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .