How to Extract Images from a PDF in Power Automate
Getting image objects directly from a PDF document is challenging in a programmatic workflow. If we convert the PDF to Word document format first, however, we can take advantage of a more easily navigable file structure to get the image content we want.
Quick multi-step process to extract PDF images
Using Cloudmersive Document Conversion connectors in Power Automate, we can build a reusable instant cloud flow that 1) converts a text-based or vector PDF to Word, 2) extracts image(s) from the Word document, 3) automatically detects/validates image format, and 4) saves the image(s) in our file system.
We’ll walk through a quick example flow in this article to see that end-to-end process take shape. In this example, we’ll extract the logo shown in the example PDF below:
Create an Instant cloud flow
We’ll start by creating a manually triggered Instant cloud flow.
Within our manual trigger step, we’ll create a file input field which asks flow users to upload a PDF document (we won’t worry about validating the file type as PDF for now — but that’s worth keeping in mind).
Navigate to the Cloudmersive Document Conversion connector
Next up, we’ll create a new action and search for the Cloudmersive Document Conversion connector in the Power Automate connector library.
We can easily find this by typing “Cloudmersive” into the search bar.
All the key actions we need in our flow are available on this connector’s actions list.
We can find the first action we need by clicking “See more” and searching (CTRL + F or CMD + F) Convert PDF to Word DOCX Document. This should highlight the correct action, as shown below.
Create and Save a Cloudmersive connection
After we select our first action, we’ll need to create our Document Conversion connection, which will then save for each future action we use. We’ll need a Cloudmersive API key to authorize our connection, and we can get one by visiting the Cloudmersive website & registering a free account (free API keys allow up to 800 API calls per month).
Configure the PDF to Word conversion request
Once our connection is authorized, we’ll pass the file content & file name values we captured in our trigger step to each respective Convert PDF to Word parameter.
The Convert PDF to Word action returns DOCX file bytes. In our next step, we’ll ask a second Document Conversion connector action to extract one or more images from the DOCX file.
Extract images from the converted document
After heading back to the Document Conversion actions list, we’ll search for an action titled Get images from a Word DOCX document.
To configure our request, we’ll pass OutputContent from our Convert PDF to Word action to the ReqConfig/InputFileBytes parameter (we’ll see this parameter after clicking “Show all” to view Advanced parameters).
The Get images action doesn’t return file bytes to our flow directly. Instead, it returns one secure temporary URL (referencing cached content on the Cloudmersive public cloud server) for each image in our converted DOCX file which we can use to download our image contents. This significantly reduces overhead processing in a flow that might be dealing with several large image files.
Extract image content from secure temporary URLs
To download our images from their secure temporary URLs, we’ll use a Cloudmersive action called Finish editing document, and download result from document editing.
We’ll pass the ImageContentsURL value from our Get images action to the ReqConfig/InputfileUrl parameter (once again, we’ll find this parameter by clicking “Show all” to view Advanced parameters).
After selecting the ImageContentsURL value in our Finish editing action, Power Automate should’ve placed our Finish editing action in a For each control (this accounts for documents containing multiple images). We can see this in the above screenshot.
Create the image file(s) in SharePoint
If we know the image file types in the PDF we’re extracting images from, we can jump straight to creating a new file for each image we download. We can do that using a Create file action (we’ll use a SharePoint version of that action in this example).
In the above example, we know the image type is PNG, so we’ve sliced the PDF extension off the input PDF file name and replaced it with “.png” to generate a PNG file.
Autodetect image file type (if necessary)
If we do NOT know the image file extension, we can place one final Cloudmersive Document Conversion action between our Finish editing and create file step.
The Autodetect content type and validate action will identify the extension for most common document types (including DOCX, XLSX, PPTX, PDF, PDF/A, etc., and 100+ image formats) and return that extension for each file it processes.
We can use the body/FileFormatExtension value from the Autodetect content API response in our Create file action to dynamically set the extension of each image file from our original PDF, as shown below.
Test the flow
Once we’ve finished these final steps, we’ll save our flow & run it with our example PDF.
It’s important to make sure we ONLY run this flow on text-based or vector PDFs (rasterized PDFs are entirely image based and require OCR for conversion to Word DOCX format).
When our flow finishes running, we’ll find our image(s) in our target folder.
Conclusion
In this walkthrough, we learned how to set up a reusable Power Automate flow which uses several Cloudmersive Document Conversion connector actions to extract image objects from a text-based or vector PDF document.
We can use this flow to get important pictures, logos, or embedded images of other documents from any text-based or vector PDF document.
If you’re interested to see more useful Power Automate flows in action with Cloudmersive connectors, be sure to give us a follow & check back in on our blog!