Thursday, 3 October 2013

How to replace a old text with the new text in powerpoint presentation using openxml

How to replace a old text with the new text in powerpoint presentation
using openxml

i am new to Openxml. i have a requirement that i need to replace occurence
of texts in powerpoint presentation with new text. am opening the already
created ppt file. For Example i have 30 slides and in some of the slides i
have <> text, i need to replace <> with orginal customer name which am
getting from web page. it should replace all the occurence of the <> with
the new text. How do i replace it ?
My code :
sPptPath = Server.MapPath("~/Source/PPT/") +
DdSubSolutions.SelectedItem.Text.Trim().ToString() + ".pptx";
var pkg = Package.Open(sPptPath, FileMode.Open, FileAccess.ReadWrite);
// Specify the URI of the part to be read
var uri = new Uri("/ppt/presentation.xml",
UriKind.Relative);
PackagePart part = pkg.GetPart(uri);
var xmlMainXmlDoc = new XmlDocument();
xmlMainXmlDoc.Load(part.GetStream(FileMode.Open,
FileAccess.Read));
xmlMainXmlDoc.InnerXml =
xmlMainXmlDoc.InnerXml.Replace("cust", "banu");
// Open the stream to write document
var partWrt = new
StreamWriter(part.GetStream(FileMode.Open,
FileAccess.Write));
xmlMainXmlDoc.Save(partWrt);
partWrt.Flush();
partWrt.Close();
pkg.Close();
am getting invalid operation exception in the below line var uri = new
Uri("/ppt/presentation.xml", UriKind.Relative);
Thanks, Banu

No comments:

Post a Comment