Showing posts with label Autoit Demo. Show all posts
Showing posts with label Autoit Demo. Show all posts
Posted by Catur Nugroho | File under :
Simple PDF Automation Using Autoit in C#

The topic for this article is how read the text of a PDF document, export into txt file, and return the results as text or string.

This is code for adobe reader X Version 10.1.3, so if the next time you get some code that doesn't run as well might be due to different versions.

This article will explain clearly how we can make simple PDF Automation. If you can't understanding  the code, I have set up the source code so you can debug the code and you can download it from here.

Let's start the code :

This is the image information from the PDF 

 
Parameters :
Title : "[CLASS:AcrobatSDIWindow]" or "sample.pdf - Adobe Reader"
Text : ""

private void RunButton_Click(object sender, EventArgs e)
{
        //open pdf application
        //change it if you have different pdf version 10.1.3
        string strAcrobatReaderType = "AcroRd32";        string strDir = Application.ExecutablePath.Substring(0,      Application.ExecutablePath.LastIndexOf("\\bin") + 1) + "files\\sample.pdf";
        ProcessStartInfo pInfo = new ProcessStartInfo(@"C:\Program Files\Adobe\Reader 10.0\Reader\" + strAcrobatReaderType + ".exe", strDir);
        Process.Start(pInfo);


       //wait the window is opened
       autoit.WinWaitActive("[CLASS:AcrobatSDIWindow]", "", 50);
       if (autoit.WinExists("[CLASS:AcrobatSDIWindow]", "") == 0)
       {
                MessageBox.Show("Windows didn't open");
        }


       //save as text document
       //send keys ALT + f + a

       autoit.Send("!fax", 0);


Save As Image Window Info


Parameters :
Title : "Save As"
Text : ""
Control : Edit1

      string strSaveAsTitle = "Save As";
      string strDir = Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\bin") + 1) + "files\\sample.txt";
      int iCountRentry = 0;
     

      //Wait Save As Window is open and check if don't open
      autoit.WinWaitActive(strSaveAsTitle, "", 1000);
      if (autoit.WinExists(strSaveAsTitle, "") == 0)
      {
                MessageBox.Show("Save As Windows didn't open");
       }


       //send keys file directory to save
       do
       {
                if (autoit.ControlGetText(strSaveAsTitle, "", "Edit1").Equals(strDir))
                break;

                //send CTRL + a
                Type("^a", 0, "Edit1", true, 1000, strSaveAsTitle);
                Type(strDir, 0, "Edit1", true, 1000, strSaveAsTitle);

                iCountRentry++;
        }
        while (iCountRentry < 10);
        if (iCountRentry > 10)
        {
                MessageBox.Show("Error sending keys file directory");
        }


Click Save Button Image

Parameters :
Title : "Save As"
Text : ""
Control : Button2
Control Click X : 38
Control Click Y : 10 

        //You have 2 option to save the text document
        //using click button Save As

        Click("left", 1, 44, 12, "Button2", 1000, strSaveAsTitle);

        //or using send key ENTER
        //Type("{ENTER}", 0, "Button2", false, 1000, strSaveAsTitle);

        //read text

        iCountRentry = 0;
        do
        {
                try
                {
                        using (StreamReader reader = new StreamReader(strDir))
                        {
                                 resultTextBox.Text = reader.ReadToEnd();
                        }
                        break;
                 }
                 catch
                
                        Application.DoEvents();
                        Thread.Sleep(1000);
                        iCountRentry++;
                 }
           } while (iCountRentry < 10);

}

Notes :
If this project  didn't working, you have to change some code such as Title, Control, Control Click X and Y. Autoit working because of the screen according to the PC you are using.
Using function of autoit control is better than the others because it is more accurate if used on another PC.

If you getting problems about this please leave a note in the comments so I can help you.

Download Full Source Code 


Posted by Catur Nugroho | File under :

Simple Notepad Automation Using Autoit C#

This is a simple example of how to work with Notepad for sending text, copy / paste, and save it as a file. I will try to explain as details so that you can easily understand.
You can adding this code into your project. This is additional function that I have made, you can copy and paste.

