PdfSharp System.NotImplementedException:无法为键创建值:/Info

问题描述 投票:0回答:1

我使用 PdfSharp 和 MigraDoc 使用 Visual Studio 用 C# 编写了一个应用程序。这很好用。 当我将应用程序构建为独立 exe 时,当我使用 MeasureString 方法计算文本时,应用程序将崩溃。 我将成为的异常是 System.NotImplementedException: Cannot create value for key: /Info 仅当应用程序是独立的 exe 时,我才会收到此错误。当我将它编译为不是独立的 exe 时,它就可以工作了。

我用谷歌搜索了它,但只在 GitHub 上找到了这个链接:https://github.com/ststeiger/PdfSharpCore/issues/120 他们使用的是 Unity,而不是 Visual Studio。我不知道他们添加的这些文件应该放在哪里。 有人知道如何解决我的问题吗? 如果没有任何效果,那么我必须使用包含 dll 的完整文件夹,而不是独立的 exe。

回溯:

System.NotImplementedException: Cannot create value for key: /Info
   at PdfSharp.Pdf.PdfDictionary.DictionaryElements.GetValue(String, VCF)
   at PdfSharp.Pdf.Advanced.PdfTrailer.get_Info()
   at PdfSharp.Pdf.PdfDocument.get_Info()
   at PdfSharp.Pdf.PdfDocument..ctor()
   at PDFVerificationProtocol.PDFCreator.CalculateRequiredColumnWidth(String) in Z:\PDF-Prüfprotokoll_Test\PDFVerificationProtocol\PDFVerificationProtocol\PDFCreator.cs:line 381
   at PDFVerificationProtocol.PDFCreator.AddPSetTable(Section) in Z:\PDF-Prüfprotokoll_Test\PDFVerificationProtocol\PDFVerificationProtocol\PDFCreator.cs:line 229
   at PDFVerificationProtocol.PDFCreator.Run() in Z:\PDF-Prüfprotokoll_Test\PDFVerificationProtocol\PDFVerificationProtocol\PDFCreator.cs:line 54
   at PDFVerificationProtocol.Program.RunProgram() in Z:\PDF-Prüfprotokoll_Test\PDFVerificationProtocol\PDFVerificationProtocol\Program.cs:line 34
   at PDFVerificationProtocol.Program.Main(String[]) in Z:\PDF-Prüfprotokoll_Test\PDFVerificationProtocol\PDFVerificationProtocol\Program.cs:line 11

此方法出现问题:

private double CalculateRequiredColumnWidth(string text)
        {
            // Use the global style font settings
            XFont font = new XFont(globalStyle.Font.Name, globalStyle.Font.Size);

            // Dummy
            PdfDocument document = new PdfDocument();
            document.AddPage();
            XGraphics gfx = XGraphics.FromPdfPage(document.Pages[0]);

            // Measure the width of the text using XGraphics
            XSize textSize = gfx.MeasureString(text, font);

            // Calculate the required width including padding
            double requiredWidth = textSize.Width + (2 * cellPadding);

            return requiredWidth;
        }
c# visual-studio-2022 pdfsharp migradoc pdfsharpcore
1个回答
0
投票

我可以确认,正如问题的作者已经提到的,

<TrimMode>partial</TrimMode>
下的
.csproj
文件中的
<PropertyGroup>
确实足以解决这个问题。在我的具体场景中,
.csproj
文件的相关部分如下所示:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    ...
    <TrimMode>partial</TrimMode>
  </PropertyGroup>
...

</Project>

或者,您可以在“发布”对话框中取消选中“修剪未使用的代码”选项。不过这样的话,.exe文件会比较大。

© www.soinside.com 2019 - 2024. All rights reserved.