2

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):

enter image description here

ShreevatsaR
  • 45,428
  • 10
  • 117
  • 149
  • 1
    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. – daleif Sep 14 '20 at 10:02
  • 2
    Perhaps one of the answers at https://tex.stackexchange.com/questions/15314/how-can-i-superimpose-latex-tex-output-over-a-pdf-file will help. Naturally, I prefer my own answer to that question :) – David Purton Sep 14 '20 at 10:37
  • 1
    Related: https://tex.stackexchange.com/questions/398584/image-with-some-overlaid-text –  Sep 14 '20 at 10:44

3 Answers3

2

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}
\AddToShipoutPictureFG*{%
  \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}

enter image description here

Skillmon
  • 60,462
2

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. – ShreevatsaR Sep 14 '20 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. – Andreas Matthias Sep 15 '20 at 08:36
1

Based on this method:

enter image description here

\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}
cis
  • 8,073
  • 1
  • 16
  • 45