C# powerpoint 编辑 smartart 文本

C# powerpoint edit smartart text

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

我正在使用

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

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

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

returns 与另一条消息相同的错误消息。我可能需要让 powerpoint 可见,但出于某种原因,当我让它可见时,它就会消失。 (杀毒软件?)

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

************** Exception Text **************

System.Runtime.InteropServices.COMException (0x80004005): Error HRESULT E_FAIL has been returned from a call to a COM component. at Microsoft.Office.Core.TextRange2.set_Text(String pbstrText) at *.Form1.BtnStart_Click(Object sender, EventArgs e) in *\Form1.cs:line 414 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.PerformClick() at System.Windows.Forms.Form.ProcessDialogKey(Keys keyData) at System.Windows.Forms.Control.ProcessDialogKey(Keys keyData) at System.Windows.Forms.Control.ProcessDialogKey(Keys keyData) at System.Windows.Forms.Control.ProcessDialogKey(Keys keyData) at System.Windows.Forms.Control.PreProcessMessage(Message& msg) at System.Windows.Forms.Control.PreProcessControlMessageInternal(Control target, Message& msg) at System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG& msg)

************** Loaded Assemblies **************

mscorlib Assembly Version: 4.0.0.0 Win32 Version: 4.8.4075.0 built by: NET48REL1LAST CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll

System.Windows.Forms Assembly Version: 4.0.0.0 Win32 Version: 4.8.4042.0 built by: NET48REL1LAST_C CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_0.0.0.0__b77a5c561934e089/System.Windows.Forms.dll

System Assembly Version: 4.0.0.0 Win32 Version: 4.8.4001.0 built by: NET48REL1LAST_C CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c> 561934e089/System.dll

System.Drawing Assembly Version: 4.0.0.0 Win32 Version: 4.8.3752.0 built by: NET48REL1 CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0> __b03f5f7f11d50a3a/System.Drawing.dll

System.Configuration Assembly Version: 4.0.0.0 Win32 Version: 4.8.3752.0 built by: NET48REL1 CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll

System.Core Assembly Version: 4.0.0.0 Win32 Version: 4.8.4075.0 built by: NET48REL1LAST CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b> 77a5c561934e089/System.Core.dll

System.Xml Assembly Version: 4.0.0.0 Win32 Version: 4.8.3752.0 built by: NET48REL1 CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b7> 7a5c561934e089/System.Xml.dll

Microsoft.CSharp Assembly Version: 4.0.0.0 Win32 Version: 4.8.3752.0 CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/Microsoft.CSharp/v4.0_4.0.0> .0__b03f5f7f11d50a3a/Microsoft.CSharp.dll

System.Dynamic Assembly Version: 4.0.0.0 Win32 Version: 4.8.3752.0 CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Dynamic/v4.0_4.0.0.0> __b03f5f7f11d50a3a/System.Dynamic.dll

Anonymously Hosted DynamicMethods Assembly Assembly Version: 0.0.0.0 Win32 Version: 4.8.4075.0 built by: NET48REL1LAST CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_32/mscorlib/v4.0_4.0.0.0__b77a5c561934e089/mscorlib.dll

CustomMarshalers Assembly Version: 4.0.0.0 Win32 Version: 4.8.3752.0 built by: NET48REL1 CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_32/CustomMarshalers/v4.0_4.0.0.0__b03f5f7f11d50a3a/CustomMarshalers.dll

Microsoft.Office.Interop.PowerPoint Assembly Version: 15.0.0.0 Win32 Version: 15.0.4569.1507 CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/Microsoft.Office.Interop.PowerPoint/15.0.0.0__71e9bce111e9429c/Microsoft.Office.Interop.PowerPoint.dll

office Assembly Version: 15.0.0.0 Win32 Version: 15.0.4613.1000 CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/office/15.0.0.0__71e9bce111e9429c/office.dll

System.Dynamic.DynamicAssembly Assembly Version: 0.0.0.0 Win32 Version: 4.8.3752.0 CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Dynamic/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Dynamic.dll

不确定下一步要去哪里。

***************Documentation/Reference***************

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 变体具有无法以编程方式设置的文本节点,因此尝试在其上添加文本总是会失败。这是在此处设置 SmartArt 文本,使用 VBA:

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 和互操作之间切换,直到找到更好的修复方法。

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();
}