【OpenXml】Pptx的边框虚线转为WPF的边框虚线


安装Openxml sdk

首先,我们先安装nuget的需要的有关的Openxml sdk,我们开源了解析pptx的Openxml拍平层,下面两种方式都可以安装:

  • nuget包管理器控制台:
Install-Package dotnetCampus.DocumentFormat.OpenXml.Flatten -Version 2.0.0
  • csproj引用:
<PackageReference Include="dotnetCampus.DocumentFormat.OpenXml.Flatten" Version="2.0.0" />

解析Pptx

我这里用PPTX的7种直线,分别设置7种能够设置的虚线类型,PPTX的显示效果是这样的:

然后解析代码如下,解析主要逻辑部分:

        private void PptxToGeometry(string filePath)
        {
            if (!File.Exists(filePath) || !filePath.EndsWith(".pptx", StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            var lines = new List<Line>();
            using var presentationDocument = PresentationDocument.Open(filePath, false);
            var presentationPart = presentationDocument.PresentationPart;
            var presentation = presentationPart?.Presentation;
            var slideIdList = presentation?.SlideIdList;
            if (slideIdList == null)
            {
                return;
            }
            foreach (var slideId in slideIdList.ChildElements.OfType<SlideId>())
            {
                var slidePart = (SlidePart)presentationPart.GetPartById(slideId.RelationshipId);
                var slide = slidePart.Slide;
                foreach (var shapeProperties in slide.Descendants<ShapeProperties>())
                {
                    var presetGeometry = shapeProperties.GetFirstChild<PresetGeometry>();
                    if (presetGeometry != null && presetGeometry.Preset.HasValue)
                    {
                        if (presetGeometry.Preset == ShapeTypeValues.StraightConnector1)
                        {
                            var transform2D = shapeProperties.GetFirstChild<Transform2D>();
                            var extents = transform2D?.GetFirstChild<Extents>();
                            if (extents != null)
                            {
                                var width = new Emu(extents.Cx!.Value).ToPixel().Value;
                                var height = new Emu(extents.Cy!.Value).ToPixel().Value;


                                var presetDash = shapeProperties.GetFirstChild<Outline>()?.GetFirstChild<PresetDash>()?.Val;
                                var dashArray = GetDashArrayByPresetLineDashValues(presetDash);
                                var line = ConverterToGeometry( width, height, dashArray); 
                                lines.Add(line);
                            }
                        }
                    }
                }
            }

            this.ListBox.ItemsSource = lines;
        }

PPTX映射成WPF虚线的方法:


        private DoubleCollection GetDashArrayByPresetLineDashValues(PresetLineDashValues presetLineDashValues)
        {
            DoubleCollection dashStyle = presetLineDashValues switch
            {
                PresetLineDashValues.Solid => new(),
                PresetLineDashValues.Dot => new() { 0, 2 },
                PresetLineDashValues.Dash => new() { 3, 3 },
                PresetLineDashValues.LargeDash => new() { 8, 3 },
                PresetLineDashValues.DashDot => new() { 3, 3, 1, 3 },
                PresetLineDashValues.LargeDashDot => new() { 7.5, 3.5, 1, 3.5 },
                PresetLineDashValues.LargeDashDotDot => new() { 8, 3, 1, 3, 1, 3 },
                PresetLineDashValues.SystemDash => new() { 3, 1 },
                PresetLineDashValues.SystemDot => new() { 1, 1 },
                PresetLineDashValues.SystemDashDot => new() { 2, 2, 0, 2 },
                PresetLineDashValues.SystemDashDotDot => new() { 2, 2, 0, 2 },
                _ => new DoubleCollection()
            };
            return dashStyle;
        }

最终绘制线条的方法:

        private Line ConverterToGeometry(double width, double height, DoubleCollection dashDoubleCollection)
        {
            var line = new Line
            {
                X1 = 0,
                Y1 = 0,
                X2 = width,
                Y2 = height,
                StrokeDashArray = dashDoubleCollection,
                Stroke = Stroke,
                StrokeThickness = StrokeThickness
            };
            return line;
        }

最终的效果:

我们可以看到几乎是接近的效果了,当然你也可以根据我的代码去微调更精确的值,只需要稍微改下GetDashArrayByPresetLineDashValues方法内相对应的值即可

后话

实际上,openxml文档是给出了PresetDash的值的,大致如下:

但是其值跟WPF的设置Dash的DoubleCollection不对应,因此以上的映射值都是我自己微调的

源码

BlogCodeSample/PptDashConverToWpfSample at main · ZhengDaoWang/BlogCodeSample

文章来源:https://www.cnblogs.com/ryzen/p/15740488.html

版权声明:本文为YES开发框架网发布内容,转载请附上原文出处连接
管理员
上一篇:.net 简单实现在H5中将word、jpg、png转成PDF给PDF添加水印并且控制样式和可视化编辑
下一篇:C# 编写一个小而快的 Windows 动态桌面(一)之桌面交互功能
评论列表

发表评论

评论内容
昵称:
关联文章

OpenXmlPptx边框虚线转为WPF边框虚线
使用 WPF 做个 PowerPoint 系列 基于 OpenXML 解析实现 PPT 文本描边效果
dotnet OpenXML 聊聊文本段落对齐方式
[WPF] 实现 WPF Inner Shadow
css两种颜色边框设置,边框设置多种颜色
使用 WPF 做个 PowerPoint 系列 文本 BodyProperties FontScale 与文本字号缩放
[WPF] 实现一个很久以前流行按钮样式
C# 将PDF转为线性化PDF
[WPF] 抄一个 CSS3 实现按钮
WPF_15_格式化绑定数据
[WPF] 用 Effect 实现线条光影效果
[WPF] 用 OpacityMask 模仿 UWP Text Shimmer 动画
C# 值得永久收藏WPF项目实战(经典)
C# winform无边框窗体移动四种方法
WPF对象级资源
WPF程序级资源
dotnetCampus.UITest.WPF 一个支持中文用例界面单元测试框架
[WPF] 实现两个任天堂 Switch 加载动画
WPF 双向绑定到非公开 set 方法属性在 NET 45 和 NET Core 行为不同
WPF 引用第三方库控件在设计器加上设计时数据和属性

联系我们
联系电话:15090125178(微信同号)
电子邮箱:garson_zhang@163.com
站长微信二维码
微信二维码