Basic COM Automation sample application

  • 627 Views
  • Last Post 27 March 2017
Bendik posted this 27 March 2017

Introduction

Users of PixEdit® Desktop will be familiar with its rich GUI and many of the powerful functions you can use for scanning, editing and automatically processing documents. And many tedious tasks can also be automated from the GUI by using Batch Wizard and DocServer. But there is also another level of automation available with PixEdit, which is its COM interface.

Some of you will be familiar with the COM interface of Microsoft Office applications, which lets you write powerful macros and even whole applications that simplify complex tasks. This article is a basic introduction to COM programming with PixEdit Desktop. Hopefully, it will get you started designing your own PixEdit toolbox and help you integrate it with your document flow in the best possible way.

Before you start

The article requires basic knowledge of programming in C# with Microsoft Visual Studio. Here, we will use Visual Studio 2015, but you should be able to follow along with other versions also. Still, we recommend using VS 2013 or newer.

You should also have the newest version of PixEdit® Desktop installed on your system.

Wire up your application

Open up Visual Studio and choose File, New, Project. In the left pane of the New Project dialog, browse to Visual C#, Windows and select it. Select Windows Forms Application in the right pane, choose a name and location for your project and click OK. (In this article, we will call our application PixEditAuto.)

Visual Studio prepares your application and eventually, a basic, empty form called Form1 comes up. Personally, I'm not very fond of Visual Studio's default names, so I'm renaming the form to PixEditAuto and set its title to PixEdit Automation Application.

Connecting to PixEdit

In the Solution Explorer, right click on References and choose Add Reference... In the left pane choose COM, Type Libraries and then in the right pane, scroll down to PixEdit® 8 Type Library. Check it and click OK.

 Now, if you expand References in the Solution Explorer, PixEdit should show up in the list.

Now click the form and hit F7 to open up the code for the form. In the using section, add a reference to the PixEdit Type Library:

using PixEdit;

Now go back to the form in design mode and double-click its title bar. You will pop back into the code and a Form Load handler. We will use this handler to start PixEdit when the form is loaded. 

 

        PixEdit.Application pixApp = new PixEdit.Application();

private void PixEditAuto_Load(object sender, EventArgs e) { pixApp = new PixEdit.Application(); pixApp.Visible = true; }

Build the application and run it. You should see your form coming up and then PixEdit Desktop. And when you close the application, PixEdit disappears. This is because the PixEdit.Application COM object is no longer referenced when our application closes down. 

Open a document

Go back to the form in design mode and from the Toolbox add a button. Name it btnOpenDoc and set the text "Open..." 

Double-click it, and the button click handler will be shown in code view. Now, add a member variable that will hold the document, an OpenFileDialog and a call to the PixEdit.Application.FileOpen method:

        PixEdit.Document pixDoc = null;

        private void btnOpenDoc_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlgFileOpen = new OpenFileDialog();

            if (dlgFileOpen.ShowDialog() == DialogResult.OK)
            {
                pixDoc = pixApp.FileOpen(dlgFileOpen.FileName);
            }
        }

Now build and run the application again. Click the Open... button in the form. In the Open dialog, browse to a document (for example a PDF file) and double click it. You should see the document opening in PixEdit.

Now, if you close down the app, PixEdit will not close down. This is because a document is still open. For the connection between our app and PixEdit to function correctly, we need to close the document before shutting down.

Go back to the form in design view and from the Properties bar and add a FormClosing event handler with the following code:

        private void PixEditAuto_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (pixDoc != null)
            {
                pixDoc.FileClose();
            }
        }

 (Continued in next post...)

  • Liked by
  • Andreas
Bendik posted this 27 March 2017

If you now try running the app, opening a document and the closing again, you will see that PixEdit closes the document and correctly shuts down. So what would happen if you opened more than one document before closing? Only the last opened document would be closed, so PixEdit would be left running again. This may be solved either by always closing the document before opening a new one or by keeping a list of open documents ( List<PixEdit.Document> ) which you loop through and close before shutting down. We leave this for you to decide...

Modifying the document

Now, let's try som automated editing of the document. Let's say we want to insert a text on every page of the document. Go back to your form in design mode and add a TextBox and a Button like this:

Double click the button to add a Click handler for it. Then add the following code:

        private void btnInsertText_Click(object sender, EventArgs e)
        {
            if (pixDoc != null)
            {
                for (int i = 1; i <= pixDoc.GetPageCount(); i++)
                {
                    pixDoc.ShowPage(SHOWPAGETYPE.GotoPage, i);
                    pixDoc.Text("L1", 10.0, 10.0, tbText.Text);
                }
            }
        }

This code might look a little unfamiliar to COM programmers. The reason is that PixEdit is designed as a rich GUI app, and a lot of edit functionality is tied together with the display functionality. Hence, before calling an edit method, always call ShowPage() for the page you want to modify. Now the parameters of the Text() method are pretty self explanatory apart from the "string LayerSpec". PixEdit can work with up to 8 layers in the document named "L1" to "L8". However, layers are only supported in PixEdit's own TDF format and not for instance PDF, so you will likely be using only the first layer "L1" for your automated editing.

Try running the app and insert a text. You should see the text is inserted in black at position 10,10 mm on every page.

Saving the document

The final step in this first article is to save the document. Go back to the form in design mode and add a Save button. Double-click it and add the following code:

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (pixDoc != null)
            {
                pixDoc.FileSave();
            }
        }

With the app, try inserting some text in a document again and click Save. Close down and open again, and you should see that the document was saved.

More information about COM programming with PixEdit Desktop

We have not published an official documentation of the entire COM interface of PixEdit. However, you are welcome to contact us to get more information and code samples to help you get on with your project. And we are also planning to add more articles about COM here on the forum, so stay tuned!

Download source code from GitHub

To get started quickly, download the full source code of this sample project from GitHub:

PixEdit COM Automation project

  • Liked by
  • Andreas
Close