PDFsharp & MigraDoc Forum
https://forum.pdfsharp.net/

.NET 8 AOT support
https://forum.pdfsharp.net/viewtopic.php?f=2&t=4525
Page 1 of 1

Author:  toraxe [ Tue Dec 26, 2023 12:44 pm ]
Post subject:  .NET 8 AOT support

Is it a hard task to get the PDFSharp to work with .NET 8 ahead-of-time (AOT) compiled to native code?

It would be nice to generate PDFs with C# with a high performant self containd application.

I have tried set the project property group <PublishAot>true</PublishAot>
and publish an console application.
It builds and publishes the files for win64 in my case with some trim warnings.
When i creates a new PDFDocument in the code and it throwes the exception:

Unhandled Exception: System.NotImplementedException: Cannot create value for key: /Info
at PdfSharp.Pdf.PdfDictionary.DictionaryElements.GetValue(String, VCF) + 0x34d
at PdfSharp.Pdf.Advanced.PdfTrailer.get_Info() + 0x20
at PdfSharp.Pdf.PdfDocument.get_Info() + 0x20
at PdfSharp.Pdf.PdfDocument..ctor(Stream) + 0x64

Author:  TH-Soft [ Sun Dec 31, 2023 4:16 pm ]
Post subject:  Re: .NET 8 AOT support

toraxe wrote:
Is it a hard task to get the PDFSharp to work with .NET 8 ahead-of-time (AOT) compiled to native code?
I dunno. Didn't try yet.

I think PDFsharp/MigraDoc use reflection to some extent. Creating a size-optimized self-contained package will not work as .NET removes too much code in the process.
Maybe the same problem occurs with AOT. Try a variant that does not remove heuristically unused code if there is one.

Author:  toraxe [ Tue Jan 02, 2024 7:10 am ]
Post subject:  Re: .NET 8 AOT support

TH-Soft wrote:
toraxe wrote:
Is it a hard task to get the PDFSharp to work with .NET 8 ahead-of-time (AOT) compiled to native code?
I dunno. Didn't try yet.

I think PDFsharp/MigraDoc use reflection to some extent. Creating a size-optimized self-contained package will not work as .NET removes too much code in the process.
Maybe the same problem occurs with AOT. Try a variant that does not remove heuristically unused code if there is one.


Thank you TH-Soft for your reply, I got it working by setting the property <TrimMode>partial</TrimMode>.
As you saied it dident remove as much code but it works.

Thanks!

Author:  Thomas Hoevel [ Tue Aug 20, 2024 8:11 am ]
Post subject:  Re: .NET 8 AOT support

Setting `<TrimMode>partial</TrimMode>` worked for me.
But using `<TrimmerRootAssembly Include="PdfSharp" />` instead also worked for me and the resulting .EXE was much smaller.


Code:
    <PropertyGroup>
      <OutputType>Exe</OutputType>
      <TargetFramework>net8.0</TargetFramework>
      <ImplicitUsings>enable</ImplicitUsings>
      <Nullable>enable</Nullable>
      <PublishAot>true</PublishAot>
      <PublishTrimmed>true</PublishTrimmed>
      <!--<TrimMode>partial</TrimMode>-->
      <InvariantGlobalization>true</InvariantGlobalization>
    </PropertyGroup>
   
    <ItemGroup>
      <TrimmerRootAssembly Include="PdfSharp" />
    </ItemGroup>

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/