對不起,真的很晚推遲,希望你仍然在尋找這個。請注意,不是使用SDK - 它只是使用System.IO.Packaging
和Linq(和XML Literals)。無論如何,這是做什麼:
- 創建一個演示文稿。在幻燈片3上, 添加4個文本框。
- 在其中三個文本中放置文本並將它們命名爲 「Sample1」,「Sample2」和 「Sample3」。
- 在上一個文本框中,放上兩行 的文字,然後使這些行成爲 的重點。將其命名爲「ListSample1」。
這就是你需要的。然後保存該文件並記下路徑,並更改下面的filePath
變量以反映您的演示文稿的路徑。
運行下面的控制檯應用程序:
Imports System.IO
Imports System.IO.Packaging ''# Add reference to WindowsBase for this
Imports <xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main">
Imports <xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
Imports <xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
Module Module1
Public Const documentRelationshipType As String = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"
Sub Main()
Dim slide, document As XElement
Dim pptPackage As Package = Nothing
Dim slidePart, documentPart As PackagePart
Dim filePath As String = "C:\Users\Me\Documents\visual studio 2010\Projects\FillPowerPoint\FillPowerPoint\sample.pptx"
pptPackage = Package.Open(filePath, FileMode.Open, FileAccess.ReadWrite)
Using pptPackage
Dim documentRelationship As PackageRelationship = pptPackage.GetRelationshipsByType(documentRelationshipType).FirstOrDefault
Dim documentUri As Uri = PackUriHelper.ResolvePartUri(New Uri("/", UriKind.Relative), documentRelationship.TargetUri)
documentPart = pptPackage.GetPart(documentUri)
document = XElement.Load(New StreamReader(documentPart.GetStream))
Dim slideList = From e In document.<p:sldIdLst>.<p:sldId>
Dim slideIndex As Integer = 3 ''# this is the slide number we want, 1-based
Dim slideReference As String = slideList(slideIndex - 1)[email protected]:id.ToString
slidePart = pptPackage.GetPart(PackUriHelper.ResolvePartUri(documentPart.Uri, documentPart.GetRelationship(slideReference).TargetUri))
slide = XElement.Load(New StreamReader(slidePart.GetStream))
''# Replace just text value in Sample1 textbox
Dim Sample1 = From e In slide.<p:cSld>.<p:spTree>.<p:sp> Where e.<p:nvSpPr>.<p:cNvPr>[email protected] = "Sample1" Select e.<p:txBody>.<a:p>.<a:r>.<a:t>.SingleOrDefault
Sample1.Value = "new text in sample 1"
''# Replace text and make bold inn Sample2 textbox
Dim Sample2 = From e In slide.<p:cSld>.<p:spTree>.<p:sp> Where e.<p:nvSpPr>.<p:cNvPr>[email protected] = "Sample2" Select e.<p:txBody>.<a:p>.<a:r>.SingleOrDefault
Sample2.<a:rPr>.SingleOrDefault.Add(New XAttribute("b", 1))
Sample2.<a:t>.SingleOrDefault.Value = "new bold text in sample 2"
''# Replace text and make bold inn Sample2 textbox
Dim Sample3 = From e In slide.<p:cSld>.<p:spTree>.<p:sp> Where e.<p:nvSpPr>.<p:cNvPr>[email protected] = "Sample3" Select e.<p:txBody>.SingleOrDefault
Sample3.<a:p>.Remove()
Dim newParagraphs As XElement = <placeholder>
<a:p>
<a:r>
<a:rPr lang="en-US" dirty="0" smtClean="0"/>
<a:t>Sample3</a:t>
</a:r>
</a:p>
<a:p>
<a:r>
<a:rPr lang="en-US" smtClean="0"/>
<a:t>With a new paragraph</a:t>
</a:r>
<a:endParaRPr lang="en-US" dirty="0"/>
</a:p>
</placeholder>
Sample3.SingleOrDefault.Add(newParagraphs.Elements)
''# Create a new list of bullets
Dim s() As String = {"Bullet 1", "Bullet 2", "Bullet 3"}
Dim ListSample1 = From e In slide.<p:cSld>.<p:spTree>.<p:sp> Where e.<p:nvSpPr>.<p:cNvPr>[email protected] = "ListSample1" Select e.<p:txBody>.SingleOrDefault
ListSample1.<a:p>.Remove()
ListSample1.SingleOrDefault.Add(From e In s Select <a:p>
<a:pPr marL="285750" indent="-285750">
<a:buFont typeface="Arial" pitchFamily="34" charset="0"/>
<a:buChar char="•"/>
</a:pPr>
<a:r>
<a:rPr lang="en-US" dirty="0" smtClean="0"/>
<a:t><%= e %></a:t>
</a:r>
</a:p>)
slide.Save(slidePart.GetStream)
End Using
End Sub
End Module
對不起,我知道這在很大程度上對VB和XML文本加權,但C#應該能夠做同樣的事情有一些轉換工作。
它必須是一個C#示例嗎?它可以是vb.net嗎? – 2010-10-11 15:24:30
@Otaku - vb。網很好。 .i總是可以轉換 – leora 2010-10-12 01:00:05