Convert section of PDF?

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?".
pctechtv
Posts: 22
Joined: 2016-10-09T19:47:59-07:00
Authentication code: 1151

Convert section of PDF?

Post by pctechtv » 2016-10-09T20:16:15-07:00

I have a need to create a JPEG image from part of PDF file. I need to run this via a command line. It will be used in a script. I would like to know if this is possible with ImageMagick. I need the top of the page to be the width with a 16:9 ratio result. Is this possible in ImageMagick? Thanks.

User avatar
fmw42
Posts: 25757
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert section of PDF?

Post by fmw42 » 2016-10-09T20:41:08-07:00

What is your IM version and platform? Please always provide that as syntax may differ.

I am not sure I understand your request. What do you mean by "I need the top of the page to be the width with a 16:9 ratio result"?

Can you explain further what part of the PDF file you want to make into a JPG image?

Is that 16:9 as width:height or height:width?

Do you mean take the top part of the PDF, such that your grab the full width and make the height=(9/16)*width?

A diagram might help! You can upload any diagram or your input PDF to some place such as dropbox.com and put the URL here.

pctechtv
Posts: 22
Joined: 2016-10-09T19:47:59-07:00
Authentication code: 1151

Re: Convert section of PDF?

Post by pctechtv » 2016-10-09T21:33:03-07:00

Hi, and thanks for the help. You have it correct... grab the full width and make the height=(9/16). I need to process and use as a an image programmatically in script.
http://pctechtv.com/extra/forum/imageMa ... dfArea.png

Thanks!

pctechtv
Posts: 22
Joined: 2016-10-09T19:47:59-07:00
Authentication code: 1151

Re: Convert section of PDF?

Post by pctechtv » 2016-10-09T21:37:15-07:00

Sorry the version I am using is ImageMagick-7.0.3-3-Q16-x64, I am on Window 10 64bit.

User avatar
GeeMack
Posts: 729
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Convert section of PDF?

Post by GeeMack » 2016-10-09T21:58:32-07:00

pctechtv wrote:Hi, and thanks for the help. You have it correct... grab the full width and make the height=(9/16). I need to process and use as a an image programmatically in script.
If the 16:9 section you want starts at the top left of the page, this command should extract that from the first page of a PDF...

Code: Select all

magick -density 300 mypdf.pdf[0] -crop %[w]x%[fx:w/16*9]+0+0 output.jpg
You can select which page to process by putting the page number in square brackets at the end of the filename as I did with "mypdf.pdf[0]" in that example. Remeber IM starts counting pages at 0.

You may need to specify a background color and maybe flatten the image onto the background if the background is transparent in the original PDF. Put "-background white -flatten" after the input file name.

You can change the input size and resolution by changing that "-density" setting that comes before the input filename. You can change the output size by doing a "-resize" operation before the output filename.

If you're including the command in a BAT script on Windows you'll need to use double percent signs "%%" anywhere you use single ones on the command line.

pctechtv
Posts: 22
Joined: 2016-10-09T19:47:59-07:00
Authentication code: 1151

Re: Convert section of PDF?

Post by pctechtv » 2016-10-09T23:07:53-07:00

