Create a UiPath PDF Converter Custom activity

In our previous post in this series about UiPath, we consumed the Muhimbi REST API using the out-of- box UiPath actions, but we also used a lot of ‘Invoke Code‘ activities containing C# code snippets.

UiPath is a tool that empowers Business Analysts / Power Users and those who have very limited coding knowledge, to automate business processes within their organisations. In order to highlight this, instead of using C# code snippets, we can create a custom workflow activity which can then be easily used and reused without any custom coding.

So, with that in mind, lets create our first PDF Conversion custom workflow activity.

This series consist of the following parts:

  1. Generate PDFs using UiPath (part 1).
  2. Creating a reusable UiPath activity (this post).
  3. Using a custom PDF Conversion Activity in a real world UiPath workflow (part 3).

Before we begin, please make sure the following prerequisites are in place:

  • Muhimbi PDF Converter Services Online full, Free or Trial subscription (Sign up).
  • Microsoft Visual Studio with the .NET Desktop Development workload installed.
  • NuGet Package Explorer, which can be found here.

To better understand how to write the code for a custom workflow activity, we’re going to re-create our previous post as a single step ‘Convert to PDF‘ activity.

Step 1: Create a Project in Visual Studio.

  1. Launch Microsoft Visual Studio.
  2. Click File > New >Project. The ‘New Project‘ window will then be displayed.
  3. Click ‘Visual C#‘ on the left hand menu. The list of all dependencies using C# will then be displayed.
  4. Optionally, fill in the ‘Name‘ field with a preferred name for your custom activity. In our case, we’ll use ‘UiPath Muhimbi Activity‘.
  5. Select ‘Class Library (.NET Framework)‘ and click ‘OK‘ in the bottom right hand of your screen. This allows us to export the custom activity as a .dll file.

Step 2: Adding ‘References‘ to our project.

  • Click ‘Project‘ / ‘Add References…‘.
  • Add the 3 ‘References‘ listed below:
    • System.Activities
    • System.ComponentModel.Composition
    • System.Web.Extensions.

Step 3: 

Add the code below for The ‘Muhimbi‘ custom workflow activity.

using System;
using System.Activities;
using System.ComponentModel;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;

namespace UiPath_Muhimbi_Activity
    public class Convert_to_PDF_Activity : CodeActivity
        string response;
        public InArgument<string> API_Key { get; set; }

        public InArgument<string> FilePath { get; set; }

        public InArgument<string> OutFilePath { get; set; }

        protected override void Execute(CodeActivityContext context)
            var apiKey = API_Key.Get(context);
            var filePath = FilePath.Get(context);

            Byte[] bytes = File.ReadAllBytes(filePath);
            string sourceFileContent = Convert.ToBase64String(bytes);
            string sourceFileName = Path.GetFileName(filePath);

            string client = "";
            WebRequest postwebRequest = WebRequest.Create(client);

            // ** Add POST Method
            postwebRequest.Method = "POST";

            // ** Add Headers
            postwebRequest.ContentType = "application/json";
            postwebRequest.Headers.Add("API_KEY", apiKey);

            // ** Create JSON body
            string postData = @"{
                ""use_async_pattern"": ""false"",
                 ""source_file_name"": """ + sourceFileName + @""",
                 ""source_file_content"": """ + sourceFileContent + @""",
                 ""output_format"": ""PDF"", 
                 ""copy_metadata"": ""false"",
                 ""fail_on_error"": ""true"",

            // ** Add Json Body
            // ** Execute the request
            using (var streamWriter = new StreamWriter(postwebRequest.GetRequestStream()))

                var httpresponse = postwebRequest.GetResponse();

                using (var streamreader = new StreamReader(httpresponse.GetResponseStream()))
                    response = streamreader.ReadToEnd();


                // ** Deserialize the response 
                JavaScriptSerializer ser = new JavaScriptSerializer();
                ser.MaxJsonLength = Int32.MaxValue;
                var conversionResponse = ser.Deserialize<Output>(response);

                var outFilePath = OutFilePath.Get(context);
                string outPath = Path.GetDirectoryName(outFilePath);
                Byte[] outFile = Convert.FromBase64String(conversionResponse.processed_file_content);
                string pdfPath = outPath + "\\" + conversionResponse.base_file_name + ".pdf";
                File.WriteAllBytes(@pdfPath, outFile);

