从openxml获取Font信息的更好方法

时间:2013-03-22 09:34:07

标签: c# openxml openxml-sdk

我正在用OpenXML填写一种模板Word文件(不是.dot)。我遇到的问题是,当我向文档添加新文本时,使用Word文档的默认字体和字体大小。 我希望它使用我正在添加的段落的字体和大小或我正在添加的现有表格中的格式。

我找到了一种方法,但我认为这不是一个好方法。 那么......有更好的方法吗?

这是来自段落

    private RunProperties GetRunPropertyFromParagraph(Paragraph paragraph)
    {
        var runProperties = new RunProperties();
        var fontname = "Calibri";
        var fontSize = "18";
        try
        {
            fontname =
                paragraph.GetFirstChild<ParagraphProperties>()
                         .GetFirstChild<ParagraphMarkRunProperties>()
                         .GetFirstChild<RunFonts>()
                         .Ascii;
        }
        catch
        {
            //swallow
        }
        try
        {
            fontSize =
                paragraph.GetFirstChild<Paragraph>()
                         .GetFirstChild<ParagraphProperties>()
                         .GetFirstChild<ParagraphMarkRunProperties>()
                         .GetFirstChild<FontSize>()
                         .Val;
        }
        catch
        {
            //swallow
        }
        runProperties.AppendChild(new RunFonts() { Ascii = fontname });
        runProperties.AppendChild(new FontSize() { Val = fontSize });
        return runProperties;
    }

这来自TableCell

    private RunProperties GetRunPropertyFromTableCell(TableRow rowCopy, int cellIndex)
    {
        var runProperties = new RunProperties();
        var fontname = "Calibri";
        var fontSize = "18";
        try
        {
            fontname =
                rowCopy.Descendants<TableCell>()
                       .ElementAt(cellIndex)
                       .GetFirstChild<Paragraph>()
                       .GetFirstChild<ParagraphProperties>()
                       .GetFirstChild<ParagraphMarkRunProperties>()
                       .GetFirstChild<RunFonts>()
                       .Ascii;
        }
        catch
        {
            //swallow
        }
        try
        {
            fontSize =
                   rowCopy.Descendants<TableCell>()
                          .ElementAt(cellIndex)
                          .GetFirstChild<Paragraph>()
                          .GetFirstChild<ParagraphProperties>()
                          .GetFirstChild<ParagraphMarkRunProperties>()
                          .GetFirstChild<FontSize>()
                          .Val;
        }
        catch
        {
            //swallow
        }
        runProperties.AppendChild(new RunFonts() { Ascii = fontname });
        runProperties.AppendChild(new FontSize() { Val = fontSize });
        return runProperties;
    }

代码有效。至少在我试过的文件中。 但这似乎是一种糟糕的做法,但我还没有找到更好的解决方案,所以如果你有一个请分享:)

1 个答案:

答案 0 :(得分:0)

我是这样做的: 当我更新或替换Run时,我确保检查何时删除旧的东西,如果元素有子元素,然后是否有子元素是“RunProperties”。 如果是这样,我只需保存这些属性,并在删除旧内容后将它们添加到我的新运行中。

我还没有失败。

var bkms = YourBookmarkStart;
OpenXmlElement elem = bkms.NextSibling();
RunProperties oldRunProperties = null;
while (elem != null && !(elem is BookmarkEnd))
{
    OpenXmlElement nextElem = elem.NextSibling();
    if (elem.HasChildren)
    {
        try
        {
             oldRunProperties = (RunProperties)elem.GetFirstChild<RunProperties>().Clone();
        }
        catch { }
    }
    if (!(elem is BookmarkStart))
        elem.Remove();
    elem = nextElem;
}

Run r = new Run(new Text(newText));
if (oldRunProperties != null) r.PrependChild<RunProperties>(oldRunProperties);
bkms.Parent.InsertAfter<Run>(r, bkms);

它应该获取给定段落的任何runproperties,除非它没有任何属性,在这种情况下它不会添加任何属性,因此使用文档中的默认值。