C#powerpoint编辑smartart文本



我目前正在尝试替换powerpoint smartart对象中的一些文本。我可以获取文本,但我无法设置文本值,即使很多文档让它看起来很容易。

我正在使用

shp.SmartArt.AllNodes[i].TextFrame2.TextRange.Text = shapeString;

设置文本字符串,但无论我使用什么功能,我都会出现错误

shp.SmartArt.AllNodes[i].TextFrame2.DeleteText();

返回与其他消息相同的错误消息。我可能需要使幻灯片可见,但由于某种原因,当我使其可见时,它就会消失。(防病毒?)

我可以用标准文本框替换PPT中的文本,所以我试图重复smartart的步骤,但没有成功。

**************异常文本**************

System.Runtime.InteropServices.COMException(0x80004005):对COM组件的调用返回错误HRESULT E_FAIL。位于Microsoft.Office.Core.TextRange2.set_Text(字符串pbstrText)位于中的*.Form1.BtnStart_Click(对象发送方,EventArgs e)*\Form1.cs:第414行位于System.Windows.Forms.Control.OnClick(EventArgs e)位于System.Windows.Forms.Button.OnClick(EventArgs e)位于System.Windows.Forms.Button.PerformClick()位于System.Windows.Forms.Form.ProcessDialogKey(Keys-keyData)位于System.Windows.Forms.Control.ProcessDialogKey(Keys-keyData)位于System.Windows.Forms.Control.ProcessDialogKey(Keys-keyData)位于System.Windows.Forms.Control.ProcessDialogKey(Keys-keyData)位于System.Windows.Forms.Control.PreProcessMessage(消息和消息)位于System.Windows.Forms.Control.PreProcessControlMessageInternal(控制目标,消息和消息)在System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG&MSG)

**************加载的程序集**************

mscorlib程序集版本:4.0.0.0Win32版本:4.8.4075.0构建者:NET48REL1LAST代码库:file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll

System.Windows.Forms程序集版本:4.0.0.0Win32版本:4.8.4042.0构建者:NET48REL1LAST_C代码库:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_0.0.0.0__b77a5c561934e089/System.Windows.Forms.dll

系统程序集版本:4.0.0.0Win32版本:4.8.4001.0构建者:NET48REL1LAST_C代码库:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c>561934e089/System.dll

系统图纸程序集版本:4.0.0.0Win32版本:4.8.3752.0构建者:NET48REL1代码库:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0>__b03f5f7f11d50a3a/System.Drawing.dll

系统配置程序集版本:4.0.0.0Win32版本:4.8.3752.0构建者:NET48REL1代码库:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll

System.Core程序集版本:4.0.0.0Win32版本:4.8.4075.0构建者:NET48REL1LAST代码库:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b>77a5c561934e089/System.Core.dll

System.Xml程序集版本:4.0.0.0Win32版本:4.8.3752.0构建者:NET48REL1代码库:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b7>7a5c561934e089/System.Xml.dll

Microsoft.CSharp程序集版本:4.0.0.0Win32版本:4.8.3752.0代码库:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/Microsoft.CSharp/v4.0_4.0.0>_0_b03f5f7f11d50a3a/Microsoft.CSharp.dll

System.Dynamic程序集版本:4.0.0.0Win32版本:4.8.3752.0代码库:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Dynamic/v4.0_4.0.0.0>__b03f5f7f11d50a3a/System.Dynamic.dll

匿名托管的DynamicMethods程序集程序集版本:0.0.0.0Win32版本:4.8.4075.0构建者:NET48REL1LAST代码库:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_32/mscorlib/v4.0_4.0.0.0__b77a5c561934e089/mscorlib.dll

CustomMarshallers程序集版本:4.0.0.0Win32版本:4.8.3752.0构建者:NET48REL1代码库:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_32/CustomMarshalers/v4.0_4.0.0.0__b03f5f7f11d50a3a/CustomMarshalers.dll

Microsoft.Office.Interop.PowerPoint程序集版本:15.0.0.0Win32版本:15.0.4569.1507代码库:file:///C:/WINDOWS/assembly/GAC_MSIL/Microsoft.Office.Interop.PowerPoint/15.0.0.0__71e9bce111e9429c/Microsoft.Office.Interop.PowerPoint.dll

办公室程序集版本:15.0.0.0Win32版本:15.0.4613.1000代码库:file:///C:/WINDOWS/assembly/GAC_MSIL/office/15.0.0.0__71e9bce111e9429c/office.dll

System.Dynamic.DynamicAssembly程序集版本:0.0.0.0Win32版本:4.8.3752.0代码库:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Dynamic/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Dynamic.dll

不知道下一步该去哪里。

*************文件/参考*************

https://help.syncfusion.com/file-formats/presentation/smartart

//Open a PowerPoint Presentation
IPresentation pptxDoc = Presentation.Open("SampleDocument.pptx");
//Traverse through shape in the first slide.
foreach (IShape shape in pptxDoc.Slides[0].Shapes)
{
if (shape is ISmartArt)
{
//Traverse through all nodes inside SmartArt
foreach (ISmartArtNode mainNode in (shape as ISmartArt).Nodes)
{
if (mainNode.TextBody.Text == "Old Content")
//Change the node content
mainNode.TextBody.Paragraphs[0].TextParts[0].Text = "New Content";
}
}
}
//Save the Presentation.
pptxDoc.Save("SmartArt.pptx");
//Close the Presentation.
pptxDoc.Close();

我不使用Interop,但在更改节点之前,您可以先测试节点是否有文本。有些SmartArt变体的文本节点无法通过编程设置,因此尝试在其上添加文本总是会失败。这是使用此VBA在此处设置SmartArt文本:

If ActivePresentation.Slides(1).Shapes(1).SmartArt.AllNodes(X).TextFrame2.HasText Then
ActivePresentation.Slides(1).Shapes(1).SmartArt.AllNodes(X).TextFrame2.TextRange.Text = "Text"
End If

我最终使用openxml来解决这个问题。不幸的是,在找到更好的解决方案之前,我会在openxml和interop之间切换。

using (PresentationDocument doc = PresentationDocument.Open(textBox1.Text, true))
{
PresentationPart pp = doc.PresentationPart;
SlidePart sp1 = pp.SlideParts.ToList<SlidePart>()[0];
foreach (DiagramDataPart ddp in sp1.DiagramDataParts.ToList<DiagramDataPart>())
{
DataModelRoot dmr = ddp.DataModelRoot;
List<PointList> pl = dmr.Descendants<PointList>().ToList();
foreach (PointList item in pl)
{
List<Point> ps = item.Descendants<Point>().ToList();
foreach (Point p in ps)
{
if (p.InnerText.Equals("_HELLO_"))
{
DocumentFormat.OpenXml.Drawing.Paragraph para = p.TextBody.GetFirstChild<DocumentFormat.OpenXml.Drawing.Paragraph>();
DocumentFormat.OpenXml.Drawing.Run run1 = para.GetFirstChild<DocumentFormat.OpenXml.Drawing.Run>();
DocumentFormat.OpenXml.Drawing.Text text1 = run1.GetFirstChild<DocumentFormat.OpenXml.Drawing.Text>();
text1.Text = "World";
var textreplacement = p.InnerText;
}
}
}
}
doc.Save();
}

最新更新