3

我目前使用 Gembox.Document 从 PDF 文档中读取内容。我有一个包含所有内容的类库和一个引用它的自托管 .NET Core 3.1 服务。我用 PDF 数据查询服务,它用内容响应。我现在想将此功能移至 azure 函数 (v3),但我遇到了以下错误:

无法加载文件或程序集“PresentationCore,版本=4.0.0.0,文化=中性,PublicKeyToken=31bf3856ad364e35”。该系统找不到指定的文件。

为了简化它,我只将基本部分移到了 azure 函数中,您可以在下面看到:

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");
    }
}

这是天蓝色功能的限制吗?我已经阅读了一些相互矛盾的事情,这些事情表明它可以而且不能因为它使用 WPF dll 来完成。作为记录,GemBox 网站提供了一个在 azure 函数中创建 PDF 文档的示例:https ://www.gemboxsoftware.com/document/examples/create-word-pdf-on-azure-functions-app-service/ 5901 . 所以我不明白为什么我也无法阅读!

谢谢!

编辑1:

根据 mu88 的评论,我已将 .csproj 文件更改为以下内容,但没有帮助。

在此处输入图像描述

4

2 回答 2

1

我联系了 GemBox 支持,他们回复如下

不幸的是,GemBox.Document 使用 WPF 来读取 PDF 文件。因此,即使您可以在 Azure Functions 上编写 PDF 文件,恐怕您也无法阅读它们。

而且,我应该指出 GemBox.Document 中的 PDF 阅读器从未离开 BETA 阶段,它的用途有限。如需更多信息,请查看阅读 PDF 格式(测试版)部分的支持级别。

相反,我建议您尝试 GemBox.Pdf,请参阅其阅读 示例。使用 GemBox.Pdf,您可以在 Azure Functions 上读取和写入 PDF 文件。

最后,从长远来看,我们计划用 GemBox.Pdf 中包含的更新实现替换 GemBox.Document 中 PDF 阅读器 (BETA) 和 PDF 编写器的当前(内部)实现,而不更改 GemBox.Document 的公共 API . 但这不会在今年完成,以后我现在不能说。

唉,GemBox.Document..还不可能。

于 2020-11-19T14:50:53.467 回答
0

GemBox.Document有这个问题(它可能依赖于 .net Framework 组件),但GemBox.Pdf可以正常工作,如下所示。

我使用GemBox.Pdf具有以下功能的 nuget 对其进行了测试,它适用于在部署的功能应用程序中创建和加载 pdf。

在此处输入图像描述

创建 PDF:

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

            using (var document = new PdfDocument())
            {
                // Add a page.
                var page = document.Pages.Add();

                // Write a text.
                using (var formattedText = new PdfFormattedText())
                {
                    formattedText.Append("Hello World!");

                    page.Content.DrawText(formattedText, new PdfPoint(100, 700));
                }

                var fileName = "Output.pdf";
                var options = SaveOptions.Pdf;
                using (var stream = new MemoryStream())
                {
                    document.Save(stream, options);
                    return new FileContentResult(stream.ToArray(), "application/pdf") { FileDownloadName = fileName };
                }
            }
        }

在此处输入图像描述

加载PDF:

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

            try
            {
                StringBuilder sb = new StringBuilder();
                using (var document = PdfDocument.Load(req.Body))
                {
                    foreach (var page in document.Pages)
                    {
                        sb.AppendLine(page.Content.ToString());
                    }
                }

                return new OkObjectResult(sb.ToString());
            }
            catch (Exception e)
            {
                return new ExceptionResult(e, true);
            }
        }
于 2020-11-18T16:52:46.210 回答