This repository has been archived by the owner on Feb 5, 2019. It is now read-only.
forked from OpenXmlDev/Open-Xml-PowerTools
-
Notifications
You must be signed in to change notification settings - Fork 26
/
PresentationBuilder02.cs
85 lines (77 loc) · 4.19 KB
/
PresentationBuilder02.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using DocumentFormat.OpenXml.Packaging;
using OpenXmlPowerTools;
namespace PresentationBuilder02
{
class PresentationBuilder02
{
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"));
}
}
}