[英]Using C# & Powerpoint OpenXML, is it possible to change the font size and color of text
[英]My font size set using openXML for a Powerpoint paragraph is not being used
我正在嘗試為 PowerPoint 幻燈片中的段落設置字體大小。 在我的代碼中,當我打印出字體大小時,它顯示為 9,但是當我打開 PowerPoint 時,它被設置為 12。
筆記:
我在我的電腦上沒有管理員權限,所以我無法安裝工具來幫助我在一些帖子中讀到的東西進行逆向工程。
_bulletParagraphOuterXML
是我在尋找如何通過代碼設置項目符號數小時后從預制段落中抓取的字符串。 仍然不知道如何和學習如何是未來的另一個職位。 即使它在這個字符串中說 Arial,項目符號文本也是 Times New Roman。
在代碼中,它打印的字體大小為 9,但在 PowerPoint 中,它顯示的字體大小為 12。
Bellow 是我從用於修改和更新 PowerPoint 文檔的類中提取的代碼。
using System;
using System.Collections.Generic;
using System.Linq;
using DocumentFormat.OpenXml.Packaging;
using D = DocumentFormat.OpenXml.Drawing;
using P = DocumentFormat.OpenXml.Presentation;
static string _bulletParagraphOuterXML = "<a:pPr marL=\"285750\" indent=\"-285750\" xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\"><a:buFont typeface=\"Arial\" panose=\"020B0604020202020204\" pitchFamily=\"34\" charset=\"0\" /><a:buChar char=\"•\" /></a:pPr>";
public static void ReplaceShapeBulletsText(this SlidePart slidePart, string ShapeName, List<string> newTexts)
{
P.Shape shape = slidePart.GetShapeByName(ShapeName);
D.Paragraph paragraph = shape.TextBody.Elements<D.Paragraph>().FirstOrDefault();
shape.TextBody.RemoveAllChildren<D.Paragraph>();
foreach (string text in newTexts)
{
shape.AddBulletParagraph(text);
}
}
public static void AddBulletParagraph(this P.Shape shape, string NewText)
{
D.Paragraph p = new D.Paragraph();
P.TextBody docBody = shape.TextBody;
p.ParagraphProperties = new D.ParagraphProperties(_bulletParagraphOuterXML);
D.Run run = new D.Run(new D.Text(NewText));
D.RunProperties runProp = new D.RunProperties() { Language = "en-US", FontSize = 9, Dirty = false };
run.AppendChild(runProp);
Console.WriteLine("--------------------------------------------------------------");
Console.WriteLine(runProp.FontSize.ToString());
Console.WriteLine("--------------------------------------------------------------");
p.Append(run);
docBody.Append(p);
}
在 openXML 中追加事項的順序。 在能夠讓字體大小不正確的文本和字體大小正確的文本以相同的形狀顯示后,我只注意到一個區別。
a:rPr
和a:t
出現的順序。 a:rPr
需要在a:t
之前
知道這一事實也應該可以幫助我解決我一直遇到的許多其他問題。
正確的字體大小
<a:p>
<a:pPr marL="171450" indent="-171450">
<a:buFont typeface="Arial" charset="0" pitchFamily="34" panose="020B0604020202020204"/>
<a:buChar char="•"/>
</a:pPr>
<a:r>
<a:rPr lang="en-US" sz="900"/>
<a:t>Random Text</a:t>
</a:r>
<a:endParaRPr lang="en-US" dirty="0" sz="900"/>
</a:p>
字體大小不正確
<a:p>
<a:pPr xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" marL="285750" indent="-285750">
<a:buFont typeface="Arial" charset="0" pitchFamily="34" panose="020B0604020202020204"/>
<a:buChar char="•"/>
</a:pPr>
<a:r>
<a:t>Other Random Text</a:t>
<a:rPr lang="en-US" dirty="0" sz="900"/>
</a:r>
<a:endParaRPr lang="en-US" dirty="0" sz="900"/>
</a:p>
將它與我的代碼聯系起來。
此代碼塊:
p.ParagraphProperties = new D.ParagraphProperties(_bulletParagraphOuterXML);
D.Run run = new D.Run(new D.Text(NewText));
D.RunProperties runProp = new D.RunProperties() { Language = "en-US", FontSize = 9, Dirty = false };
run.AppendChild(runProp);
已更新為
p.ParagraphProperties = new D.ParagraphProperties(_bulletParagraphOuterXML);
D.Run run = new D.Run();
D.RunProperties runProp = new D.RunProperties() { Language = "en-US", FontSize = 900, Dirty = false };
run.AppendChild(runProp);
D.Text newText = new D.Text(NewText);
run.AppendChild(newText);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.