解析OpenXml?Pptx的邊框虛線轉(zhuǎn)為WPF的邊框虛線問題
安裝Openxml sdk
首先,我們先安裝nuget的需要的有關(guān)的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種直線,分別設(shè)置7種能夠設(shè)置的虛線類型,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; }
最終的效果:
我們可以看到幾乎是接近的效果了,當(dāng)然你也可以根據(jù)我的代碼去微調(diào)更精確的值,只需要稍微改下GetDashArrayByPresetLineDashValues
方法內(nèi)相對應(yīng)的值即可
后話
實際上,openxml文檔是給出了PresetDash的值的,大致如下:
但是其值跟WPF的設(shè)置Dash的DoubleCollection
不對應(yīng),因此以上的映射值都是我自己微調(diào)的
源碼
BlogCodeSample/PptDashConverToWpfSample at main · ZhengDaoWang/BlogCodeSample
到此這篇關(guān)于OpenXml?Pptx的邊框虛線轉(zhuǎn)為WPF的邊框虛線的文章就介紹到這了,更多相關(guān)Pptx邊框虛線轉(zhuǎn)為WPF的邊框虛線內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
c# 線程定時器 System.Threading.Timer的使用
本文主要介紹了c# 線程定時器 System.Threading.Timer的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02C#使用protobuf-net進(jìn)行序列化的詳細(xì)操作
本文帶領(lǐng)大家學(xué)習(xí)C#中protobuf-net工具的另一種使用體驗,這個工具的使用體驗屬于Code-First模式,先定義類型,并使用注解進(jìn)行標(biāo)記,不需要先編寫.proto文件,感興趣的朋友跟隨小編一起看看吧2021-11-11C# WinForm狀態(tài)欄實時顯示當(dāng)前時間(窗體狀態(tài)欄StatusStrip示例)
這篇文章主要介紹了C# WinForm狀態(tài)欄實時顯示當(dāng)前時間(窗體狀態(tài)欄StatusStrip示例),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01