web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / how to get the total n...
Power Automate
Suggested Answer

how to get the total number pages from a pdf file

(1) ShareShare
ReportReport
Posted on by 4
Hello All,

i am trying to fetch the total no.of pages from a pdf file in power automate desktop. so,is there any way to fetch it?

thanks in advance.

 
I have the same question (0)
  • Suggested answer
    Deenuji_Loganathan_ Profile Picture
    6,250 Super User 2025 Season 2 on at
    Hi 
     
    Please find the below example which provided in the Power automate desktop to get No. of pages in pdf file.
     
     
    Code also attached below for your quicker reference:(Copy and paste into your power automate desktop designer):
     
    # The custom form prompts users to select a PDF file to count its pages.
    @@statistics_TextBlock: '1'
    @@statistics_Input_File: '1'
    @@statistics_Action_Submit: '1'
    Display.ShowCustomDialog CardTemplateJson: '''{
      \"type\": \"AdaptiveCard\",
      \"version\": \"1.4\",
      \"id\": \"AdaptiveCard\",
      \"body\": [
        {
          \"type\": \"TextBlock\",
          \"id\": \"Text block\",
          \"size\": \"medium\",
          \"weight\": \"bolder\",
          \"text\": \"${Text_block_Text}\"
        },
        {
          \"type\": \"Input.File\",
          \"id\": \"File input\",
          \"isRequired\": true,
          \"label\": \"${File_input_Label}\"
        }
      ],
      \"actions\": [
        {
          \"type\": \"Action.Submit\",
          \"id\": \"Ok\",
          \"title\": \"${Ok_Title}\"
        }
      ]
    }''' CustomFormData=> CustomFormData ButtonPressed=> ButtonPressed @Text_block_Text: $'''Find the number of pages in PDF''' @File_input_Label: $'''Select a PDF file:''' @Ok_Title: $'''OK'''
    /# The 'If' action checks whether the user pressed the 'OK' button in the custom form. If yes, the main logic of the flow runs.
    #/
    IF ButtonPressed = $'''Ok''' THEN
        # The following actions retrieve the file path of the selected file. The retrieved directory part of the path is used as a destination folder for the extracted temporary file. The 'Count' variable increases for each page in the PDF file.
        File.GetPathPart File: CustomFormData['File input'] RootPath=> RootPath Directory=> Directory FileName=> FileName FileNameWithoutExtension=> FileNameNoExtension Extension=> FileExtension
        SET Count TO 0
        # The loop repeats until the 'Extract PDF file pages to new PDF file' action fails. When the action fails, the file doesn't have more pages. The 'Extract PDF file pages to new PDF file' action creates a temporary file for each page and the 'Delete file(s)' deletes it.
        LOOP WHILE ($'''True''') = ($'''True''')
            Pdf.ExtractPages PDFFile: CustomFormData['File input'] PageSelection: Count + 1 ExtractedPDFPath: $'''%Directory%\\Temp PDF from a desktop flow''' IfFileExists: Pdf.IfFileExists.AddSequentialSuffix ExtractedPDFFile=> ExtractedPDF
            ON ERROR PageOutOfBoundsError
                GOTO Finish
            END
            File.Delete Files: ExtractedPDF
            Variables.IncreaseVariable Value: Count IncrementValue: 1
        END
    END
    LABEL Finish
    Display.ShowMessageDialog.ShowMessage Title: $'''Flow finished running...''' Message: $'''The selected PDF file has %Count% pages.''' Icon: Display.Icon.None Buttons: Display.Buttons.OK DefaultButton: Display.DefaultButton.Button1 IsTopMost: True ButtonPressed=> ButtonPressed2
    
     
     
    Thanks,
    Deenu
  • Suggested answer
    VishnuReddy1997 Profile Picture
    2,656 Super User 2025 Season 2 on at
    Hey Hi,
     
    If the pages count is too high,it will be a time take process @deenuji has provided.
     
    It will be faster and easiest process if you try with Python code and you can invoke the Python code as below shown in PAD.
     
     
    Python Code :
    Please save this python code as .py file.
     
    import fitz  # PyMuPDF
    import sys
    
    def count_pages(pdf_path):
        pdf_document = fitz.open(pdf_path)
        num_pages = pdf_document.page_count
        return num_pages
    
    if __name__ == "__main__":
        if len(sys.argv) != 2:
            print("Usage: python count_pages_pymupdf.py <pdf_path>")
            sys.exit(1)
    
        pdf_path = sys.argv[1]
        num_pages = count_pages(pdf_path)
        print(f"{num_pages}")
     
     
    (Note:- if you got your solution you can mark as solution and gives kudos)
     
    Thanks & Regards
    Vishnu Reddy

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 519 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 296 Moderator

#3
abm abm Profile Picture

abm abm 232 Most Valuable Professional

Last 30 days Overall leaderboard