PdfSharpCore
PdfSharpCore copied to clipboard
Render Document to bitmap (question)
Hi! I apologize if I missed something obvious, but I have a question: is it possible (or should be possible) to render MigraDocCore.DocumentObjectModel.Document to bitmap? I tried something like that:
var img = ... // make XImage
var gfx = XGraphics.FromImage(img);
var renderer = new DocumentRenderer(document);
renderer.PrepareDocument();
renderer.RenderPage(gfx, 1, PageRenderOptions.All);
but it throws NullReferenceException (and when I looked at code it seems to be not implemented). I also considered to make custom implementation of PdfSharpCore.Drawing.IXGraphicsRenderer interface, but I can't find a way to make XGraphics from IXGraphicsRenderer.
Would it be ok to make IXGraphicsRenderer public and add a method to create XGraphics from it like that: https://github.com/mishun/PdfSharpCore/commit/138413549516f92cd530476d1430a1cc7a5b8604
I tried to make very bare-bone example of rendering Document into Avalonia control and it seems to be working for now.
@mishun: When you do that, you break backwards-compatilibity with everyone implementing the interface.
I'm sorry, I don't understand: IXGraphicsRenderer was declared as internal --- no one outside PdfSharpCore assembly can implement it. It can break something only if client code had some other interface with the same name in it's scope.
Well, in that case you can.
I looked into custom IXGraphicsRenderer implementations a bit more, and it appears exposing some additional internals of XLinearGradientBrush, XRadialGradientBrush and XGraphicsPath (CoreGraphicsPath in particular, or it's content could be moved into XGraphicsPath directly) as public is necessary for it to work properly.
Also, for performance sake, any drawing backend should probably cache objects it wraps XPen-s, XBrush-es, XImage-s, XFont-s (... so on) into and, as a result, needs to have some way of tracking when X-Whatever changes. There is _dirty boolean flag now, but it isn't sufficient in that case. My idea is for every object to have unique int64 version number that changes on every mutating operation (something like existing X-Whatever classes implementing IXVersioned interface with long Version property).
If these changes are ok, I'll PR them when they're done. After that SixLabors.ImageSharp.Drawing could be wrapped into IXGraphicsRenderer and rendering into image should be more or less working.
Fine with me. Just make sure caching works correctly.