![](/img/trans.png)
[英]How to insert hyperlink in powerpoint slide using Open XML?
[英]Set image size in powerpoint using open xml
我正在使用本教程在這里生成一個 ppt 文件
步驟 4 描述了如何換出圖像占位符。 我的圖像有不同的尺寸,這使得一些圖像看起來有點太有趣了。
有什么方法可以調整占位符的大小以保持尺寸嗎?
編輯:好的,更好的解釋:用戶可以上傳自己的圖片。 圖像存儲在服務器上。 我正在生成一個 ppt 文件,每張幻燈片有一個用戶。 每張幻燈片都會有一張圖片,如果有的話。 我當然可以獲得每個圖像的尺寸,但是如何用占位符以外的另一個維度的圖像替換占位符?
好吧,我不能根據那個教程告訴你,但我可以告訴你它是在 Open XML 中完成的(即不是 SDK)。
您的圖片將有一個帶有一組值的xfrm
元素,如下所示:
<p:spPr>
<a:xfrm>
<a:off x="7048050" y="6248401"/>
<a:ext cx="972000" cy="288000"/>
</a:xfrm>
</p:spPr>
您要更改的值是a:ext
的cx
和cy
。 從System.Drawing.Image
對象中獲取新圖片的尺寸(h 和 w),並獲取每個值並乘以 12700。因此,如果圖片的寬度為 400 像素,則a:ext
的cx
值將是(400 x 12700 = 5080000)。
我是這樣做的:
using DocumentFormat.OpenXml.Packaging;
讓我們假設你有你的SlidePart
就我而言,我想檢查圖片的 alt 標題,如果與我的密鑰匹配,則替換它們。
//find all image alt title (description) in the slide
List<DocumentFormat.OpenXml.Presentation.Picture> slidePictures = slidePart.Slide.Descendants<DocumentFormat.OpenXml.Presentation.Picture>()
.Where(a => a.NonVisualPictureProperties.NonVisualDrawingProperties.Description.HasValue).Distinct().ToList();
現在我們檢查所有圖像:
//check all images in the slide and replace them if it matches our parameter
foreach (DocumentFormat.OpenXml.Presentation.Picture imagePlaceHolder in slidePictures)
現在在循環中我們尋找Transform2D
並用我們的值修改它:
Transform2D transform = imagePlaceHolder.Descendants<Transform2D>().First();
Tuple<Int64Value, Int64Value> aspectRatio = CorrectAspectRatio(param.Image.FullName, transform.Extents.Cx, transform.Extents.Cy);
transform.Extents.Cx = aspectRatio.Item1;
transform.Extents.Cy = aspectRatio.Item2;
這個函數看起來像這樣:
public static Tuple<Int64Value, Int64Value> CorrectAspectRatio(string fileName, Int64Value cx, Int64Value cy)
{
BitmapImage img = new();
using (FileStream fs = new(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
img.BeginInit();
img.StreamSource = fs;
img.EndInit();
}
int widthPx = img.PixelWidth;
int heightPx = img.PixelHeight;
const int EMUsPerInch = 914400;
Int64Value x = (Int64Value)(widthPx * EMUsPerInch / img.DpiX);
Int64Value y = (Int64Value)(heightPx * EMUsPerInch / img.DpiY);
if (x > cx)
{
decimal ratio = cx * 1.0m / x;
x = cx;
y = (Int64Value)(cy * ratio);
}
if (y > cy)
{
decimal ratio = cy * 1.0m / y;
y = cy;
x = (Int64Value)(cx * ratio);
}
return new Tuple<Int64Value, Int64Value>(x, y);
}
需要注意的重要一點是EMU per inch
是914400
。 在大多數情況下,您只需將其除以96
,但對於某些顯示器,情況有所不同。 因此,最好將它划分為x
和y
的DPI
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.