I have written an app in C# with Visual Studio using PdfSharp & MigraDoc. This works fine. When I build the app as standalone exe then the app will crash when I will calculate the text with with the method MeasureString. The Exception which I will become is System.NotImplementedException: Cannot create value for key: /Info I will only get this error when the app is a standalone exe. When I compile it as not a standalone exe then it works.
I have googled it but only find this link here on GitHub: https://github.com/ststeiger/PdfSharpCore/issues/120 They are using Unity and not Visual Studio. And I don't know where to place this files which they have added. Does somebody know how to solve my problem? If nothing works, then I have to use the complete folder with the dll's inside instead of a standalone exe.
The traceback:
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
In this method occurs the problem:
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;
}
I can confirm that, as the author of the question already mentioned, <TrimMode>partial</TrimMode>
in the .csproj
file under <PropertyGroup>
indeed is sufficient to resolve this issue. In my specific scenario, the relevant portion of the .csproj
file appears as follows:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
...
<TrimMode>partial</TrimMode>
</PropertyGroup>
...
</Project>
Alternatively, you can uncheck the "Trim unsued code" option in the Publish dialog. However, in this case, the .exe file will be relatively large.