empira / PDFsharp

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

Trimming self-contained deployment fails with an exception #149

Closed kebox7 closed 3 months ago

kebox7 commented 3 months 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 3 months ago

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

kebox7 commented 3 months 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.