1

I am Adding ConvertApi nuget package to Convert PDF to Doc file, But getting below Error

Could not install package 'ConvertApi 2.7.0'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.6.1', but the package does not contain any assembly references or content files that are compatible with that framework.

Note: You can Suggesst some other API's as well to achieve the above task.

1 Answers1

1

The ConvertApi 2.7.0 NuGet package is .NET Core 2 version library and can be installed on .NET 4.7 or higher. However, you can use plain C# implementation to call ConvertAPI REST API, the example below use WebClient to send an MS Word file for conversion to PDF document.

using System;
using System.Net;
using System.IO;

class MainClass {
  public static void Main (string[] args) {
            const string fileToConvert = "test.docx";
            const string fileToSave = "test.pdf";           
            const string Secret="";

            if (string.IsNullOrEmpty(Secret))
              Console.WriteLine("The secret is missing, get one for free at https://www.convertapi.com/a");
            else
              try
              {
                  Console.WriteLine("Please wait, converting!");
                  using (var client = new WebClient())
                  {
                      client.Headers.Add("accept", "application/octet-stream");
                      var resultFile = client.UploadFile(new Uri("http://v2.convertapi.com/convert/docx/to/pdf?Secret=" + Secret), fileToConvert); 
                      File.WriteAllBytes(fileToSave, resultFile );
                      Console.WriteLine("File converted successfully");
                  }
              }
              catch (WebException e)
              {
                  Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
                  Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
                  Console.WriteLine("Body : {0}", new StreamReader(e.Response.GetResponseStream()).ReadToEnd());
              }
  }
}
Tomas
  • 17,551
  • 43
  • 152
  • 257