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 byte[] Run(List <PersonFiles> files) { var mergeXml = GetMergeXml(files); MergeDocument documentXml = mergeXml.Items.First(); IPresentationTools presentationTools = new PresentationTools(); byte[] emptyTemplate = files.Where(p => p.Person == "/" && p.Name == "template.pptx").Select(d => d.Data).FirstOrDefault(); using (MemoryStream emptyDocInMemoryStream = new MemoryStream(emptyTemplate, 0, emptyTemplate.Length, true, true)) { OpenXmlPowerToolsDocument emptyDocPowerTools = new OpenXmlPowerToolsDocument(string.Empty, emptyDocInMemoryStream); using (OpenXmlMemoryStreamDocument streamEmptyDoc = new OpenXmlMemoryStreamDocument(emptyDocPowerTools)) { PresentationDocument emptyPresentation = streamEmptyDoc.GetPresentationDocument(); foreach (MergeDocumentPart part in documentXml.Part) { byte[] byteArray = files.Where(p => p.Person == part.Name && p.Name == part.Id).Select(d => d.Data).FirstOrDefault(); using (MemoryStream partDocInMemoryStream = new MemoryStream(byteArray, 0, byteArray.Length, true, true)) { OpenXmlPowerToolsDocument partDocPowerTools = new OpenXmlPowerToolsDocument(string.Empty, partDocInMemoryStream); using (OpenXmlMemoryStreamDocument streamDividedDoc = new OpenXmlMemoryStreamDocument(partDocPowerTools)) { PresentationDocument templatePresentation = streamDividedDoc.GetPresentationDocument(); presentationTools.InsertSlidesFromTemplate(emptyPresentation, templatePresentation); } } } return(streamEmptyDoc.GetModifiedDocument().DocumentByteArray); } } }
public List <PersonFiles> SaveSplitDocument(Stream document) { List <PersonFiles> resultList = new List <PersonFiles>(); byte[] byteArray = StreamTools.ReadFully(document); using (MemoryStream documentInMemoryStream = new MemoryStream(byteArray, 0, byteArray.Length, true, true)) { OpenXmlPowerToolsDocument docPowerTools = new OpenXmlPowerToolsDocument(DocumentName, documentInMemoryStream); using (OpenXmlMemoryStreamDocument streamTemplateDoc = new OpenXmlMemoryStreamDocument(docPowerTools)) { PresentationDocument templatePresentation = streamTemplateDoc.GetPresentationDocument(); foreach (OpenXMLDocumentPart <SlideId> element in DocumentElements) { OpenXmlPowerToolsDocument emptyDocPowerTools = new OpenXmlPowerToolsDocument(DocumentName, documentInMemoryStream); using (OpenXmlMemoryStreamDocument streamDividedDoc = new OpenXmlMemoryStreamDocument(emptyDocPowerTools)) { var relationshipIds = element.CompositeElements.Select(c => c.RelationshipId.Value).ToList(); PresentationDocument dividedPresentation = streamDividedDoc.GetPresentationDocument(); PresentationTools.InsertSlidesFromTemplate(PresentationTools.RemoveAllSlides(dividedPresentation), templatePresentation, relationshipIds); var person = new PersonFiles(); person.Person = element.PartOwner; resultList.Add(person); person.Name = element.Guid.ToString(); person.Data = streamDividedDoc.GetModifiedDocument().DocumentByteArray; } } } using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(docPowerTools)) { PresentationDocument preDoc = streamDoc.GetPresentationDocument(); PresentationTools.RemoveAllSlides(preDoc); var person = new PersonFiles(); person.Person = "/"; resultList.Add(person); person.Name = "template.pptx"; person.Data = streamDoc.GetModifiedDocument().DocumentByteArray; } var xmlPerson = new PersonFiles(); xmlPerson.Person = "/"; resultList.Add(xmlPerson); xmlPerson.Name = "mergeXmlDefinition.xml"; xmlPerson.Data = CreateMergeXml(); } return(resultList); }
public void SaveSplitDocumentShouldReturnValidTest2dDocuments() { var personFilesList = DocSampleSplit.SaveSplitDocument(PreSampleDocInMemory); var docs = personFilesList.Where(p => p.Person == "test2").Select(u => u.Data); List <ValidationErrorInfo> validationErrors = new List <ValidationErrorInfo>(); foreach (byte[] doc in docs) { MemoryStream partDocInMemory = new MemoryStream(doc, 0, doc.Length, true, true); var partDocPowerTools = new OpenXmlPowerToolsDocument("test2.pptx", partDocInMemory); OpenXmlMemoryStreamDocument partDocInMemoryExpandable = new OpenXmlMemoryStreamDocument(partDocPowerTools); validationErrors.AddRange(DocValidator.Validate(partDocInMemoryExpandable.GetPresentationDocument())); } Assert.IsTrue(docs.Count() > 0); Assert.AreEqual(0, validationErrors.Count()); }
public void PublishUsingPublishSlides(string sourcePath) { var targetDir = Path.Combine(TargetDirectory, Path.GetFileNameWithoutExtension(sourcePath)); if (Directory.Exists(targetDir)) { Directory.Delete(targetDir, true); } Directory.CreateDirectory(targetDir); var document = new PmlDocument(sourcePath); string title; DateTime?modified; using (var streamDoc = new OpenXmlMemoryStreamDocument(document)) { using var srcDoc = streamDoc.GetPresentationDocument(new OpenSettings { AutoSave = false }); title = srcDoc.PackageProperties.Title; modified = srcDoc.PackageProperties.Modified; } var sameTitle = 0; foreach (var slide in PresentationBuilder.PublishSlides(document)) { slide.SaveAs(Path.Combine(targetDir, Path.GetFileName(slide.FileName))); using var streamDoc = new OpenXmlMemoryStreamDocument(slide); using var slideDoc = streamDoc.GetPresentationDocument(new OpenSettings { AutoSave = false }); Assert.Equal(modified, slideDoc.PackageProperties.Modified); if (title.Equals(slideDoc.PackageProperties.Title)) { sameTitle++; } } Assert.InRange(sameTitle, 0, 4); }
public void PublishUsingPublishSlides(string sourcePath) { var targetDir = Path.Combine(TargetDirectory, Path.GetFileNameWithoutExtension(sourcePath)); if (Directory.Exists(targetDir)) { Directory.Delete(targetDir, true); } Directory.CreateDirectory(targetDir); using var srcStream = File.Open(sourcePath, FileMode.Open); var openSettings = new OpenSettings { AutoSave = false }; using var srcDoc = OpenXmlExtensions.OpenPresentation(srcStream, false, openSettings); var title = srcDoc.PackageProperties.Title ?? string.Empty; var modified = srcDoc.PackageProperties.Modified; var sameTitle = 0; foreach (var slide in PresentationBuilder.PublishSlides(srcDoc, sourcePath)) { slide.SaveAs(Path.Combine(targetDir, Path.GetFileName(slide.FileName))); using var streamDoc = new OpenXmlMemoryStreamDocument(slide); using var slideDoc = streamDoc.GetPresentationDocument(new OpenSettings { AutoSave = false }); Assert.Equal(modified, slideDoc.PackageProperties.Modified); if (title.Equals(slideDoc.PackageProperties.Title)) { sameTitle++; } } Assert.InRange(sameTitle, 0, 4); }
public void Init() { PreTools = new PresentationTools(); DocValidator = new OpenXmlValidator(); byte[] PreCGWBytes = File.ReadAllBytes(@"../../../Files/6.CGW15-prezentacja.pptx"); byte[] PreSampleBytes = File.ReadAllBytes(@"../../../Files/przykladowa-prezentacja.pptx"); MemoryStream PreCGWDocInMemory = new MemoryStream(PreCGWBytes, 0, PreCGWBytes.Length, true, true); MemoryStream PreSampleDocInMemory = new MemoryStream(PreSampleBytes, 0, PreSampleBytes.Length, true, true); OpenXmlPowerToolsDocument PreCGWDocPowerTools = new OpenXmlPowerToolsDocument("6.CGW15 - prezentacja.pptx", PreCGWDocInMemory); OpenXmlPowerToolsDocument PreSampleDocPowerTools = new OpenXmlPowerToolsDocument("6.CGW15 - przykladowa-prezentacja.pptx", PreSampleDocInMemory); PreCGWDocInMemoryExpandable = new OpenXmlMemoryStreamDocument(PreCGWDocPowerTools); PreSampleDocInMemoryExpandable = new OpenXmlMemoryStreamDocument(PreSampleDocPowerTools); PreCGWDoc = PreCGWDocInMemoryExpandable.GetPresentationDocument(); PreSampleDoc = PreSampleDocInMemoryExpandable.GetPresentationDocument(); PreCGWDocInMemory.Close(); PreSampleDocInMemory.Close(); }
private static void BuildPresentation(List <SlideSource> sources, PresentationDocument output) { using var fluentBuilder = new FluentPresentationBuilder(output); var sourceNum = 0; var openSettings = new OpenSettings { AutoSave = false }; foreach (var source in sources) { using var streamDoc = new OpenXmlMemoryStreamDocument(source.PmlDocument); using var doc = streamDoc.GetPresentationDocument(openSettings); try { if (source.KeepMaster) { foreach (var slideMasterPart in doc.PresentationPart.SlideMasterParts) { fluentBuilder.AppendMaster(doc, slideMasterPart); } } fluentBuilder.AppendSlides(doc, source.Start, source.Count); } catch (PresentationBuilderInternalException dbie) { if (dbie.Message.Contains("{0}")) { throw new PresentationBuilderException(string.Format(dbie.Message, sourceNum)); } throw; } sourceNum++; } }
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 static IList <PmlDocument> PublishSlides(PmlDocument src) { using var streamSrcDoc = new OpenXmlMemoryStreamDocument(src); using var srcDoc = streamSrcDoc.GetPresentationDocument(new OpenSettings { AutoSave = false }); return(PublishSlides(srcDoc, src.FileName).ToList()); }
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")); }