目的

目的

例如,假设我使用该pdfpages包并导入 PDF。有什么方法(不使用 Python 等外部脚本语言)可以访问 PDF 的尺寸吗?我可以将这些尺寸传递给包geometry吗?

我想要一种简单的方法来创建文档输出

应该使用这些新尺寸有效地绘制网格覆盖(从输入中抓取的变量应该传递给 TikZ),因为我不想仅仅使网格足够大以覆盖任何可能的纸张尺寸(对于尺寸较小的输入来说效率不高)。

目的

这对于逆向工程 PDF 模板/布局(例如来自 InDesign)或图像非常有用。通过逆向工程,我的意思是我可以轻松使用 TikZ 的页面坐标设计精确的布局。

代码

在此示例中,我注释掉了pdfinput主体中的。我想在此处获取 pdf 尺寸,并创建\setpaperwidth\setpaperheight设置\inputwidth\inputheight变量的宏。

\documentclass{article}
\usepackage{fontspec}

\newcommand\inputwidth{5.5in}  % <- set from input
\newcommand\inputheight{8.5in} % <- set from input

\usepackage[%
paperwidth=\inputwidth,  
paperheight=\inputheight,  
verbose,             % show the values of the parameters in the log file
]{geometry}

\usepackage{pdfpages}
\usepackage{tikz} % Support for drawing grid
\usetikzlibrary{backgrounds,calc}
\usepackage{atbegshi} % Add support for the showgrid overlay
\newcommand{\showgrid}{%
  \AtBeginShipoutNext{\AtBeginShipoutAddToBoxForeground{%
      \begin{tikzpicture}
        [
          overlay,
          remember picture,
          inner sep=0pt,
          outer sep=0pt,
          minor line/.style={help lines, draw=black!50, on background layer},
          major line/.style={help lines, draw=black},
          major number/.style={font=\fontsize{3}{5}\selectfont\bfseries},
          minor number/.style={font=\fontsize{1}{2}\selectfont},
        ]
        \pgfmathtruncatemacro\xmaxstep{\paperwidth/1mm}% calculate needed steps in x direction
        \pgfmathtruncatemacro\ymaxstep{\paperheight/1mm}% calculate needed steps in y direction
        \foreach \step in {0,1,...,\xmaxstep} {
          \pgfmathsetmacro\gridlineconfig{ifthenelse(equal(int(mod(\step,10)),0),"major line","minor line")}%
          \draw [\gridlineconfig] ($(current page.north west) + (\step mm,0)$) -- ($(current page.south west) + (\step mm,0)$);
        }
        \foreach \step in {0,1,...,\ymaxstep} {
          \pgfmathsetmacro\gridlineconfig{ifthenelse(equal(int(mod(\step,10)),0),"major line","minor line")}%
          \pgfmathsetmacro\numberconfig{ifthenelse(equal(int(mod(\step,10)),0),"major number","minor number")}%
          \draw [\gridlineconfig] ($(current page.north west) - (0,\step mm)$) -- ($(current page.north east) - (0,\step mm)$);
          \node [anchor=north,\numberconfig] at ($ (current page.north west) + (\step mm,0) $) {\step};
          \node [anchor=west,\numberconfig] at ($ (current page.north west) - (0,\step mm) $) {\step};
        }
      \end{tikzpicture}
    }%
  }%
}
\pagenumbering{gobble} % Remove Page Numbers (without fancyhdr)
\begin{document}
\showgrid
\null
%\includepdf[noscale]{pdffile.pdf} % <- Grab dimensions here
\end{document}

答案1

您可以使用graphicx\includegraphics存储文档的一页并对其进行测量:

