public void ExtractMasters(string fileName) { var source = new PmlDocument(Path.Combine(SourceDirectory, fileName)); int numberOfMasters; using (var stream = new OpenXmlMemoryStreamDocument(source)) { using var doc1 = stream.GetPresentationDocument(); numberOfMasters = doc1.PresentationPart.SlideMasterParts.Count(); } var onlyMaster = PresentationBuilder.BuildPresentation( new List <SlideSource> { new SlideSource(source, 0, 0, true) }); onlyMaster.FileName = fileName.Replace(".pptx", "_masterOnly.pptx"); onlyMaster.SaveAs(Path.Combine(TargetDirectory, onlyMaster.FileName)); using var streamDoc = new OpenXmlMemoryStreamDocument(onlyMaster); using var resDoc = streamDoc.GetPresentationDocument(); Assert.Empty(resDoc.PresentationPart.SlideParts); Assert.Equal(numberOfMasters, resDoc.PresentationPart.SlideMasterParts.Count()); }
public void ReassemblePresentationWithMaster(string fileName) { var file = Path.Combine(SourceDirectory, fileName); var presentation = new PmlDocument(file); // generate presentation with all masters var onlyMaster = PresentationBuilder.BuildPresentation( new List <SlideSource> { new SlideSource(presentation, 0, 0, true) }); // publish slides with one-layout masters var slides = PresentationBuilder.PublishSlides(presentation); // compose them together using only master as the first source var sources = new List <SlideSource> { new SlideSource(onlyMaster, true) }; sources.AddRange(slides.Select(x => new SlideSource(x, false))); var newDocument = PresentationBuilder.BuildPresentation(sources); newDocument.FileName = fileName.Replace(".pptx", "_reassembledWithMaster.pptx"); newDocument.SaveAs(Path.Combine(TargetDirectory, newDocument.FileName)); var baseSize = slides.Sum(x => x.DocumentByteArray.Length) + onlyMaster.DocumentByteArray.Length; Assert.InRange(newDocument.DocumentByteArray.Length, 0.85 * baseSize, 1.1 * baseSize); }
static void Main(string[] args) { var n = DateTime.Now; var tempDi = new DirectoryInfo(string.Format("ExampleOutput-{0:00}-{1:00}-{2:00}-{3:00}{4:00}{5:00}", n.Year - 2000, n.Month, n.Day, n.Hour, n.Minute, n.Second)); tempDi.Create(); string source1 = "../../Contoso.pptx"; string source2 = "../../Companies.pptx"; string source3 = "../../Customer Content.pptx"; string source4 = "../../Presentation One.pptx"; string source5 = "../../Presentation Two.pptx"; string source6 = "../../Presentation Three.pptx"; string contoso1 = "../../Contoso One.pptx"; string contoso2 = "../../Contoso Two.pptx"; string contoso3 = "../../Contoso Three.pptx"; List <SlideSource> sources = null; var sourceDoc = new PmlDocument(source1); sources = new List <SlideSource>() { new SlideSource(sourceDoc, 0, 1, false), // Title new SlideSource(sourceDoc, 1, 1, false), // First intro (of 3) new SlideSource(sourceDoc, 4, 2, false), // Sales bios new SlideSource(sourceDoc, 9, 3, false), // Content slides new SlideSource(sourceDoc, 13, 1, false), // Closing summary }; PresentationBuilder.BuildPresentation(sources).SaveAs(Path.Combine(tempDi.FullName, "Out1.pptx")); sources = new List <SlideSource>() { new SlideSource(new PmlDocument(source2), 2, 1, true), // Choose company new SlideSource(new PmlDocument(source3), false), // Content }; PresentationBuilder.BuildPresentation(sources).SaveAs(Path.Combine(tempDi.FullName, "Out2.pptx")); sources = new List <SlideSource>() { new SlideSource(new PmlDocument(source4), true), new SlideSource(new PmlDocument(source5), true), new SlideSource(new PmlDocument(source6), true), }; PresentationBuilder.BuildPresentation(sources).SaveAs(Path.Combine(tempDi.FullName, "Out3.pptx")); sources = new List <SlideSource>() { new SlideSource(new PmlDocument(contoso1), true), new SlideSource(new PmlDocument(contoso2), true), new SlideSource(new PmlDocument(contoso3), true), }; PresentationBuilder.BuildPresentation(sources).SaveAs(Path.Combine(tempDi.FullName, "Out4.pptx")); }
public void PB002_Formatting() { var name2 = "PB001-Input2.pptx"; var sourceDir = new DirectoryInfo("../../../../TestFiles/"); var source2Pptx = new FileInfo(Path.Combine(sourceDir.FullName, name2)); var sources = new List <SlideSource>() { new SlideSource(new PmlDocument(source2Pptx.FullName), 0, true), }; var processedDestPptx = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, "PB002-Formatting.pptx")); PresentationBuilder.BuildPresentation(sources, processedDestPptx.FullName); }
public void ExtractOneSlide(string fileName, int slideNumber) { var file = Path.Combine(SourceDirectory, fileName); var document = new PmlDocument(file); var source = new SlideSource(document, slideNumber - 1, 1, true); var slide = PresentationBuilder.BuildPresentation(new List <SlideSource> { source }); slide.FileName = document.FileName.Replace(".pptx", $"_{slideNumber:000}.pptx"); slide.SaveAs(Path.Combine(TargetDirectory, Path.GetFileName(slide.FileName))); }
public void PB002_Formatting() { string name2 = "PB001-Input2.pptx"; FileInfo source2Pptx = new FileInfo(Path.Combine(TestUtil.SourceDir.FullName, name2)); List <SlideSource> sources = null; sources = new List <SlideSource>() { new SlideSource(new PmlDocument(source2Pptx.FullName), 0, true), }; var processedDestPptx = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, "PB002-Formatting.pptx")); PresentationBuilder.BuildPresentation(sources, processedDestPptx.FullName); }
static void Main(string[] args) { string source1 = "../../Contoso.pptx"; string source2 = "../../Companies.pptx"; string source3 = "../../Customer Content.pptx"; string source4 = "../../Presentation One.pptx"; string source5 = "../../Presentation Two.pptx"; string source6 = "../../Presentation Three.pptx"; string contoso1 = "../../Contoso One.pptx"; string contoso2 = "../../Contoso Two.pptx"; string contoso3 = "../../Contoso Three.pptx"; List <SlideSource> sources = null; var sourceDoc = new PmlDocument(source1); sources = new List <SlideSource>() { new SlideSource(sourceDoc, 0, 1, false), // Title new SlideSource(sourceDoc, 1, 1, false), // First intro (of 3) new SlideSource(sourceDoc, 4, 2, false), // Sales bios new SlideSource(sourceDoc, 9, 3, false), // Content slides new SlideSource(sourceDoc, 13, 1, false), // Closing summary }; PresentationBuilder.BuildPresentation(sources, "Out1.pptx"); sources = new List <SlideSource>() { new SlideSource(new PmlDocument(source2), 2, 1, true), // Choose company new SlideSource(new PmlDocument(source3), false), // Content }; PresentationBuilder.BuildPresentation(sources, "Out2.pptx"); sources = new List <SlideSource>() { new SlideSource(new PmlDocument(source4), true), new SlideSource(new PmlDocument(source5), true), new SlideSource(new PmlDocument(source6), true), }; PresentationBuilder.BuildPresentation(sources, "Out3.pptx"); sources = new List <SlideSource>() { new SlideSource(new PmlDocument(contoso1), true), new SlideSource(new PmlDocument(contoso2), true), new SlideSource(new PmlDocument(contoso3), true), }; PresentationBuilder.BuildPresentation(sources, "Out4.pptx"); }
public void Sample1() { var source1 = GetFilePath("Sample1/Contoso.pptx"); var source2 = GetFilePath("Sample1/Companies.pptx"); var source3 = GetFilePath("Sample1/Customer Content.pptx"); var source4 = GetFilePath("Sample1/Presentation One.pptx"); var source5 = GetFilePath("Sample1/Presentation Two.pptx"); var source6 = GetFilePath("Sample1/Presentation Three.pptx"); var contoso1 = GetFilePath("Sample1/Contoso One.pptx"); var contoso2 = GetFilePath("Sample1/Contoso Two.pptx"); var contoso3 = GetFilePath("Sample1/Contoso Three.pptx"); var sourceDoc = new PmlDocument(source1); var sources = new List <SlideSource> { new(sourceDoc, 0, 1, false), // Title new(sourceDoc, 1, 1, false), // First intro (of 3) new(sourceDoc, 4, 2, false), // Sales bios new(sourceDoc, 9, 3, false), // Content slides new(sourceDoc, 13, 1, false), // Closing summary }; PresentationBuilder.BuildPresentation(sources).SaveAs(Path.Combine(TempDir, "Out1.pptx")); sources = new List <SlideSource> { new(new PmlDocument(source2), 2, 1, true), // Choose company new(new PmlDocument(source3), false), // Content }; PresentationBuilder.BuildPresentation(sources).SaveAs(Path.Combine(TempDir, "Out2.pptx")); sources = new List <SlideSource> { new(new PmlDocument(source4), true), new(new PmlDocument(source5), true), new(new PmlDocument(source6), true), }; PresentationBuilder.BuildPresentation(sources).SaveAs(Path.Combine(TempDir, "Out3.pptx")); sources = new List <SlideSource> { new(new PmlDocument(contoso1), true), new(new PmlDocument(contoso2), true), new(new PmlDocument(contoso3), true), }; PresentationBuilder.BuildPresentation(sources).SaveAs(Path.Combine(TempDir, "Out4.pptx")); }
public void ReassemblePresentation(string fileName) { var file = Path.Combine(SourceDirectory, fileName); var document = new PmlDocument(file); var slides = PresentationBuilder.PublishSlides(document); var sources = slides.Select(x => new SlideSource(x, true)).ToList(); var newDocument = PresentationBuilder.BuildPresentation(sources); newDocument.FileName = fileName.Replace(".pptx", "_reassembled.pptx"); newDocument.SaveAs(Path.Combine(TargetDirectory, newDocument.FileName)); var baseSize = slides.Sum(x => x.DocumentByteArray.Length); Assert.InRange(newDocument.DocumentByteArray.Length, 0.9 * baseSize, 1.1 * baseSize); }
public void PB004_Formatting() { var name1 = "PB001-Input1.pptx"; var name2 = "PB001-Input3.pptx"; var sourceDir = new DirectoryInfo("../../../../TestFiles/"); var source1Pptx = new FileInfo(Path.Combine(sourceDir.FullName, name1)); var source2Pptx = new FileInfo(Path.Combine(sourceDir.FullName, name2)); var sources = new List <SlideSource> { new(new PmlDocument(source2Pptx.FullName), 0, true), new(new PmlDocument(source1Pptx.FullName), 1, true), }; var processedDestPptx = new FileInfo(Path.Combine(TempDir, "PB004-Formatting.pptx")); PresentationBuilder.BuildPresentation(sources).SaveAs(processedDestPptx.FullName); }
public void PB006_VideoFormats() { // This presentation contains videos with content types video/mp4, video/quicktime, video/unknown, video/x-ms-asf, and video/x-msvideo. var sourceDir = new DirectoryInfo("../../../../TestFiles/"); var sourcePptx = new FileInfo(Path.Combine(sourceDir.FullName, "PP006-Videos.pptx")); var oldMediaDataContentTypes = GetMediaDataContentTypes(sourcePptx); var sources = new List <SlideSource>() { new SlideSource(new PmlDocument(sourcePptx.FullName), true), }; var processedDestPptx = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, "PB006-Videos.pptx")); PresentationBuilder.BuildPresentation(sources, processedDestPptx.FullName); var newMediaDataContentTypes = GetMediaDataContentTypes(processedDestPptx); Assert.Equal(oldMediaDataContentTypes, newMediaDataContentTypes); }
public void PB004_Formatting() { string name1 = "PB001-Input1.pptx"; string name2 = "PB001-Input3.pptx"; DirectoryInfo sourceDir = new DirectoryInfo("../../../../TestFiles/"); FileInfo source1Pptx = new FileInfo(Path.Combine(sourceDir.FullName, name1)); FileInfo source2Pptx = new FileInfo(Path.Combine(sourceDir.FullName, name2)); List <SlideSource> sources = null; sources = new List <SlideSource>() { new SlideSource(new PmlDocument(source2Pptx.FullName), 0, true), new SlideSource(new PmlDocument(source1Pptx.FullName), 1, true), }; var processedDestPptx = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, "PB004-Formatting.pptx")); PresentationBuilder.BuildPresentation(sources, processedDestPptx.FullName); }
static void Main(string[] args) { string currentdirectory = Directory.GetCurrentDirectory(); string[] listofppt = Directory.GetFiles(currentdirectory, "*.pptx"); NumericComparer ns = new NumericComparer(); Array.Sort(listofppt, ns); Console.WriteLine("The current directory is {0}", currentdirectory); string savename = "COMBINED.pptx"; List <SlideSource> sources = new List <SlideSource>(); if (listofppt.Length > 0) { for (int i = 0; i < listofppt.Count(); i++) { sources.Add(new SlideSource(new PmlDocument(listofppt[i]), true)); } PresentationBuilder.BuildPresentation(sources, savename); } }
static void Main(string[] args) { var n = DateTime.Now; var tempDi = new DirectoryInfo(string.Format("ExampleOutput-{0:00}-{1:00}-{2:00}-{3:00}{4:00}{5:00}", n.Year - 2000, n.Month, n.Day, n.Hour, n.Minute, n.Second)); tempDi.Create(); string presentation = "../../Presentation1.pptx"; string hiddenPresentation = "../../HiddenPresentation.pptx"; // First, load both presentations into byte arrays, simulating retrieving presentations from some source // such as a SharePoint server var baPresentation = File.ReadAllBytes(presentation); var baHiddenPresentation = File.ReadAllBytes(hiddenPresentation); // Next, replace "thee" with "the" in the main presentation var pmlMainPresentation = new PmlDocument("Main.pptx", baPresentation); PmlDocument modifiedMainPresentation = null; using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(pmlMainPresentation)) { using (PresentationDocument document = streamDoc.GetPresentationDocument()) { var pXDoc = document.PresentationPart.GetXDocument(); foreach (var slideId in pXDoc.Root.Elements(P.sldIdLst).Elements(P.sldId)) { var slideRelId = (string)slideId.Attribute(R.id); var slidePart = document.PresentationPart.GetPartById(slideRelId); var slideXDoc = slidePart.GetXDocument(); var paragraphs = slideXDoc.Descendants(A.p).ToList(); OpenXmlRegex.Replace(paragraphs, new Regex("thee"), "the", null); slidePart.PutXDocument(); } } modifiedMainPresentation = streamDoc.GetModifiedPmlDocument(); } // Combine the two presentations into a single presentation var slideSources = new List <SlideSource>() { new SlideSource(modifiedMainPresentation, 0, 1, true), new SlideSource(new PmlDocument("Hidden.pptx", baHiddenPresentation), true), new SlideSource(modifiedMainPresentation, 1, true), }; PmlDocument combinedPresentation = PresentationBuilder.BuildPresentation(slideSources); // Replace <# TRADEMARK #> with AdventureWorks (c) PmlDocument modifiedCombinedPresentation = null; using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(combinedPresentation)) { using (PresentationDocument document = streamDoc.GetPresentationDocument()) { var pXDoc = document.PresentationPart.GetXDocument(); foreach (var slideId in pXDoc.Root.Elements(P.sldIdLst).Elements(P.sldId).Skip(1).Take(1)) { var slideRelId = (string)slideId.Attribute(R.id); var slidePart = document.PresentationPart.GetPartById(slideRelId); var slideXDoc = slidePart.GetXDocument(); var paragraphs = slideXDoc.Descendants(A.p).ToList(); OpenXmlRegex.Replace(paragraphs, new Regex("<# TRADEMARK #>"), "AdventureWorks (c)", null); slidePart.PutXDocument(); } } modifiedCombinedPresentation = streamDoc.GetModifiedPmlDocument(); } // we now have a PmlDocument (which is essentially a byte array) that can be saved as necessary. modifiedCombinedPresentation.SaveAs(Path.Combine(tempDi.FullName, "Modified.pptx")); }
public void Sample2() { var presentation = GetFilePath("Sample2/Presentation1.pptx"); var hiddenPresentation = GetFilePath("Sample2/HiddenPresentation.pptx"); // First, load both presentations into byte arrays, simulating retrieving presentations from some source // such as a SharePoint server var baPresentation = File.ReadAllBytes(presentation); var baHiddenPresentation = File.ReadAllBytes(hiddenPresentation); // Next, replace "thee" with "the" in the main presentation var pmlMainPresentation = new PmlDocument("Main.pptx", baPresentation); PmlDocument modifiedMainPresentation; using (var streamDoc = new OpenXmlMemoryStreamDocument(pmlMainPresentation)) { using (var document = streamDoc.GetPresentationDocument()) { var pXDoc = document.PresentationPart.GetXDocument(); foreach (var slideId in pXDoc.Root.Elements(P.sldIdLst).Elements(P.sldId)) { var slideRelId = (string)slideId.Attribute(R.id); var slidePart = document.PresentationPart.GetPartById(slideRelId); var slideXDoc = slidePart.GetXDocument(); var paragraphs = slideXDoc.Descendants(A.p).ToList(); OpenXmlRegex.Replace(paragraphs, new Regex("thee"), "the", null); slidePart.PutXDocument(); } } modifiedMainPresentation = streamDoc.GetModifiedPmlDocument(); } // Combine the two presentations into a single presentation var slideSources = new List <SlideSource> { new(modifiedMainPresentation, 0, 1, true), new(new PmlDocument("Hidden.pptx", baHiddenPresentation), true), new(modifiedMainPresentation, 1, true), }; var combinedPresentation = PresentationBuilder.BuildPresentation(slideSources); // Replace <# TRADEMARK #> with AdventureWorks (c) PmlDocument modifiedCombinedPresentation; using (var streamDoc = new OpenXmlMemoryStreamDocument(combinedPresentation)) { using (var document = streamDoc.GetPresentationDocument()) { var pXDoc = document.PresentationPart.GetXDocument(); foreach (var slideId in pXDoc.Root.Elements(P.sldIdLst).Elements(P.sldId).Skip(1).Take(1)) { var slideRelId = (string)slideId.Attribute(R.id); var slidePart = document.PresentationPart.GetPartById(slideRelId); var slideXDoc = slidePart.GetXDocument(); var paragraphs = slideXDoc.Descendants(A.p).ToList(); OpenXmlRegex.Replace(paragraphs, new Regex("<# TRADEMARK #>"), "AdventureWorks (c)", null); slidePart.PutXDocument(); } } modifiedCombinedPresentation = streamDoc.GetModifiedPmlDocument(); } // we now have a PmlDocument (which is essentially a byte array) that can be saved as necessary. modifiedCombinedPresentation.SaveAs(Path.Combine(TempDir, "ModifiedCombinedPresentation.pptx")); }