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 


1 comment: