【发布时间】:2011-09-03 01:11:02
【问题描述】:
我正在尝试寻找可以从 OpenXml 生成 PDF 的 SDK。我已经使用 Open Xml Power Tools 来转换开放的 XML 和 html,并使用 iTextSharp 将 Html 解析为 PDF。但结果是一个看起来非常糟糕的 PDF。
我还没有尝试过 iText 的 RTF 解析器。如果我朝这个方向发展,我最终将需要一个 RTF 转换器,使简单的转换成为双步噩梦。
看起来我最终可能会编写一个基于电动工具 OpenXml 到 html 转换器的自定义转换器。任何建议表示赞赏。我现在真的不能选择专业的转换器,因为许可证太贵了(Aspose Word/TxText)。
我想我会在我的调查中付出更多的努力。我回到转换实用程序“http://msdn.microsoft.com/en-us/library/ff628051.aspx”并查看了它的代码。鉴于它错过的最大的事情是读取底层样式并生成样式属性。由于不处理自定义 true type 字体的限制,PDF 看起来好多了。明天再调查。我希望已经做了类似的事情/遇到了奇怪的问题,并且可以有所启发。
private static StringDictionary GetStyle(XElement el)
{
IEnumerable jcL = el.Elements(W.jc);
IEnumerable spacingL = el.Elements(W.spacing);
IEnumerable rPL = el.Elements(W.rPr);
StringDictionary sd = new StringDictionary();
if (HasAttribute(jcL, W.val)) sd.Add("text-align", GetAttribute(jcL, W.val));
// run prop exists
if (rPL.Count() > 0)
{
XElement r = rPL.First();
IEnumerable ftL = el.Elements(W.rFonts);
if (r.Element(W.b) != null) sd.Add("font-weight", "bolder");
if (r.Element(W.i) != null) sd.Add("font-style", "italic");
if (r.Element(W.u) != null) sd.Add("text-decoration", "underline");
if (r.Element(W.color) != null && HasAttribute(r.Element(W.color), W.val)) sd.Add("color", "#" + GetAttribute(r.Element(W.color), W.val));
if (r.Element(W.rFonts) != null )
{
//
if(HasAttribute(r.Element(W.rFonts), W.cs)) sd.Add("font-family", GetAttribute(r.Element(W.rFonts), W.cs));
else if (HasAttribute(r.Element(W.rFonts), W.hAnsi)) sd.Add("font-family", GetAttribute(r.Element(W.rFonts), W.hAnsi));
}
if (r.Element(W.sz) != null && HasAttribute(r.Element(W.sz), W.val)) sd.Add("font-size", GetAttribute(r.Element(W.sz), W.val) + "pt");
}
return sd.Keys.Count > 0 ? sd : null;
}
【问题讨论】: