PdfSharpCore
PdfSharpCore copied to clipboard
Unable to draw XForm into PdfPage
Hi, I have a problem with rendering XForm into a PdfPage. First I draw image into XForm, then i try to draw XForm into PdfPage of a PdfDocument. After saving the document, the page is empty.
I am trying it in a small sample project and I think I have followed the documentation exactly. Please tell me what I am doing wrong.
using PdfSharpCore.Drawing;
using PdfSharpCore.Pdf;
var image = XImage.FromFile(Path.Combine(Directory.GetCurrentDirectory(), "bg_red.png"));
var document = new PdfDocument();
var page = document.AddPage();
var form = new XForm(document, XUnit.FromMillimeter(30), XUnit.FromMillimeter(45));
var templateGfx = XGraphics.FromForm(form);
templateGfx.DrawImage(image, 0, 0);
templateGfx.Dispose();
var pageGfx = XGraphics.FromPdfPage(page);
pageGfx.DrawImage(form, 0, 0);
pageGfx.Dispose();
using(var stream = new MemoryStream()){
document.Save(stream);
document.Close();
File.WriteAllBytes(Path.Combine(Directory.GetCurrentDirectory(), "result.pdf"), stream.ToArray());
}
PS: I also tried drawing the same image right into the PdfPage, which worked as expected and image was there.
Have a similar issue as @Martinus-Novakus:
var document = new PdfDocument();
var page = document.AddPage();
var form = new XForm(document, page.Width, page.Height);
var font = new XFont("OpenSans", 20, XFontStyle.Bold);
var formGfx = XGraphics.FromForm(form);
formGfx.DrawString(
"Hello World!", font, XBrushes.Black,
new XRect(0, 0, form.PixelWidth, form.PixelHeight),
XStringFormats.Center);
formGfx.Dispose();
var pageGfx = XGraphics.FromPdfPage(page);
pageGfx.DrawImage(form, 0, 0);
pageGfx.Dispose();
document.Save("test.pdf");
Using .NET 5, the previous code delivers a test.pdf, showing "Hello World!" (as expected) Using .NET 6, the previous code delivers a much smaller test.pdf (regarding filesize), showing nothing.
UPDATE: ... wild guess: this could have sth. to do with it: https://github.com/ststeiger/PdfSharpCore/blob/90eb7dbb98fc5eeb18e2ee20c69018916a4865cc/PdfSharpCore/Drawing/XGraphics.cs#L210
UPDATE 2: after fixing and testing it, that was the issue. PR https://github.com/ststeiger/PdfSharpCore/pull/295 should solve it