I have a project using MigraDoc to generate pdfs on a AspNet Core webserver using .net 6.0.
The actual document generation lives in a library project separate from the server project so it can be re-used.
Currently with default settings when a pdf is rendered it is blank unless I set CompressContentStreams to false, this is even the case even in a simple hello world type document.
Additionally when I attempt to add Images (pngs) from files the image don't appear to be rendering properly a simple red square renders as black and a logo image isn't rendered at all.
Here's some relevant code snippets:
Code:
Library.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net481;net6.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="PDFsharp-MigraDoc" Version="6.1.0" />
</ItemGroup>
</Project>
Code:
WebApp.csproj
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
...
<ItemGroup>
<ProjectReference Include="..\Library\Library.csproj" />
</ItemGroup>
<ItemGroup>
<Content Include="Resources\*.*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>
Code:
Code to Build and generate file to be returned to client
public Document BuildDocument() {
Document document = new Document();
document.UseCmykColor = true;
Section section = document.AddSection();
section.PageSetup = document.DefaultPageSetup.Clone();
float pageWidth = (float)(section.PageSetup.PageWidth.Inch - section.PageSetup.LeftMargin.Inch - section.PageSetup.RightMargin.Inch);
section.AddParagraph().AddFormattedText("Hello World", _largeFont);
var image = section.AddParagraph().AddImage("Resources/google-logo-0.png");
section.AddParagraph().AddImage("Resources/RedSquare.png");
image.Height = 100.0f;
section.AddParagraph().AddText("after square");
return document;
}
public byte[] CreateDocument() {
PdfDocumentRenderer renderer = new PdfDocumentRenderer();
renderer.Document = document;
renderer.PdfDocument = new PdfSharp.Pdf.PdfDocument();
renderer.PdfDocument.Options.CompressContentStreams = false;
renderer.RenderDocument();
byte[] reportBytes;
using (MemoryStream ms = new MemoryStream())
{
renderer.Save(ms, false);
reportBytes = new byte[ms.Length];
ms.Read(reportBytes, 0, (int)ms.Length);
}
return reportBytes;
}
The output is attached. The square should be red and there should be a google logo there.