empira / PDFsharp

PDFsharp and MigraDoc Foundation for .NET 6 and .NET Framework
https://docs.pdfsharp.net/
Other
492 stars 114 forks source link

Trimming self-contained deployment fails with an exception #149

Closed kebox7 closed 1 month ago

kebox7 commented 1 month ago

Describe the bug

Trimming self-contained deployment fails with an exception if the trim mode is set to <TrimMode>full</TrimMode>. Stack trace:

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 MigraDoc.Rendering.PdfDocumentRenderer.CreatePdfDocument()
   at MigraDoc.Rendering.PdfDocumentRenderer.get_PdfDocument()
   at MigraDoc.Rendering.PdfDocumentRenderer.PrepareDocumentRenderer(Boolean)
   at MigraDoc.Rendering.PdfDocumentRenderer.PrepareRenderPages()
   at MigraDoc.Rendering.PdfDocumentRenderer.RenderDocument()

If the trim mode is set to <TrimMode>partial</TrimMode> then everything works fine.

Code:

public void SaveToFile(string fileName, Document document) {
    var renderer = new PdfDocumentRenderer() {
        Document = document
    };
    renderer.RenderDocument();
    renderer.PdfDocument.Save(fileName);
}

Additional context

ThomasHoevel commented 1 month ago

It's not a bug, it's a feature. PDFsharp uses reflection and "full trimming" eliminates required code.

kebox7 commented 1 month ago

@ThomasHoevel, thanks for the answer.

For myself, I solved this issue as follows, I've marked the PdfSharp assembly as the root: <TrimmerRootAssembly Include="PdfSharp" />. It's not the most beautiful solution, but it works.