# Drawing on a background image

I'd like to take pages of an existing PDF (which are simply images), and draw several boxes on each page. (The bounding boxes of words as detected by an external OCR program.)

What I've tried so far:

• can use \includepdf (from the pdfpages package) with option [fitpaper=true] to make pages of the resulting PDF same as those of the original PDF.

• can use TikZ to draw rectangles/polygons, with coordinates specified using current page.north west and some arithmetic (which I got from this answer), though there are multiple problems:

1. They end up on a separate page,

2. This separate page has the default (letter/A4) TeX dimensions, not those of the included PDF (though this can be set explicitly)

Here's what I have so far (using example-image-a instead of my PDF file):

\documentclass{article}
\pagestyle{empty}
\usepackage{pdfpages}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\paperwidth=319.999bp
\paperheight=239.999bp
\pagewidth=319.999bp
\pageheight=239.999bp
\begin{document}
\includepdf[fitpaper=true]{example-image-a}%
\begin{tikzpicture}[remember picture,overlay]
\draw [line width=1mm,opacity=.25] (current page.center) circle (3cm);
\draw[red, thick] ($(current page.north west)+(102 bp,-72 bp)$) -- ($(current page.north west)+(132 bp,-72 bp)$) -- ($(current page.north west)+(132 bp,-90 bp)$) -- ($(current page.north west)+(102 bp,-90 bp)$) -- cycle;
\end{tikzpicture}%
\end{document}


Results in two pages (in the other order if I put the \includepdf later):

• It is wellknown that \includepdf inserts a full page and then switches to the next page. If you put your tikz code into a macro, then use can use the pagecommand or pictuercommand options for \includepdf to execute this code on the same page as the included PDF. I've used this several times to remove headers and foters when PhD students want to include PDF versions of their published articles in their dissertations. Sep 14, 2020 at 10:02
• Perhaps one of the answers at tex.stackexchange.com/questions/15314/… will help. Naturally, I prefer my own answer to that question :) Sep 14, 2020 at 10:37
• – user30471
Sep 14, 2020 at 10:44

Using eso-pic's \AddToShipoutPictureFG* one can achieve this (I got undefined control sequence errors on \pagewidth and \pageheight and commented them):

\documentclass{article}
\pagestyle{empty}
\usepackage{eso-pic}
\usepackage{pdfpages}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\paperwidth=319.999bp
\paperheight=239.999bp
%\pagewidth=319.999bp
%\pageheight=239.999bp
\begin{document}
\put(0,0){\begin{tikzpicture}[remember picture,overlay]
\draw [line width=1mm,opacity=.25] (current page.center) circle (3cm);
\draw[red, thick] ($(current page.north west)+(102 bp,-72 bp)$) -- ($(current page.north west)+(132 bp,-72 bp)$) -- ($(current page.north west)+(132 bp,-90 bp)$) -- ($(current page.north west)+(102 bp,-90 bp)$) -- cycle;
\end{tikzpicture}%
}}%
\includepdf[fitpaper=true]{example-image-a}%
\end{document}


• Thanks! \pagewidth and \pageheight are LuaTeX's names for what are called \pdfpagewidth and \pdfpageheight in pdfTeX and XeTeX. Sep 16, 2020 at 22:39
• @ShreevatsaR ah, thanks for the explanation. Sep 17, 2020 at 6:34

Use option picturecommand of \includepdf and put your tikzpicture-stuff in there:

\documentclass{article}
\usepackage{pdfpages}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}

\begin{document}
\includepdf[
fitpaper=true,
picturecommand={%
\begin{tikzpicture}[remember picture,overlay]
\draw [line width=1mm,opacity=.25] (current page.center) circle (3cm);
\draw[red, thick]
($(current page.north west)+(102 bp,-72 bp)$) --
($(current page.north west)+(132 bp,-72 bp)$) --
($(current page.north west)+(132 bp,-90 bp)$) --
($(current page.north west)+(102 bp,-90 bp)$) -- cycle;
\end{tikzpicture}}
]{example-image-a}
\end{document}


Note that picturecommand itself uses \AddToShipoutPicture from the eso-pic package, thus this is exactly the right place where to put your drawing stuff.

• Thanks, pagecommand works too, as mentioned by @daleif in a comment on the question. Sep 14, 2020 at 22:32
• Yes it works, but I recommend using picturecommand though. pagecommand is a very old interface used to plug in things like \thispagestyle{empty}, but picturecommand is the better place to plug in 'visible' material. Sep 15, 2020 at 8:36

Based on this method:

\documentclass[a4paper]{article}
\usepackage{pdfpages}
\usepackage{tikz}
\begin{document}
\includepdf[
fitpaper=true,
picturecommand={%
\begin{tikzpicture}[remember picture,overlay,
x={(current page.south east)},y={(current page.north west)}
]
% Help CoSy
\draw[help lines,xstep=.1,ystep=.1] (0,0) grid (1,1);
\foreach \x in {0,1,...,9} { \node [anchor=south] at (\x/10,0) {0.\x}; }
\foreach \y in {1,...,9} { \node [anchor=west] at (0,\y/10) {0.\y}; }
% Stuff
\draw[red, thick, rounded corners] (0.1,0.9) rectangle (0.25,0.75);
\draw [cyan, very thick] (0.5,0.5) circle[radius=2cm];
\draw[yellow, line width=4mm, ->] (0.7,0.1) -- (0.9,0.3);
\draw [blue, very thick] (current page.center) circle[radius=3cm];
\end{tikzpicture}}
]{example-image-a.pdf}
\end{document}