Draw rectangles in different pages with different coodinates

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
mariachi
Posts: 2
Joined: 2016-06-14T02:41:04-07:00
Authentication code: 1151

Draw rectangles in different pages with different coodinates

Post by mariachi »

Hello! In my work I need to hide patient names from pdfs with blood values. Up to now people have been printing the pdf and then photocopying it again with a paper on top of the name part -- very wasteful. I want to optimize the process.

Code: Select all

mogrify -density 300 -compress ZIP -fill black -stroke black -strokewidth 5 -draw 'rectangle 1500,650 2300,730' *.pdf
This will draw a black rectangle on top of the name and it's placed correctly on the first page, but the second page has less white space on the top margin, so the rectangle is slightly below the name. How can I draw the second second for the second, third, "n" page with coordinates slightly above the ones from the first page?

I'm new to this and although I use Linux in my laptop, other people in my lab use windows and the computer where are these results are is also using windows. What is the easiest option to create a script that would simply do this to all selected pdfs or all pdfs in a folder? My co-workers are not tech savvy at all and a very simple solution is needed.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Draw rectangles in different pages with different coodinates

Post by snibgo »

If the different pages within the PDF have the patient name in different locations, then you need an IM command that treats each page differently. After reading the PDF, it would draw a rectangle to each page in turn, then write the PDF output.

You probably can't do this in mogrify, so would need a script, with "convert" being called in a "for" loop. A bash script would be slightly different to a Windows BAT script.
snibgo's IM pages: im.snibgo.com
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Draw rectangles in different pages with different coodinates

Post by GeeMack »

mariachi wrote:This will draw a black rectangle on top of the name and it's placed correctly on the first page, but the second page has less white space on the top margin, so the rectangle is slightly below the name. How can I draw the second second for the second, third, "n" page with coordinates slightly above the ones from the first page?
I'm using ImageMagick 7 on a Windows 7 64 computer. I worked out a solution that will work with ImageMagick 7 on Windows, but probably not with IM6 or earlier versions.

This uses a "for" loop to run through all the PDF files in the folder, and creates output files with their original names in a folder named "done".

Code: Select all

for %I in ( *.pdf ) do (
   magick ^
      -density 300 ^
      "%I" ^
      -set filename:f "%[t]" ^
      -fill black ^
      -stroke black ^
      -strokewidth 5 ^
      -draw "rectangle 1500,%[fx:(t==0?40:0)+650] 2300,%[fx:(t==0?40:0)+730]" ^
      -compress zip ^
      "done/%[filename:f].pdf"
)
The "-draw" operator uses an FX expression to place the black rectangle 40 pixels lower on only the first page. The "t" in the FX expression stands for the page number, starting with 0 for the first page, and if it is zero it adds 40 pixels to make the vertical location of the rectangle lower than on the rest of the pages.

That output folder "done" must already exist, but if you're using a batch file you could create it ahead of the "for" loop.

If you put this command in a batch file you'll need to make all the single percent signs "%" into doubles "%%".
mariachi
Posts: 2
Joined: 2016-06-14T02:41:04-07:00
Authentication code: 1151

Re: Draw rectangles in different pages with different coodinates

Post by mariachi »

Very nice it works exactly like I want! I had to fiddle with the position a little bit but it's perfect now, thank you so much.
Post Reply