網(wǎng)站首頁 編程語言 正文
安裝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)的值即可
后話
實(shí)際上,openxml文檔是給出了PresetDash的值的,大致如下:
但是其值跟WPF的設(shè)置Dash的DoubleCollection
不對應(yīng),因此以上的映射值都是我自己微調(diào)的
源碼
BlogCodeSample/PptDashConverToWpfSample at main · ZhengDaoWang/BlogCodeSample
原文鏈接:https://www.cnblogs.com/ryzen/p/15740488.html
相關(guān)推薦
- 2022-03-31 解決Android加殼過程中mprotect調(diào)用失敗的原因分析_Android
- 2022-12-24 C++中析構(gòu)函數(shù)為何是虛函數(shù)_C 語言
- 2022-06-07 詳解ASP.NET?Core高性能服務(wù)器HTTP.SYS_實(shí)用技巧
- 2023-10-09 時間戳轉(zhuǎn)日期格式-自動補(bǔ)零,日期格式轉(zhuǎn)時間戳
- 2022-07-07 Python數(shù)據(jù)分析之?Matplotlib?散點(diǎn)圖繪制_python
- 2022-08-13 beginInvoke加回調(diào)函數(shù)lamad
- 2022-05-13 CLion 中文輸出亂碼
- 2022-03-24 使用sublime?Text3過程中的各種問題的解決_相關(guān)技巧
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支