我正在使用openXML搜索特定文本,并使用openxml在ppt中突出显示它(更改其颜色)。下面是代码,但是它没有突出显示ppt中的文本。
突出显示的文本:
if (text.Text == "John")
{
A.Run run1 = new A.Run();
A.RunProperties runProperties1 = new A.RunProperties() { Language = "en-US", FontSize = 6000, Dirty = false };//Set Font-Size to 60px.
A.SolidFill solidFill1 = new A.SolidFill();
A.RgbColorModelHex rgbColorModelHex1 = new A.RgbColorModelHex() { Val = "00B050" };//Set Font-Color to Green (Hex "00B050").
solidFill1.Append(rgbColorModelHex1);
runProperties1.Append(solidFill1);
A.Text text1 = new A.Text();
text1.Text = text.Text;
run1.Append(runProperties1);
run1.Append(text1);
slidePart.Slide.Save();
}
完整代码-
using Text = DocumentFormat.OpenXml.Presentation.Text;
using A = DocumentFormat.OpenXml.Drawing;
public static void GetSlideIdAndText(string docName)
{
using (PresentationDocument ppt = PresentationDocument.Open(docName, true))
{
// Get the relationship ID of the first slide.
PresentationPart part = ppt.PresentationPart;
OpenXmlElementList slideIds = part.Presentation.SlideIdList.ChildElements;
SlidePart[] slidePartList = part.SlideParts.ToArray();
//string relId = (slideIds[index] as SlideId).RelationshipId;
StringBuilder paragraphText = new StringBuilder();
// Get the slide part from the relationship ID.
//SlidePart slide = (SlidePart)part.GetPartById(relId);
LinkedList<string> texts = new LinkedList<string>();
foreach (SlidePart slidePart in slidePartList)
{
// If the slide exists...
if (slidePart.Slide != null)
{
// Iterate through all the paragraphs in the slide.
foreach (var paragraph in slidePart.Slide.Descendants<DocumentFormat.OpenXml.Drawing.Paragraph>())
{
// Create a new string builder.
// StringBuilder paragraphText = new StringBuilder();
// Iterate through the lines of the paragraph.
foreach (var text in paragraph.Descendants<DocumentFormat.OpenXml.Drawing.Text>())
{
// Append each line to the previous lines.
paragraphText.Append(text.Text);
if (text.Text == "John")
{
A.Run run1 = new A.Run();
A.RunProperties runProperties1 = new A.RunProperties() { Language = "en-US", FontSize = 6000, Dirty = false };//Set Font-Size to 60px.
A.SolidFill solidFill1 = new A.SolidFill();
A.RgbColorModelHex rgbColorModelHex1 = new A.RgbColorModelHex() { Val = "00B050" };//Set Font-Color to Green (Hex "00B050").
solidFill1.Append(rgbColorModelHex1);
runProperties1.Append(solidFill1);
A.Text text1 = new A.Text();
text1.Text = text.Text;
run1.Append(runProperties1);
run1.Append(text1);
slidePart.Slide.Save();
}
}
if (paragraphText.Length > 0)
{
// Add each paragraph to the linked list.
texts.AddLast(paragraphText.ToString());
}
}
}
}
}
}