在背景圖像上繪圖

在背景圖像上繪圖

我想獲取現有 PDF 的頁面(只是圖像),並在每個頁面上繪製幾個框。 (外部 OCR 程式偵測到的單字邊界框。)

到目前為止我嘗試過的:

  • 可以使用\includepdf(從pdfpages套件中)選項[fitpaper=true]可使產生的 PDF 頁面與原始 PDF 頁面相同。

  • 可以使用 TikZ 繪製矩形/多邊形,並使用指定的座標current page.north west和一些算術(我從這個答案),儘管存在多個問題:

    1. 他們最終出現在一個單獨的頁面上,

    2. 此單獨頁面具有預設(letter/A4)TeX 尺寸,而不是包含的 PDF 尺寸(儘管可以明確設定)

這是我到目前為止所擁有的(使用example-image-a而不是我的 PDF 文件):

\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}

結果有兩頁(如果我放在後面,則按其他順序\includepdf):

在此輸入影像描述

答案1

使用eso-pic's\AddToShipoutPictureFG*可以實現這一點(我在 和 上遇到了未定義的控制序列錯誤\pagewidth\pageheight對其進行了評論):

\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}

在此輸入影像描述

答案2

使用選項picturecommand並將\includepdf你的tikzpicture-stuff 放在那裡:

\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}

請注意,它picturecommand本身是\AddToShipoutPictureeso-pic包裝中使用的,因此這正是放置繪圖材料的正確位置。

答案3

基於方法:

在此輸入影像描述

\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}

相關內容