• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
1

PDFServicesSDK Execute command does not work when invoked from Windows Forms UI

New Here ,
Aug 31, 2023 Aug 31, 2023

Copy link to clipboard

Copied

Hi

 

I am new to using PDFServicesSDK - I have used example code from:

https://developer.adobe.com/document-services/docs/overview/pdf-services-api/quickstarts/dotnet/

To create a test example that will convert pdf to docx.

I wrapped the example in the following class:

    public class AdobeAPI
    {
        private const string PDF_SERVICES_CLIENT_ID = "clientid";
        private const string PDF_SERVICES_CLIENT_SECRET = "secret";

        public static void ExportToDocx()
        {
            try
            {
                var input = "d:\\Test.pdf";
                var output = "d:\\Test.docx";

                if (File.Exists(output))
                {
                    File.Delete(output);
                }

                // Initial setup, create credentials instance.                
                Credentials credentials = Credentials.ServicePrincipalCredentialsBuilder()
                    .WithClientId(PDF_SERVICES_CLIENT_ID)
                    .WithClientSecret(PDF_SERVICES_CLIENT_SECRET)
                    .Build();
                
                // Create an ExecutionContext using credentials and create a new operation instance.
                ExecutionContext executionContext = ExecutionContext.Create(credentials);
                ExportPDFOperation exportPdfOperation = ExportPDFOperation.CreateNew(ExportPDFTargetFormat.DOCX);
                // Provide an input FileRef for the operation.
                FileRef sourceFileRef = FileRef.CreateFromLocalFile(input);
                exportPdfOperation.SetInput(sourceFileRef);
                // Execute the operation.
                FileRef result = exportPdfOperation.Execute(executionContext);

                // Save the result to the specified location.
                result.SaveAs(output);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

When I invoke this from Console Application:

namespace AdobeAPIConsole
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Start");
            AdobeAPI.ExportToDocx();
            Console.WriteLine("Done");
        }
    }
}

It works fine and file is being converted.

 

When I create a simple Windows Forms App with a single button and invoke this on button click:

namespace AdobeAPIWindowsForms
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void sbTest_Click(object sender, EventArgs e)
        { 
            AdobeAPI.ExportToDocx();
        }
    }
}

Then execution stops at:

// Execute the operation.
FileRef result = exportPdfOperation.Execute(executionContext);

 It doesn't crash, it doesn't throw any error. It simply hangs there forever.

 

What am I doing wrong, I need to be able to use PDFServicesSDK from Windows Forms app. Can anyone point me what can be the possible cause of this behaviour?

 

Thanks

Views

425

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 20, 2023 Sep 20, 2023

Copy link to clipboard

Copied

Is there any solution to resolve this?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 20, 2023 Sep 20, 2023

Copy link to clipboard

Copied

The behavior you're describing, where the execution hangs indefinitely when calling exportPdfOperation.Execute(executionContext) from a Windows Forms application, suggests a potential issue with how the Adobe PDF Services SDK interacts with the UI thread in a Windows Forms application.

In Windows Forms applications, long-running or blocking operations should not be executed on the UI thread because they can freeze the user interface and make the application unresponsive. To avoid this issue, you should perform potentially time-consuming operations on a separate background thread or using asynchronous programming.

Here's how you can modify your code to run the AdobeAPI.ExportToDocx() method on a separate thread using Task.Run:

 

 

private async void sbTest_Click(object sender, EventArgs e)
{
    await Task.Run(() => AdobeAPI.ExportToDocx());
    MessageBox.Show("Conversion complete!");
}

 

In this updated code:

  1. async is added to the event handler to allow asynchronous execution.

  2. Task.Run is used to run the AdobeAPI.ExportToDocx() method on a background thread. This ensures that the UI thread remains responsive.

  3. After the conversion is complete, a message box is displayed to notify the user.

By running the conversion operation on a background thread, you should avoid the hanging issue, and your Windows Forms application will remain responsive during the conversion process.

Please note that you may need to handle exceptions and provide appropriate user feedback in your application, especially when dealing with file operations and external services like the Adobe PDF Services SDK.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 21, 2023 Sep 21, 2023

Copy link to clipboard

Copied

LATEST

Thanks for your reply and example. That indeed works and is a workaround that I can use.

 

I still think that there must be something wrong with pdf api though to hang the app indefintely. Note that I have no problem with my app being unresponsive while pdf call is executed - I have a problem with it never finishing execution.  My test call takes less than 2 seconds to execute. It is not a problem to have app unresponisve for 2 seconds but it remains unresponsive forever.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources