out*_*ind 6 c# powerpoint openxml openxml-sdk presentationml
我想创建一个包含来自数据库的数据的演示文稿.我设法获得在PowerPoint中打开的有效演示文稿(SDK 2.5 的Open XML Productivity Tool对此有很大帮助).但是,如何计算文本框形状的大小?我看到放置值的位置,但new Extents()默认为零宽度和高度.当我Shape从现有演示文稿中获取a的某些值时,我可能会获得正确的高度(至少对于带有一行或固定行数的文本),但文本将溢出到右侧或底部(取决于wordwrap)设置).
也将NormalAutoFit在BodyProperties的TextBody不帮助-为必要的值FontScale并LineSpaceReduction没有在计算的OpenXML.
那么,设置Extentsa 的最佳做法Shape是TextBody什么?
是否有内置的方法来计算Extents给定的TextBody或Shape?(内置方法的一些经验法则会比什么都好)
我知道PowerPoint将重新计算NormalAutoFit任何更改后的值(至少在更改之前和之后的一堆幻灯片),但是在更改之前启动演示文稿时(或者如果它是从PowerPoint查看器开始).
这是一项重要(但可行)的任务。
经过多次实验,我发现 System.Windows.Forms.TextRenderer 中的文本度量方法给了我最好的结果。这是 WmlToHtmlConverter 使用的文本度量功能。您可以查看 WmlToHtmlConverter 中的代码作为使用 TextRenderer 的一个示例。
这是我根据 Eric White 的 WmlToHtmlConverter、这篇文章和这篇. 我使用它来计算文本水印的 TextBox 尺寸和 Word 文档的 OpenXml 图像水印的尺寸。
private static D.Size pixelsToEmus(int widthPx, int heightPx, double resDpiX, double resDpiY, int zoomX, int zoomY)
{
const int emusPerInch = 914400;
const int emusPerCm = 360000;
const decimal maxWidthCm = 16.51m;
var widthEmus = (int)(widthPx / resDpiX * emusPerInch) * zoomX / 100;
var heightEmus = (int)(heightPx / resDpiY * emusPerInch) * zoomY / 100;
var maxWidthEmus = (int)(maxWidthCm * emusPerCm);
if (widthEmus > maxWidthEmus)
{
var ratio = ((decimal)heightEmus / (decimal)widthEmus);
widthEmus = maxWidthEmus;
heightEmus = (int)(widthEmus * ratio);
}
return new D.Size(widthEmus, heightEmus);
}
public static D.Size GetTextSize(this CWatermarkItemBase watermark, string runText)
{
var fs = watermark.GetFontStyle();
var sz = watermark.FontSize;
var proposedSize = new D.Size(int.MaxValue, int.MaxValue);
D.Size sf;
using (var ff = new D.FontFamily(watermark.FontFamily))
{
try
{
using (var f = new D.Font(ff, (float)sz, fs))
{
const TextFormatFlags tff = TextFormatFlags.NoPadding;
sf = TextRenderer.MeasureText(runText, f, proposedSize, tff);
}
}
catch (ArgumentException)
{
try
{
const D.FontStyle fs2 = D.FontStyle.Regular;
using (D.Font f = new D.Font(ff, (float)sz, fs2))
{
const TextFormatFlags tff = TextFormatFlags.NoPadding;
sf = TextRenderer.MeasureText(runText, f, proposedSize, tff);
}
}
catch (ArgumentException)
{
const D.FontStyle fs2 = D.FontStyle.Bold;
try
{
using (var f = new D.Font(ff, (float)sz, fs2))
{
const TextFormatFlags tff = TextFormatFlags.NoPadding;
sf = TextRenderer.MeasureText(runText, f, proposedSize, tff);
}
}
catch (ArgumentException)
{
// if both regular and bold fail, then get metrics for Times New Roman
// use the original FontStyle (in fs)
using (var ff2 = new D.FontFamily("Times New Roman"))
using (var f = new D.Font(ff2, (float)sz, fs))
{
const TextFormatFlags tff = TextFormatFlags.NoPadding;
sf = TextRenderer.MeasureText(runText, f, proposedSize, tff);
}
}
}
}
}
D.Size s2 = pixelsToEmus(sf.Width, sf.Height, 96, 96, 100, 100);
return s2;
}
public static D.Size GetImageSize(this CWatermarkItemImage watermarkItem)
{
var img = new BitmapImage(new Uri(watermarkItem.FilePath, UriKind.RelativeOrAbsolute));
return pixelsToEmus(img.PixelWidth, img.PixelHeight, img.DpiX, img.DpiY, watermarkItem.ZoomWidth, watermarkItem.ZoomHeight);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1051 次 |
| 最近记录: |