\newsavebox{\measurebox}
\newlength{\measuredwidth}
\newlength{\measuredheight}
\newcommand\measureimage[2][1]{%
    \savebox{\measurebox}{\includegraphics[page=#1]{#2}}%
    \setlength{\measuredwidth}{\wd\measurebox}%
    \setlength{\measuredheight}{\ht\measurebox}%
    \savebox{\measurebox}{}%
    }

...

\measureimage[2]{myfigure} % measures the 2nd page of the file.

\showthe\measuredwidth % shows the width
\showthe\measuredheight % shows the height

发出后\measureimage[pagenumber]{filename},图像的宽度和高度存储在\measurewidth和中\measuredheight。默认页码为1。请记住,这样做不是很快,因此您应该只在少量文件中使用它(如果您需要单独测量每页,那就不好了)。

答案2

适合输入页面尺寸并绘制网格叠加

在尝试了你的答案之后,我找到了一种使用任何 PDF 或图像文件输入来实现此目的的方法。

给定图像hike.png

  • identify -format "%wx%h\n" hike.png-> 483x271
  • file hike.png- >hike.png:PNG 图像数据,483 x 271,8 位/彩色 RGBA,非隔行

在此处输入图片描述

在主体中包含\measureimage[1]{hike.png},其执行以下操作:

  1. 抓斗尺寸hike.png
  2. 重置 TikZ 节点的 paperwidth 和 paperheight
  3. 重置 pdfpaperwidth 和 pdfpaperheight 以获得实际的纸张尺寸输出\eject
  4. 使用 TikZ 节点添加图像叠加
  5. 以 1 毫米为间隔在图像上应用网格(使用\pdfpagewidth\pdfpageheight作为 x 和 y 的最大值)。

代码

\documentclass{article}
\usepackage{fontspec}

\usepackage[%
%paperwidth=\inputwidth,  % Unneeded
%paperheight=\inputheight,% Unneeded
verbose,             % show the values of the parameters in the log file
]{geometry}

\usepackage{pdfpages}
\usepackage{tikz} % Support for drawing grid
\usetikzlibrary{backgrounds,calc}
\usepackage{atbegshi} % Add support for the showgrid overlay
\newcommand{\showgrid}{%
  \AtBeginShipoutNext{\AtBeginShipoutAddToBoxForeground{%
      \begin{tikzpicture}
        [
          overlay,
          remember picture,
          inner sep=0pt,
          outer sep=0pt,
          minor line/.style={help lines, draw=black!50, on background layer},
          major line/.style={help lines, draw=black},
          major number/.style={font=\fontsize{3}{5}\selectfont\bfseries},
          minor number/.style={font=\fontsize{1}{2}\selectfont},
        ]
        \pgfmathtruncatemacro\xmaxstep{\pdfpagewidth/1mm}% calculate needed steps in x direction
        \pgfmathtruncatemacro\ymaxstep{\pdfpageheight/1mm}% calculate needed steps in y direction
        \foreach \step in {0,1,...,\xmaxstep} {
          \pgfmathsetmacro\gridlineconfig{ifthenelse(equal(int(mod(\step,10)),0),"major line","minor line")}%
          \draw [\gridlineconfig] ($(current page.north west) + (\step mm,0)$) -- ($(current page.south west) + (\step mm,0)$);
        }
        \foreach \step in {0,1,...,\ymaxstep} {
          \pgfmathsetmacro\gridlineconfig{ifthenelse(equal(int(mod(\step,10)),0),"major line","minor line")}%
          \pgfmathsetmacro\numberconfig{ifthenelse(equal(int(mod(\step,10)),0),"major number","minor number")}%
          \draw [\gridlineconfig] ($(current page.north west) - (0,\step mm)$) -- ($(current page.north east) - (0,\step mm)$);
          \node [anchor=north,\numberconfig] at ($ (current page.north west) + (\step mm,0) $) {\step};
          \node [anchor=west,\numberconfig] at ($ (current page.north west) - (0,\step mm) $) {\step};
        }
      \end{tikzpicture}
    }%
  }%
}
\pagenumbering{gobble} % Remove Page Numbers (without fancyhdr)

\newsavebox{\measurebox}
\newlength{\measuredwidth}
\newlength{\measuredheight}

\newcommand\measureimage[2][1]{%
    \savebox{\measurebox}{\includegraphics[page=#1]{#2}}%
    \setlength{\measuredwidth}{\wd\measurebox}%
    \setlength{\measuredheight}{\ht\measurebox}%
    \savebox{\measurebox}{}%
    \paperwidth=\measuredwidth \paperheight=\measuredheight     % paper dimensions for TikZ page nodes
    \eject \pdfpagewidth=\measuredwidth \pdfpageheight=\measuredheight % set paper dimensions in XeLaTeX
    \tikz [overlay, remember picture] \node [anchor=north west,inner sep=0pt,outer sep=0pt] at (current page.north west) {\includegraphics[page=#1]{#2}};
    \showgrid
    }

\begin{document}
\measureimage[1]{hike.png} % measures the 2nd page of the file.

%Height: \the\measuredwidth % shows the width of the paper 

%Width: \the\measuredheight % shows the height of the paper
\end{document}

输出

在此处输入图片描述

相关内容