c# - 使用 openxml 合并演示文稿文件时显示空白的图像
问题描述
我取消使用 OpenXml 将幻灯片从多个演示文稿复制到新演示文稿。下面的代码工作正常,除了复制幻灯片后没有显示图像。创建新的演示文件时图像显示为空白。
public async Task<bool> Copy(PresentationDetailsModel slides)
{
string path = HttpContext.Current.Server.MapPath("~/"); //get server path
string mergedPresentation = "newlygen.pptx"; //create new ppt
string presentationTemplate = "present1.pptx"; //copy presentation file
string[] sourcePresentations = new string[]
{ "present2.pptx" }; //files list for copying slides
string presentationFolder = path + @"/Data/";
// Make a copy of the template presentation. This will throw an
// exception if the template presentation does not exist.
File.Copy(presentationFolder + presentationTemplate,
presentationFolder + mergedPresentation, true);
// Loop through each source presentation and merge the slides
// into the merged presentation.
foreach (string sourcePresentation in sourcePresentations)
MergeSlides(presentationFolder, sourcePresentation,
mergedPresentation);
}
static void MergeSlides(string presentationFolder, string sourcePresentation, string destPresentation)
{
int id = 0;
// Open the destination presentation.
using (PresentationDocument myDestDeck =
PresentationDocument.Open(presentationFolder + destPresentation,
true))
{
PresentationPart destPresPart = myDestDeck.PresentationPart;
// If the merged presentation does not have a SlideIdList
// element yet, add it.
if (destPresPart.Presentation.SlideIdList == null)
destPresPart.Presentation.SlideIdList = new SlideIdList();
// Open the source presentation. This will throw an exception if
// the source presentation does not exist.
using (PresentationDocument mySourceDeck = PresentationDocument.Open(presentationFolder + sourcePresentation, false))
{
PresentationPart sourcePresPart =
mySourceDeck.PresentationPart;
// Get unique ids for the slide master and slide lists
// for use later.
var uniqueId =
GetMaxSlideMasterId(
destPresPart.Presentation.SlideMasterIdList);
uint maxSlideId =
GetMaxSlideId(destPresPart.Presentation.SlideIdList);
// Copy each slide in the source presentation, in order, to
// the destination presentation.
foreach (SlideId slideId in
sourcePresPart.Presentation.SlideIdList)
{
SlidePart sp;
SlidePart destSp;
SlideMasterPart destMasterPart;
string relId;
SlideMasterId newSlideMasterId;
SlideId newSlideId;
// Create a unique relationship id.
id++;
sp =
(SlidePart)sourcePresPart.GetPartById(
slideId.RelationshipId);
relId =
sourcePresentation.Remove(
sourcePresentation.IndexOf('.')) + id;
// Add the slide part to the destination presentation.
destSp = destPresPart.AddPart<SlidePart>(sp, relId);
// The slide master part was added. Make sure the
// relationship between the main presentation part and
// the slide master part is in place.
destMasterPart = destSp.SlideLayoutPart.SlideMasterPart;
destPresPart.AddPart(destMasterPart);
// Add the slide master id to the slide master id list.
uniqueId++;
newSlideMasterId = new SlideMasterId();
newSlideMasterId.RelationshipId =
destPresPart.GetIdOfPart(destMasterPart);
newSlideMasterId.Id = uniqueId;
destPresPart.Presentation.SlideMasterIdList.Append(
newSlideMasterId);
// Add the slide id to the slide id list.
maxSlideId++;
newSlideId = new SlideId();
newSlideId.RelationshipId = relId;
newSlideId.Id = maxSlideId;
destPresPart.Presentation.SlideIdList.Append(newSlideId);
}
// Make sure that all slide layout ids are unique.
FixSlideLayoutIds(destPresPart, uniqueId);
}
// Save the changes to the destination deck.
destPresPart.Presentation.Save();
}
}
static void FixSlideLayoutIds(PresentationPart presPart, uint uniqueId)
{
// Make sure that all slide layouts have unique ids.
foreach (SlideMasterPart slideMasterPart in
presPart.SlideMasterParts)
{
foreach (SlideLayoutId slideLayoutId in
slideMasterPart.SlideMaster.SlideLayoutIdList)
{
uniqueId++;
slideLayoutId.Id = (uint)uniqueId;
}
slideMasterPart.SlideMaster.Save();
}
}
static uint GetMaxSlideId(SlideIdList slideIdList)
{
// Slide identifiers have a minimum value of greater than or
// equal to 256 and a maximum value of less than 2147483648.
uint max = 256;
if (slideIdList != null)
// Get the maximum id value from the current set of children.
foreach (SlideId child in slideIdList.Elements<SlideId>())
{
uint id = child.Id;
if (id > max)
max = id;
}
return max;
}
static uint GetMaxSlideMasterId(SlideMasterIdList slideMasterIdList)
{
// Slide master identifiers have a minimum value of greater than
// or equal to 2147483648.
uint max = 2147483648;
if (slideMasterIdList != null)
// Get the maximum id value from the current set of children.
foreach (SlideMasterId child in
slideMasterIdList.Elements<SlideMasterId>())
{
uint id = child.Id;
if (id > max)
max = id;
}
return max;
}
上面的代码适用于将幻灯片从一个演示文稿复制到新创建的演示文稿,但特定幻灯片的图像显示为空白。任何想法?我发现下面的代码。谁能帮我将下面的代码与上面的代码合并。
foreach (sourceSlide.ImageParts 中的 ImagePart 部分) { ImagePart newpart = slidePartClone.AddNewPart(part.ContentType, sourceSlide.GetIdOfPart(part)); newpart.FeedData(part.GetStream()); }
解决方案
推荐阅读
- python - 如何将多个用户输入添加到文本文件?
- python - 从 Github API 解码 base64 内容
- mobile - 如何创建 M-dot 网站?
- php - mysqli_connect(): (HY000/2002): 使用 ipv6 地址拒绝连接
- python - 当我将损失乘以标量时,为什么在训练 Keras 模型时会得到不同的结果?
- php - Sonata 管理员无法加载“Sonata\CoreBundle\Twig\Extension\FlashMessageRuntime”运行时
- dns - 公司域分配 Heroku IP
- c - 使用scanf函数运行for循环的正确方法是什么
- swift - collectionViewCell.xib 中的容器视图在哪里
- node.js - 运行节点命令时出现错误