[英]How to resize and image in a PowerPoint presentation using c# and OpenXML
抱歉,如果這已經得到回答,但我已經看到很多類似的帖子,到目前為止沒有任何幫助。
基本上,我正在構建一個應用程序,它采用 PowerPoint“模板”並替換來自數據庫的文本和圖像。 大部分內容已完成,但我在替換圖像時正在努力解決問題。 該模板插入了大量空白圖像,隨時可以替換,它們都是方形的,因為它們只是占位符。 我可以毫無問題地替換它們,但是根據我從數據庫中提取的圖像,新圖像會垂直或水平拉伸。 它基本上是填充占位符形狀。
我必須承認我發現很難理解文檔模型,所以也許我試圖改變錯誤的東西,我曾試圖改變 blip.parent.shape 的“范圍”屬性,但我在猜測和沒有快樂。
下面是用於交換圖像的代碼(歸功於原作者 amos http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=2291 )。 一旦圖像到位或之前,我什至不確定在哪里嘗試調整大小......任何幫助將不勝感激。 我現在不擔心實際大小,我可以自己計算,只是能夠更改圖像大小才是問題。
以及示例代碼(可能不是那么有用),我已經鏈接到我的示例項目。 樣本和文件都應該放在 c:\\test 中才能工作。 它只是一些圖像、c# 項目和 program.cs 文件,以及一個示例 PPTX 文件。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Presentation;
using d = DocumentFormat.OpenXml.Drawing;
using System.IO;
namespace PPTX_Image_Replace_test
{
class Program
{
static void Main(string[] args)
{
TestImageReplace(@"C:\test\TestImageReplaceAndResize.pptx");
}
public static void TestImageReplace(string fileName)
{
// make a copy of the original and edit that
string outputFileName = Path.Combine(Path.GetDirectoryName(fileName), "New_" + Path.GetFileName(fileName));
File.Copy(fileName, outputFileName,true);
using (PresentationDocument document = PresentationDocument.Open(outputFileName, true))
{
ReplaceFirstImageMatchingAltText(document, @"C:\test\Arrow_UP.png", "changeme");
}
}
public static void ReplaceFirstImageMatchingAltText(PresentationDocument presentationDocument, string newImagePath, string alternateTextToFind)
{
OpenXmlElementList slideIds = presentationDocument.PresentationPart.Presentation.SlideIdList.ChildElements;
foreach (SlideId sID in slideIds) // loop thru the SlideIDList
{
string relId = sID.RelationshipId; // get first slide relationship
SlidePart slide = (SlidePart)presentationDocument.PresentationPart.GetPartById(relId); // Get the slide part from the relationship ID.
var pictures = slide.Slide.Descendants<ShapeTree>().First().Descendants<Picture>().ToList();
foreach (var picture in pictures)
{
// get photo desc to see if it matches search text
var nonVisualPictureProperties = picture.Descendants<NonVisualPictureProperties>().FirstOrDefault();
if (nonVisualPictureProperties == null)
continue;
var nonVisualDrawingProperties99 = nonVisualPictureProperties.Descendants<NonVisualDrawingProperties>().FirstOrDefault();
if (nonVisualDrawingProperties99 == null)
continue;
var desc = nonVisualDrawingProperties99.Description;
if (desc == null || desc.Value == null || !desc.Value.Contains(alternateTextToFind))
continue;
BlipFill blipFill = picture.Descendants<BlipFill>().First();
var blip = blipFill.Descendants<DocumentFormat.OpenXml.Drawing.Blip>().First();
string embedId = blip.Embed; // now we need to find the embedded content and update it.
// find the content
ImagePart imagePart = (ImagePart)slide.GetPartById(embedId);
if (imagePart != null)
{
using (FileStream fileStream = new FileStream(newImagePath, FileMode.Open))
{
imagePart.FeedData(fileStream);
fileStream.Close();
}
return; // found the photo and replaced it.
}
}
}
}
}
}
通常在幾天后無處可去,一旦我發布問題,我就會找到答案! 最后非常簡單。
我已經有了對圖片的引用,所以我簡單地修改了那里的 trasnform,最后一點代碼現在看起來像這樣..
// find the content
ImagePart imagePart = (ImagePart)slide.GetPartById(embedId);
if (imagePart != null)
{
d.Transform2D transform = picture.Descendants<d.Transform2D>().First();
transform.Extents.Cx = 800000; // replace with correct calcualted values eventually
transform.Extents.Cy = 400000; // replace with correct calculated values eventually
using (FileStream fileStream = new FileStream(newImagePath, FileMode.Open))
{
imagePart.FeedData(fileStream);
fileStream.Close();
}
return; // found the photo and replaced it.
}
猜猜我只是看不到樹木的木材......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.