MShah
MShah

Reputation: 1267

How to draw PDF on canvas using skia sharp?

I have referred official document of skia: https://learn.microsoft.com/en-us/dotnet/api/skiasharp.skcanvas?view=skiasharp-1.68.1#constructing-a-pdf-document

// create the document
var stream = SKFileWStream.OpenStream("document.pdf");
var document = SKDocument.CreatePdf(stream);

// get the canvas from the page
var canvas = document.BeginPage(256, 256);

// draw on the canvas ...

// end the page and document
document.EndPage();
document.Close();

As per the above code I'm getting canvas, but how can I draw that on my canvas view?

 <skia:SKCanvasView x:Name="canvasViews"/>

Also do I need to use scroll view to display those multiple pages on canvas?

Any help on this appreciated. Thanks.

Upvotes: 2

Views: 4798

Answers (2)

bld
bld

Reputation: 121

This is a relatively old question, but this answer could be useful anyway.

I believe what you are looking for is a library I've been working on recently https://github.com/BobLd/PdfPig.Rendering.Skia

It uses SkiaSharp to render pdf documents. At the time of writing the library is still early stage.

Using the following you can get the SKPicture of the page, that you can then draw on a canvas

using UglyToad.PdfPig.Graphics.Colors;
using UglyToad.PdfPig;
using UglyToad.PdfPig.Rendering.Skia;
using SkiaSharp;

[...]

using (var document = PdfDocument.Open(_path))
{
    document.AddSkiaPageFactory(); // Same as document.AddPageFactory<SKPicture, SkiaPageFactory>()

    for (int p = 1; p <= document.NumberOfPages; p++)
    {
        var picture = document.GetPage<SKPicture>(p);
        // Use the SKPicture
    }
}

Upvotes: 0

Matthew
Matthew

Reputation: 5222

I think you got the reverse issue as to what SkiaSharp supports. SkiaSharp CREATES PDF files, not renders them.

I do not know of any PDF renderers offhand that I can recommend, but I would start by searching SO or doing a bit of Googling. I see this post: How to render pdfs using C#

Upvotes: 0

Related Questions