Trimming a PDF online with LaTeX: new feature added

Submitted by Nicola Rainiero on 2019-07-08 (last updated on 2019-07-09)

I added a new chance to my LaTeX script that trims empty spaces from every PDF interactively and visually: the option to add the page number in the bottom of the final cropped PDF. It is also possible to do the cut online thanks to Overleaf. And now by editing two variables: it is possible to turn on/off the page number feature and to adjust its distance from the bottom of the page.

Recently I have cropped a PDF that contained two very little slide per page without page numbering, so after printing my resized PDF, I wasted a lot of time to sort it. I added this function to save me in the future!

If you want to discover all the features of my LaTeX script, I suggest you to read the previous article: How to crop and split a PDF online and in LaTeX but without using it.

Step by step guide

Here instead I show you the new function added and how it works.

1. Load the PDF file you want to crop, set the name in the proper variable in this case example.pdf, size correctly its page size, the total number of the pages you want to trim and the starting page, include only the first comment \includecomment{first}, double{1} if your PDF contains two pages per page, otherwise double{0}. Adjust opportunely the size of the virtual rectangle/s and put it/them changing the coordinates.
\includecomment{first} 		% for discovering the dimension of the cuts
\excludecomment{second}		% for printing a cleaned and trimmed PDF
\excludecomment{third}		% for joining the pages of the II stage without white spaces
% (save and use the new PDF before to do it)

%%
%% Define the file name and the dimension of your PDF
%% Put the size in mm
%% The typical size of an A4 document is 210x297 mm
\newcommand\units{mm}					           % <--- set the units (default mm)
\newcommand\fileName{example.pdf}		           % <--- the name of the PDF to process
\newcommand\fileNameToJoin{example_trimmed.pdf}    % <--- the name of the PDF trimmed to join
\newcommand\width{210} 					           % <--- the width size in mm of your PDF
\newcommand\height{297} 				           % <--- the height size in mm of your PDF

%%
%% 							Do you want to crop the white spaces around the PDF?
%%
\newcommand\crop{1} 					% <-- "0" FALSE "1" TRUE
% If true put the dimension of the virtual rectangle that covers the pages
\newcommand\cutterW{130} 				% <--- the width size in mm of your cutter
\newcommand\cutterH{100} 				% <--- the height size in mm of your cutter

%%
%% 							Do you want to cut two sections per page?
%%
\newcommand\double{1} 					% <--- "0" FALSE "1" TRUE
%% If true put x_i, y_i the coordinates for moving the virtual rectangle in the page
%% the system start at the bottom left of the whole page
%% YOU CAN PUT ONLY INTEGER NUMBER
%% first rectangle in page (generally odd) or the only one
\newcommand\xOdd{40}					% <-- x_1
\newcommand\yOdd{160}						% <-- y_1
%% second rectangle in page (generally even)
\newcommand\xEven{40}					% <-- x_2
\newcommand\yEven{36}					% <-- y_2

%%
%% 							How many pages do you want to process?
%% default pages + 1, pages +0 if there are two page increment in the 2 step

\newcommand\numberPages{5} 				% <--- the numbers of pages
\newcommand\firstPage{1} 				% <--- fix the first page to start

2. Include only the second comment \includecomment{second} and turn on the number of page if you want it in the resulting cropped PDF , size opportunely the distance from the bottom, save the trimmed PDF in this case example_trimmed.pdf.
\excludecomment{first} 		% for discovering the dimension of the cuts
\includecomment{second}		% for printing a cleaned and trimmed PDF
\excludecomment{third}		% for joining the pages of the II stage without white spaces
% (save and use the new PDF before to do it)
[...]
%%
%%                          Do you want page number in the trimmed PDF?
\newcommand\pagenumber{1} 					% <-- "0" FALSE "1" TRUE
%% 							Fix the distance of the page number from the bottom
\newcommand\distance{3.5} 				% <--- in mm


3. The last command is experimental and under testing, because the joined pages doesn't fit correctly the page's size: load the trimmed PDF saved in the second step and set its name in the corresponding variable, include only the third comment \includecomment{third} and check the result in the screen. If it is ok save the new PDF, or use the last one and print 2 page per sheet.
\excludecomment{first} 		% for discovering the dimension of the cuts
\excludecomment{second}		% for printing a cleaned and trimmed PDF
\includecomment{third}		% for joining the pages of the II stage without white spaces
% (save and use the new PDF before to do it)
[...]
\newcommand\fileNameToJoin{example_trimmed.pdf}    % <--- the name of the PDF trimmed to join
[...]
% -------------------------
% 				THIRD STEP:
% -------------------------
% Useful if you want to join a cropped PDF
%
% Usage: nup=2x1 means print 2 columns x 1 row (= 2 pages per page)
% To activated it: \includecomment{third}
%
% Save the PDF output of the II stage and load it (Project --> files)
\begin{third}
\includepdf[pages=-,templatesize={\the\numexpr\cutterW mm}{\the\numexpr\cutterH*1.5 mm}, nup=1x2, landscape=false, fitpaper=true]{\fileNameToJoin}%<-your last PDF
\end{third}


Related Content:

Nicola Rainiero

A civil geotechnical engineer with the ambition to facilitate own work with free software for a knowledge and collective sharing. Also, I deal with green energy and in particular shallow geothermal energy. I have always been involved in web design and 3D modelling.