// Delete the specified slide from the presentation. public static void DeleteSlide(PresentationDocument presentationDocument, int slideIndex) { if (presentationDocument == null) { throw new ArgumentNullException("presentationDocument"); } // Use the CountSlides sample to get the number of slides in the presentation. int slidesCount = CountSlides(presentationDocument); if (slideIndex < 0 || slideIndex >= slidesCount) { throw new ArgumentOutOfRangeException("slideIndex"); } // Get the presentation part from the presentation document. PresentationPart presentationPart = presentationDocument.PresentationPart; // Get the presentation from the presentation part. Presentation presentation = presentationPart.Presentation; // Get the list of slide IDs in the presentation. SlideIdList slideIdList = presentation.SlideIdList; // Get the slide ID of the specified slide SlideId slideId = slideIdList.ChildElements[slideIndex] as SlideId; // Get the relationship ID of the slide. string slideRelId = slideId.RelationshipId; // Remove the slide from the slide list. slideIdList.RemoveChild(slideId); // // Remove references to the slide from all custom shows. if (presentation.CustomShowList != null) { // Iterate through the list of custom shows. foreach (var customShow in presentation.CustomShowList.Elements <CustomShow>()) { if (customShow.SlideList != null) { // Declare a link list of slide list entries. LinkedList <SlideListEntry> slideListEntries = new LinkedList <SlideListEntry>(); foreach (SlideListEntry slideListEntry in customShow.SlideList.Elements()) { // Find the slide reference to remove from the custom show. if (slideListEntry.Id != null && slideListEntry.Id == slideRelId) { slideListEntries.AddLast(slideListEntry); } } // Remove all references to the slide from the custom show. foreach (SlideListEntry slideListEntry in slideListEntries) { customShow.SlideList.RemoveChild(slideListEntry); } } } } // Save the modified presentation. presentation.Save(); // Get the slide part for the specified slide. SlidePart slidePart = presentationPart.GetPartById(slideRelId) as SlidePart; // Remove the slide part. presentationPart.DeletePart(slidePart); }
// Insert the specified slide into the presentation at the specified position. public static Slide InsertNewSlide(PresentationDocument presentationDocument, int position, string slideTitle, FileInfo ChartFile) { if (presentationDocument == null) { throw new ArgumentNullException("presentationDocument"); } if (slideTitle == null) { throw new ArgumentNullException("slideTitle"); } PresentationPart presentationPart = presentationDocument.PresentationPart; // Verify that the presentation is not empty. if (presentationPart == null) { throw new InvalidOperationException("The presentation document is empty."); } // Declare and instantiate a new slide. Slide slide = new Slide(new CommonSlideData(new ShapeTree())); //uint drawingObjectId = 1; // Construct the slide content. // Specify the non-visual properties of the new slide. NonVisualGroupShapeProperties nonVisualProperties = slide.CommonSlideData.ShapeTree.AppendChild(new NonVisualGroupShapeProperties()); nonVisualProperties.NonVisualDrawingProperties = new NonVisualDrawingProperties() { Id = 1, Name = "" }; nonVisualProperties.NonVisualGroupShapeDrawingProperties = new NonVisualGroupShapeDrawingProperties(); nonVisualProperties.ApplicationNonVisualDrawingProperties = new ApplicationNonVisualDrawingProperties(); // Specify the group shape properties of the new slide. slide.CommonSlideData.ShapeTree.AppendChild(new GroupShapeProperties()); //// Declare and instantiate the title shape of the new slide. //DocumentFormat.OpenXml.Presentation.Shape titleShape = slide.CommonSlideData.ShapeTree.AppendChild(new DocumentFormat.OpenXml.Presentation.Shape()); //drawingObjectId++; //// Specify the required shape properties for the title shape. //titleShape.NonVisualShapeProperties = new NonVisualShapeProperties // (new NonVisualDrawingProperties() { Id = drawingObjectId, Name = "Title" }, // new NonVisualShapeDrawingProperties(new Drawing.ShapeLocks() { NoGrouping = true }), // new ApplicationNonVisualDrawingProperties(new PlaceholderShape() { Type = PlaceholderValues.Title })); //titleShape.ShapeProperties = new DocumentFormat.OpenXml.Presentation.ShapeProperties(); //// Specify the text of the title shape. //titleShape.TextBody = new TextBody(new Drawing.BodyProperties(), // new Drawing.ListStyle(), // new Drawing.Paragraph(new Drawing.Run(new Drawing.Text() { Text = slideTitle }))); //// Declare and instantiate the body shape of the new slide. //DocumentFormat.OpenXml.Presentation.Shape bodyShape = slide.CommonSlideData.ShapeTree.AppendChild(new DocumentFormat.OpenXml.Presentation.Shape()); //drawingObjectId++; //// Specify the required shape properties for the body shape. //bodyShape.NonVisualShapeProperties = new NonVisualShapeProperties(new NonVisualDrawingProperties() { Id = drawingObjectId, Name = "Content Placeholder" }, // new NonVisualShapeDrawingProperties(new Drawing.ShapeLocks() { NoGrouping = true }), // new ApplicationNonVisualDrawingProperties(new PlaceholderShape() { Index = 1 })); //bodyShape.ShapeProperties = new DocumentFormat.OpenXml.Presentation.ShapeProperties(); //// Specify the text of the body shape. //bodyShape.TextBody = new TextBody(new Drawing.BodyProperties(), // new Drawing.ListStyle(), // new Drawing.Paragraph()); // Declare and instantiate the body shape of the new slide. //DocumentFormat.OpenXml.Presentation.Shape ImgShape = slide.CommonSlideData.ShapeTree.AppendChild(new DocumentFormat.OpenXml.Presentation.Shape()); //drawingObjectId++; // Specify the required shape properties for the body shape. //ImgShape.NonVisualShapeProperties = new NonVisualShapeProperties(new NonVisualDrawingProperties() { Id = drawingObjectId, Name = "" }, // new NonVisualGroupShapeDrawingProperties(), // new ApplicationNonVisualDrawingProperties()); //ImgShape.ShapeProperties = new DocumentFormat.OpenXml.Presentation.ShapeProperties(); // Specify the text of the body shape. //ImgShape.TextBody = new TextBody(new Drawing.BodyProperties(), // new Drawing.ListStyle(), // new Drawing.Paragraph()); //Drawing.Picture picture = new Drawing.Picture(); ////string embedId = string.Empty; //string embedId = "rId" + (slide.Elements().Count() + 915).ToString(); //Drawing.NonVisualPictureProperties nonVisualPictureProperties = new Drawing.NonVisualPictureProperties( // new Drawing.NonVisualDrawingProperties() { Id = (UInt32Value)4U, Name = "Picture" + ChartFile.Name }, // new Drawing.NonVisualPictureDrawingProperties(new Drawing.PictureLocks() { NoChangeAspect = true }), // new ApplicationNonVisualDrawingProperties()); //BlipFill blipFill1 = new BlipFill(); //Drawing.Blip blip1 = new Drawing.Blip() { Embed = embedId, CompressionState = Drawing.BlipCompressionValues.Print }; //// Creates an BlipExtensionList instance and adds its children //Drawing.BlipExtensionList blipExtensionList = new Drawing.BlipExtensionList(); //Drawing.BlipExtension blipExtension = new Drawing.BlipExtension() { Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}" }; //A14.UseLocalDpi useLocalDpi = new A14.UseLocalDpi() { Val = false }; //useLocalDpi.AddNamespaceDeclaration("a14", // "http://schemas.microsoft.com/office/drawing/2010/main"); //blipExtension.Append(useLocalDpi); //blipExtensionList.Append(blipExtension); //blip1.Append(blipExtensionList); //Drawing.Stretch stretch1 = new Drawing.Stretch(); //Drawing.FillRectangle fillRectangle1 = new Drawing.FillRectangle(); //stretch1.Append(fillRectangle1); //blipFill1.Append(blip1); //blipFill1.Append(stretch1); //Drawing.ShapeProperties shapeProperties = new Drawing.ShapeProperties(); //Drawing.Transform2D transform2D = new Drawing.Transform2D(); //Drawing.Offset offset = new Drawing.Offset() { X = 0L, Y = 0L }; //Drawing.Extents extents = new Drawing.Extents() { Cx = 8000000L, Cy = 6000000L }; //transform2D.Append(offset); //transform2D.Append(extents); //Drawing.PresetGeometry presetGeometry = new Drawing.PresetGeometry() { Preset = Drawing.ShapeTypeValues.Rectangle }; //Drawing.AdjustValueList adjustValueList = new Drawing.AdjustValueList(); //presetGeometry.Append(adjustValueList); //shapeProperties.Append(transform2D); //shapeProperties.Append(presetGeometry); //picture.Append(nonVisualPictureProperties); //picture.Append(blipFill1); //picture.Append(shapeProperties); //slide.CommonSlideData.ShapeTree.AppendChild(picture); // Create the slide part for the new slide. SlidePart slidePart = presentationPart.AddNewPart <SlidePart>(); // Save the new slide part. slide.Save(slidePart); // Modify the slide ID list in the presentation part. // The slide ID list should not be null. SlideIdList slideIdList = presentationPart.Presentation.SlideIdList; // Find the highest slide ID in the current list. uint maxSlideId = 1; SlideId prevSlideId = null; foreach (SlideId slideId in slideIdList.ChildElements) { if (slideId.Id > maxSlideId) { maxSlideId = slideId.Id; } position--; if (position == 0) { prevSlideId = slideId; } } maxSlideId++; // Get the ID of the previous slide. SlidePart lastSlidePart; if (prevSlideId != null) { lastSlidePart = (SlidePart)presentationPart.GetPartById(prevSlideId.RelationshipId); } else { lastSlidePart = (SlidePart)presentationPart.GetPartById(((SlideId)(slideIdList.ChildElements[0])).RelationshipId); } // Use the same slide layout as that of the previous slide. if (null != lastSlidePart.SlideLayoutPart) { slidePart.AddPart(lastSlidePart.SlideLayoutPart); } // Insert the new slide into the slide list after the previous slide. SlideId newSlideId = slideIdList.InsertAfter(new SlideId(), prevSlideId); newSlideId.Id = maxSlideId; newSlideId.RelationshipId = presentationPart.GetIdOfPart(slidePart); // Save the modified presentation. //presentationPart.Presentation.Save(); return(GetSlideByRelationShipId(presentationPart, newSlideId.RelationshipId)); }
private static void AddImage(string file, List <ImagePath> image) { using (var presentationDocument = PresentationDocument.Open(file, true)) { var slideCount = presentationDocument.PresentationPart.SlideParts.Count(); SlideIdList slideIdList = presentationDocument.PresentationPart.Presentation.SlideIdList; Presentation presentation = presentationDocument.PresentationPart.Presentation; PresentationPart presentationPart = presentationDocument.PresentationPart; OpenXmlElementList slideIds = presentationPart.Presentation.SlideIdList.ChildElements; int cnt = 0; // 画像の添付数 int j = 0; // 画像添付スライド位置 string relId = (slideIds[j] as SlideId).RelationshipId; SlidePart slidePart = (SlidePart)presentationPart.GetPartById(relId); foreach (ImagePath imgPath in image) { ImagePart part = slidePart .AddImagePart(ImagePartType.Png); using (var stream = File.OpenRead(imgPath.Path)) { part.FeedData(stream); } var tree = slidePart .Slide .Descendants <DocumentFormat.OpenXml.Presentation.ShapeTree>() .First(); var picture = new DocumentFormat.OpenXml.Presentation.Picture(); picture.NonVisualPictureProperties = new DocumentFormat.OpenXml.Presentation.NonVisualPictureProperties(); picture.NonVisualPictureProperties.Append(new DocumentFormat.OpenXml.Presentation.NonVisualDrawingProperties { Name = "My Shape", Id = (UInt32)tree.ChildElements.Count - 1 }); var nonVisualPictureDrawingProperties = new DocumentFormat.OpenXml.Presentation.NonVisualPictureDrawingProperties(); nonVisualPictureDrawingProperties.Append(new Drawing2.PictureLocks() { NoChangeAspect = true }); picture.NonVisualPictureProperties.Append(nonVisualPictureDrawingProperties); picture.NonVisualPictureProperties.Append(new DocumentFormat.OpenXml.Presentation.ApplicationNonVisualDrawingProperties()); var blipFill = new DocumentFormat.OpenXml.Presentation.BlipFill(); var blip1 = new Drawing2.Blip() { Embed = slidePart.GetIdOfPart(part) }; var blipExtensionList1 = new Drawing2.BlipExtensionList(); var blipExtension1 = new Drawing2.BlipExtension() { Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}" }; var useLocalDpi1 = new DocumentFormat.OpenXml.Office2010.Drawing.UseLocalDpi() { Val = false }; useLocalDpi1.AddNamespaceDeclaration("a14", "http://schemas.microsoft.com/office/drawing/2010/main"); blipExtension1.Append(useLocalDpi1); blipExtensionList1.Append(blipExtension1); blip1.Append(blipExtensionList1); var stretch = new Drawing2.Stretch(); stretch.Append(new Drawing2.FillRectangle()); blipFill.Append(blip1); blipFill.Append(stretch); picture.Append(blipFill); picture.ShapeProperties = new DocumentFormat.OpenXml.Presentation.ShapeProperties(); picture.ShapeProperties.Transform2D = new Drawing2.Transform2D(); int rotation = 0; switch (imgPath.Rot) { case RotateFlipType.RotateNoneFlipNone: rotation = 0; picture.ShapeProperties.Transform2D.Append(new Drawing2.Offset { X = POSITION0[cnt % 6].X, Y = POSITION0[cnt % 6].Y, }); break; case RotateFlipType.Rotate180FlipNone: // 時計回りに180度回転しているので、180度回転して戻す rotation = 60000 * 180; picture.ShapeProperties.Transform2D.Append(new Drawing2.Offset { X = POSITION0[cnt % 6].X, Y = POSITION0[cnt % 6].Y, }); break; case RotateFlipType.Rotate90FlipNone: // 時計回りに270度回転しているので、90度回転して戻す rotation = 60000 * 90; picture.ShapeProperties.Transform2D.Append(new Drawing2.Offset { X = POSITION90[cnt % 6].X, Y = POSITION90[cnt % 6].Y, }); break; case RotateFlipType.Rotate270FlipNone: // 時計回りに90度回転しているので、270度回転して戻す rotation = 60000 * 270; picture.ShapeProperties.Transform2D.Append(new Drawing2.Offset { X = POSITION90[cnt % 6].X, Y = POSITION90[cnt % 6].Y, }); break; default: rotation = 0; picture.ShapeProperties.Transform2D.Append(new Drawing2.Offset { X = POSITION0[cnt % 6].X, Y = POSITION0[cnt % 6].Y, }); break; } picture.ShapeProperties.Transform2D.Rotation = rotation; // 縦向き picture.ShapeProperties.Transform2D.Append(new Drawing2.Extents { Cx = imgPath.Size.Width, Cy = imgPath.Size.Height, }); picture.ShapeProperties.Append(new Drawing2.PresetGeometry { Preset = Drawing2.ShapeTypeValues.Rectangle }); tree.Append(picture); if (cnt % 6 == 5) { if (j < slideCount - 1) { j++; relId = (slideIds[j] as SlideId).RelationshipId; slidePart = (SlidePart)presentationPart.GetPartById(relId); } else { // 画像ループを抜ける break; } } cnt++; } // スライドを削除する for (int i = slideCount - 1; i > j; i--) { //Console.WriteLine(i); SlideId slideId = slideIds[i] as SlideId; string slideRelId = slideId.RelationshipId; slideIdList.RemoveChild(slideId); if (presentation.CustomShowList != null) { // Iterate through the list of custom shows. foreach (var customShow in presentation.CustomShowList.Elements <CustomShow>()) { if (customShow.SlideList != null) { // Declare a link list of slide list entries. LinkedList <SlideListEntry> slideListEntries = new LinkedList <SlideListEntry>(); foreach (SlideListEntry slideListEntry in customShow.SlideList.Elements()) { // Find the slide reference to remove from the custom show. if (slideListEntry.Id != null && slideListEntry.Id == slideRelId) { slideListEntries.AddLast(slideListEntry); } } // Remove all references to the slide from the custom show. foreach (SlideListEntry slideListEntry in slideListEntries) { customShow.SlideList.RemoveChild(slideListEntry); } } } } presentation.Save(); SlidePart slidePart2 = presentationPart.GetPartById(slideRelId) as SlidePart; presentationPart.DeletePart(slidePart2); } } }
public static MemoryStream CreatePptxCertificates(Course course, string sourceFile) { const string fullNameTxt = "FullName"; byte[] byteArray = File.ReadAllBytes(sourceFile); MemoryStream stream = new MemoryStream(); stream.Write(byteArray, 0, byteArray.Length); using (PresentationDocument document = PresentationDocument.Open(stream, true)) { var master1LayoutPart = document.PresentationPart.SlideMasterParts.First() .SlideLayoutParts.First(); #region MasterSlide var formattedDate = course.StartFacultyLocal.ToString("dddd, MMMM dd, yyyy", course.Department.Institution.Culture.CultureInfo); var organisers = course.CourseParticipants.Where(cp => cp.IsOrganiser).ToList(); var organiserCells = new List <OrganiserXml>(organisers.Count); organiserCells.Add(new OrganiserXml()); Draw.Text fullName = null; foreach (var t in master1LayoutPart.SlideLayout.Descendants <Draw.Text>()) { t.Text = t.Text .Replace("CourseTypeDescription", course.CourseFormat.CourseType.Description) .Replace("CourseTypeAbbreviation", course.CourseFormat.CourseType.Abbreviation) .Replace("CourseFormatDescription", course.CourseFormat.Description) .Replace("DepartmentName", course.Department.Name) .Replace("DepartmentAbbreviation", course.Department.Abbreviation) .Replace("InstitutionName", course.Department.Institution.Name) .Replace("InstitutionAbbreviation", course.Department.Institution.Abbreviation) .Replace("StartDate", formattedDate); if (t.Text.Contains(organiserName)) { organiserCells[0].NameCell = t.FindFirstAncestor <Draw.TableCell>(); } if (t.Text.Contains(organiserRole)) { organiserCells[0].RoleCell = t.FindFirstAncestor <Draw.TableCell>(); } if (t.Text.Contains(fullNameTxt)) { fullName = t; } } if (organisers.Count > 0) { var parentTable = (organiserCells[0].NameCell ?? organiserCells[0].RoleCell).FindFirstAncestor <Draw.Table>(); var grid = parentTable.ChildElements <Draw.TableGrid>().First(); var gridCols = grid.ChildElements <Draw.GridColumn>().ToList(); var tableWidth = gridCols.Sum(gc => gc.Width); var requiredColWidths = (tableWidth / organisers.Count); //assuming for now table with 1 col for (int i = 1; i < organisers.Count; i++) { grid.AppendChild(new Draw.GridColumn() { Width = requiredColWidths }); organiserCells.Add(organiserCells[0].Clone()); } for (int i = 0; i < organisers.Count; i++) { organiserCells[i].SetText(organisers[i].Participant.FullName, organisers[i].Participant.ProfessionalRole.Description); } } ; #endregion //MasterSlide #region certificates var participants = (from cp in course.CourseParticipants where !cp.IsFaculty orderby cp.Participant.FullName select cp.Participant.FullName).ToList(); //find equivalent id in first slide document.PresentationPart.DeleteParts(document.PresentationPart.SlideParts); SlideIdList slideIdList = document.PresentationPart.Presentation.SlideIdList; foreach (var c in slideIdList.ChildElements) { c.Remove(); } document.PresentationPart.Presentation.Save(); foreach (var part in participants) { IEnumerable <Shape> placeholderShapes; AppendNewSlide(document.PresentationPart, master1LayoutPart, out placeholderShapes); foreach (var r in (from phs in placeholderShapes from dr in phs.Descendants <Draw.Run>() where dr.Text.Text.IndexOf(fullNameTxt, StringComparison.OrdinalIgnoreCase) > -1 select dr)) { //should probably be using innerText and xml powertools regex replace r.Text.Text = r.Text.Text.Replace(fullNameTxt, part); } } document.PresentationPart.Presentation.Save(); //find equivalent id in 1st slide #endregion //certificates } return(stream); }
// Insert the specified slide into the presentation at the specified position. public static void InsertNewSlide(PresentationDocument presentationDocument, int position, string slideTitle, string slideContent) { if (presentationDocument == null) { throw new ArgumentNullException("presentationDocument"); } if (slideTitle == null) { throw new ArgumentNullException("slideTitle"); } PresentationPart presentationPart = presentationDocument.PresentationPart; // Verify that the presentation is not empty. if (presentationPart == null) { throw new InvalidOperationException("The presentation document is empty."); } // Declare and instantiate a new slide. Slide slide = new Slide(new CommonSlideData(new ShapeTree())); uint drawingObjectId = 1; // Construct the slide content. // Specify the non-visual properties of the new slide. NonVisualGroupShapeProperties nonVisualProperties = slide.CommonSlideData.ShapeTree.AppendChild(new NonVisualGroupShapeProperties()); nonVisualProperties.NonVisualDrawingProperties = new NonVisualDrawingProperties() { Id = 1, Name = "" }; nonVisualProperties.NonVisualGroupShapeDrawingProperties = new NonVisualGroupShapeDrawingProperties(); nonVisualProperties.ApplicationNonVisualDrawingProperties = new ApplicationNonVisualDrawingProperties(); // Specify the group shape properties of the new slide. slide.CommonSlideData.ShapeTree.AppendChild(new GroupShapeProperties()); // Declare and instantiate the title shape of the new slide. Shape titleShape = slide.CommonSlideData.ShapeTree.AppendChild(new Shape()); drawingObjectId++; // Specify the required shape properties for the title shape. titleShape.NonVisualShapeProperties = new NonVisualShapeProperties (new NonVisualDrawingProperties() { Id = drawingObjectId, Name = "Title" }, new NonVisualShapeDrawingProperties(new Drawing.ShapeLocks() { NoGrouping = true }), new ApplicationNonVisualDrawingProperties(new PlaceholderShape() { Index = 0, Type = PlaceholderValues.Title })); titleShape.ShapeProperties = new ShapeProperties(); // Specify the text of the title shape. titleShape.TextBody = new TextBody(new Drawing.BodyProperties(), new Drawing.ListStyle(), new Drawing.Paragraph(new Drawing.Run(new Drawing.Text() { Text = slideTitle }))); // Declare and instantiate the body shape of the new slide. Shape bodyShape = slide.CommonSlideData.ShapeTree.AppendChild(new Shape()); drawingObjectId++; // Specify the required shape properties for the body shape. bodyShape.NonVisualShapeProperties = new NonVisualShapeProperties (new NonVisualDrawingProperties() { Id = drawingObjectId, Name = "Content" }, new NonVisualShapeDrawingProperties(new Drawing.ShapeLocks() { NoGrouping = true }), new ApplicationNonVisualDrawingProperties(new PlaceholderShape() { Index = 1 })); bodyShape.ShapeProperties = new ShapeProperties(); // Specify the text of the body shape. bodyShape.TextBody = new TextBody(new Drawing.BodyProperties(), new Drawing.ListStyle(), new Drawing.Paragraph(new Drawing.Run(new Drawing.Text() { Text = slideContent }))); // Create the slide part for the new slide. SlidePart slidePart = presentationPart.AddNewPart <SlidePart>(); // Save the new slide part. slide.Save(slidePart); // Modify the slide ID list in the presentation part. // The slide ID list should not be null. SlideIdList slideIdList = presentationPart.Presentation.SlideIdList; // Find the highest slide ID in the current list. uint maxSlideId = 1; SlideId prevSlideId = null; foreach (SlideId slideId in slideIdList.ChildElements) { if (slideId.Id > maxSlideId) { maxSlideId = slideId.Id; } position--; if (position == 0) { prevSlideId = slideId; } } maxSlideId++; // Get the ID of the previous slide. SlidePart lastSlidePart; if (prevSlideId != null) { lastSlidePart = (SlidePart)presentationPart.GetPartById(prevSlideId.RelationshipId); } else { lastSlidePart = (SlidePart)presentationPart.GetPartById(((SlideId)(slideIdList.ChildElements[0])).RelationshipId); } // Use the same slide layout as that of the previous slide. if (null != lastSlidePart.SlideLayoutPart) { slidePart.AddPart(lastSlidePart.SlideLayoutPart); } // Insert the new slide into the slide list after the previous slide. SlideId newSlideId = slideIdList.InsertAfter(new SlideId(), prevSlideId); newSlideId.Id = maxSlideId; newSlideId.RelationshipId = presentationPart.GetIdOfPart(slidePart); // Save the modified presentation. presentationPart.Presentation.Save(); }
// Insert the specified slide into the presentation at the specified position. public void InsertNewSlide(PresentationPart presentationPart) { // Verify that the presentation is not empty. if (presentationPart == null) { throw new InvalidOperationException("The presentation document is empty."); } // Declare and instantiate a new slide. Slide slide = new Slide(new CommonSlideData(new P.ShapeTree())); // Specify the group shape properties of the new slide. slide.CommonSlideData.ShapeTree.AppendChild(new P.GroupShapeProperties()); // Create the slide part for the new slide. SlidePart slidePart = presentationPart.AddNewPart <SlidePart>(); // Save the new slide part. slide.Save(slidePart); // Modify the slide ID list in the presentation part. // The slide ID list should not be null. SlideIdList slideIdList = presentationPart.Presentation.SlideIdList; // Find the highest slide ID in the current list. uint maxSlideId = Convert.ToUInt32(slideIdList.ChildElements.Count()); int position = slideIdList.ChildElements.Count(); SlideId prevSlideId = null; foreach (SlideId slideId in slideIdList.ChildElements) { if (slideId.Id > maxSlideId) { maxSlideId = slideId.Id; } position--; if (position == 0) { prevSlideId = slideId; } } maxSlideId++; // Get the ID of the previous slide. SlidePart lastSlidePart; if (prevSlideId != null) { lastSlidePart = (SlidePart)presentationPart.GetPartById(prevSlideId.RelationshipId); } else { lastSlidePart = (SlidePart)presentationPart.GetPartById(((SlideId)(slideIdList.ChildElements[0])).RelationshipId); } // Use the same slide layout as that of the previous slide. if (null != lastSlidePart.SlideLayoutPart) { slidePart.AddPart(lastSlidePart.SlideLayoutPart); } // Insert the new slide into the slide list after the previous slide. SlideId newSlideId = slideIdList.InsertAfter(new SlideId(), prevSlideId); newSlideId.Id = maxSlideId; newSlideId.RelationshipId = presentationPart.GetIdOfPart(slidePart); // Save the modified presentation. presentationPart.Presentation.Save(); }
public static void GeneratePresentationPart1Content(PresentationPart presentationPart1) { Presentation presentation1 = new Presentation() { ShowSpecialPlaceholderOnTitleSlide = false, RemovePersonalInfoOnSave = true, StrictFirstAndLastChars = false, SaveSubsetFonts = true }; presentation1.AddNamespaceDeclaration("a", "http://schemas.openxmlformats.org/drawingml/2006/main"); presentation1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships"); presentation1.AddNamespaceDeclaration("p", "http://schemas.openxmlformats.org/presentationml/2006/main"); SlideMasterIdList slideMasterIdList1 = new SlideMasterIdList(); SlideMasterId slideMasterId1 = new SlideMasterId() { Id = (UInt32Value)2147483648U, RelationshipId = "rId4" }; slideMasterIdList1.Append(slideMasterId1); NotesMasterIdList notesMasterIdList1 = new NotesMasterIdList(); NotesMasterId notesMasterId1 = new NotesMasterId() { Id = "rId6" }; notesMasterIdList1.Append(notesMasterId1); HandoutMasterIdList handoutMasterIdList1 = new HandoutMasterIdList(); HandoutMasterId handoutMasterId1 = new HandoutMasterId() { Id = "rId7" }; handoutMasterIdList1.Append(handoutMasterId1); SlideIdList slideIdList1 = new SlideIdList(); SlideId slideId1 = new SlideId() { Id = (UInt32Value)371U, RelationshipId = "rId5" }; slideIdList1.Append(slideId1); SlideSize slideSize1 = new SlideSize() { Cx = 9906000, Cy = 6858000, Type = SlideSizeValues.A4 }; NotesSize notesSize1 = new NotesSize() { Cx = 6797675L, Cy = 9874250L }; DefaultTextStyle defaultTextStyle1 = new DefaultTextStyle(); A.DefaultParagraphProperties defaultParagraphProperties1 = new A.DefaultParagraphProperties(); A.DefaultRunProperties defaultRunProperties1 = new A.DefaultRunProperties() { Language = "en-US" }; defaultParagraphProperties1.Append(defaultRunProperties1); A.Level1ParagraphProperties level1ParagraphProperties1 = new A.Level1ParagraphProperties() { Alignment = A.TextAlignmentTypeValues.Left, RightToLeft = false, EastAsianLineBreak = false, FontAlignment = A.TextFontAlignmentValues.Baseline, Height = false }; A.SpaceBefore spaceBefore1 = new A.SpaceBefore(); A.SpacingPercent spacingPercent1 = new A.SpacingPercent() { Val = 0 }; spaceBefore1.Append(spacingPercent1); A.SpaceAfter spaceAfter1 = new A.SpaceAfter(); A.SpacingPercent spacingPercent2 = new A.SpacingPercent() { Val = 0 }; spaceAfter1.Append(spacingPercent2); A.DefaultRunProperties defaultRunProperties2 = new A.DefaultRunProperties() { FontSize = 2400, Kerning = 1200 }; A.SolidFill solidFill1 = new A.SolidFill(); A.SchemeColor schemeColor1 = new A.SchemeColor() { Val = A.SchemeColorValues.Text1 }; solidFill1.Append(schemeColor1); A.LatinFont latinFont1 = new A.LatinFont() { Typeface = "Arial", Panose = "020B0604020202020204", PitchFamily = 34, CharacterSet = 0 }; A.EastAsianFont eastAsianFont1 = new A.EastAsianFont() { Typeface = "MS PGothic", Panose = "020B0600070205080204", PitchFamily = 34, CharacterSet = -128 }; A.ComplexScriptFont complexScriptFont1 = new A.ComplexScriptFont() { Typeface = "+mn-cs" }; defaultRunProperties2.Append(solidFill1); defaultRunProperties2.Append(latinFont1); defaultRunProperties2.Append(eastAsianFont1); defaultRunProperties2.Append(complexScriptFont1); level1ParagraphProperties1.Append(spaceBefore1); level1ParagraphProperties1.Append(spaceAfter1); level1ParagraphProperties1.Append(defaultRunProperties2); A.Level2ParagraphProperties level2ParagraphProperties1 = new A.Level2ParagraphProperties() { LeftMargin = 457200, Alignment = A.TextAlignmentTypeValues.Left, RightToLeft = false, EastAsianLineBreak = false, FontAlignment = A.TextFontAlignmentValues.Baseline, Height = false }; A.SpaceBefore spaceBefore2 = new A.SpaceBefore(); A.SpacingPercent spacingPercent3 = new A.SpacingPercent() { Val = 0 }; spaceBefore2.Append(spacingPercent3); A.SpaceAfter spaceAfter2 = new A.SpaceAfter(); A.SpacingPercent spacingPercent4 = new A.SpacingPercent() { Val = 0 }; spaceAfter2.Append(spacingPercent4); A.DefaultRunProperties defaultRunProperties3 = new A.DefaultRunProperties() { FontSize = 2400, Kerning = 1200 }; A.SolidFill solidFill2 = new A.SolidFill(); A.SchemeColor schemeColor2 = new A.SchemeColor() { Val = A.SchemeColorValues.Text1 }; solidFill2.Append(schemeColor2); A.LatinFont latinFont2 = new A.LatinFont() { Typeface = "Arial", Panose = "020B0604020202020204", PitchFamily = 34, CharacterSet = 0 }; A.EastAsianFont eastAsianFont2 = new A.EastAsianFont() { Typeface = "MS PGothic", Panose = "020B0600070205080204", PitchFamily = 34, CharacterSet = -128 }; A.ComplexScriptFont complexScriptFont2 = new A.ComplexScriptFont() { Typeface = "+mn-cs" }; defaultRunProperties3.Append(solidFill2); defaultRunProperties3.Append(latinFont2); defaultRunProperties3.Append(eastAsianFont2); defaultRunProperties3.Append(complexScriptFont2); level2ParagraphProperties1.Append(spaceBefore2); level2ParagraphProperties1.Append(spaceAfter2); level2ParagraphProperties1.Append(defaultRunProperties3); A.Level3ParagraphProperties level3ParagraphProperties1 = new A.Level3ParagraphProperties() { LeftMargin = 914400, Alignment = A.TextAlignmentTypeValues.Left, RightToLeft = false, EastAsianLineBreak = false, FontAlignment = A.TextFontAlignmentValues.Baseline, Height = false }; A.SpaceBefore spaceBefore3 = new A.SpaceBefore(); A.SpacingPercent spacingPercent5 = new A.SpacingPercent() { Val = 0 }; spaceBefore3.Append(spacingPercent5); A.SpaceAfter spaceAfter3 = new A.SpaceAfter(); A.SpacingPercent spacingPercent6 = new A.SpacingPercent() { Val = 0 }; spaceAfter3.Append(spacingPercent6); A.DefaultRunProperties defaultRunProperties4 = new A.DefaultRunProperties() { FontSize = 2400, Kerning = 1200 }; A.SolidFill solidFill3 = new A.SolidFill(); A.SchemeColor schemeColor3 = new A.SchemeColor() { Val = A.SchemeColorValues.Text1 }; solidFill3.Append(schemeColor3); A.LatinFont latinFont3 = new A.LatinFont() { Typeface = "Arial", Panose = "020B0604020202020204", PitchFamily = 34, CharacterSet = 0 }; A.EastAsianFont eastAsianFont3 = new A.EastAsianFont() { Typeface = "MS PGothic", Panose = "020B0600070205080204", PitchFamily = 34, CharacterSet = -128 }; A.ComplexScriptFont complexScriptFont3 = new A.ComplexScriptFont() { Typeface = "+mn-cs" }; defaultRunProperties4.Append(solidFill3); defaultRunProperties4.Append(latinFont3); defaultRunProperties4.Append(eastAsianFont3); defaultRunProperties4.Append(complexScriptFont3); level3ParagraphProperties1.Append(spaceBefore3); level3ParagraphProperties1.Append(spaceAfter3); level3ParagraphProperties1.Append(defaultRunProperties4); A.Level4ParagraphProperties level4ParagraphProperties1 = new A.Level4ParagraphProperties() { LeftMargin = 1371600, Alignment = A.TextAlignmentTypeValues.Left, RightToLeft = false, EastAsianLineBreak = false, FontAlignment = A.TextFontAlignmentValues.Baseline, Height = false }; A.SpaceBefore spaceBefore4 = new A.SpaceBefore(); A.SpacingPercent spacingPercent7 = new A.SpacingPercent() { Val = 0 }; spaceBefore4.Append(spacingPercent7); A.SpaceAfter spaceAfter4 = new A.SpaceAfter(); A.SpacingPercent spacingPercent8 = new A.SpacingPercent() { Val = 0 }; spaceAfter4.Append(spacingPercent8); A.DefaultRunProperties defaultRunProperties5 = new A.DefaultRunProperties() { FontSize = 2400, Kerning = 1200 }; A.SolidFill solidFill4 = new A.SolidFill(); A.SchemeColor schemeColor4 = new A.SchemeColor() { Val = A.SchemeColorValues.Text1 }; solidFill4.Append(schemeColor4); A.LatinFont latinFont4 = new A.LatinFont() { Typeface = "Arial", Panose = "020B0604020202020204", PitchFamily = 34, CharacterSet = 0 }; A.EastAsianFont eastAsianFont4 = new A.EastAsianFont() { Typeface = "MS PGothic", Panose = "020B0600070205080204", PitchFamily = 34, CharacterSet = -128 }; A.ComplexScriptFont complexScriptFont4 = new A.ComplexScriptFont() { Typeface = "+mn-cs" }; defaultRunProperties5.Append(solidFill4); defaultRunProperties5.Append(latinFont4); defaultRunProperties5.Append(eastAsianFont4); defaultRunProperties5.Append(complexScriptFont4); level4ParagraphProperties1.Append(spaceBefore4); level4ParagraphProperties1.Append(spaceAfter4); level4ParagraphProperties1.Append(defaultRunProperties5); A.Level5ParagraphProperties level5ParagraphProperties1 = new A.Level5ParagraphProperties() { LeftMargin = 1828800, Alignment = A.TextAlignmentTypeValues.Left, RightToLeft = false, EastAsianLineBreak = false, FontAlignment = A.TextFontAlignmentValues.Baseline, Height = false }; A.SpaceBefore spaceBefore5 = new A.SpaceBefore(); A.SpacingPercent spacingPercent9 = new A.SpacingPercent() { Val = 0 }; spaceBefore5.Append(spacingPercent9); A.SpaceAfter spaceAfter5 = new A.SpaceAfter(); A.SpacingPercent spacingPercent10 = new A.SpacingPercent() { Val = 0 }; spaceAfter5.Append(spacingPercent10); A.DefaultRunProperties defaultRunProperties6 = new A.DefaultRunProperties() { FontSize = 2400, Kerning = 1200 }; A.SolidFill solidFill5 = new A.SolidFill(); A.SchemeColor schemeColor5 = new A.SchemeColor() { Val = A.SchemeColorValues.Text1 }; solidFill5.Append(schemeColor5); A.LatinFont latinFont5 = new A.LatinFont() { Typeface = "Arial", Panose = "020B0604020202020204", PitchFamily = 34, CharacterSet = 0 }; A.EastAsianFont eastAsianFont5 = new A.EastAsianFont() { Typeface = "MS PGothic", Panose = "020B0600070205080204", PitchFamily = 34, CharacterSet = -128 }; A.ComplexScriptFont complexScriptFont5 = new A.ComplexScriptFont() { Typeface = "+mn-cs" }; defaultRunProperties6.Append(solidFill5); defaultRunProperties6.Append(latinFont5); defaultRunProperties6.Append(eastAsianFont5); defaultRunProperties6.Append(complexScriptFont5); level5ParagraphProperties1.Append(spaceBefore5); level5ParagraphProperties1.Append(spaceAfter5); level5ParagraphProperties1.Append(defaultRunProperties6); A.Level6ParagraphProperties level6ParagraphProperties1 = new A.Level6ParagraphProperties() { LeftMargin = 2286000, Alignment = A.TextAlignmentTypeValues.Left, DefaultTabSize = 914400, RightToLeft = false, EastAsianLineBreak = true, LatinLineBreak = false, Height = true }; A.DefaultRunProperties defaultRunProperties7 = new A.DefaultRunProperties() { FontSize = 2400, Kerning = 1200 }; A.SolidFill solidFill6 = new A.SolidFill(); A.SchemeColor schemeColor6 = new A.SchemeColor() { Val = A.SchemeColorValues.Text1 }; solidFill6.Append(schemeColor6); A.LatinFont latinFont6 = new A.LatinFont() { Typeface = "Arial", Panose = "020B0604020202020204", PitchFamily = 34, CharacterSet = 0 }; A.EastAsianFont eastAsianFont6 = new A.EastAsianFont() { Typeface = "MS PGothic", Panose = "020B0600070205080204", PitchFamily = 34, CharacterSet = -128 }; A.ComplexScriptFont complexScriptFont6 = new A.ComplexScriptFont() { Typeface = "+mn-cs" }; defaultRunProperties7.Append(solidFill6); defaultRunProperties7.Append(latinFont6); defaultRunProperties7.Append(eastAsianFont6); defaultRunProperties7.Append(complexScriptFont6); level6ParagraphProperties1.Append(defaultRunProperties7); A.Level7ParagraphProperties level7ParagraphProperties1 = new A.Level7ParagraphProperties() { LeftMargin = 2743200, Alignment = A.TextAlignmentTypeValues.Left, DefaultTabSize = 914400, RightToLeft = false, EastAsianLineBreak = true, LatinLineBreak = false, Height = true }; A.DefaultRunProperties defaultRunProperties8 = new A.DefaultRunProperties() { FontSize = 2400, Kerning = 1200 }; A.SolidFill solidFill7 = new A.SolidFill(); A.SchemeColor schemeColor7 = new A.SchemeColor() { Val = A.SchemeColorValues.Text1 }; solidFill7.Append(schemeColor7); A.LatinFont latinFont7 = new A.LatinFont() { Typeface = "Arial", Panose = "020B0604020202020204", PitchFamily = 34, CharacterSet = 0 }; A.EastAsianFont eastAsianFont7 = new A.EastAsianFont() { Typeface = "MS PGothic", Panose = "020B0600070205080204", PitchFamily = 34, CharacterSet = -128 }; A.ComplexScriptFont complexScriptFont7 = new A.ComplexScriptFont() { Typeface = "+mn-cs" }; defaultRunProperties8.Append(solidFill7); defaultRunProperties8.Append(latinFont7); defaultRunProperties8.Append(eastAsianFont7); defaultRunProperties8.Append(complexScriptFont7); level7ParagraphProperties1.Append(defaultRunProperties8); A.Level8ParagraphProperties level8ParagraphProperties1 = new A.Level8ParagraphProperties() { LeftMargin = 3200400, Alignment = A.TextAlignmentTypeValues.Left, DefaultTabSize = 914400, RightToLeft = false, EastAsianLineBreak = true, LatinLineBreak = false, Height = true }; A.DefaultRunProperties defaultRunProperties9 = new A.DefaultRunProperties() { FontSize = 2400, Kerning = 1200 }; A.SolidFill solidFill8 = new A.SolidFill(); A.SchemeColor schemeColor8 = new A.SchemeColor() { Val = A.SchemeColorValues.Text1 }; solidFill8.Append(schemeColor8); A.LatinFont latinFont8 = new A.LatinFont() { Typeface = "Arial", Panose = "020B0604020202020204", PitchFamily = 34, CharacterSet = 0 }; A.EastAsianFont eastAsianFont8 = new A.EastAsianFont() { Typeface = "MS PGothic", Panose = "020B0600070205080204", PitchFamily = 34, CharacterSet = -128 }; A.ComplexScriptFont complexScriptFont8 = new A.ComplexScriptFont() { Typeface = "+mn-cs" }; defaultRunProperties9.Append(solidFill8); defaultRunProperties9.Append(latinFont8); defaultRunProperties9.Append(eastAsianFont8); defaultRunProperties9.Append(complexScriptFont8); level8ParagraphProperties1.Append(defaultRunProperties9); A.Level9ParagraphProperties level9ParagraphProperties1 = new A.Level9ParagraphProperties() { LeftMargin = 3657600, Alignment = A.TextAlignmentTypeValues.Left, DefaultTabSize = 914400, RightToLeft = false, EastAsianLineBreak = true, LatinLineBreak = false, Height = true }; A.DefaultRunProperties defaultRunProperties10 = new A.DefaultRunProperties() { FontSize = 2400, Kerning = 1200 }; A.SolidFill solidFill9 = new A.SolidFill(); A.SchemeColor schemeColor9 = new A.SchemeColor() { Val = A.SchemeColorValues.Text1 }; solidFill9.Append(schemeColor9); A.LatinFont latinFont9 = new A.LatinFont() { Typeface = "Arial", Panose = "020B0604020202020204", PitchFamily = 34, CharacterSet = 0 }; A.EastAsianFont eastAsianFont9 = new A.EastAsianFont() { Typeface = "MS PGothic", Panose = "020B0600070205080204", PitchFamily = 34, CharacterSet = -128 }; A.ComplexScriptFont complexScriptFont9 = new A.ComplexScriptFont() { Typeface = "+mn-cs" }; defaultRunProperties10.Append(solidFill9); defaultRunProperties10.Append(latinFont9); defaultRunProperties10.Append(eastAsianFont9); defaultRunProperties10.Append(complexScriptFont9); level9ParagraphProperties1.Append(defaultRunProperties10); defaultTextStyle1.Append(defaultParagraphProperties1); defaultTextStyle1.Append(level1ParagraphProperties1); defaultTextStyle1.Append(level2ParagraphProperties1); defaultTextStyle1.Append(level3ParagraphProperties1); defaultTextStyle1.Append(level4ParagraphProperties1); defaultTextStyle1.Append(level5ParagraphProperties1); defaultTextStyle1.Append(level6ParagraphProperties1); defaultTextStyle1.Append(level7ParagraphProperties1); defaultTextStyle1.Append(level8ParagraphProperties1); defaultTextStyle1.Append(level9ParagraphProperties1); PresentationExtensionList presentationExtensionList1 = new PresentationExtensionList(); PresentationExtension presentationExtension1 = new PresentationExtension() { Uri = "{EFAFB233-063F-42B5-8137-9DF3F51BA10A}" }; P15.SlideGuideList slideGuideList1 = new P15.SlideGuideList(); slideGuideList1.AddNamespaceDeclaration("p15", "http://schemas.microsoft.com/office/powerpoint/2012/main"); P15.ExtendedGuide extendedGuide1 = new P15.ExtendedGuide() { Id = (UInt32Value)1U, Orientation = DirectionValues.Horizontal, Position = 240 }; P15.ColorType colorType1 = new P15.ColorType(); A.RgbColorModelHex rgbColorModelHex1 = new A.RgbColorModelHex() { Val = "A4A3A4" }; colorType1.Append(rgbColorModelHex1); extendedGuide1.Append(colorType1); P15.ExtendedGuide extendedGuide2 = new P15.ExtendedGuide() { Id = (UInt32Value)2U, Orientation = DirectionValues.Horizontal, Position = 3648, IsUserDrawn = true }; P15.ColorType colorType2 = new P15.ColorType(); A.RgbColorModelHex rgbColorModelHex2 = new A.RgbColorModelHex() { Val = "A4A3A4" }; colorType2.Append(rgbColorModelHex2); extendedGuide2.Append(colorType2); P15.ExtendedGuide extendedGuide3 = new P15.ExtendedGuide() { Id = (UInt32Value)4U, Position = 2736, IsUserDrawn = true }; P15.ColorType colorType3 = new P15.ColorType(); A.RgbColorModelHex rgbColorModelHex3 = new A.RgbColorModelHex() { Val = "A4A3A4" }; colorType3.Append(rgbColorModelHex3); extendedGuide3.Append(colorType3); P15.ExtendedGuide extendedGuide4 = new P15.ExtendedGuide() { Id = (UInt32Value)5U, Position = 5904 }; P15.ColorType colorType4 = new P15.ColorType(); A.RgbColorModelHex rgbColorModelHex4 = new A.RgbColorModelHex() { Val = "A4A3A4" }; colorType4.Append(rgbColorModelHex4); extendedGuide4.Append(colorType4); P15.ExtendedGuide extendedGuide5 = new P15.ExtendedGuide() { Id = (UInt32Value)6U, Position = 336 }; P15.ColorType colorType5 = new P15.ColorType(); A.RgbColorModelHex rgbColorModelHex5 = new A.RgbColorModelHex() { Val = "A4A3A4" }; colorType5.Append(rgbColorModelHex5); extendedGuide5.Append(colorType5); P15.ExtendedGuide extendedGuide6 = new P15.ExtendedGuide() { Id = (UInt32Value)7U, Orientation = DirectionValues.Horizontal, IsUserDrawn = true }; P15.ColorType colorType6 = new P15.ColorType(); A.RgbColorModelHex rgbColorModelHex6 = new A.RgbColorModelHex() { Val = "A4A3A4" }; colorType6.Append(rgbColorModelHex6); extendedGuide6.Append(colorType6); P15.ExtendedGuide extendedGuide7 = new P15.ExtendedGuide() { Id = (UInt32Value)8U, Orientation = DirectionValues.Horizontal, Position = 1152, IsUserDrawn = true }; P15.ColorType colorType7 = new P15.ColorType(); A.RgbColorModelHex rgbColorModelHex7 = new A.RgbColorModelHex() { Val = "A4A3A4" }; colorType7.Append(rgbColorModelHex7); extendedGuide7.Append(colorType7); P15.ExtendedGuide extendedGuide8 = new P15.ExtendedGuide() { Id = (UInt32Value)9U, Orientation = DirectionValues.Horizontal, Position = 1200, IsUserDrawn = true }; P15.ColorType colorType8 = new P15.ColorType(); A.RgbColorModelHex rgbColorModelHex8 = new A.RgbColorModelHex() { Val = "A4A3A4" }; colorType8.Append(rgbColorModelHex8); extendedGuide8.Append(colorType8); P15.ExtendedGuide extendedGuide9 = new P15.ExtendedGuide() { Id = (UInt32Value)10U, Position = 2784, IsUserDrawn = true }; P15.ColorType colorType9 = new P15.ColorType(); A.RgbColorModelHex rgbColorModelHex9 = new A.RgbColorModelHex() { Val = "A4A3A4" }; colorType9.Append(rgbColorModelHex9); extendedGuide9.Append(colorType9); P15.ExtendedGuide extendedGuide10 = new P15.ExtendedGuide() { Id = (UInt32Value)12U, Orientation = DirectionValues.Horizontal, Position = 2448, IsUserDrawn = true }; P15.ColorType colorType10 = new P15.ColorType(); A.RgbColorModelHex rgbColorModelHex10 = new A.RgbColorModelHex() { Val = "A4A3A4" }; colorType10.Append(rgbColorModelHex10); extendedGuide10.Append(colorType10); P15.ExtendedGuide extendedGuide11 = new P15.ExtendedGuide() { Id = (UInt32Value)13U, Orientation = DirectionValues.Horizontal, Position = 2496, IsUserDrawn = true }; P15.ColorType colorType11 = new P15.ColorType(); A.RgbColorModelHex rgbColorModelHex11 = new A.RgbColorModelHex() { Val = "A4A3A4" }; colorType11.Append(rgbColorModelHex11); extendedGuide11.Append(colorType11); P15.ExtendedGuide extendedGuide12 = new P15.ExtendedGuide() { Id = (UInt32Value)14U, Position = 1872, IsUserDrawn = true }; P15.ColorType colorType12 = new P15.ColorType(); A.RgbColorModelHex rgbColorModelHex12 = new A.RgbColorModelHex() { Val = "A4A3A4" }; colorType12.Append(rgbColorModelHex12); extendedGuide12.Append(colorType12); P15.ExtendedGuide extendedGuide13 = new P15.ExtendedGuide() { Id = (UInt32Value)15U, Position = 2688, IsUserDrawn = true }; P15.ColorType colorType13 = new P15.ColorType(); A.RgbColorModelHex rgbColorModelHex13 = new A.RgbColorModelHex() { Val = "A4A3A4" }; colorType13.Append(rgbColorModelHex13); extendedGuide13.Append(colorType13); P15.ExtendedGuide extendedGuide14 = new P15.ExtendedGuide() { Id = (UInt32Value)16U, Orientation = DirectionValues.Horizontal, Position = 288, IsUserDrawn = true }; P15.ColorType colorType14 = new P15.ColorType(); A.RgbColorModelHex rgbColorModelHex14 = new A.RgbColorModelHex() { Val = "A4A3A4" }; colorType14.Append(rgbColorModelHex14); extendedGuide14.Append(colorType14); P15.ExtendedGuide extendedGuide15 = new P15.ExtendedGuide() { Id = (UInt32Value)17U, Orientation = DirectionValues.Horizontal, Position = 1104, IsUserDrawn = true }; P15.ColorType colorType15 = new P15.ColorType(); A.RgbColorModelHex rgbColorModelHex15 = new A.RgbColorModelHex() { Val = "A4A3A4" }; colorType15.Append(rgbColorModelHex15); extendedGuide15.Append(colorType15); slideGuideList1.Append(extendedGuide1); slideGuideList1.Append(extendedGuide2); slideGuideList1.Append(extendedGuide3); slideGuideList1.Append(extendedGuide4); slideGuideList1.Append(extendedGuide5); slideGuideList1.Append(extendedGuide6); slideGuideList1.Append(extendedGuide7); slideGuideList1.Append(extendedGuide8); slideGuideList1.Append(extendedGuide9); slideGuideList1.Append(extendedGuide10); slideGuideList1.Append(extendedGuide11); slideGuideList1.Append(extendedGuide12); slideGuideList1.Append(extendedGuide13); slideGuideList1.Append(extendedGuide14); slideGuideList1.Append(extendedGuide15); presentationExtension1.Append(slideGuideList1); PresentationExtension presentationExtension2 = new PresentationExtension() { Uri = "{2D200454-40CA-4A62-9FC3-DE9A4176ACB9}" }; P15.NotesGuideList notesGuideList1 = new P15.NotesGuideList(); notesGuideList1.AddNamespaceDeclaration("p15", "http://schemas.microsoft.com/office/powerpoint/2012/main"); P15.ExtendedGuide extendedGuide16 = new P15.ExtendedGuide() { Id = (UInt32Value)1U, Orientation = DirectionValues.Horizontal, Position = 3110, IsUserDrawn = true }; P15.ColorType colorType16 = new P15.ColorType(); A.RgbColorModelHex rgbColorModelHex16 = new A.RgbColorModelHex() { Val = "A4A3A4" }; colorType16.Append(rgbColorModelHex16); extendedGuide16.Append(colorType16); P15.ExtendedGuide extendedGuide17 = new P15.ExtendedGuide() { Id = (UInt32Value)2U, Position = 2140, IsUserDrawn = true }; P15.ColorType colorType17 = new P15.ColorType(); A.RgbColorModelHex rgbColorModelHex17 = new A.RgbColorModelHex() { Val = "A4A3A4" }; colorType17.Append(rgbColorModelHex17); extendedGuide17.Append(colorType17); notesGuideList1.Append(extendedGuide16); notesGuideList1.Append(extendedGuide17); presentationExtension2.Append(notesGuideList1); presentationExtensionList1.Append(presentationExtension1); presentationExtensionList1.Append(presentationExtension2); presentation1.Append(slideMasterIdList1); presentation1.Append(notesMasterIdList1); presentation1.Append(handoutMasterIdList1); presentation1.Append(slideIdList1); presentation1.Append(slideSize1); presentation1.Append(notesSize1); presentation1.Append(defaultTextStyle1); presentation1.Append(presentationExtensionList1); presentationPart1.Presentation = presentation1; }
public static void DeleteSlide(string presentationFile) { if (presentationFile == null) { throw new ArgumentNullException("presentationDocument"); } using (PresentationDocument presentationDocument = PresentationDocument.Open(presentationFile, true)) { // Get the presentation part from the presentation document. PresentationPart presentationPart = presentationDocument.PresentationPart; // Get the presentation from the presentation part. DocumentFormat.OpenXml.Presentation.Presentation presentation = presentationPart.Presentation; // Get the list of slide IDs in the presentation. SlideIdList slideIdList = presentation.SlideIdList; int slideIdx = -1; foreach (SlideId _slideId in presentation.SlideIdList) { slideIdx++; // Get the slide ID of the specified slide SlideId slideId = slideIdList.ChildElements[slideIdx] as SlideId; // Get the relationship ID of the slide. string slideRelId = slideId.RelationshipId; // Get the slide part for the specified slide. SlidePart slidePart = presentationPart.GetPartById(slideRelId) as SlidePart; if (slidePart.Slide.Show != null) { if (slidePart.Slide.Show.HasValue != null) { // Remove the slide from the slide list. slideIdList.RemoveChild(slideId); // Remove references to the slide from all custom shows. if (presentation.CustomShowList != null) { // Iterate through the list of custom shows. foreach (var customShow in presentation.CustomShowList.Elements <CustomShow>()) { if (customShow.SlideList != null) { // Declare a link list of slide list entries. LinkedList <SlideListEntry> slideListEntries = new LinkedList <SlideListEntry>(); foreach (SlideListEntry slideListEntry in customShow.SlideList.Elements()) { // Find the slide reference to remove from the custom show. if (slideListEntry.Id != null && slideListEntry.Id == slideRelId) { slideListEntries.AddLast(slideListEntry); } } // Remove all references to the slide from the custom show. foreach (SlideListEntry slideListEntry in slideListEntries) { customShow.SlideList.RemoveChild(slideListEntry); } } } } // Save the modified presentation. presentation.Save(); // Remove the slide part. presentationPart.DeletePart(slidePart); break; } } } } }
public static int PPTReorderSlides(string fileName, int originalPosition, int newPosition) { // Assume that no slide moves; return -1. int returnValue = -1; // Moving to and from same position? Get out now. if (newPosition == originalPosition) { return(returnValue); } using (PresentationDocument doc = PresentationDocument.Open(fileName, true)) { // Get the presentation part of the document. PresentationPart presentationPart = doc.PresentationPart; // No presentation part? Something's wrong with the document. if (presentationPart == null) { throw new ArgumentException("fileName"); } // If you're here, you know that presentationPart exists. int slideCount = presentationPart.SlideParts.Count(); // No slides? Just return -1 indicating that nothing happened. if (slideCount == 0) { return(returnValue); } // There are slides. Calculate real positions. int maxPosition = slideCount - 1; // Adjust the positions, if necessary. CalcPositions(ref originalPosition, ref newPosition, maxPosition); // The two positions could have ended up being the same // thing. There's nothing to do, in that case. Otherwise, // do the work. if (newPosition != originalPosition) { Presentation presentation = presentationPart.Presentation; SlideIdList slideIdList = presentation.SlideIdList; // Get the slide ID of the source and target slides. SlideId sourceSlide = (SlideId)(slideIdList.ChildElements[originalPosition]); SlideId targetSlide = (SlideId)(slideIdList.ChildElements[newPosition]); // Remove the source slide from its parent tree. You can't // move a slide while it's part of an XML node tree. sourceSlide.Remove(); if (newPosition > originalPosition) { slideIdList.InsertAfter(sourceSlide, targetSlide); } else { slideIdList.InsertBefore(sourceSlide, targetSlide); } // Set the return value. returnValue = newPosition; // Save the modified presentation. presentation.Save(); } } return(returnValue); }
public ISlide InsertSlide(ISlideLayout slideLayout, int index) { if (!(slideLayout is IOpenXmlSlideLayout internalSlideLayout)) { throw new ArgumentException(); } Slide slide = new Slide() { // CommonSlideData = slideLayoutRef.SlideLayoutPart.SlideLayout.CommonSlideData.CloneNode(true) as CommonSlideData CommonSlideData = new CommonSlideData() { ShapeTree = new ShapeTree() { NonVisualGroupShapeProperties = internalSlideLayout.SlideLayoutPart.SlideLayout.CommonSlideData.ShapeTree.NonVisualGroupShapeProperties.CloneNode(true) as NonVisualGroupShapeProperties, GroupShapeProperties = internalSlideLayout.SlideLayoutPart.SlideLayout.CommonSlideData.ShapeTree.GroupShapeProperties.CloneNode(true) as GroupShapeProperties } } }; slide.CommonSlideData.ShapeTree.Append( internalSlideLayout.SlideLayoutPart.SlideLayout.CommonSlideData.ShapeTree .Select(element => OpenXmlVisualFactory.TryCreateVisual(internalSlideLayout, element, out IOpenXmlVisual visual) ? visual : null).Where(visual => visual != null) .Where(visual => visual?.IsPlaceholder ?? false) .Select(visual => visual.CloneForSlide()) ); SlidePart slidePart = this.presentationPart.AddNewPart <SlidePart>(); slide.Save(slidePart); slidePart.CreateRelationshipToPartDefaultId(internalSlideLayout.SlideLayoutPart); if (this.presentationPart.Presentation.SlideIdList == null) { this.presentationPart.Presentation.SlideIdList = new SlideIdList(); } SlideIdList slideIdList = this.presentationPart.Presentation.SlideIdList; SlideId refSlideId = slideIdList.Elements <SlideId>().Take(index).LastOrDefault(); uint id = refSlideId?.Id ?? 256; SlideId slideId; if (refSlideId != null) { slideId = slideIdList.InsertAfter( new SlideId() { Id = ++id, RelationshipId = presentationPart.GetIdOfPart(slidePart) }, refSlideId ); } else { slideId = slideIdList.PrependChild( new SlideId() { Id = ++id, RelationshipId = presentationPart.GetIdOfPart(slidePart) } ); } for (slideId = slideId.NextSibling <SlideId>(); slideId != null; slideId = slideId.NextSibling <SlideId>()) { slideId.Id = ++id; } return(new OpenXmlSlide(this, slidePart)); }