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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / get x and y coordinate...
Power Automate
Unanswered

get x and y coordinates on the basis of text in pdf using power automate desktop

(0) ShareShare
ReportReport
Posted on by 159

Hi,

is it possible to find particular text and get x and y coordinates for that text

already help is provided by @Deenuji very thankful for him

Like - 

neerajpokh_0-1710183015630.png

 

 

want to find x and y coordinates for that highlighted text in pdf.

Please help

Thanks

Neeraj

I have the same question (0)
  • Deenuji_Loganathan_ Profile Picture
    6,250 Moderator on at

    @neerajpokh 

    Thanks for creating new topic.

    Here you go for the requested code.

     

    Input Parameters variables:

    1. FilePath
    2. Text to Search

    Output Parameters variables:

    1. Text found at page Number
    2. X coordinate 
    3. Y coordinate

    You can utilize the same ITextsharp dll version for the use case and add one more reference in .Net script reference textbox:

    Deenuji_0-1710232387391.png

    Code[Copy and paste the below code into your PAD designer]

     

    SET InputFilePath TO $'''C:\\Users\\Documents\\PAD_Document.pdf'''
    SET Search_Text TO $'''add text and image to existing pdf in power automate desktop'''
    SET Text_Found_At_PageNo TO $'''%''%'''
    SET Result_X TO $'''%''%'''
    SET Result_Y TO $'''%''%'''
    Scripting.RunDotNetScript Imports: $'''iTextSharp.text
    iTextSharp.text.pdf
    iTextSharp.text.pdf.parser''' Language: System.DotNetActionLanguageType.CSharp ReferenceRootPath: $'''C:\\Deenu\\Assemblies''' Script: $'''using (PdfReader reader = new PdfReader(filePath))
    {
     for (int pageNum = 1; pageNum <= reader.NumberOfPages; pageNum++)
     {
     // Extract text with coordinates using the custom strategy
     string text = PdfTextExtractor.GetTextFromPage(reader, pageNum);
    
     // Split the extracted text into lines
     string[] lines = text.Split(\'\\n\');
    
     // Loop through each line of text
     foreach (string line in lines)
     {
     // Check if the search text is found in the current line
     if (line.Contains(searchText))
     {
     // Output page number and coordinates of the found text
     StrPageNum =Convert.ToString(pageNum);
     Str_Cordinates_X = Convert.ToString(reader.GetPageSize(pageNum).Width / 2);
     Str_Cordinates_Y = Convert.ToString(reader.GetPageSize(pageNum).Top - reader.GetPageSize(pageNum).Bottom);
     
     break; // Exit the loop after finding the text
     }
     }
     }
    }''' @'name:filePath': InputFilePath @'type:filePath': $'''String''' @'direction:filePath': $'''In''' @'name:searchText': Search_Text @'type:searchText': $'''String''' @'direction:searchText': $'''In''' @'name:StrPageNum': $'''''' @'type:StrPageNum': $'''String''' @'direction:StrPageNum': $'''Out''' @'name:Str_Cordinates_X': $'''''' @'type:Str_Cordinates_X': $'''String''' @'direction:Str_Cordinates_X': $'''Out''' @'name:Str_Cordinates_Y': $'''''' @'type:Str_Cordinates_Y': $'''String''' @'direction:Str_Cordinates_Y': $'''Out''' @StrPageNum=> Text_Found_At_PageNo @Str_Cordinates_X=> Result_X @Str_Cordinates_Y=> Result_Y

     

     
    Thanks,
    Deenuji Loganathan
    🤖Automation Evangelist
    Deenuji - Follow me on LinkedIn

    -------------------------------------------------------------------------------------------------------------
    If I've helped solve your query, kindly mark my response as the solution ✔ and give it a thumbs up!👍 Your feedback supports future seekers 🌟

  • neerajpokh Profile Picture
    159 on at

    Hi @Deenuji 

     

    Split(\'\\n\');

     

    is throwing error

  • Deenuji_Loganathan_ Profile Picture
    6,250 Moderator on at

    @neerajpokh 

    Share some screenshot pls? you are copy and pasted the same code in your PAD flow?

     

  • neerajpokh Profile Picture
    159 on at

    Hi @Deenuji 

    ran it in visual studio first

  • neerajpokh Profile Picture
    159 on at

    Hi @Deenuji 

    here is my code

    requirement is to get text position and add image there

    using (var reader = new PdfReader("C:\\New folder\\New folder\\Altagas.pdf"))
    {

    using (var stamper = new PdfStamper(reader, new FileStream("C:\\New folder\\output.pdf", FileMode.Create)))
    {
    var count = reader.NumberOfPages;
    for (int i = 1; i <= reader.NumberOfPages; i++)
    {
    PdfContentByte content = stamper.GetOverContent(i);
    iTextSharp.text.pdf.PdfDocument doc = new iTextSharp.text.pdf.PdfDocument();
    if (i == 1)
    {
    //add text
    content.BeginText();
    iTextSharp.text.Rectangle pagesize = reader.GetPageSize(1);
    float x = pagesize.Left + 200;
    float y = pagesize.Top - 40;
    content.SetFontAndSize(BaseFont.CreateFont(), 12);
    content.ShowTextAligned(0, "Hello how are you?", x, y, 0);
    content.EndText();
    }

    // Add image
    if (count == i)
    {
    iTextSharp.text.Rectangle pagesize = reader.GetPageSize(i);
    //float x = pagesize.Left + 100;
    //float y = pagesize.Top - 680;
    var Str_Cordinates_X = "";
    var Str_Cordinates_Y = "";
    var textfound=false;
    float x = 0;
    float y = 0;
    string text = iTextSharp.text.pdf.parser.PdfTextExtractor.GetTextFromPage(reader, i);
    string[] lines = text.Split(\'\\n\');
    foreach (string line in lines)
    {
    // Check if the search text is found in the current line
    if (line.Contains("Confirmed on the date first above written:"))
    {
    // Output page number and coordinates of the found text
    var StrPageNum = Convert.ToString(i);
    Str_Cordinates_X = Convert.ToString(reader.GetPageSize(i).Width / 2);
    Str_Cordinates_Y = Convert.ToString(reader.GetPageSize(i).Top - reader.GetPageSize(i).Bottom);
    textfound = true;
    }
    if (textfound == true)
    {
    break;
    }
    //break;
    }
    //Str_Cordinates_X= float.Parse("41.00027357629127", CultureInfo.InvariantCulture.NumberFormat);
    x = float.Parse(Str_Cordinates_X, CultureInfo.InvariantCulture.NumberFormat);
    y=float.Parse(Str_Cordinates_Y, CultureInfo.InvariantCulture.NumberFormat);
    iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance("C:\\New folder\\New folder\\Kirby Linzmeyer.png");
    //image.SetAbsolutePosition(100, 100); // Set position
    //object result=stamper.fin
    //content.ShowTextKerned("on the date first above written:");
    image.SetAbsolutePosition(x, pagesize.Top - 680);
    content.AddImage(image);
    }
    }
    }
    }

  • Deenuji_Loganathan_ Profile Picture
    6,250 Moderator on at

    @neerajpokh 

    Please first try the code for what you asked then talk about your integration issue please.

    However, please find the updated code for your use case(Insert text and image + Get X and Y coordinates for given text).

    Code: 

     

    using (var reader = new PdfReader("C:\\Users\\Documents\\Loading.pdf"))
    {
    
    using (var stamper = new PdfStamper(reader, new FileStream("C:\\Deenu\\RPA.pdf", FileMode.Create)))
    {
    var count = reader.NumberOfPages;
    for (int i = 1; i <= reader.NumberOfPages; i++)
    {
    PdfContentByte content = stamper.GetOverContent(i);
    iTextSharp.text.pdf.PdfDocument doc = new iTextSharp.text.pdf.PdfDocument();
    if (i == 1)
    {
    //add text
    content.BeginText();
    iTextSharp.text.Rectangle pagesize = reader.GetPageSize(1);
    float x = pagesize.Left + 200;
    float y = pagesize.Top - 40;
    content.SetFontAndSize(BaseFont.CreateFont(), 12);
    content.ShowTextAligned(0, "Contents", x, y, 0);
    content.EndText();
    }
    
    // Add image
    if (count == i)
    {
    iTextSharp.text.Rectangle pagesize = reader.GetPageSize(i);
    //float x = pagesize.Left + 100;
    //float y = pagesize.Top - 680;
    var Str_Cordinates_X = "";
    var Str_Cordinates_Y = "";
    var textfound = false;
    float x = 0;
    float y = 0;
    string text = iTextSharp.text.pdf.parser.PdfTextExtractor.GetTextFromPage(reader, i);
    // Split the extracted text into lines
    string[] lines = text.Split('\n');
    
    
    foreach (string line in lines)
    {
    // Check if the search text is found in the current line
    if (line.Contains("Loading Reference: 12345 "))
    {
    // Output page number and coordinates of the found text
    var StrPageNum = Convert.ToString(i);
    Str_Cordinates_X = Convert.ToString(reader.GetPageSize(i).Width / 2);
    Str_Cordinates_Y = Convert.ToString(reader.GetPageSize(i).Top - reader.GetPageSize(i).Bottom);
    textfound = true;
    }
    if (textfound == true)
    {
    break;
    }
    //break;
    }
    //Str_Cordinates_X= float.Parse("41.00027357629127", CultureInfo.InvariantCulture.NumberFormat);
    x = float.Parse(Str_Cordinates_X, CultureInfo.InvariantCulture.NumberFormat);
    y = float.Parse(Str_Cordinates_Y, CultureInfo.InvariantCulture.NumberFormat);
    iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance("C:\\Users\\Downloads\\Download BAckup\\Article1\\6.png");
    //image.SetAbsolutePosition(100, 100); // Set position
    //object result=stamper.fin
    //content.ShowTextKerned("on the date first above written:");
    image.SetAbsolutePosition(x, pagesize.Top - 680);
    content.AddImage(image);
    }
    }
    }
    }

    Hope you are using System.Globalization dll as well for your above code.

     


    Thanks,
    Deenuji Loganathan
    🤖 Automation Evangelist
    Deenuji - Follow me on LinkedIn

    -------------------------------------------------------------------------------------------------------------
    If I've helped solve your query, kindly mark my response as the solution ✔ and give it a thumbs up!👍 Your feedback supports future seekers 🌟

     

  • neerajpokh Profile Picture
    159 on at

    Hi @Deenuji 

    in that case from above code, x and y coordinates values are same for pdfs 

    like some has more bottom space and some has less but x and y are coming  same

  • Deenuji_Loganathan_ Profile Picture
    6,250 Moderator on at

    Hello @neerajpokh,

     

    Could you please elaborate your issue with some example and screenshot please?

  • neerajpokh Profile Picture
    159 on at

    Hi @Deenuji 

    i have multiple pdfs are available and need to insert image in different pdf

     

    neerajpokh_0-1710265517444.png

     

    screenshot above has more space in bottom

     

    neerajpokh_1-1710265594484.png

    and here we have less space in bottom

    so want to find text and get corresponding x and y coordinates so can insert image in correct place

     

    hope you understand

  • Deenuji_Loganathan_ Profile Picture
    6,250 Moderator on at

    @neerajpokh 

    So, with respect to which text input are you aiming to obtain the X and Y coordinates in the above attached pdf like last title? If feasible, could you share a PDF with me via private message? Please provide screenshots highlighting the specific text you are looking to obtain X and Y values from, as well as indicating where you would like the images to be placed(Do it manually and share some screenshot for my understanding)

     

    Additionally, are you inserting the same images across all PDFs, or are they different? Will the image size vary, or is it fixed?

     

     

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Leaderboard > Power Automate

#1
Haque Profile Picture

Haque 589

#2
Valantis Profile Picture

Valantis 328

#3
David_MA Profile Picture

David_MA 284 Super User 2026 Season 1

Last 30 days Overall leaderboard