64

I have a form as a PDF file. I would like to use LaTeX/TeX to overlay my text over the form, and send the output to either a print or another PDF file.

Is this possible? How would I go about it?

13 Answers 13

41

One idea is to include pdf page using pdfpages (see pdfpages at CTAN), and then put a tikzpicture on top. Tikzpicture can be absolutely positioned with remember picture, overlay option. The question is similar to Can I add tikzpictures to pages included with pdfpages.

A MWE would look like this:

\documentclass[12pt]{article}

\usepackage{pdfpages}
\usepackage{tikz}
\begin{document}
\includepdf[pagecommand={\begin{tikzpicture}[remember picture, overlay]\node at (current page.center) {overlayed text};\end{tikzpicture}}]{filename}
\end{document}
1
  • 1
    This is great. If you also need absolute coordinate positioning, check out this answer. Jul 12, 2020 at 18:13
22

A very simple but powerful way is to use the overpic package together with the LaTeX picture environment. It has a grid to position your text, you can work in absolute units or as a percentage of the figure dimensions, etc.

\usepackage[abs]{overpic}
\usepackage{pict2e}

Then you can use it anywhere

\begin{overpic}[scale=1.0,unit=1mm,grid]{Figure file}
    \put(26,75){Your stuff here}
    :
    :
\end{overpic}
13

You can use TikZ to place the form as image at the center of an otherwise empty page and then draw on it. This is similar to the suggested pdfpages solution but avoid passing around the tikzpictures, which isn't really necessary.

See also Drawing on an image with TikZ and Is there the easiest way to toggle (show/hide) navigational grids in TikZ? for related code.

You could even add real PDF form fields to it. See Creating fillable PDFs for how it can be done.

\documentclass[letterpaper]{article}% ensure identical page size

\usepackage{tikz}

\pagestyle{empty}
\begin{document}
% Page 1
\begin{tikzpicture}[remember picture,overlay]
    \node at (current page.center) {\includegraphics[page=1]{form}};
    \begin{scope}[shift={(current page.south west)},every node/.style={anchor=base west}]
        % Grid to help find the positions (remove in final version)
        \draw [help lines] (0,0) grid (current page.north east);
        \draw [help lines,thick] (0,0) grid [step=5cm] (current page.north east);
        %
        \node at (2cm,9.75cm) {John Doe};
        \node at (13cm,9.75cm) {\today};
    \end{scope}
\end{tikzpicture}
\clearpage
% Page 2
\begin{tikzpicture}[remember picture,overlay]
    \node at (current page.center) {\includegraphics[page=2]{form}};
    \begin{scope}[shift={(current page.south west)},every node/.style={anchor=base west}]
        % Grid to help find the positions (remove in final version)
        \draw [help lines] (0,0) grid (current page.north east);
        \draw [help lines,thick] (0,0) grid [step=5cm] (current page.north east);
        %
        %\node at (2.5cm,10.75cm) {John Doe};
    \end{scope}
\end{tikzpicture}
\clearpage
\end{document}

This example used the IEEE copyright form. Just download it and rename it to form.pdf. The second page actually does not include any fillable form fields, but I found it important to show how to handle multiple pages.

8

The eso-pic package provides a means for adding content to the background or foreground of a page at shipout - the package is an extension of the everyshi package. Therefore, you could typeset your overlay text in the foreground and source PDF pages (via the pdfpages package) using

\AddToShipoutPictureFG*{%
  <your overlay text>
}
\includepdf[pages=X]{<source pdf>}%

where X refers to the specific page in <source pdf> you want to overlay with <your overlay text>. This addition of overlay text has to be done on a per-page basis (hence the starred * version of \AddToShipoutPictureFG). eso-pic provides a means for adding a grid in order to fine-tune the placement of content, as well as some "helper macros" the are predefined in terms of the layout position:

  • \AtPageUpperLeft{...} - paper-related positions
  • \AtPageLowerLeft{...}
  • \AtPageCenter{...}
  • \AtTextUpperLeft{...} - text block-related positions
  • \AtTextLowerLeft{...}
  • \AtTextCenter{...}
  • \AtStockUpperLeft{...} - stock-related positions (if using the memoir document class)
  • \AtStockLowerLeft{...}
  • \AtStockCenter{...}

