facebooktwittermenuarrow-up

GemBox Support Forum

Read PDF using GemBox.Document in azure function

Hi,

I am able to read a PDF using GemBox.Document in a .NET Core 3.1 hosted service. I am using the following code:

using (var ms = new MemoryStream(data))
{
    // Load document from file's path.
    var document = DocumentModel.Load(ms, LoadOptions.PdfDefault);

    // .. read content
}

However, if I try to run the same code in an azure function (v3) I get the following error:

Could not load file or assembly ‘PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35’. The system cannot find the file specified.

Can anyone explain why? I have even followed the example code of how to create a PDF in an azure function (https://www.gemboxsoftware.com/document/examples/create-word-pdf-on-azure-functions-app-service/5901) but reading one doesn’t seem to work.

Thanks

Full code included below:

public static class Function1
{
    [FunctionName("Function1")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
        ILogger log)
    {
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");
        ComponentInfo.FreeLimitReached += (sender, args) => args.FreeLimitReachedAction = FreeLimitReachedAction.ContinueAsTrial;

        try
        {
            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            ParseRequest data = JsonConvert.DeserializeObject<ParseRequest>(requestBody);

            StringBuilder sb = new StringBuilder();

            using (var ms = new MemoryStream(data.Data))
            {
                // Load document from file's path.
                var document = DocumentModel.Load(ms, LoadOptions.PdfDefault);

                foreach (var childElement in document.GetChildElements(true, ElementType.Paragraph))
                {
                    sb.AppendLine(childElement.Content.ToString());
                }
            }

            return new OkObjectResult(sb.ToString());
        }
        catch (Exception e)
        {
            return new OkObjectResult("Unable to read document");
        }
    }

    public class ParseRequest
    {
        public byte[] Data { get; set; }
    }
}

Hi Aaron,

Unfortunately, GemBox.Document uses WPF for reading PDF files.
So, even though you can write PDF files on Azure Functions, I’m afraid you cannot read them.

But also, I should point out that PDF reader in GemBox.Document never left the BETA stage, it has limited usage. For more information, please check the Support level for reading PDF format (beta) section.

Instead, I would suggest you try out GemBox.Pdf, see its Reading example.
With GemBox.Pdf you can read and write PDF files on Azure Functions.

Last, in the long term, we plan to replace the current (internal) implementations of both PDF reader (BETA) and PDF writer in GemBox.Document with a newer implementation that’s contained in GemBox.Pdf without changing the public API of GemBox.Document.
But that will not be done in the current year and for later I cannot say at this moment.

Regards,
Mario