private void Type(string strKeys, int nMode, string strControl, bool blnSleepKeys, int nSleep, string strWindow)
        {
            if (blnSleepKeys)
            {
                char[] achKeys = strKeys.ToCharArray();

                foreach (char ch in achKeys)
                {
                    autoit.ControlSend(strWindow, "", strControl, ch.ToString(), nMode);
                    Thread.Sleep(100);
                    Trace.WriteLine("Sent Key: '" + ch.ToString() + "'");
                }
            }
            else
            {
                autoit.ControlSend(strWindow, "", strControl, strKeys, nMode);
                Trace.WriteLine("Sent Keys: " + strKeys);
            }

            for (int i = 0; i < (nSleep / 100); i++)
            {
                Application.DoEvents();
                Thread.Sleep(100);
            }
        }

        private void Click(string strButton, int nNumClicks, int nX, int nY, string strControl, int nSleep, string strWindow)
        {
            autoit.ControlClick(strWindow, "", strControl, strButton, nNumClicks, nX, nY);

            for (int i = 0; i < (nSleep / 100); i++)
            {
                Application.DoEvents();
                Thread.Sleep(100);
            }
        }


The first step that must be done is to determine the required parameters like as title, text, control, etc..
You can see the images below.



Parameters :
Title : "[CLASS:Notepad]" or "Untitled - Notepad"
Text : ""
Control : Edit1
nX : 78
nY : 53
private void runButton_Click(object sender, EventArgs e)
        {
            //check if notepad is already exist
            if (autoit.WinExists("[CLASS:Notepad]", "") == 1)
            {
                //notepad closed
                autoit.WinKill("[CLASS:Notepad]", "");
            }
            //execute notepad.exe
            autoit.Run("Notepad.exe", "", 1);
            //wait for max 25 seconds
            autoit.WinWaitActive("[CLASS:Notepad]", "", 25);
            //check if notepad doesn't exist
            if (autoit.WinExists("[CLASS:Notepad]", "") == 0)
            {
                MessageBox.Show("Notepad didn't opened");
            }
            else
            {
                //activate the notepad
                //or set notepad into front

                autoit.WinActivate("[CLASS:Notepad]", "");
            }
            //send some text into text area
            string strText = "autoitsourcecode.blogspot.com";
            Type(strText, 0, "Edit1", true, 500, "[CLASS:Notepad]");
            //try to copy / paste

            //send CTRL+ a
            Type("^a", 0, "Edit1", true, 1000, "[CLASS:Notepad]");

            //send CTRL + c (Copy)
            Type("^c", 0, "Edit1", true, 1000, "[CLASS:Notepad]");

            //send CTRL + v (paste)
            Type("^v", 0, "Edit1", true, 1000, "[CLASS:Notepad]");

            //send CTRL + v (paste)
            Type("^v", 0, "Edit1", true, 1000, "[CLASS:Notepad]");

            //send CTRL + v (paste)
            Type("^v", 0, "Edit1", true, 1000, "[CLASS:Notepad]");
            //send CTRL + v (paste)
             Type("^v", 0, "Edit1", true, 1000, "[CLASS:Notepad]");
            //save the file
            //send key alt + f + a

            Type("!fa", 0, "Edit1", true, 500, "[CLASS:Notepad]");
            //wait the "save as" win dialog is opened until 25 seconds
            autoit.WinWaitActive("Save As", "", 25);
            //check if didn't open
            if (autoit.WinExists("Save As", "") == 0)
            {
                MessageBox.Show("The can't saved");
            }


This is images Save As window info



Parameters :
Title : "Save As"
Text : ""
Control : Edit1
nX : 109
nY : 8
            //send key directory folder
            Type(@"C:\autoitDemo.txt", 0, "Edit1", true, 1000, "Save As");
            //check the files
            if (File.Exists(@"C:\autoitDemo.txt"))
                File.Delete(@"C:\autoitDemo.txt");
The image for click Save button


Parameters :
Title : "Save As"
Text : ""
Control : Edit1
nX : 46
nY :13

            //click button save
            Click("left", 1, 42, 15, "Button1", 1000, "Save As");
            //check if notepad is already exist
            if (autoit.WinExists("[CLASS:Notepad]", "") == 1)
            {
                //notepad closed
                autoit.WinKill("[CLASS:Notepad]", "");
            }
        }

Download Full Source Code