This question really holds two separate questions:
First the question is of the DPI of a document, and the file in question is a PDF file. PDF files are complicated in nature, and actually allows a single document to hold objects of varying DPIs.
So even though the PDF specifies a DPI for the document itself, this means that it doesn't always make sense to programmatically determine the DPI of a document, simply because there doesn't have to be one single DPI for all of the document's contents.
The command given in the question reads out numbers for the PDF document itself, but rather you want to get the information about the actual image object inside the PDF like this:
pdfimages -list document.pdf
This will list the number of pixels per inch for each image object contained within the document in the "x-ppi" and "y-ppi" columns.
If the PDF files always contain a single image, you can read out the PPI from this command as the way to programmatically obtain the PPI. Note that it might differ from the value you have from
identify for the PDF document as a whole, even though the file contains just a single image.
Secondary the question is about images in general - no file formats listed. Some of the most common file formats for images are JPEG and PNG files, which are simpler than PDF files.
For PNG files, the DPI is embedded in the pHY chunk that describes the physical pixel dimensions for the image. The DPI is allowed to be either unspecified, or specified in terms of an integer number of pixels per meter. Note - it is not stored in pixels per inch or pixels per centimeter, but rather as pixels per meter. This has implications for the precision of the values.
For JPEG files, the DPI is embedded in the JFIF APP0 marker segment that describes the X and Y density of the image as integers. The unit is similarly described in that segment as either unspecified, pixels per inch or pixels per centimeter.
So some JPEG files will have their density specified in pixels per inch, and others as pixels per centimeters.
In both cases you can use
identify to extract the DPI information from the files:
identify -units PixelsPerInch -format '%[resolution.x] x %[resolution.y] %[units]' file.png
identify -units PixelsPerInch -format '%[resolution.x] x %[resolution.y] %[units]' file.jpeg
Note that in this case we ask ImageMagick to convert the actual density information to pixels per inch instead of some times getting the value as pixels per centimeter, and some times as pixels per inch. ImageMagick does not support getting the numbers in their native unit, as only those units can be output by the program - and for example PNG files store it as pixels per meter (as described above) - so a conversion takes place in those cases anyways.
Final, and probably most important note: For bitmap images, such as JPEG and PNG files, the actual image data is "DPI agnostics". Meaning that if you change the DPI of the file from for example 72x72 to 200x200, the actual image data does not change at all. The density information is just a metadata field in the file, and it does not as such affect the actual image data. It only comes into play when printing the image on physical paper.
What this means is that when processing "random images" for different sources, there's a high risk that the DPI information in the image is just some default value from the image processing software used. This means that you'll often see 72x72 DPI, but the value has no relation to the actual image data or how large the creator of the image actually thought it would be in the physical world (if he/she thought anything about that at all). Therefore it is typical not a good idea to rely on the DPI information for such "random images".