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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
| using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Presentation;
using DocumentFormat.OpenXml.Validation;
namespace safeprojectname
{
public partial class Form1 : Form
{
static uint uniqueId;
public Form1()
{
InitializeComponent();
}
static private uint GetMaxSlideMasterId(SlideMasterIdList slideMasterIdList)
{
uint max = 2147483648;
if (slideMasterIdList != null)
{// retourne l'id max de la liste des enfant
foreach (SlideMasterId child in slideMasterIdList.Elements<SlideMasterId>())
{
uint id = child.Id;
max = id;
}
}
return max;
}
static private uint GetmaxSlideId(SlideIdList slideIdList)
{
uint max = 256;
if (slideIdList != null)
{// retourne l'id max de la liste des enfant
foreach (SlideId child in slideIdList.Elements<SlideId>())
{
uint id = child.Id;
max = id;
}
}
return max;
}
//make sure all slidee layout have an unique ids"
static private void FixSlideLayoutIds(PresentationPart presPart)
{
foreach (SlideMasterPart slideMasterpart in presPart.SlideMasterParts)
{
foreach (SlideLayoutId slideLayoutId in slideMasterpart.SlideMaster.SlideLayoutIdList)
{
uniqueId++;
slideLayoutId.Id = (uint)uniqueId;
}
slideMasterpart.SlideMaster.Save();
}
}
private string ValidateError(IEnumerable<ValidationErrorInfo> errors)
{
int errorindex = 1;
StringBuilder builder = new StringBuilder();
foreach (ValidationErrorInfo errorinfo in errors)
{
builder.Append(errorinfo.Description);
builder.Append("\n");
builder.Append(errorinfo.Path.XPath);
if (++errorindex <= errors.Count())
{
builder.Append("---------------------");
}
}
return builder.ToString();
}
private static void MergePresentationSlides(string sourceFolderLocation, string sourcePresentation, string targetFolderPresentation, string targetPresentation)
{
int id = 0;
using (PresentationDocument myDestinationDeck = PresentationDocument.Open(targetFolderPresentation + targetPresentation, true))
{
PresentationPart targetPresPart = myDestinationDeck.PresentationPart;
if (targetPresPart.Presentation.SlideIdList == null)//verifie que le fichier powerpoint de destination possede une list de slide ayant un id
{ targetPresPart.Presentation.SlideIdList = new SlideIdList();
}
// on ouvre les fichiers sources
using (PresentationDocument mySourceDeck = PresentationDocument.Open(sourceFolderLocation + sourcePresentation, true))
{
PresentationPart sourcePresentationPart = mySourceDeck.PresentationPart;
// recherche les ids unique du slide master et les slide list pour la prochaine utilisation
uniqueId = GetMaxSlideMasterId(targetPresPart.Presentation.SlideMasterIdList);
uint maxSlideId = GetmaxSlideId(targetPresPart.Presentation.SlideIdList);
foreach (SlideId slideId in sourcePresentationPart.Presentation.SlideIdList)
{
SlidePart sp;
SlidePart destSp;
SlideMasterPart destMasterPart;
string relId;
SlideMasterId newSlideMasterId;
SlideId newSlideId;
//create a unique relationshipId
id++;
sp = (SlidePart)sourcePresentationPart.GetPartById(slideId.RelationshipId);
relId = sourcePresentation.Remove(sourcePresentation.IndexOf('.')) + id;
destSp = targetPresPart.AddPart<SlidePart>(sp, relId);
//la partie master a ete ajoutee.veridifions que la relation est en place
destMasterPart = destSp.SlideLayoutPart.SlideMasterPart;
targetPresPart.AddPart(destMasterPart);
// add slide master to slide list
uniqueId++;
newSlideMasterId = new SlideMasterId();
newSlideMasterId.RelationshipId = targetPresPart.GetIdOfPart(destMasterPart);
newSlideMasterId.Id = uniqueId;
// add slide to slide list
maxSlideId++;
newSlideId = new SlideId();
newSlideId.RelationshipId = relId;
newSlideId.Id = maxSlideId;
targetPresPart.Presentation.SlideMasterIdList.Append(newSlideMasterId);
targetPresPart.Presentation.SlideIdList.Append(newSlideId);
}
// make sure that the slide are unique
FixSlideLayoutIds(targetPresPart);
//save
targetPresPart.Presentation.Save();
}
}
}
private void buttonMerge()
{
string mergedPresentation = saveFileDialog.FileName;
string presentationTemplate = openFileDialogTemplate.FileName;
string[] sourcePresentations = openFileDialogPresentations.FileNames;
try
{
File.Copy(presentationTemplate, mergedPresentation, true);// si le template n'existe pas il yaura une exception
}
catch(Exception)
{
MessageBox.Show("Veuillez saisir un template");
}
foreach( string sourcePresentation in sourcePresentations)
{
MergePresentationSlides(sourcePresentation.Substring(0,sourcePresentation.LastIndexOf("\\"))+@"\",
sourcePresentation.Replace(sourcePresentation.Substring(0,sourcePresentation.LastIndexOf("\\")) + @"\",""),
mergedPresentation.Substring(0, mergedPresentation.LastIndexOf("\\"))+@"\",
mergedPresentation.Replace(mergedPresentation.Substring(0,mergedPresentation.LastIndexOf("\\")) + @"\",""));
OpenXmlValidator validator = new OpenXmlValidator();
PresentationDocument prezo = PresentationDocument.Open(mergedPresentation, true);
var errors = validator.Validate(prezo);
if (errors.Count() > 0)
{
StringBuilder builder = new StringBuilder();
builder.Append("the merge process completed but the merged presentation fail to validate");
builder.Append("\n");
builder.Append("there are " + errors.Count() + "errors:\r\n");
builder.Append(ValidateError(errors));
MessageBox.Show(builder.ToString());
}
else
MessageBox.Show(" le processus de fusion s'est deroulé avec succès");
}
}
private void buttonPresentations_Click(object sender, EventArgs e)
{
openFileDialogPresentations.ShowDialog();
}
private void buttonTemplate_Click(object sender, EventArgs e)
{
openFileDialogTemplate.ShowDialog();
}
private void buttonMergedOutput_Click(object sender, EventArgs e)
{
saveFileDialog.ShowDialog();
}
private void button1_Click(object sender, EventArgs e)
{
buttonMerge();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
} |
Partager