[Office 2010 開發 ] 利用 OpenXML 來把你的多份 PowerPoint 合併在一起
而在本文中將會主要介紹下方幾項實作內容:
- 透過 Open XML SDK 開啟 PowerPoint 目標文件檔
- 再開啟 PowerPoint 來源文件檔
- 針對目標文件在來源檔新增投影片
- 把每一個投影片都附予唯一的 ID 值以辨識
- 存檔
>> 這是來源檔案 ( one.pptx )
>> 這是目標檔案 ( Two.pptx )
>> 這是最終的整合後檔案 ( output.pptx )
☆ 程式部份
◇ 請先建立一個 Concole Application (主控台)
◇ 再把 Program.cs 檔更改成如下的程式碼
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: using System.IO;
6: using System.IO.Packaging;
7: using DocumentFormat.OpenXml;
8: using DocumentFormat.OpenXml.Packaging;
9: using DocumentFormat.OpenXml.Presentation;
10:
11: namespace AssemblePowerPointDecks
12: {
13: class Program
14: {
15: //可以透過 ID 來針對兩份投影片的主 ID 及投影片佈景主題 ID 清單
16: static uint uniqueId;
17:
18: static void Main(string[] args)
19: {
20: string destDeck = "output.pptx";
21:
22: File.Copy("one.pptx", destDeck, true);
23: MergeDecks("Two.pptx", destDeck);
24: }
25:
26: static void MergeDecks(string sourceDeck, string destDeck)
27: {
28: int id = 1;
29: //開啟目標檔案
30: using (PresentationDocument myDestDeck = PresentationDocument.Open(destDeck, true))
31: {
32: PresentationPart destPresPart = myDestDeck.PresentationPart;
33:
34: //開啟來源檔案
35: using (PresentationDocument mySourceDeck = PresentationDocument.Open(sourceDeck, true))
36: {
37: PresentationPart sourcePresPart = mySourceDeck.PresentationPart;
38:
39: //需取得投影片(佈景主題)清單中唯一的id以利稍後使用
40: uniqueId = GetMaxIdFromChild(destPresPart.Presentation.SlideMasterIdList);
41: uint maxSlideId = GetMaxIdFromChild(destPresPart.Presentation.SlideIdList);
42:
43: //複製每一個來源投影片
44: foreach (SlideId slideId in sourcePresPart.Presentation.SlideIdList)
45: {
46: SlidePart sp;
47: SlidePart destSp;
48: SlideMasterPart destMasterPart;
49: string relId;
50: SlideMasterId newSlideMasterId;
51: SlideId newSlideId;
52:
53: //準備唯一關連的ID值
54: id++;
55: sp = (SlidePart)sourcePresPart.GetPartById(slideId.RelationshipId);
56: relId = sourceDeck.Remove(sourceDeck.IndexOf('.')) + id;
57: destSp = destPresPart.AddPart<SlidePart>(sp, relId);
58:
59: //Master part was added, but now we need to make sure the relationship is in place
60: destMasterPart = destSp.SlideLayoutPart.SlideMasterPart;
61: destPresPart.AddPart(destMasterPart);
62:
63: //新增投影片母片至投影片母片清單中
64: uniqueId++;
65: newSlideMasterId = new SlideMasterId();
66: newSlideMasterId.RelationshipId = destPresPart.GetIdOfPart(destMasterPart);
67: newSlideMasterId.Id = uniqueId;
68:
69: //新增投影片至投影片清單中
70: maxSlideId++;
71: newSlideId = new SlideId();
72: newSlideId.RelationshipId = relId;
73: newSlideId.Id = maxSlideId;
74:
75: destPresPart.Presentation.SlideMasterIdList.Append(newSlideMasterId);
76: destPresPart.Presentation.SlideIdList.Append(newSlideId);
77: }
78: //確定所有投影片的 id 皆是唯一值
79: FixSlideLayoutIds(destPresPart);
80: }
81: destPresPart.Presentation.Save();
82: }
83: }
84:
85: static void FixSlideLayoutIds(PresentationPart presPart)
86: {
87: //確定所有投影片佈景主題是有唯一 id
88: foreach (SlideMasterPart slideMasterPart in presPart.SlideMasterParts)
89: {
90: foreach (SlideLayoutId slideLayoutId in slideMasterPart.SlideMaster.SlideLayoutIdList)
91: {
92: uniqueId++;
93: slideLayoutId.Id = (uint)uniqueId;
94: }
95: slideMasterPart.SlideMaster.Save();
96: }
97: }
98:
99: static uint GetMaxIdFromChild(OpenXmlElement el)
100: {
101: uint max = 1;
102: //從設立的子群中取得最大ID值
103: foreach (OpenXmlElement child in el.ChildElements)
104: {
105: OpenXmlAttribute attribute = child.GetAttribute("id", "");
106:
107: uint id = uint.Parse(attribute.Value);
108:
109: if (id > max)
110: max = id;
111: }
112: return max;
113: }
114: }
115: }
>> 檔案下載:點我下載
---> 本文預設於 2010.05.20 登入「Office/Sharepoint 開發組」
>> 參考翻譯及引用:How to Assemble Multiple PowerPoint Decks