作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个扩展名为 .potx 的 powerpoint 模板,我需要创建模板文件的副本,但创建的文件中没有任何内容,它是一个空白演示文稿,下面是我正在使用的功能使用模板文件创建目标文件。
/// <summary>
/// Creates a copy of the source template file
/// </summary>
/// <param name="sourceFile">sourcce file which has to be copied</param>
/// <param name="destinationFile">destination file where the new file has to be placed.</param>
public static void CreateFileFromTemplate(this string sourceFile, string destinationFile)
{
try
{
//File.Copy(sourceFile, destinationFile, true);
if (System.IO.Path.GetExtension(sourceFile) == ".potx")
{
if (System.IO.Path.GetExtension(destinationFile) == ".potx")
{
PresentationDocument destinationDoc = PresentationDocument.Create(destinationFile, PresentationDocumentType.Presentation);
PresentationPart presentationPart = destinationDoc.AddPresentationPart();
presentationPart.Presentation = new Presentation();
presentationPart.Presentation.Save();
destinationDoc.Close();
//Package destinationPackage = Package.Open(destinationFile, FileMode.Open, FileAccess.ReadWrite);
using (destinationDoc = PresentationDocument.Open(destinationFile, true))
{
Package templatePackage = Package.Open(sourceFile, FileMode.Open);
using (PresentationDocument templateDocument = PresentationDocument.Open(templatePackage))
{
PresentationPart sourcePresPart = templateDocument.PresentationPart;
PresentationPart destinationPresrPart = destinationDoc.PresentationPart;
//var templateSlideCount = templateDocument.CountSlides();
//for (int slides = 1; slides <= templateSlideCount; slides++)
//{
//}
SlidePart sourceSlidePart;
SlidePart destinationSlidePart;
foreach (SlideId slideId in sourcePresPart.Presentation.SlideIdList)
{
//int i=0;
sourceSlidePart = (SlidePart)sourcePresPart.GetPartById(slideId.RelationshipId);
destinationSlidePart = sourceSlidePart.Clone();
SlideIdList slideIdList = destinationPresrPart.Presentation.SlideIdList;
destinationPresrPart.AddPart(destinationSlidePart);
// Save the modified presentation.
destinationPresrPart.Presentation.Save();
}
}
}
}
else
{
throw new FileFormatException("Invalid destination file format, Valid file should have .pptx as extension");
}
}
else
{
throw new FileFormatException("Invalid file format, Valid file should have .potx as extension");
}
}
catch (Exception ex)
{
throw;
}
}
用于克隆幻灯片的扩展方法
/// <summary>
/// Clones the specified slide.
/// </summary>
/// <param name="sourceSlide">The slide to clone.</param>
/// <returns>Cloned slide.</returns>
public static SlidePart Clone(this SlidePart sourceSlide)
{
// find the presentationPart: makes the API more fluent
var presentationPart = sourceSlide.GetParentParts()
.OfType<PresentationPart>()
.Single();
// clone slide contents
Slide currentSlide = (Slide)sourceSlide.Slide.CloneNode(true);
var slidePartClone = presentationPart.AddNewPart<SlidePart>();
currentSlide.Save(slidePartClone);
// copy layout part
slidePartClone.AddPart(sourceSlide.SlideLayoutPart);
//copy all embedded elements
foreach (ChartPart part in sourceSlide.ChartParts)
{
ChartPart newpart = slidePartClone.AddNewPart<ChartPart>(part.ContentType, sourceSlide.GetIdOfPart(part));
newpart.FeedData(part.GetStream());
newpart.AddNewPart<EmbeddedPackagePart>(part.EmbeddedPackagePart.ContentType, part.GetIdOfPart(part.EmbeddedPackagePart));
newpart.EmbeddedPackagePart.FeedData(part.EmbeddedPackagePart.GetStream());
}
foreach (EmbeddedObjectPart part in sourceSlide.EmbeddedObjectParts)
{
EmbeddedObjectPart newpart = slidePartClone.AddNewPart<EmbeddedObjectPart>(part.ContentType, sourceSlide.GetIdOfPart(part));
newpart.FeedData(part.GetStream());
}
foreach (EmbeddedPackagePart part in sourceSlide.EmbeddedPackageParts)
{
EmbeddedPackagePart newpart = slidePartClone.AddNewPart<EmbeddedPackagePart>(part.ContentType, sourceSlide.GetIdOfPart(part));
newpart.FeedData(part.GetStream());
}
foreach (ImagePart part in sourceSlide.ImageParts)
{
ImagePart newpart = slidePartClone.AddNewPart<ImagePart>(part.ContentType, sourceSlide.GetIdOfPart(part));
newpart.FeedData(part.GetStream());
}
foreach (VmlDrawingPart part in sourceSlide.VmlDrawingParts)
{
VmlDrawingPart newpart = slidePartClone.AddNewPart<VmlDrawingPart>(part.ContentType, sourceSlide.GetIdOfPart(part));
newpart.FeedData(part.GetStream());
}
foreach (UserDefinedTagsPart part in sourceSlide.UserDefinedTagsParts)
{
UserDefinedTagsPart newpart = slidePartClone.AddNewPart<UserDefinedTagsPart>(part.ContentType, sourceSlide.GetIdOfPart(part));
newpart.FeedData(part.GetStream());
}
return slidePartClone;
}
在我的主要方法中,我有以下代码调用这些函数来创建 ppt
string sourceFilePath =@"C:\TempFolder\template.potx";
string destinationFilePath = @"C:\TempFolder\ClonedFile.potx";
sourceFilePath.CreateFileFromTemplate(destinationFilePath);
但最终创建的ppt里面不会有任何幻灯片,
任何帮助将不胜感激
最佳答案
我之前发布了一个关于将 excel 模板保存到实际 excel 文档的类似答案 here并且您应该能够将相同的逻辑应用于 powerpoint 模板。您需要使用 PresentationDocument
而不是 SpreadsheetDocument
并对其调用 ChangeDocumentType。沿着这些线的东西:
byte[] byteArray = File.ReadAllBytes("C:\\temp\\sa123.potx");
using (MemoryStream stream = new MemoryStream())
{
stream.Write(byteArray, 0, (int)byteArray.Length);
using (PresentationDocument presentationDoc = PresentationDocument.Open(stream, true))
{
// Change from template type to presentation type
presentationDoc.ChangeDocumentType(PresentationDocumentType.Presentation);
}
File.WriteAllBytes("C:\\temp\\sa123.pptx", stream.ToArray());
}
然后您可以重新打开保存的文件,并在不触及模板文件的情况下对它执行任何您想要的额外操作。
关于c# - 使用 openXML 创建模板演示文稿 ".potx"的副本到新的 ".pptx",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30190697/
我是一名优秀的程序员,十分优秀!