Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从openxml获取字体信息的更好方法_C#_Openxml_Openxml Sdk - Fatal编程技术网

C# 从openxml获取字体信息的更好方法

C# 从openxml获取字体信息的更好方法,c#,openxml,openxml-sdk,C#,Openxml,Openxml Sdk,我正在用OpenXML填写一种模板Word文件,而不是.dot。我遇到的问题是,当我向文档添加新文本时,使用Word文档的默认字体和字体大小。 我希望它使用我要添加到的段落的字体和大小,或者我要添加到的现有表中的格式 我找到了一条路,但我认为这不是一条好路。 那么…有更好的方法吗 这是一段话 private RunProperties GetRunPropertyFromParagraph(Paragraph paragraph) { var runPropert

我正在用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;
    }
这是一个桌上小房间里的

    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;
    }
代码是有效的。至少在我尝试过的文件中。 但这似乎是一种糟糕的做法,但我还没有找到更好的解决方案,因此如果您有一个解决方案,请分享:

我这样做: 当我更新或替换运行时,我确保在删除旧内容时进行检查,检查元素是否有子元素,然后检查它是否有一个子元素是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,除非它没有任何runproperties,在这种情况下,它不会添加任何runproperties,因此使用文档中的默认值。

这种方法的问题是,runproperties只存在于有run的情况下。可能有一个段落没有运行,在这种情况下,我认为您需要段落MarkRunProperties,但不确定在向该段落添加运行时如何使用它。