What should be the Content type for pdf for Azure AI Translator .NET library

Ubana, Siddharth 40 Reputation points
2025-07-29T18:45:11.4666667+00:00

Hi, I am using Azure AI translator .NET SDK for translating a sample pdf document. I have created a Web API and inside that I have an action method with following code to translate a sample pdf. When I am running the API, I am getting the exception 'The format parameter is not valid.'. This seems like an error with the content type 'application/pdf'. Could you please help me resolve this issue.

 [HttpPost]
 public async Task<ActionResult<string>> Translate()
 {
     var credential = new AzureKeyCredential(_translationSettings.Key);
     var client = new SingleDocumentTranslationClient(new Uri(_translationSettings.Endpoint), credential);

     try
     {
         string filePath = @"C:\Users\siubana\Desktop\TestTranslatorDocument.pdf";
         using Stream fileStream = System.IO.File.OpenRead(filePath);
         var sourceDocument = new MultipartFormFileData(Path.GetFileName(filePath), fileStream, "application/pdf");
         DocumentTranslateContent content = new DocumentTranslateContent(sourceDocument);
         var response = await client.TranslateAsync("hi", content);

         var responseString = response.ToString();

         return Ok(new { translation = responseString });
     }
     catch (RequestFailedException exception)
     {
         _logger.LogError(exception, "Translation failed.");
         return StatusCode(500, "Translation failed.");
     }
 }

Azure AI Translator
Azure AI Translator
An Azure service to easily conduct machine translation with a simple REST API call.
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Nikhil Jha (Accenture International Limited) 230 Reputation points Microsoft External Staff Moderator
    2025-08-06T18:33:46.14+00:00

    Hello Ubana, Siddharth,
    Thank you for your question and for sharing your implementation details using the Azure AI Translator .NET SDK. The error message: "The format parameter is not valid." typically occurs when the file format is not supported by the synchronous document translation API. While your code correctly sets the content type to "application/pdf", the issue might lie in the API’s current limitations indicating a mismatch between the content type and accepted formats for the translation operation.

    Key Points to Address Your Scenario:

    1. Azure AI Translator Supported Formats

    • As of the most recent Azure documentation, the Document Translation service supports various file formats, including PDF. The typical content type for PDFs is application/pdf.
    • However, the error you are facing stems not just from the MIME type but also from the format parameter required by the Document Translator.
    1. How to Specify the Format Parameter
    • In most SDK and API scenarios, the format parameter is explicitly passed to indicate the document format (e.g., 'pdf', 'docx', etc.), not automatically derived just from the MIME/content type.
    • If you are using the REST API, you'd set format=pdf in the request URL or body.
    • In the .NET SDK, you typically do not have to set this manually if the input is correctly constructed. But some SDKs expect certain constructors or methods for PDFs.
    1. SingleDocumentTranslationClient and DocumentTranslateContent
    • The SingleDocumentTranslationClient is a newer model, and from current SDK documentation, it expects you to wrap the PDF file as a supported DocumentTranslateContent.
    • However, mistakes in content construction or the use of MultipartFormFileData might lead to format detection errors.

    Recommended solutions:

    1. To translate PDF documents, you must use the asynchronous document translation API, which supports a broader range of formats including scanned and native PDFs. This process requires:
    • Uploading the source PDF to an Azure Blob Storage container
    • Creating a target container for the translated output
    • Using the DocumentTranslationClient.BeginTranslationAsync( ) method with SAS URLs

    Here’s a quick reference:

    var client = new DocumentTranslationClient(new Uri(endpoint), new AzureKeyCredential(key));
    var operation = await client.StartTranslationAsync(sourceUri, targetUri, targetLanguage);
    await operation.WaitForCompletionAsync();
    
    1. Verify SDK Version and Documentation
    • Ensure you are using the most up-to-date Azure.AI.Translation.Document SDK.
    • Review the relevant Azure AI Translator .NET SDK documentation for method signatures and supported formats.
    1. Azure AI Translator's document translation API does support PDFs, but the .NET SDK expects the correct format to be explicitly declared during request construction. The DocumentTranslateContent class must receive both the file stream and a correctly specified format.
        try
        {
            string filePath = @"C:\Users\siubana\Desktop\TestTranslatorDocument.pdf";
            using Stream fileStream = System.IO.File.OpenRead(filePath);
    
            // Provide the correct format explicitly as "pdf"
            var sourceDocument = new MultipartFormFileData(
                fileName: Path.GetFileName(filePath),
                content: fileStream,
                contentType: "application/pdf",
                format: "pdf" // This line is critical
            );
    
            DocumentTranslateContent content = new DocumentTranslateContent(sourceDocument);
            var response = await client.TranslateAsync("hi", content);
    
            return Ok(new { translation = response.ToString() });
        }
    
    1. General Troubleshooting
    • Make sure your file stream is not empty, corrupted, or locked.
    • Ensure that the translation region and service endpoint align with your subscription.

     

    Reference Links:

    1. https://learn.microsoft.com/en-us/dotnet/api/azure.ai.translation.document?view=azure-dotnet
    2. https://learn.microsoft.com/en-us/azure/ai-services/translator/document-translation/overview#synchronous-supported-document-formats
    3. https://learn.microsoft.com/en-us/answers/questions/1661419/azure-translator-synchronous-document-translation

    Happy to help !! 😊


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.