# Attaching files using plain TeX (pdfTeX)

I am currently in the process of writing a document using plain TeX and would like to be able to attach other relevant files to the resulting pdf. I have experience using the attachfile, attachfile2 and embedfile packages (successfully) for this purpose (in LaTeX) in the past.

Whilst I could revert to using LaTeX, I would prefer to define a suitable macro for use in plain TeX. I have read the relevant sections of the pdf specification as well as the pdfTeX user manual however I have not yet been able to create a macro which makes this possible.

I would greatly appreciate any thoughts and/or assistance.

Edit 1: Reference Documents

Please find below links to relevant reference documents:

Edit 2: Current Solution Approach

System Information:

• Fedora 20
• pdfTeX 3.1415926

Please find below the code for a minimal working example as well as my current approach which uses only plain TeX and pdfTeX primitives.

File: alphabet.txt

This is the file that is to be attached to the document.

abcdefghijklmnopqrstuvwxyz


File: minimal.tex

This file makes use of the attachfile2 package to demonstrate how this can be achieved using LaTeX (pdfLaTeX).

\pdfcompresslevel=0
\documentclass{report}
\usepackage{attachfile2}
\begin{document}

Hello, world!

\textattachfile{alphabet.txt}{some annotation}

\end{document}


File: test.tex

\pdfcompresslevel=0
\catcode@=11
\def\res@space{ }

\newcount\res@count@obj@file
\newcount\res@count@obj@xform
\newcount\res@count@obj@fspec

\newdimen\res@box@dp
\newdimen\res@box@ht
\newdimen\res@box@wd

\def\res@embefile#1{%
\immediate\pdfobj stream attr{%
/Type /EmbeddedFile
/Params <<
/Size \pdffilesize{#1}
/ModDate (\pdffilemoddate{#1})
/CheckSum <\pdfmdfivesum file {#1}>
>>
} file {#1}
\res@count@obj@file=\the\pdflastobj
}

\def\res@appearance@box#1{%
\setbox1=\hbox{#1}
\immediate\pdfxform 1
\res@count@obj@xform=\the\pdflastxform
\res@box@dp=\the\dp1
\res@box@ht=\the\ht1
\res@box@wd=\the\wd1
}

\def\res@filespec#1{%
\immediate\pdfobj{
<<
/Type /Filespec
/FS (#1)
/EF <<
/F \the\res@count@obj@file\res@space 0 R
>>
>>
}
\res@count@obj@fspec=\the\pdflastobj
}

\def\res@annot{
\pdfannot   width \the\res@box@wd
height \the\res@box@ht
depth \the\res@box@dp {
/Subtype /FileAttachment
/AP <<
/N \the\res@count@obj@xform\res@space 0 R
/R \the\res@count@obj@xform\res@space 0 R
/D \the\res@count@obj@xform\res@space 0 R
>>
/F \the\res@count@obj@file
/FS \the\res@count@obj@fspec\res@space 0 R
}
}

\def\res@attachfile#1#2{%    Arg 1:  Annotation
%    Arg 2:  Name of file to be attached
\res@embedfile{#2}
\res@filespec{#2}
\res@appearance@box{#1}
\res@annot
}

Hello, world!

\res@attachfile{some annotation}{alphabet.txt}

\bye


Provided that alphabet.txt is stored in the same directory, the desired output can be produced by running:

pdflatex minimal.tex


My current approach is capable of producing a .pdf file which is viewable using Document Viewer (I have not yet checked whether it is capable of being opened by Adobe Reader). The .pdf can be produced by running:

pdftex test.tex


By comparing the output between the two approaches, it appears that the attachment is being embedded correctly. I am not however, able to 'see' the annotation on the pdf and I am unable to open the attachment.

It would be much appreciated if someone could identify the error for me.

• Oh! It is very nice that you are thinking of plain TeX equivalents of LaTeX constructions! Difficult, but very interesting. And BTW Welcome to TeX.sx! – Przemysław Scherwentke Oct 19 '14 at 22:57
• When you say you'd rather define suitable macros for use in plain TeX, do you mean that you explicitly wish to circumvent the miniltx.tex macros? – 1010011010 Oct 19 '14 at 23:27
• @1010011010 Thank you for making me aware of the miniltx.tex macros. It would be my preference to avoid using the miniltx.tex macros in order to avoid introducing dependencies. The purpose of that package appears to be introducing a minimal set of macros to plain TeX in order to make it possible to include LaTeX files directly. Please correct me if I am wrong as I only briefly looked at the file. – opensourceadvocate Oct 19 '14 at 23:57
• @PrzemysławScherwentke Thank you for your welcome! – opensourceadvocate Oct 20 '14 at 0:02
• @MarcelKorpel I used a MWE of \input navigator \embeddedfile[foo]{bar}[baz]{159.txt} ABC \finishpdffile \bye with XeTeX and it works for me in Acrobat Pro. – ChrisS Oct 24 '14 at 0:12

## 1 Answer

The navigator package allows access to links, attachments and other PDF features in Plain TeX, LaTeX and ConteXt. If you add

\input navigator


to your preamble and

\finishpdffile


to the end of your document, you can attach a file to your document with

\embeddedfile[<description>]{<object name>}[<alternate file name>]{<file>}


where <description> is the description listed in the PDF viewer, <object name> is the internal name of the attachment, <alternate file name> is the file name listed in the PDF viewer and <file>` is, of course, the file you want to attach.