using CPF.Drawing;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Linq;
namespace CPF.Skia
{
public class SkiaPdf
{
///
/// 从指定控件生成PDF
///
/// 要生成的控件
/// 保存路径
public static void CreatePdf(UIElement control, string Path)
{
using (var document = SkiaSharp.SKDocument.CreatePdf(Path))
{
using (var canvas = document.BeginPage(control.ActualSize.Width, control.ActualSize.Height))
{
var context = new SkiaDrawingContext(canvas, (SkiaDrawingFactory)DrawingFactory.Default);
if (control.Root.LayoutManager.VisibleUIElements.Element == control)
{
control.Root.RenderUIElement(context, control.Root.LayoutManager.VisibleUIElements, control.RenderBounds);
}
else
{
List vEles = new List();
Action gwl = null;
gwl = (Children) =>
{
if (Children.Element == control)
{
vEles.Add(Children);
}
Children.Children.ForEach(gwl);
};
gwl(control.Root.LayoutManager.VisibleUIElements);
//找到就全部使用pdf context渲染一边
foreach (var item in vEles)
{
control.Root.RenderUIElement(context, item, control.RenderBounds);
}
}
document.EndPage();
}
document.Close();
}
}
}
}