PdfExporter.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="PdfExporter.cs" company="OxyPlot">
  3. // Copyright (c) 2020 OxyPlot contributors
  4. // </copyright>
  5. // --------------------------------------------------------------------------------------------------------------------
  6. namespace OxyPlot.SkiaSharp
  7. {
  8. using global::SkiaSharp;
  9. using System.IO;
  10. /// <summary>
  11. /// Provides functionality to export plots to pdf using the SkiaSharp renderer.
  12. /// </summary>
  13. public class PdfExporter : IExporter
  14. {
  15. /// <summary>
  16. /// Gets or sets the export height (in points, where 1 point equals 1/72 inch).
  17. /// </summary>
  18. public float Height { get; set; }
  19. /// <summary>
  20. /// Gets or sets the export width (in points, where 1 point equals 1/72 inch).
  21. /// </summary>
  22. public float Width { get; set; }
  23. /// <summary>
  24. /// Gets or sets a value indicating whether text shaping should be used when rendering text.
  25. /// </summary>
  26. public bool UseTextShaping { get; set; } = true;
  27. /// <summary>
  28. /// Exports the specified model to a file.
  29. /// </summary>
  30. /// <param name="model">The model.</param>
  31. /// <param name="path">The path.</param>
  32. /// <param name="width">The width (points).</param>
  33. /// <param name="height">The height (points).</param>
  34. public static void Export(IPlotModel model, string path, float width, float height)
  35. {
  36. using var stream = File.OpenWrite(path);
  37. Export(model, stream, width, height);
  38. }
  39. /// <summary>
  40. /// Exports the specified model to a stream.
  41. /// </summary>
  42. /// <param name="model">The model.</param>
  43. /// <param name="stream">The output stream.</param>
  44. /// <param name="width">The width (points).</param>
  45. /// <param name="height">The height (points).</param>
  46. public static void Export(IPlotModel model, Stream stream, float width, float height)
  47. {
  48. var exporter = new PdfExporter { Width = width, Height = height };
  49. exporter.Export(model, stream);
  50. }
  51. /// <inheritdoc/>
  52. public void Export(IPlotModel model, Stream stream)
  53. {
  54. using var document = SKDocument.CreatePdf(stream);
  55. using var pdfCanvas = document.BeginPage(this.Width, this.Height);
  56. using var context = new SkiaRenderContext { RenderTarget = RenderTarget.VectorGraphic, SkCanvas = pdfCanvas, UseTextShaping = this.UseTextShaping };
  57. const float dpiScale = 72f / 96;
  58. context.DpiScale = dpiScale;
  59. model.Update(true);
  60. pdfCanvas.Clear(model.Background.ToSKColor());
  61. model.Render(context, new OxyRect(0, 0, this.Width / dpiScale, this.Height / dpiScale));
  62. }
  63. }
  64. }