public static void GetSlideTitles(PresentationDocument presentationDocument, string store) { if (presentationDocument == null) { throw new ArgumentNullException("presentationDocument"); } // Get a PresentationPart object from the PresentationDocument object. PresentationPart presentationPart = presentationDocument.PresentationPart; if (presentationPart != null && presentationPart.Presentation != null) { // Get a Presentation object from the PresentationPart object. Presentation presentation = presentationPart.Presentation; if (presentation.SlideIdList != null) { // Get the title of each slide in the slide order. foreach (var slideId in presentation.SlideIdList.Elements <SlideId>()) { SlidePart slidePart = presentationPart.GetPartById(slideId.RelationshipId) as SlidePart; // Get the slide title. GetSlide(slidePart, store); // An empty title can also be added. } } } }
public static string GetSlideIdAndText(string docName, int index) { StringBuilder paragraphText = new StringBuilder(); using (PresentationDocument ppt = PresentationDocument.Open(docName, false)) { // Get the relationship ID of the first slide. PresentationPart part = ppt.PresentationPart; OpenXmlElementList slideIds = part.Presentation.SlideIdList.ChildElements; string relId = (slideIds[index] as SlideId).RelationshipId; // Get the slide part from the relationship ID. SlidePart slide = (SlidePart)part.GetPartById(relId); // Get the inner text of the slide IEnumerable <A.Text> texts = slide.Slide.Descendants <A.Text>(); foreach (A.Text text in texts) { paragraphText.Append(text.Text); } // Get any notes from the slide if (slide.NotesSlidePart != null) { paragraphText.Append(slide.NotesSlidePart.NotesSlide.InnerText); } } return(paragraphText.ToString()); }
public static bool ReadFileCompateText(string path, string s) { using (PresentationDocument presentationDocument = PresentationDocument.Open(path, false)) { Regex r = new Regex(s, RegexOptions.IgnoreCase); // Get the relationship ID of the first slide. PresentationPart part = presentationDocument.PresentationPart; OpenXmlElementList slideIds = part.Presentation.SlideIdList.ChildElements; for (int i = 0; i < slideIds.Count; i++) { string relId = (slideIds[i] as SlideId).RelationshipId; // Get the slide part from the relationship ID. SlidePart slide = (SlidePart)part.GetPartById(relId); // Get the inner text of the slide: IEnumerable <A.Text> texts = slide.Slide.Descendants <A.Text>(); foreach (A.Text text in texts) { Match m = r.Match(text.InnerText); if (m.Success) { presentationDocument.Close(); return(true); } } } presentationDocument.Close(); } return(false); }
/// <summary> /// ファイル名とスライド番号に対応するテキストをすべて取得して結合して返却する。 /// </summary> /// <param name="sldText">スライドに設定されたString</param> /// <param name="docName">ファイル名</param> /// <param name="index">スライド番号</param> public static void GetSlideIdAndText(out string sldText, string docName, int index) { using (PresentationDocument ppt = PresentationDocument.Open(docName, false)) { // Get the relationship ID of the first slide. PresentationPart part = ppt.PresentationPart; OpenXmlElementList slideIds = part.Presentation.SlideIdList.ChildElements; // スライド番号から、.NETで扱うためのスライドIDを取得する。 string relId = (slideIds[index] as SlideId).RelationshipId; // Get the slide part from the relationship ID. SlidePart slide = (SlidePart)part.GetPartById(relId); // Build a StringBuilder object. StringBuilder paragraphText = new StringBuilder(); // Get the inner text of the slide: // slide.Slide(current element's)がもつ DocumentFormat.OpenXml.Drawing.Text型のXML上の要素をすべて取得する。 IEnumerable <A.Text> texts = slide.Slide.Descendants <A.Text>(); foreach (A.Text text in texts) { paragraphText.Append(text.Text).Append(" "); Console.WriteLine("-------------------------------------"); Console.WriteLine("Parent:" + text.LocalName); Console.WriteLine("Parent:" + text.Parent.ToString()); } sldText = paragraphText.ToString(); } }
public ToxySlideshow Parse() { if (!File.Exists(Context.Path)) { throw new FileNotFoundException("File " + Context.Path + " is not found"); } ToxySlideshow ss = new ToxySlideshow(); using (PresentationDocument ppt = PresentationDocument.Open(Context.Path, false)) { // Get the relationship ID of the first slide. PresentationPart part = ppt.PresentationPart; DocumentFormat.OpenXml.OpenXmlElementList slideIds = part.Presentation.SlideIdList.ChildElements; for (int index = 0; index < slideIds.Count; index++) { string relId = (slideIds[index] as SlideId).RelationshipId; relId = (slideIds[index] as SlideId).RelationshipId; // Get the slide part from the relationship ID. SlidePart slide = (SlidePart)part.GetPartById(relId); var tslide = Parse(slide); ss.Slides.Add(tslide); } } return(ss); }
private IEnumerable <SlidePart> GetPresentationSlideParts(PresentationDocument presentationDocument) { var slideParts = new List <SlidePart>(); PresentationPart presentationPart = presentationDocument.PresentationPart; if (presentationPart != null && presentationPart.Presentation != null) { Presentation presentation = presentationPart.Presentation; if (presentation.SlideIdList != null) { foreach (var slideId in presentation.SlideIdList.Elements <SlideId>()) { try { slideParts.Add((SlidePart)presentationPart.GetPartById(slideId.RelationshipId)); } catch (Exception ex) { throw new ArgumentNullException("Unable to add slide part." + ex.InnerException); } } } } return(slideParts); }
/// <summary> /// 获取所有文本 /// </summary> /// <param name="pdoc">ppt对象</param> /// <returns></returns> private static List <string> GetSlideIdAndText(PresentationDocument pdoc) { IEnumerable <SlidePart> slideParts = pdoc.PresentationPart.SlideParts; List <string> result = new List <string>(); for (int i = 0; i < slideParts.Count(); i++) { PresentationPart part = pdoc.PresentationPart; OpenXmlElementList slideIds = part.Presentation.SlideIdList.ChildElements; string relId = (slideIds[i] as SlideId).RelationshipId; // 取得幻灯片的关系ID SlidePart slide = (SlidePart)part.GetPartById(relId); StringBuilder paragraphText = new StringBuilder(); // 得到幻灯片的文本内容 IEnumerable <D.Text> texts = slide.Slide.Descendants <D.Text>(); foreach (D.Text text in texts) { paragraphText.Append(text.Text + "\t"); } result.Add(paragraphText.ToString()); } return(result); }
public ActionResult Charts(string Id) { if (presentationDocument == null) { presentationDocument = PresentationDocument.Open(Server.MapPath("~/App_Data/TDMGuestServices.pptx"), true); } presentationPart = presentationDocument.PresentationPart; var slide = (SlidePart)presentationPart.GetPartById(Id); var lst = new List <ChartDTO>(); foreach (var chart in slide.ChartParts) { foreach (var item in chart.ChartSpace.Descendants()) { if (item.GetType() == typeof(DocumentFormat.OpenXml.Drawing.Charts.Title)) { lst.Add(new ChartDTO { Name = ((DocumentFormat.OpenXml.Drawing.Charts.Title)item).ChartText.InnerText, Id = ((DocumentFormat.OpenXml.Drawing.Charts.Title)item).ChartText.InnerText }); } } } //var lst = new List<RunTB>(); return(View(lst)); }
private void GetSlideDurations(string powerPointFileName) { try { textBox.Text = ""; TimeSpan presentationDuration = TimeSpan.FromSeconds(0); using (PresentationDocument pptDocument = PresentationDocument.Open(powerPointFileName, false)) { PresentationPart presentationPart = pptDocument.PresentationPart; Presentation presentation = presentationPart.Presentation; int slideNumber = 1; foreach (var slideId in presentation.SlideIdList.Elements <SlideId>()) { SlidePart slidePart = presentationPart.GetPartById(slideId.RelationshipId) as SlidePart; var advanceAfterTimeDuration = ConvertStringToInt(GetSlideAdvanceAfterTimeDuration(slidePart)); var anitationsDuration = GetSlideAnimationsDuration(slidePart); var transitionDuration = ConvertStringToInt(GetSlideTransitionsDuration(slidePart)); var totalSlideDuration = advanceAfterTimeDuration + anitationsDuration + transitionDuration; TimeSpan slideTime = TimeSpan.FromMilliseconds(totalSlideDuration); presentationDuration = presentationDuration.Add(slideTime); textBox.Text += $"Slide {slideNumber} Total Duration: {totalSlideDuration} ms. (aat: {advanceAfterTimeDuration} ms, ani: {anitationsDuration} ms trn: {transitionDuration} ms)" + Environment.NewLine; slideNumber++; } textBox.Text += $"Total Presentation Duration: {presentationDuration.TotalMilliseconds} msecs." + Environment.NewLine; } } catch (Exception ex) { textBox.Text = $"Problem occurred parsing file {powerPointFileName}. Exception: {ex}"; } }
public static Slide GetDetailSlide(PresentationDocument _presentationDocument, int _slideIndex) { // Get the presentation part of the presentation document. presentationPart = _presentationDocument.PresentationPart; if (presentationPart != null && presentationPart.Presentation != null) { Presentation presentation = presentationPart.Presentation; if (presentation.SlideIdList != null) { DocumentFormat.OpenXml.OpenXmlElementList slideIds = presentation.SlideIdList.ChildElements; // If the slide ID is in range... if (_slideIndex < slideIds.Count) { // Get the relationship ID of the slide. string slidePartRelationshipId = (slideIds[_slideIndex] as SlideId).RelationshipId; // Get the specified slide part from the relationship ID. slidePart = (SlidePart)presentationPart.GetPartById(slidePartRelationshipId); return(slidePart.Slide); } } } return(null); }
public override string Parse() { if (!File.Exists(Context.Path)) { throw new FileNotFoundException("File " + Context.Path + " is not found"); } StringBuilder sb = new StringBuilder(); using (PresentationDocument ppt = PresentationDocument.Open(Context.Path, false)) { // Get the relationship ID of the first slide. PresentationPart part = ppt.PresentationPart; OpenXmlElementList slideIds = part.Presentation.SlideIdList.ChildElements; for (int index = 0; index < slideIds.Count; index++) { string relId = (slideIds[index] as SlideId).RelationshipId; relId = (slideIds[index] as SlideId).RelationshipId; // Get the slide part from the relationship ID. SlidePart slide = (SlidePart)part.GetPartById(relId); string[] texts = GetAllTextInSlide(slide); foreach (string text in texts) { sb.AppendLine(text); } } return(sb.ToString()); } }
public void ExtractTextFromPresentation(PresentationDocument document, StringBuilder sb) { PresentationPart pPart = document.PresentationPart; int slidesCount = 0; if (pPart != null) { slidesCount = pPart.SlideParts.Count(); } if (slidesCount < 1) { return; } OpenXmlElementList slideIds = pPart.Presentation.SlideIdList.ChildElements; for (var i = 0; i < slidesCount; i++) { string relId = (slideIds[i] as SlideId).RelationshipId; SlidePart slide = (SlidePart)pPart.GetPartById(relId); IEnumerable <A.Text> texts = slide.Slide.Descendants <A.Text>(); foreach (A.Text text in texts) { sb.Append(text.Text); sb.Append(" "); } } }
private static IList <SlidePart> GetSlideParts(PresentationDocument presentationDocument) { PresentationPart presentationPart = presentationDocument.PresentationPart; if (presentationPart != null && presentationPart.Presentation != null) { Presentation presentation = presentationPart.Presentation; if (presentation.SlideIdList != null) { var slideIds = presentation.SlideIdList.ChildElements; IList <SlidePart> slideParts = new List <SlidePart>(); for (int i = 0; i < slideIds.Count; i++) { string slidePartRelationshipId = (slideIds[i] as SlideId).RelationshipId; SlidePart slidePart = (SlidePart)presentationPart.GetPartById(slidePartRelationshipId); slideParts.Add(slidePart); } return(slideParts); } } // No slide found return(null); }
public static void GetSlideIdAndText(out string sldText, string docName, int index) { using (PresentationDocument ppt = PresentationDocument.Open(docName, false)) { // Get the relationship ID of the first slide. PresentationPart part = ppt.PresentationPart; OpenXmlElementList slideIds = part.Presentation.SlideIdList.ChildElements; string relId = (slideIds[index] as SlideId).RelationshipId; // Get the slide part from the relationship ID. SlidePart slide = (SlidePart)part.GetPartById(relId); // Build a StringBuilder object. StringBuilder paragraphText = new StringBuilder(); // Get the inner text of the slide: IEnumerable <A.Text> texts = slide.Slide.Descendants <A.Text>(); foreach (A.Text text in texts) { paragraphText.Append(text.Text); } sldText = paragraphText.ToString(); } }
public static void GetSlideIdAndText(out int foundCounter, string searchString, Stream docName, int index) { using (PresentationDocument ppt = PresentationDocument.Open(docName, false)) { //Reset Counter for founded String int stringCount = 0; // Get the relationship ID of the first slide. PresentationPart part = ppt.PresentationPart; OpenXmlElementList slideIds = part.Presentation.SlideIdList.ChildElements; string relId = (slideIds[index] as SlideId).RelationshipId; // Get the slide part from the relationship ID. SlidePart slide = (SlidePart)part.GetPartById(relId); // Build a StringBuilder object. StringBuilder paragraphText = new StringBuilder(); // Get the inner text of the slide: IEnumerable <A.Text> texts = slide.Slide.Descendants <A.Text>(); foreach (A.Text text in texts) { if (text.Text.Contains(searchString) && searchString != "") { stringCount = stringCount + 1; } } foundCounter = stringCount; } }
public static ICollection <IEnumerable <string> > GetAllSlides(string path) { using (PresentationDocument presentationDocument = PresentationDocument.Open(path, false)) { PresentationPart presentationPart = presentationDocument.PresentationPart; if (presentationPart != null && presentationPart.Presentation != null) { Presentation presentation = presentationPart.Presentation; if (presentation.SlideIdList != null) { var slideIds = presentation.SlideIdList.ChildElements; ICollection <IEnumerable <string> > slides = new List <IEnumerable <string> >(); for (int i = 0; i < slideIds.Count; i++) { string slidePartRelationshipId = (slideIds[i] as SlideId).RelationshipId; SlidePart slidePart = (SlidePart)presentationPart.GetPartById(slidePartRelationshipId); var slideText = GetAllTextInSlide(slidePart, i); slides.Add(slideText); } return(slides); } } return(null); } }
public static void changeSlideIdAndText(string docName, int index) { using (PresentationDocument ppt = PresentationDocument.Open(docName, true)) { // Get the relationship ID of the first slide. PresentationPart part = ppt.PresentationPart; OpenXmlElementList slideIds = part.Presentation.SlideIdList.ChildElements; // スライド番号から、.NETで扱うためのスライドIDを取得する。 string relId = (slideIds[index] as SlideId).RelationshipId; // Get the slide part from the relationship ID. // スライド番号からスライドのオブジェクトを取得 SlidePart slide = (SlidePart)part.GetPartById(relId); // Get the inner text of the slide: // slide.Slide(current element's)がもつ DocumentFormat.OpenXml.Drawing.Text型のXML上の要素をすべて取得する。 IEnumerable <A.Text> texts = slide.Slide.Descendants <A.Text>(); foreach (A.Text text in texts) { text.Text = text.Text + " Chage"; Console.WriteLine("-------------------------------------"); Console.WriteLine("Change後テキスト:" + text.Text); } } }
/// <summary> /// Reading file raw data from file byte data. /// </summary> /// <param name="data">File resources as byte arrays.</param> /// <returns>An array of objects containing raw data.</returns> public TempData[] ReadFileRawDataFromByteArray(byte[] data) { try { using (PresentationDocument presentationDocument = PresentationDocument.Open(new MemoryStream(data), false)) { List <TempData> result = new List <TempData>(); PresentationPart presentationPart = presentationDocument.PresentationPart; OpenXmlElementList openXmlElementList = presentationPart.Presentation.SlideIdList.ChildElements; // Get all SlideParts. var slideParts = from item in openXmlElementList select(SlidePart) presentationPart.GetPartById((item as SlideId).RelationshipId); // Retrieve the text of each slidePart. var slideText = from item in slideParts select GetSlideText(item); result.AddRange(TempData.GetTempDataIEnumerable(StorageType.TextType, slideText)); // Find image and add to the result. // Insert code here! return(result.ToArray()); } } catch (Exception ex) { throw ex; } }
/// <summary> /// Parses the data into a list of slides. /// </summary> /// <returns> /// A list of parsed slides. /// </returns> public List <Model.Slide> Parse() { var parsedSlides = new List <Model.Slide>(); PresentationPart presentationPart = doc.PresentationPart; // Verify that the presentation part and presentation exist. if (presentationPart != null && presentationPart.Presentation != null) { // Get the Presentation object from the presentation part. Presentation presentation = presentationPart.Presentation; // Verify that the slide ID list exists. if (presentation.SlideIdList != null) { // Loop on the collection of slide IDs from the slide ID list. foreach (SlideId slideId in presentation.SlideIdList.ChildElements) { // Get the relationship ID of the slide. string slidePartRelationshipId = slideId.RelationshipId; // Get the specified slide part from the relationship ID. SlidePart slidePart = (SlidePart)presentationPart.GetPartById(slidePartRelationshipId); parsedSlides.Add(ParseSlide(slidePart)); } } } return(parsedSlides); }
public ToxySlide Parse(int slideIndex) { if (!File.Exists(Context.Path)) { throw new FileNotFoundException("File " + Context.Path + " is not found"); } using (PresentationDocument ppt = PresentationDocument.Open(Context.Path, false)) { // Get the relationship ID of the first slide. PresentationPart part = ppt.PresentationPart; DocumentFormat.OpenXml.OpenXmlElementList slideIds = part.Presentation.SlideIdList.ChildElements; if (slideIds.Count - 1 < slideIndex) { throw new ArgumentOutOfRangeException(string.Format("This file only contains {0} slide(s).", slideIds.Count)); } string relId = (slideIds[slideIndex] as SlideId).RelationshipId; relId = (slideIds[slideIndex] as SlideId).RelationshipId; // Get the slide part from the relationship ID. SlidePart slide = (SlidePart)part.GetPartById(relId); var tslide = Parse(slide); return(tslide); } }
public static void Main(string[] args) { File.Copy(TemplatePath, OutputPath, true); using (PresentationDocument presentationDoc = PresentationDocument.Open(OutputPath, true)) { PresentationPart presPart = presentationDoc.PresentationPart; // Step 1: Identify the proper Part Id SlidePart contentSlidePart = (SlidePart)presPart.GetPartById("rId2"); // Step 2: Replace one image with external file string imageRel = "rIdImg"; int imageRelId = 1; var imgId = imageRel + imageRelId; ImagePart imagePart = contentSlidePart.AddImagePart(ImagePartType.Jpeg, imgId); using (Image image = Image.FromFile(ImagePath)) { using (MemoryStream m = new MemoryStream()) { image.Save(m, image.RawFormat); m.Position = 0; imagePart.FeedData(m); SwapPhoto(contentSlidePart, imgId); } } // Step 3: Replace text matched by the key SwapPlaceholderText(contentSlidePart, "{{Title}}", "Testing Title for IBM"); // Step 4: Fill out multi value fields by table var tupleList = new List <(string category, string model, string price)> { ("Automobile", "Ford", "$25K"), ("Automobile", "Toyota", "$30K"), ("Computer", "IBM PC", "$2.5K"), ("Laptop", "Dell", "$1K"), ("Laptop", "Microsoft", "$2K") }; Drawing.Table tbl = contentSlidePart.Slide.Descendants <Drawing.Table>().First(); foreach (var row in tupleList) { Drawing.TableRow tr = new Drawing.TableRow(); tr.Height = 100; tr.Append(CreateTextCell(row.category)); tr.Append(CreateTextCell(row.model)); tr.Append(CreateTextCell(row.price)); tbl.Append(tr); } // Step 5: Save the presentation presPart.Presentation.Save(); } }
public static IEnumerable <SlidePart> GetSlidePartsInOrder(this PresentationPart presentationPart) { SlideIdList slideIdList = presentationPart.Presentation.SlideIdList; return(slideIdList.ChildElements .Cast <SlideId>() .Select(x => presentationPart.GetPartById(x.RelationshipId)) .Cast <SlidePart>()); }
public static void DeleteSlide(PresentationDocument presentationDocument, int slideIndex, int slidesCount) { slideIndex--; if (presentationDocument == null) { throw new ArgumentNullException("presentationDocument"); } if (slideIndex < 0 || slideIndex >= slidesCount) { throw new ArgumentOutOfRangeException("slideIndex"); } PresentationPart presentationPart = presentationDocument.PresentationPart; Presentation presentation = presentationPart.Presentation; SlideIdList slideIdList = presentation.SlideIdList; SlideId slideId = slideIdList.ChildElements[slideIndex] as SlideId; string slideRelId = slideId.RelationshipId; slideIdList.RemoveChild(slideId); if (presentation.CustomShowList != null) { foreach (var customShow in presentation.CustomShowList.Elements <CustomShow>()) { if (customShow.SlideList != null) { LinkedList <SlideListEntry> slideListEntries = new LinkedList <SlideListEntry>(); foreach (SlideListEntry slideListEntry in customShow.SlideList.Elements()) { if (slideListEntry.Id != null && slideListEntry.Id == slideRelId) { slideListEntries.AddLast(slideListEntry); } } foreach (SlideListEntry slideListEntry in slideListEntries) { customShow.SlideList.RemoveChild(slideListEntry); } } } } presentation.Save(); SlidePart slidePart = presentationPart.GetPartById(slideRelId) as SlidePart; presentationPart.DeletePart(slidePart); }
public void MergePresentationsSlides(string sourceFolderLocation, string sourcePresentation, string destinationFolderLocation, string destPresentation) { int id = 0; // Open Destination presentation using (PresentationDocument myDestDeck = PresentationDocument.Open(destinationFolderLocation + destPresentation, true)) { PresentationPart destPresPart = myDestDeck.PresentationPart; if (destPresPart.Presentation.SlideIdList == null) { destPresPart.Presentation.SlideIdList = new SlideIdList(); } // Open source presentation using (PresentationDocument mySourceDeck = PresentationDocument.Open(sourceFolderLocation + sourcePresentation, false)) { PresentationPart sourcePresPart = mySourceDeck.PresentationPart; uint uniqueId = GetMaxSlideMasterId(destPresPart.Presentation.SlideMasterIdList); uint maxSlideId = GetMaxSlideId(destPresPart.Presentation.SlideIdList); foreach (SlideId slideId in sourcePresPart.Presentation.SlideIdList) { SlidePart sp; SlidePart destSp; SlideMasterPart destMasterPart; string relId; SlideMasterId newSlideMasterId; SlideId newSlideId; id++; sp = (SlidePart)sourcePresPart.GetPartById(slideId.RelationshipId); relId = sourcePresentation.Remove(sourcePresentation.IndexOf('.')) + id; destSp = destPresPart.AddPart <SlidePart>(sp, relId); destMasterPart = destSp.SlideLayoutPart.SlideMasterPart; destPresPart.AddPart(destMasterPart); uniqueId++; newSlideMasterId = new SlideMasterId(); newSlideMasterId.RelationshipId = destPresPart.GetIdOfPart(destMasterPart); newSlideMasterId.Id = uniqueId; maxSlideId++; newSlideId = new SlideId(); newSlideId.RelationshipId = relId; newSlideId.Id = maxSlideId; destPresPart.Presentation.SlideMasterIdList.Append(newSlideMasterId); destPresPart.Presentation.SlideIdList.Append(newSlideId); FixSlideLayoutIds(destPresPart, ref uniqueId); } destPresPart.Presentation.Save(); } } }
/// <summary> /// Gets the slide from the SlideId using RelationshipId /// </summary> /// <param name="slideId">The slide identifier.</param> /// <returns></returns> internal SlidePart GetSlide(SlideId slideId) { ParameterHelpers.NotNull <SlideId>(slideId, "slideId"); if (PresentationPart != null) { return((SlidePart)PresentationPart.GetPartById(slideId.RelationshipId)); } return(null); }
private Slide getSlideByRelationShipId(PresentationPart presentationPart, StringValue relationshipId) { SlidePart slidePart = presentationPart.GetPartById(relationshipId) as SlidePart; if (slidePart != null) { return(slidePart.Slide); } return(null); }
static void Main(string[] args) { IDictionary <string, string> toReplace = new Dictionary <string, string>(); toReplace.Add("Sample2", "Sample - Text!"); toReplace.Add("Sample3", "Sample 3 - Text!"); using (PresentationDocument presentationDocument = PresentationDocument.Open("C:\\Users\\beggers\\Desktop\\Test.pptx", true)) { // Get the presentation part of the presentation document. PresentationPart presentationPart = presentationDocument.PresentationPart; // Verify that the presentation part and presentation exist. if (presentationPart != null && presentationPart.Presentation != null) { // Get the Presentation object from the presentation part. Presentation presentation = presentationPart.Presentation; // Verify that the slide ID list exists. if (presentation.SlideIdList != null) { int slideNo = 1; foreach (var slideId in presentation.SlideIdList.Elements <SlideId>()) { Console.WriteLine("Slide number: {0}", slideNo); SlidePart slidePart = presentationPart.GetPartById(slideId.RelationshipId) as SlidePart; ShapeTree tree = slidePart.Slide.CommonSlideData.ShapeTree; foreach (DocumentFormat.OpenXml.Presentation.Shape shape in tree.Elements <DocumentFormat.OpenXml.Presentation.Shape>()) { // Run through all the paragraphs in the document foreach (Paragraph paragraph in shape.Descendants().OfType <Paragraph>()) { foreach (DocumentFormat.OpenXml.Drawing.Run run in paragraph.Elements <Run>()) { foreach (var kvp in toReplace) { if (run.Text.InnerText.Contains(kvp.Key)) { run.Text = new DocumentFormat.OpenXml.Drawing.Text(kvp.Value); } } } } } slideNo++; } } } } Console.ReadLine(); }
public SlidePart CloneInputSlide() { if (Presentation == null) { throw new ArgumentNullException("presentationDocument"); } PresentationPart presentationPart = Presentation.PresentationPart; SlideId slideId = presentationPart.Presentation.SlideIdList.GetFirstChild <SlideId>(); string relId = slideId.RelationshipId; // Get the slide part by the relationship ID. SlidePart inputSlide = (SlidePart)presentationPart.GetPartById(relId); if (inputSlide == default(SlidePart)) { throw new ArgumentException("SlidePart"); } //Create a new slide part in the presentation. SlidePart newSlidePart = presentationPart.AddNewPart <SlidePart>("OutPutSlideResult-" + SlideRef); SlideRef++; //Add the slide template content into the new slide. newSlidePart.FeedData(inputSlide.GetStream(FileMode.Open)); //Make sure the new slide references the proper slide layout. newSlidePart.AddPart(inputSlide.SlideLayoutPart); //Get the list of slide ids. SlideIdList slideIdList = presentationPart.Presentation.SlideIdList; //Deternmine where to add the next slide (find max number of slides). uint maxSlideId = 1; SlideId prevSlideId = null; foreach (SlideId slideID in slideIdList.ChildElements) { if (slideID.Id > maxSlideId) { maxSlideId = slideID.Id; prevSlideId = slideID; } } maxSlideId++; //Add the new slide at the end of the deck. SlideId newSlideId = slideIdList.InsertAfter(new SlideId(), prevSlideId); //Make sure the id and relid are set appropriately. newSlideId.Id = maxSlideId; newSlideId.RelationshipId = presentationPart.GetIdOfPart(newSlidePart); return(newSlidePart); }
/// <summary> /// Gets a <see cref="SlidePart"/> instance by slide index. /// </summary> /// <param name="prePart"></param> /// <param name="sldIndex"></param> /// <returns></returns> public static SlidePart GetSlidePartByIndex(this PresentationPart prePart, int sldIndex) { // Get the collection of slide IDs OpenXmlElementList slideIds = prePart.Presentation.SlideIdList.ChildElements; string relId = ((SlideId)slideIds[sldIndex]).RelationshipId; // Get the specified slide part from the relationship ID SlidePart slidePart = (SlidePart)prePart.GetPartById(relId); return(slidePart); }
// Get the slide part of the first slide in the presentation document. private static SlidePart GetFirstSlide(PresentationDocument presentationDocument) { // Get relationship ID of the first slide PresentationPart part = presentationDocument.PresentationPart; SlideId slideId = part.Presentation.SlideIdList.GetFirstChild <SlideId>(); string relId = slideId.RelationshipId; // Get the slide part by the relationship ID. SlidePart slidePart = (SlidePart)part.GetPartById(relId); return(slidePart); }