using (var wordDoc = WordprocessingDocument.Open("MyDocument.docx", true)) { var mainPart = wordDoc.MainDocumentPart; var context = mainPart.DeletePart("Header1"); // Use the SafeDeleteContext to safely delete the "Header1" part from the document using (context) { mainPart.DeleteParts(mainPart.HeaderParts); } wordDoc.Save(); }
using (var presentation = PresentationDocument.Open("MyPresentation.pptx", true)) { var slidePart = presentation.PresentationPart.SlideParts.First(); var context = slidePart.DeletePart("Image1"); // Use the SafeDeleteContext to safely delete the "Image1" part from the slide using (context) { slidePart.DeleteParts(slidePart.ImageParts); } presentation.Save(); }This example opens a PowerPoint presentation and deletes the "Image1" part from the first slide using the SafeDeleteContext. The ImageParts collection is passed as an argument to the DeleteParts method, which deletes all image parts in the slide part. The DocumentFormat.OpenXml.Packaging package library is used to work with Open XML documents, such as Word documents, Excel spreadsheets, and PowerPoint presentations. It provides a set of classes for creating, reading, and modifying Open XML documents. The SafeDeleteContext class is part of this library and provides a safe way to delete parts from Open XML documents.