解析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
到此这篇关于openxml pptx的边框虚线转为wpf的边框虚线的文章就介绍到这了,更多相关pptx边框虚线转为wpf的边框虚线内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: 蓝桥杯 2019 第4题:迷宫
下一篇: SHOPEX快递物流单号查询插件