Page 1 of 1

Draw rectangles in different pages with different coodinates

Posted: 2016-06-14T02:48:01-07:00
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.

Re: Draw rectangles in different pages with different coodinates

Posted: 2016-06-14T08:19:59-07:00
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.

Re: Draw rectangles in different pages with different coodinates

Posted: 2016-06-14T09:36:06-07:00
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 "%%".

Re: Draw rectangles in different pages with different coodinates

Posted: 2016-06-15T04:26:38-07:00
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.