These placement macros could be nested inside \AddToShipoutPictureFG* for relative positioning.

1
  • I got pdfpages to render a whole 210 page pdf from the outset which seems most practical than to put page=1 page=2 etc etc but how would you begin writing on the next page would it just be a case of\newpage and you will be on the next page beginning a new shipoutforground text box... im just a little sketpical about this method thats all and feel that at the 75th page when i go onto a new page everything will mess up and ill be back to the drawing board. So im actually using overpic on each page atm , takes about 5 minutes to enter the page numbers for 140 page document but by default the floa
    – john
    Sep 5, 2011 at 22:29
7

This answer by Michael Underwood to question PDF letterhead as document background provides a one-line solution using the wallpaper package.

6

Another approach to this is to use the pdfoverlay package. (Disclaimer: I'm the package author.)

From the CTAN package description:

It is often desirable to take an existing PDF and easily add annotations or text overlaying the PDF. This might arise if you wish to add comments to a PDF, fill in a PDF form, or add text to a PDF where space has been left for notes.

This package provides a simple interface to do this without having to resort to inserting one page at a time. Some or all of the pages of the PDF can be included and not all pages of the PDF need have overlayed text. It is also possible to include text between pages of the PDF.

Another advantage of this package is that the overlayed text can be set as normal flowing from one page to another or with manual page breaks if you wish. It is also possible to use any standard method to position text at arbitrary places on a given page.

A simple document might look like:

\documentclass{article}
\usepackage{pdfoverlay}
\pdfoverlaySetPDF{filename.pdf}
\begin{document}
Annotation on page 1.
\pdfoverlayIncludeToPage{10}
Annotation on page 10.
\pdfoverlayIncludeToPage{50}
Annotation on page 50.
\pdfoverlayIncludeToLastPage
\end{document}
1
  • Combined with the textpos package, which can position text at an absolute location, this appears to be the most straightforward solution. Sep 16, 2020 at 8:19
4

Ipavlic's excellent answer does not explicitly include the case for putting a single annotation on a multi-page PDF. To make this work requires making multiple calls to \includepdf as follows:

\documentclass[12pt]{article}
\usepackage{pdfpages}
\usepackage{tikz}
\begin{document}
\includepdf[pagecommand=
{\begin{tikzpicture}[remember picture,overlay]
 \node at (current page.center) {overlayed text};
 \end{tikzpicture}}
,pages=1]{filename.pdf}
\includepdf[pages=2-]{filename.pdf}
\end{document}

This adds "overlayed text" to the first page of filename.pdf.

4

If one has to add very simple text (like a few words, in my case), and is already using pdfpages, the package comes with the picturecommand option and the \put command, which can be used as follows, in a very straightforward way:

\includepdf[pages=1-4]{example.pdf}
\includepdf[pages=5, % the page where the text has to be put
         picturecommand*={%
         \put(200,300){My name}%
         \put(200,350){My address}%
    }]{example.pdf}
\includepdf[pages=6-end]{example.pdf}

Source

3

I've just used LaTeX to fill in a form that was sent to me as a PDF. The form was not a proper PDF form, just an ordinary PDF with spaces where I should write/type. Previously, I've either used xournal or used the method outlined by ipvalic above. I prefer the control of LaTeX but find it a little tedious with positioning. However, I recently was told about WhizzyTeX and advi (via How can I execute a macro for every node in TikZ?) and thought that it might make it easier to do this. Which it does!

Here's my document:

\documentclass[a4paper]{article}
\usepackage{tikz}
\usepackage{wallpaper}
\usepackage{ifpdf}
\thispagestyle{empty}
\pagestyle{empty}

\ifpdf
\def\bgext{pdf}
\def\adviedit#1#2{}
\else
\usepackage{advi}
\def\bgext{ps}
\fi


\makeatletter
\tikzset{
  advi/.style={
    append after command={[advi/set advi={#1}]},
    anchor=south west,
    advi/.cd,
    #1,
    /tikz/at={(\pgfkeysvalueof{/tikz/advi/x},\pgfkeysvalueof{/tikz/advi/y})}
  },
  advi/.cd,
  x/.initial=0,
  y/.initial=0,
  set advi/.code={
    \tikz@scan@one@point\pgfutil@firstofone(\tikzlastnode.north east)
    \pgf@xa=\pgf@x
    \pgf@ya=\pgf@y
    \tikz@scan@one@point\pgfutil@firstofone(\tikzlastnode.south west)
    \pgfmathsetmacro{\advi@node@w}{(\pgf@xa - \pgf@x)/1em}%
    \pgfmathsetmacro{\advi@node@h}{(\pgf@ya - \pgf@y)/1em}%
    \adviedit{comm=\advinode,w=\advi@node@w,h=\advi@node@h,#1}{}%}
  }
}
\makeatother

\newcommand{\advinode}[1]{%
  \node[advi={#1}]
}

\begin{document}
\CenterWallPaper{1}{FormToFill.\bgext}%
\begin{tikzpicture}[x=1em,y=1em,overlay]
\advinode{x=0,y=0}{Andrew Edgell Stacey};
\end{tikzpicture}
\end{document}

It's mostly taken from How can I execute a macro for every node in TikZ?.

When used with WhizzyTeX mode in Emacs and the advi previewer then it is possible to drag the nodes around on the page and so position them exactly where wanted. Thus control over the text via TeX and control over the positioning via drag-and-drop. Perfect.

The main wrinkle with this method is that the previewer is a DVI previewer, not PDF. So to display the PDF background we have to have a version available that the previewer will display. Experiments show that PS is fine. When compiling with pdflatex then it complains a lot about non-DVI specials, so we turn off the special repositioning stuff if compiled with pdflatex. The idea is that we use advi to get the positioning right but then use pdflatex to produce the final version.

I'm not claiming to have figured out all the best ways to do this - it's only the first time I've used it and it took a little tweaking to get it a working system, but next time it'll be much, much easier (if anyone has any suggestions for improvements, please let me know).

And, yes, that is my middle name.

2
1

Nothing wrong with TiKZ pictures, but let me mention that a regular LaTeX picture environment will also do for this job.

0

You could use 'IPE' (the "extensible drawing editor", http://ipe7.sourceforge.net/) to import the pdf with the pdftoipe command and typeset 'latex' text and draw vector graphics on it and save it as pdf.

Possible drawback: I admit that I'm not sure what it would do to text already included in the pdf. If the text is recognised, it will be typeset as latex "boxes", which might change the typeface etc. That might also depend on the pdf you want to import.

The advantage is:
You have a GUI and can place the text boxes where you want, align them, add layers etc. Just have a look at the manual: http://ipe7.sourceforge.net/manual/manual.pdf

If you can make available the pdf you want to "write on", I could give it a try, however I'm no IPE expert (so if I can't make it work, this won't mean that it is impossible). You'll find a very active and responsive community of developer and users here: http://old.nabble.com/Ipe---General-f1929.html

0

Xournal++ works well enough for me. They have an "Add/Edit Tex" button where you can add and edit LaTeX. It supports Window, macOS, and Linux. Of course, you do need a LaTeX distribution for it to work (I have MikTeX, and it works fine).

0

In case the dimensions of the original pdf must be kept, I found the following solution the easiest to work with:

\documentclass[12pt]{standalone}
\usepackage{graphicx}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}
        \node at (0,0) {\includegraphics{myfile.pdf}};
        \node at (0,0) {my overlay text};
    \end{tikzpicture}
\end{document}

You must log in to answer this question.

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