C# 使用Openxml从pptx获取字体正确的文本
我使用以下方法获取文本:C# 使用Openxml从pptx获取字体正确的文本,c#,openxml,C#,Openxml,我使用以下方法获取文本: public static string[] GetAllTextInSlide(SlidePart slidePart) { // Verify that the slide part exists. if (slidePart == null) { throw new ArgumentNullException("slidePart"); } // Cr
public static string[] GetAllTextInSlide(SlidePart slidePart)
{
// Verify that the slide part exists.
if (slidePart == null)
{
throw new ArgumentNullException("slidePart");
}
// Create a new linked list of strings.
LinkedList<string> texts = new LinkedList<string>();
// 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 (paragraphText.Length > 0)
{
// Add each paragraph to the linked list.
texts.AddLast(paragraphText.ToString());
}
}
}
if (texts.Count > 0)
{
// Return an array of strings.
return texts.ToArray();
}
else
{
return null;
}
}
公共静态字符串[]GetAllTextInSlide(SlidePart SlidePart)
{
//验证滑动部件是否存在。
如果(slidePart==null)
{
抛出新ArgumentNullException(“slidePart”);
}
//创建一个新的字符串链接列表。
LinkedList text=新建LinkedList();
//如果幻灯片存在。。。
如果(slidePart.Slide!=null)
{
//反复浏览幻灯片中的所有段落。
foreach(slidePart.Slide.subjections()中的变量段落)
{
//创建一个新的字符串生成器。
StringBuilder段落文本=新建StringBuilder();
//重复这一段的行。
foreach(段落.subjects()中的var文本)
{
//将每一行附加到前面的行。
段落text.Append(text.text);
}
如果(段落text.Length>0)
{
//将每个段落添加到链接列表中。
text.AddLast(paragraphText.ToString());
}
}
}
如果(text.Count>0)
{
//返回字符串数组。
返回文本。ToArray();
}
其他的
{
返回null;
}
}
及
公共静态字符串[]GetAllTextInSlide(PresentationDocument PresentationDocument,int slideIndex)
{
//验证演示文稿文档是否存在。
如果(presentationDocument==null)
{
抛出新ArgumentNullException(“presentationDocument”);
}
//确认幻灯片索引未超出范围。
如果(滑动索引<0)
{
抛出新ArgumentOutOfRangeException(“slideIndex”);
}
//获取演示文稿文档的演示文稿部分。
PresentationPart PresentationPart=presentationDocument.PresentationPart;
//验证演示文稿部分和演示文稿是否存在。
if(presentationPart!=null&&presentationPart.Presentation!=null)
{
//从演示文稿部分获取演示文稿对象。
演示文稿=演示文稿部分。演示文稿;
//验证幻灯片ID列表是否存在。
如果(presentation.SlideIdList!=null)
{
//从幻灯片ID列表中获取幻灯片ID的集合。
var slideIds=presentation.SlideIdList.ChildElements;
//如果幻灯片ID在范围内。。。
if(slideIndex
但他们只是告诉我这些话。如何获得字体和颜色正确的文本?试试这个
using System;
using System.Linq;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Drawing;
namespace OpenXmlGetPowerpointTextInfo
{
class Program
{
static void Main(string[] args)
{
using (PresentationDocument myPres = PresentationDocument.Open(@"test.pptx", true))
{
PresentationPart presPart = myPres.PresentationPart;
//SlidePart slide = presPart.GetPartsOfType<SlidePart>().FirstOrDefault();
SlidePart[] slidePartList = presPart.SlideParts.ToArray();
foreach (SlidePart part in slidePartList)
{
RunProperties[] runProList = part.Slide.Descendants<RunProperties>().ToArray();
foreach (RunProperties r in runProList)
{
Console.WriteLine(r.FontSize.Value);
}
}
}
}
}
}
使用系统;
使用System.Linq;
使用DocumentFormat.OpenXml.Packaging;
使用DocumentFormat.OpenXml.Drawing;
命名空间OpenXmlGetPowerpointTextInfo
{
班级计划
{
静态void Main(字符串[]参数)
{
使用(PresentationDocument myPres=PresentationDocument.Open(@“test.pptx”,true))
{
PresentationPart presPart=myPres.PresentationPart;
//SlidePart slide=presPart.GetPartsOfType().FirstOrDefault();
SlidePart[]slidePartList=prepart.SlideParts.ToArray();
foreach(slidePartList中的SlidePart部分)
{
RunProperties[]runProList=part.Slide.subjects().ToArray();
foreach(runProList中的RunProperties r)
{
Console.WriteLine(r.FontSize.Value);
}
}
}
}
}
}
using System;
using System.Linq;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Drawing;
namespace OpenXmlGetPowerpointTextInfo
{
class Program
{
static void Main(string[] args)
{
using (PresentationDocument myPres = PresentationDocument.Open(@"test.pptx", true))
{
PresentationPart presPart = myPres.PresentationPart;
//SlidePart slide = presPart.GetPartsOfType<SlidePart>().FirstOrDefault();
SlidePart[] slidePartList = presPart.SlideParts.ToArray();
foreach (SlidePart part in slidePartList)
{
RunProperties[] runProList = part.Slide.Descendants<RunProperties>().ToArray();
foreach (RunProperties r in runProList)
{
Console.WriteLine(r.FontSize.Value);
}
}
}
}
}
}