Thanks for the help. This community is great so fast to help. How should I be entering my file location I have entered with/without quotes and I keep getting something like this.
FailedToExecuteCommand `"gswin32c.exe" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -... much longer saying no file found and other things. I am entering a full path like so "C:\CTestAC1\Pointer.pdf"

Code: Select all

magick -density 300 "C:\CTestAC1\Pointer.pdf"[0] -crop %[w]x%[fx:w*0.5625]+0+0 "C:\CTestAC1\School1.pdf"
Thanks

snibgo
Posts: 12428
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert section of PDF?

Post by snibgo » 2016-10-09T23:46:58-07:00

You probably haven't installed Ghostscript.
snibgo's IM pages: im.snibgo.com

pctechtv
Posts: 22
Joined: 2016-10-09T19:47:59-07:00
Authentication code: 1151

Re: Convert section of PDF?

Post by pctechtv » 2016-10-10T03:56:25-07:00

You probably haven't installed Ghostscript.
That is exactly what the problem was. Thanks so much!

pctechtv
Posts: 22
Joined: 2016-10-09T19:47:59-07:00
Authentication code: 1151

Re: Convert section of PDF?

Post by pctechtv » 2016-10-13T11:46:37-07:00

The code sample @GeeMack provided is really cool because it captures a 16:9 ratio no matter what the size of the pdf is. I now see that if my pdf is wider than 8.5 inches I want to scale it down. How could I make the output of the image scale to an 8.5 inch width but stay 16:9 after capture? Thanks

snibgo
Posts: 12428
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert section of PDF?

Post by snibgo » 2016-10-13T12:26:22-07:00

pctechtv wrote:How could I make the output of the image scale to an 8.5 inch width but stay 16:9 after capture?
Do you want to change the number of pixels so that the density (dots per inch) remain the same but the number of inches changes to 8.5?

Or do you simply want to change the density so the number of pixels stays the same but they span 8.5 inches?
snibgo's IM pages: im.snibgo.com

pctechtv
Posts: 22
Joined: 2016-10-09T19:47:59-07:00
Authentication code: 1151

Re: Convert section of PDF?

Post by pctechtv » 2016-10-13T14:57:48-07:00

Because I want to sometimes print the image (on 8.5 x 11) paper, I want to keep the whole page width in view. so I think that is the first option. I want to scale the page down evenly. Evenly to 8.5 width but still see everything. Actually, I may have confused things by saying, "after capture." It can happen within the capture command. If the original command also scales the size to 8.5 inch if would be great. Thanks

snibgo
Posts: 12428
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert section of PDF?

Post by snibgo » 2016-10-13T16:03:12-07:00

At 300 dpi, the width 8.5 inches will be 300*8.5=2550 pixels. The height will be 8.5*9/16 inches, or 300*8.5*9/16=1434 pixels.

So add "-resize 2550x1434" after the crop. (I would also add "+repage" between the crop and the resize.)
snibgo's IM pages: im.snibgo.com

User avatar
GeeMack
Posts: 729
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Convert section of PDF?

Post by GeeMack » 2016-10-13T18:50:18-07:00

pctechtv wrote:If the original command also scales the size to 8.5 inch if would be great. Thanks
You're using ImageMagick 7, so you have some really handy options for doing just that. If you want to resize to 8.5 inches wide using the current resolution you can do the calculation right in the resize operation, something like this...

Code: Select all

magick -density 300 mypdf.pdf[0] ^
    -crop %[w]x%[fx:w/16*9]+0+0 +repage -resize %[fx:resolution.x*8.5]x output.jpg
That will make the width of your output 2550 pixels with the 300 pixel resolution (-density), and the height will change to whatever is needed to maintain the 16:9 aspect. If you were to change the density to 600 in the command, the output will resize to 5100 pixels/dots wide. That FX calculation just multiplies the resolution by your desired width of 8.5 inches.

The "-density" setting before the input file name can be important when processing a PDF because it determines the quality of the rest of the processing and the output.

As snibgo mentioned, it's usually a good idea to put a "+repage" after your "-crop" operation.

pctechtv
Posts: 22
Joined: 2016-10-09T19:47:59-07:00
Authentication code: 1151

Re: Convert section of PDF?

Post by pctechtv » 2016-10-13T20:31:53-07:00

Wow thank you again @snibgo and @GeeMack super helpful. I have been able to do EXACTLY what I wanted to do. The other great part is your syntax examples are helping understand what everything is in the ImageMagick guides. :)

pctechtv
Posts: 22
Joined: 2016-10-09T19:47:59-07:00
Authentication code: 1151

Re: Convert section of PDF?

Post by pctechtv » 2016-10-15T17:41:26-07:00

I'm thinking there must be an option to leave off the last part with the output directory and tell ImageMagick just to create the jpeg in the original file's directory. If so how is this done? Thanks

Post Reply