public static NoteSlideHelper GetNotesPageSizesFromFile() { NoteSlideHelper nsh = new NoteSlideHelper(); OpenFileDialog fDialog = new OpenFileDialog { Title = "Select PowerPoint File.", Filter = "PowerPoint | *.pptx", RestoreDirectory = true, InitialDirectory = @"%userprofile%" }; if (fDialog.ShowDialog() == DialogResult.OK) { using (PresentationDocument document = PresentationDocument.Open(fDialog.FileName, false)) { nsh.pNotesSz.Cx = document.PresentationPart.Presentation.NotesSize.Cx; nsh.pNotesSz.Cy = document.PresentationPart.Presentation.NotesSize.Cy; ShapeTree mSt = document.PresentationPart.NotesMasterPart.NotesMaster.CommonSlideData.ShapeTree; foreach (var mShp in mSt) { if (mShp.ToString() == StringResources.dfopShape) { PShape ps = (PShape)mShp; NonVisualDrawingProperties nvdpr = ps.NonVisualShapeProperties.NonVisualDrawingProperties; Transform2D t2d = ps.ShapeProperties.Transform2D; if (nvdpr.Name == StringResources.pptHeaderPlaceholder1) { nsh.t2dHeader.OffsetX = t2d.Offset.X; nsh.t2dHeader.OffsetY = t2d.Offset.Y; nsh.t2dHeader.ExtentsCx = t2d.Extents.Cx; nsh.t2dHeader.ExtentsCy = t2d.Extents.Cy; } if (nvdpr.Name == StringResources.pptDatePlaceholder2) { nsh.t2dDate.OffsetX = t2d.Offset.X; nsh.t2dDate.OffsetY = t2d.Offset.Y; nsh.t2dDate.ExtentsCx = t2d.Extents.Cx; nsh.t2dDate.ExtentsCy = t2d.Extents.Cy; } if (nvdpr.Name == StringResources.pptSlideImagePlaceholder3) { nsh.t2dSlideImage.OffsetX = t2d.Offset.X; nsh.t2dSlideImage.OffsetY = t2d.Offset.Y; nsh.t2dSlideImage.ExtentsCx = t2d.Extents.Cx; nsh.t2dSlideImage.ExtentsCy = t2d.Extents.Cy; } if (nvdpr.Name == StringResources.pptNotesPlaceholder4) { nsh.t2dNotes.OffsetX = t2d.Offset.X; nsh.t2dNotes.OffsetY = t2d.Offset.Y; nsh.t2dNotes.ExtentsCx = t2d.Extents.Cx; nsh.t2dNotes.ExtentsCy = t2d.Extents.Cy; } if (nvdpr.Name == StringResources.pptFooterPlaceholder5) { nsh.t2dFooter.OffsetX = t2d.Offset.X; nsh.t2dFooter.OffsetY = t2d.Offset.Y; nsh.t2dFooter.ExtentsCx = t2d.Extents.Cx; nsh.t2dFooter.ExtentsCy = t2d.Extents.Cy; } if (nvdpr.Name == StringResources.pptSlideNumberPlaceholder6) { nsh.t2dSlideNumber.OffsetX = t2d.Offset.X; nsh.t2dSlideNumber.OffsetY = t2d.Offset.Y; nsh.t2dSlideNumber.ExtentsCx = t2d.Extents.Cx; nsh.t2dSlideNumber.ExtentsCy = t2d.Extents.Cy; } if (nvdpr.Name == StringResources.pptPicture) { nsh.t2dPicture.OffsetX = t2d.Offset.X; nsh.t2dPicture.OffsetY = t2d.Offset.Y; nsh.t2dPicture.ExtentsCx = t2d.Extents.Cx; nsh.t2dPicture.ExtentsCy = t2d.Extents.Cy; } } } } } return(nsh); }
/// <summary> /// Use custom placeholder values from another file /// </summary> /// <param name="pDoc">oxml doc to change</param> public static void UseCustomNotesPageSize(string filename) { using (PresentationDocument document = PresentationDocument.Open(filename, true)) { NoteSlideHelper nsh = GetNotesPageSizesFromFile(); if (nsh.pNotesSz.Cx == 0) { return; } // Get the presentation part of document PresentationPart presentationPart = document.PresentationPart; if (presentationPart != null) { Presentation p = presentationPart.Presentation; // Step 1 : Resize the presentation notesz prop NotesSize defaultNotesSize = new NotesSize() { Cx = nsh.pNotesSz.Cx, Cy = nsh.pNotesSz.Cy }; // first reset the notes size values p.NotesSize = defaultNotesSize; // now save up the part p.Save(); // Step 2 : loop the shapes in the notes master and reset their sizes if (Properties.Settings.Default.ResetNotesMaster == true) { // we need to reset sizes in the notes master for each shape ShapeTree mSt = presentationPart.NotesMasterPart.NotesMaster.CommonSlideData.ShapeTree; foreach (var mShp in mSt) { if (mShp.ToString() == StringResources.dfopShape) { PShape ps = (PShape)mShp; NonVisualDrawingProperties nvdpr = ps.NonVisualShapeProperties.NonVisualDrawingProperties; Transform2D t2d = ps.ShapeProperties.Transform2D; if (nvdpr.Name.ToString().Contains(StringResources.pptHeaderPlaceholder)) { t2d.Offset.X = nsh.t2dHeader.OffsetX; t2d.Offset.Y = nsh.t2dHeader.OffsetY; t2d.Extents.Cx = nsh.t2dHeader.ExtentsCx; t2d.Extents.Cy = nsh.t2dHeader.ExtentsCy; } if (nvdpr.Name.ToString().Contains(StringResources.pptDatePlaceholder)) { t2d.Offset.X = nsh.t2dDate.OffsetX; t2d.Offset.Y = nsh.t2dDate.OffsetY; t2d.Extents.Cx = nsh.t2dDate.ExtentsCx; t2d.Extents.Cy = nsh.t2dDate.ExtentsCy; } if (nvdpr.Name.ToString().Contains(StringResources.pptSlideImagePlaceholder)) { t2d.Offset.X = nsh.t2dSlideImage.OffsetX; t2d.Offset.Y = nsh.t2dSlideImage.OffsetY; t2d.Extents.Cx = nsh.t2dSlideImage.ExtentsCx; t2d.Extents.Cy = nsh.t2dSlideImage.ExtentsCy; } if (nvdpr.Name.ToString().Contains(StringResources.pptNotesPlaceholder)) { t2d.Offset.X = nsh.t2dNotes.OffsetX; t2d.Offset.Y = nsh.t2dNotes.OffsetY; t2d.Extents.Cx = nsh.t2dNotes.ExtentsCx; t2d.Extents.Cy = nsh.t2dNotes.ExtentsCy; } if (nvdpr.Name.ToString().Contains(StringResources.pptFooterPlaceholder)) { t2d.Offset.X = nsh.t2dFooter.OffsetX; t2d.Offset.Y = nsh.t2dFooter.OffsetY; t2d.Extents.Cx = nsh.t2dFooter.ExtentsCx; t2d.Extents.Cy = nsh.t2dFooter.ExtentsCy; } if (nvdpr.Name.ToString().Contains(StringResources.pptSlideNumberPlaceholder)) { t2d.Offset.X = nsh.t2dSlideNumber.OffsetX; t2d.Offset.Y = nsh.t2dSlideNumber.OffsetY; t2d.Extents.Cx = nsh.t2dSlideNumber.ExtentsCx; t2d.Extents.Cy = nsh.t2dSlideNumber.ExtentsCy; } if (nvdpr.Name == StringResources.pptPicture) { t2d.Offset.X = nsh.t2dPicture.OffsetX; t2d.Offset.Y = nsh.t2dPicture.OffsetY; t2d.Extents.Cx = nsh.t2dPicture.ExtentsCx; t2d.Extents.Cy = nsh.t2dPicture.ExtentsCy; } } } // Step 3 : we need to delete the size values for placeholders on each notes slide foreach (var slideId in p.SlideIdList.Elements <SlideId>()) { SlidePart slidePart = presentationPart.GetPartById(slideId.RelationshipId) as SlidePart; ShapeTree st = slidePart.NotesSlidePart.NotesSlide.CommonSlideData.ShapeTree; List <RunProperties> rpList = slidePart.NotesSlidePart.NotesSlide.Descendants <RunProperties>().ToList(); foreach (var s in st) { // we only want to make changes to the shapes if (s.ToString() == StringResources.dfopShape) { PShape ps = (PShape)s; NonVisualDrawingProperties nvdpr = ps.NonVisualShapeProperties.NonVisualDrawingProperties; Transform2D t2d = ps.ShapeProperties.Transform2D; if (t2d == null) { A.Transform2D t2dn = new Transform2D(); A.Offset offset1 = new A.Offset() { X = 0L, Y = 0L }; A.Extents extents1 = new A.Extents() { Cx = 0L, Cy = 0L }; t2d = t2dn; t2d.Offset = offset1; t2d.Extents = extents1; } if (nvdpr.Name.ToString().Contains(StringResources.pptHeaderPlaceholder)) { t2d.Offset.X = nsh.t2dHeader.OffsetX; t2d.Offset.Y = nsh.t2dHeader.OffsetY; t2d.Extents.Cx = nsh.t2dHeader.ExtentsCx; t2d.Extents.Cy = nsh.t2dHeader.ExtentsCy; } if (nvdpr.Name.ToString().Contains(StringResources.pptDatePlaceholder)) { t2d.Offset.X = nsh.t2dDate.OffsetX; t2d.Offset.Y = nsh.t2dDate.OffsetY; t2d.Extents.Cx = nsh.t2dDate.ExtentsCx; t2d.Extents.Cy = nsh.t2dDate.ExtentsCy; } if (nvdpr.Name.ToString().Contains(StringResources.pptSlideImagePlaceholder)) { t2d.Offset.X = nsh.t2dSlideImage.OffsetX; t2d.Offset.Y = nsh.t2dSlideImage.OffsetY; t2d.Extents.Cx = nsh.t2dSlideImage.ExtentsCx; t2d.Extents.Cy = nsh.t2dSlideImage.ExtentsCy; } if (nvdpr.Name.ToString().Contains(StringResources.pptNotesPlaceholder)) { t2d.Offset.X = nsh.t2dNotes.OffsetX; t2d.Offset.Y = nsh.t2dNotes.OffsetY; t2d.Extents.Cx = nsh.t2dNotes.ExtentsCx; t2d.Extents.Cy = nsh.t2dNotes.ExtentsCy; } if (nvdpr.Name.ToString().Contains(StringResources.pptFooterPlaceholder)) { t2d.Offset.X = nsh.t2dFooter.OffsetX; t2d.Offset.Y = nsh.t2dFooter.OffsetY; t2d.Extents.Cx = nsh.t2dFooter.ExtentsCx; t2d.Extents.Cy = nsh.t2dFooter.ExtentsCy; } if (nvdpr.Name.ToString().Contains(StringResources.pptSlideNumberPlaceholder)) { t2d.Offset.X = nsh.t2dSlideNumber.OffsetX; t2d.Offset.Y = nsh.t2dSlideNumber.OffsetY; t2d.Extents.Cx = nsh.t2dSlideNumber.ExtentsCx; t2d.Extents.Cy = nsh.t2dSlideNumber.ExtentsCy; } } else if (s.ToString() == StringResources.dfopPresentationPicture) { DocumentFormat.OpenXml.Presentation.Picture pic = (DocumentFormat.OpenXml.Presentation.Picture)s; Transform2D t2d = pic.ShapeProperties.Transform2D; // there are times when pictures get moved with the rest of the slide objects, need to reset those back if (t2d == null) { t2d.Offset.X = nsh.t2dPicture.OffsetX; t2d.Offset.Y = nsh.t2dPicture.OffsetY; t2d.Extents.Cx = nsh.t2dPicture.ExtentsCx; t2d.Extents.Cy = nsh.t2dPicture.ExtentsCy; } else { t2d.Offset.X = 217831L; t2d.Offset.Y = 4470109L; t2d.Extents.Cx = 3249763L; t2d.Extents.Cy = 2795946L; } } } foreach (RunProperties r in rpList) { r.FontSize = 1200; } } } p.Save(); } } }
/// <summary> /// Check the notes page size and reset values /// </summary> /// <param name="pDoc">oxml doc to change</param> public static void ChangeNotesPageSize(PresentationDocument pDoc) { if (pDoc == null) { throw new ArgumentNullException("pDoc = null"); } // Get the presentation part of document PresentationPart presentationPart = pDoc.PresentationPart; if (presentationPart != null) { Presentation p = presentationPart.Presentation; // Step 1 : Resize the presentation notesz prop // if the notes size is already the default, no need to make any changes if (p.NotesSize.Cx != 6858000 || p.NotesSize.Cy != 9144000) { // setup default size NotesSize defaultNotesSize = new NotesSize() { Cx = 6858000L, Cy = 9144000L }; // first reset the notes size values p.NotesSize = defaultNotesSize; // now save up the part p.Save(); } // Step 2 : loop the shapes in the notes master and reset their sizes // need to find a way to flag a file if the notes master and/or notes slides become corrupt // hiding behind a setting checkbox for now if (Properties.Settings.Default.ResetNotesMaster == true) { // we need to reset sizes in the notes master for each shape ShapeTree mSt = presentationPart.NotesMasterPart.NotesMaster.CommonSlideData.ShapeTree; foreach (var mShp in mSt) { if (mShp.ToString() == StringResources.dfopShape) { PShape ps = (PShape)mShp; NonVisualDrawingProperties nvdpr = ps.NonVisualShapeProperties.NonVisualDrawingProperties; Transform2D t2d = ps.ShapeProperties.Transform2D; // use default values if (nvdpr.Name == StringResources.pptHeaderPlaceholder1) { t2d.Offset.X = 0L; t2d.Offset.Y = 0L; t2d.Extents.Cx = 2971800L; t2d.Extents.Cy = 458788L; } if (nvdpr.Name == StringResources.pptDatePlaceholder2) { t2d.Offset.X = 3884613L; t2d.Offset.Y = 0L; t2d.Extents.Cx = 2971800L; t2d.Extents.Cy = 458788L; } if (nvdpr.Name == StringResources.pptSlideImagePlaceholder3) { t2d.Offset.X = 685800L; t2d.Offset.Y = 1143000L; t2d.Extents.Cx = 5486400L; t2d.Extents.Cy = 3086100L; } if (nvdpr.Name == StringResources.pptNotesPlaceholder4) { t2d.Offset.X = 685800L; t2d.Offset.Y = 4400550L; t2d.Extents.Cx = 5486400L; t2d.Extents.Cy = 3600450L; } if (nvdpr.Name == StringResources.pptFooterPlaceholder5) { t2d.Offset.X = 0L; t2d.Offset.Y = 8685213L; t2d.Extents.Cx = 2971800L; t2d.Extents.Cy = 458787L; } if (nvdpr.Name == StringResources.pptSlideNumberPlaceholder6) { t2d.Offset.X = 3884613L; t2d.Offset.Y = 8685213L; t2d.Extents.Cx = 2971800L; t2d.Extents.Cy = 458787L; } } } // Step 3 : we need to delete the size values for placeholders on each notes slide foreach (var slideId in p.SlideIdList.Elements <SlideId>()) { SlidePart slidePart = presentationPart.GetPartById(slideId.RelationshipId) as SlidePart; ShapeTree st = slidePart.NotesSlidePart.NotesSlide.CommonSlideData.ShapeTree; List <RunProperties> rpList = slidePart.NotesSlidePart.NotesSlide.Descendants <RunProperties>().ToList(); foreach (var s in st) { // we only want to make changes to the shapes if (s.ToString() == StringResources.dfopShape) { PShape ps = (PShape)s; Transform2D t2d = ps.ShapeProperties.Transform2D; TextBody tb = ps.TextBody; // if the transform exists, delete it for each shape if (t2d != null) { t2d.Remove(); } // if there are drawing paragraph props, reset the margin and indent to 0 if (ps.TextBody != null) { foreach (var x in tb.ChildElements) { if (x.ToString() == "DocumentFormat.OpenXml.Drawing.Paragraph") { Paragraph para = (Paragraph)x; if (para.ParagraphProperties != null) { if (para.ParagraphProperties.LeftMargin != null) { para.ParagraphProperties.LeftMargin = 0; } if (para.ParagraphProperties.Indent != null) { para.ParagraphProperties.Indent = 0; } } } } } } else if (s.ToString() == StringResources.dfopPresentationPicture) { DocumentFormat.OpenXml.Presentation.Picture pic = (DocumentFormat.OpenXml.Presentation.Picture)s; Transform2D t2d = pic.ShapeProperties.Transform2D; // there are times when pictures get moved with the rest of the slide objects, need to reset those back if (t2d != null) { t2d.Offset.X = 217831L; t2d.Offset.Y = 4470109L; t2d.Extents.Cx = 3249763L; t2d.Extents.Cy = 2795946L; } } } foreach (RunProperties r in rpList) { r.FontSize = 1200; } } } } }