Step 4: 

Add another ‘Class…‘  file to you project as shown in the image below and name your class file ‘Output.cs‘.

  • Add  your code to ‘Output.cs‘. In our case, it needs to look something like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UiPath_Muhimbi_Activity
    class Output
        public string processed_file_content { get; set; }
        public string base_file_name { get; set; }
        public string result_code { get; set; }
        public string result_details { get; set; }

Step 5:

  • Click on ‘Build‘ in the main menu bar, then select ‘Build Solution‘. The Output panel is then displayed, informing you that that the file is built and displays its path.

Step 6:

Adding the external assembly (.dll) in UiPath.

Before the external assembly can be added in UiPath, a ‘NuGet Package‘ needs to be created first.

Creating a ‘NuGet Package‘ is done as follows:

  1. Launch ‘NuGet Package Explorer‘.
  2. Click ‘Create a new package‘ (Ctrl + N). A split-window will be displayed, which shows ‘Package metadata‘ and ‘Package contents‘.
  3. Right-click the ‘Package contents‘ section to open the context menu.
  4. Click ‘Add lib folder‘ and add a new ‘lib‘ item is created in the ‘Package contents‘ section.
  5. Right-click ‘lib‘ and select ‘Add Existing File‘….
  6. Load the external assembly (.dll) created previously.
  7. Click Edit > Edit Metadata. The ‘Package metadata‘ section will be displayed.
  8. Fill in the fields as you see fit to better describe your custom activity.
  9. Fill in the ‘Id‘ field. In our case, we decided on: ‘ActivitiesUiPathMuhimbiPDFConverter‘.
  10. Click File > Save. In our case, the ‘ActivitiesUiPathMuhimbiPDFConverter.1.0.0.nupkg‘ file is created.
  11. Copy the file inside the ‘Packages‘ folder of your ‘UiPath Studio‘ install location (%USERPROFILE%\.nuget\Packages). The ‘NuGet Package‘ containing your custom workflow activity is now ready to be loaded in ‘UiPath Studio‘.

Note:  The ‘NuGet Package‘ Id field must contain the keyword ‘Activities‘ to appear in the Manage Packages window, in Studio.

Step 7:

  1. Open ‘UiPath Studio‘ and create a new Project of type, ‘Process‘.
  2. Give the new, blank process a meaningful ‘Name‘, ‘Location‘ and ‘Description‘(optional) and click on ‘Create‘.

Step 8:

Click on the ‘Activities‘ blade in the ‘UiPath Studio‘ and search for ‘ActivitiesUiPathMuhimbiPDFConverter.1.0.0‘ (or whatever name you used instead) and then click on ‘Search in available packages‘.

Step 9: 

The ‘Manage Packages‘ Window will open, then select the package and click on ‘Install‘ and then ‘Save‘.

Step 10:

Search for ‘Muhimbi‘ in the ‘Activities‘ pane. The ‘Convert To PDF Activity‘ should be visible in the ‘UiPath Studio‘ Activity pane.

Now that we have our ‘Custom Activity‘ ready, lets create a new workflow and automate ‘Conversion of all the documents in a Folder’ as described in this post.

Reference articles:

  • Creating a Custom Activity in UiPath (link).
  • Uploading Your Custom Activity to the Community Repository (link).

Subscribe to this blog for the latest updates about SharePoint Online, Power Automate (Microsoft Flow), Power Apps and document conversion and manipulation using The PDF Converter.

2 thoughts on “Create a UiPath PDF Converter Custom activity

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s