本文來告訴大家,在 OpenXML 的 BodyProperties 的 NormalAutoFit 的 FontScale 屬性縮放文本框的文本字號的方法
通過 ECMA 376 檔案可以了解到 BodyProperties 屬性是作用在文本框,放在 BodyProperties 的 NormalAutoFit 的 FontScale 屬性,能修改文本框的文本字號縮放
更多關于 BodyProperties 屬性,請看 dotnet OpenXML 文本 BodyProperties 的屬性作用
通過 ECMA 376 的 21.1.2.1.3 章可以了解到使用 NormalAutoFit 的 FontScale 屬性是按照百分比縮放文本框
開始之前,先使用 C# dotnet 使用 OpenXml 決議 PPT 檔案 的方法讀取測驗的檔案,此測驗檔案將可以在下文拿到
var file = new FileInfo("Test.pptx");
using var presentationDocument = PresentationDocument.Open(file.FullName, false);
獲取首頁的第一個形狀元素,此形狀元素包含一個文本,此文本內容里設定了 NormalAutoFit 的 FontScale 屬性
var slide = presentationDocument.PresentationPart!.SlideParts.First().Slide;
var shape = slide.CommonSlideData!.ShapeTree!.GetFirstChild<Shape>()!;
此形狀的 OpenXML 內容如下
<p:sp>
<p:nvSpPr>
<p:cNvPr id="4" name="文本框 3" />
<p:cNvSpPr txBox="1" />
<p:nvPr />
</p:nvSpPr>
<p:spPr>
<a:xfrm>
<a:off x="4168346" y="914401" />
<a:ext cx="6096000" cy="3170090" />
</a:xfrm>
<a:prstGeom prst="rect">
<a:avLst />
</a:prstGeom>
<a:noFill />
</p:spPr>
<p:txBody>
<a:bodyPr wrap="square" rtlCol="0">
<a:normAutofit fontScale="60000"/>
</a:bodyPr>
<a:lstStyle />
<a:p>
<a:r>
<a:rPr lang="zh-CN" altLang="en-US" sz="10000">
</a:rPr>
<a:t>一行文本</a:t>
</a:r>
<a:endParaRPr lang="en-US" sz="10000" dirty="0" />
</a:p>
</p:txBody>
</p:sp>
在 a:bodyPr 的 a:normAutofit 的文本縮放設定為 60000 的值
從 ECMA 376 檔案可以了解 FontScale 屬性的單位是千倍百分比,可使用 Office Open XML 的測量單位 提供的 dotnetCampus.OpenXMLUnitConverter 庫的 Percentage 型別作為單位
獲取文本框屬性,可以使用以下代碼
var shapeProperties = shape.ShapeProperties!;
var textBody = shape.TextBody!;
Debug.Assert(textBody != null);
var textBodyProperties = textBody.BodyProperties!;
Debug.Assert(textBodyProperties != null);
文本框屬性里包含 NormalAutoFit 屬性,可采用以下代碼獲取
var normalAutoFit = textBodyProperties.GetFirstChild<NormalAutoFit>()!;
Debug.Assert(normalAutoFit != null);
如果檔案里的 OpenXML 內容沒有存在 NormalAutoFit 或者 FontScale 屬性,那就是默認不做文本字號縮放,可以相當于默認的倍數是 1 的值,因為任何值乘以 1 都等于原值,相當于不做縮放
Percentage fontScale = normalAutoFit.FontScale is null
? Percentage.FromDouble(1)
: new Percentage(normalAutoFit.FontScale);
此 Percentage 型別是 dotnetCampus.OpenXMLUnitConverter 庫提供的
獲取文本內容,在 WPF 顯示代碼如下
foreach (var paragraph in textBody.Elements<DocumentFormat.OpenXml.Drawing.Paragraph>())
{
// 一個文本里面有很多段落
// 段落里面,文本有不同的樣式,如一段可以有不同加粗的文本
// 相同的樣式的文本放在一個 TextRun 里面,不同的樣式的文本放在不同的 TextRun 里面
// 這個文本段落是沒有屬性的,為了方便樣式,就不寫代碼
//if (paragraph.ParagraphProperties != null)
foreach (var run in paragraph.Elements<DocumentFormat.OpenXml.Drawing.Run>())
{
var runProperties = run.RunProperties!;
var fontSize = new PoundHundredfold(runProperties.FontSize!.Value).ToPound();
// 默認字體前景色是黑色
var text = run.Text!.Text;
var textBlock = new TextBlock()
{
Text = text,
FontSize = fontSize.ToPixel().Value * fontScale.DoubleValue,
FontFamily = new FontFamily("宋體"),
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
};
Root.Children.Add(textBlock);
}
}
運行以上代碼效果如下,可以看到文本大小和 Office 的 PPT 相同

本文所有代碼和測驗檔案放在github 和 gitee 歡迎訪問
可以通過如下方式獲取本文的源代碼,先創建一個空檔案夾,接著使用命令列 cd 命令進入此空檔案夾,在命令列里面輸入以下代碼,即可獲取到本文的代碼
git init
git remote add origin https://gitee.com/lindexi/lindexi_gd.git
git pull origin 24230fc0bb8202c567ccf9ffffb49eebc08be120
以上使用的是 gitee 的源,如果 gitee 不能訪問,請替換為 github 的源
git remote remove origin
git remote add origin https://github.com/lindexi/lindexi_gd.git
獲取代碼之后,進入 Pptx 檔案夾
更多請看 Office 使用 OpenXML SDK 決議檔案博客目錄
博客園博客只做備份,博客發布就不再更新,如果想看最新博客,請到 https://blog.lindexi.com/

本作品采用知識共享署名-非商業性使用-相同方式共享 4.0 國際許可協議進行許可,歡迎轉載、使用、重新發布,但務必保留文章署名[林德熙](http://blog.csdn.net/lindexi_gd)(包含鏈接:http://blog.csdn.net/lindexi_gd ),不得用于商業目的,基于本文修改后的作品務必以相同的許可發布,如有任何疑問,請與我[聯系](mailto:[email protected]),
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/417008.html
標籤:.NET技术
