How take a SkiaSharp SKData and write out a pdf?

Will.ItraxWill.Itrax CAMember ✭✭✭

Our app uses SkiaSharp and Xamarin Forms to create sketches. We can take a completed sketch and write it out as a jpg.
SKData skData = surface.Snapshot().Encode(); using (Stream stream = File.Open(fileout, FileMode.OpenOrCreate, System.IO.FileAccess.Write, FileShare.ReadWrite)) { skData.SaveTo(stream); }

How do I write it out as a pdf?

One step is to create an SKDocument like this:
SKDocument skDocument = SKDocument.CreatePdf(SKWStream, dpi);
How do I turn the SKData into an SKWStream?

Thanks for having a look at this.

Tagged:

Best Answer

  • Will.ItraxWill.Itrax CA ✭✭✭
    Accepted Answer

    I found an excellent sample which creates a pdf in skiasharp at:
    https://github.com/mono/SkiaSharp/blob/master/samples/Gallery/Shared/Samples/CreatePdfSample.cs#L47

    What was key for me:

            using (var stream = new SKFileWStream(path))
            using (var document = SKDocument.CreatePdf(stream, metadata))
            using (var paint = new SKPaint())
            {
                paint.TextSize = 64.0f;
                paint.IsAntialias = true;
                paint.Color = (SKColor)0xFF9CAFB7;
                paint.IsStroke = true;
                paint.StrokeWidth = 3;
                paint.TextAlign = SKTextAlign.Center;
    
                var width = 840;
                var height = 1188;
    
                // draw page 1
                using (var pdfCanvas = document.BeginPage(width, height))
                { 
                    // draw everything on the pdfCanvas
                    ...
                }
                // end the doc
                document.Close();
            }
    

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    Since you can get the stream, why not try another API:
    public static SkiaSharp.SKDocument CreatePdf (System.IO.Stream stream);?

  • Will.ItraxWill.Itrax CAMember ✭✭✭
    Accepted Answer

    I found an excellent sample which creates a pdf in skiasharp at:
    https://github.com/mono/SkiaSharp/blob/master/samples/Gallery/Shared/Samples/CreatePdfSample.cs#L47

    What was key for me:

            using (var stream = new SKFileWStream(path))
            using (var document = SKDocument.CreatePdf(stream, metadata))
            using (var paint = new SKPaint())
            {
                paint.TextSize = 64.0f;
                paint.IsAntialias = true;
                paint.Color = (SKColor)0xFF9CAFB7;
                paint.IsStroke = true;
                paint.StrokeWidth = 3;
                paint.TextAlign = SKTextAlign.Center;
    
                var width = 840;
                var height = 1188;
    
                // draw page 1
                using (var pdfCanvas = document.BeginPage(width, height))
                { 
                    // draw everything on the pdfCanvas
                    ...
                }
                // end the doc
                document.Close();
            }
    
Sign In or Register to comment.