使用Open XML更改文档的字体

时间:2016-09-09 04:01:32

标签: java fonts ms-word openxml document

我无法使用Open XML更改文档的字体。我尝试使用PrependChild将新字体应用于段落。但是,它抛出异常“无法插入OpenXmlElement”newChild“因为它是树的一部分。”经过一些研究,我发现我需要将该元素克隆到一个单独的对象中并在那里进行更改,所以我采用了这种方法

clonedRun = (Run)runList[j].CloneNode(true);                      
clonedRun.PrependChild(runProp);
runList[j] = clonedRun;

但该异常消息仍然出现。我试图删除该运行的父元素,使其不再是树的一部分,但是,它的父元素是空的(当然)。

static void Main(string[] args)
{
    using (var doc = WordprocessingDocument.Open(@"mydoc.docx", true))
    {
        RunProperties runProp = new RunProperties(); // Create run properties.
        RunFonts runFont = new RunFonts();           // Create font
        runFont.Ascii = "Arial";                     // Specify font family
        FontSize size = new FontSize();
        size.Val = new StringValue("48");  // 48 half-point font size
        runProp.Append(runFont);
        runProp.Append(size);

        var clonedParagraph = new Paragraph();
        var clonedRun = new Run();

        var paragraphList = doc.MainDocumentPart.RootElement.Descendants<Paragraph>()
            .ToList<Paragraph>();
        for (int i = 0; i < paragraphList.Count; i++)
        {
            clonedParagraph = (Paragraph)paragraphList[i].CloneNode(true);
            var runList = clonedParagraph.Elements<Run>().ToList();

            for (int j = 0; j < runList.ToList().Count; j++)
            {
                clonedRun = (Run)runList[j].CloneNode(true);                      
                clonedRun.PrependChild(runProp);
                runList[j] = clonedRun;
            }
            paragraphList[i] = clonedParagraph;
        }
        doc.MainDocumentPart.Document.Save();
    }
}

这是clonedNode的内部XML

<w:r w:rsidRPr="00E474DB" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
    <w:rPr>
        <w:rFonts w:ascii="Calibri" w:hAnsi="Calibri" w:eastAsia="MS Pゴシック" w:cs="Calibri" />
        <w:sz w:val="48" />
        <w:szCs w:val="48" />
        <w:lang w:eastAsia="ja-JP" />       
    </w:rPr>
    <w:t>Curriculum Vitae</w:t>
</w:r>

1 个答案:

答案 0 :(得分:0)

我发现了我的代码问题。我需要做的是更改RunPropeties元素,而不是Run。以下是我的新工作代码

(defun aborn/backward-kill-word ()
  "Customize/Smart backward-kill-word."
  (interactive)
  (let* ((cp (point))
         (backword)
         (end)
         (space-pos)
         (backword-char (if (bobp)
                            ""           ;; cursor in begin of buffer
                          (buffer-substring cp (- cp 1)))))
    (if (equal (length backword-char) (string-width backword-char))
        (progn
          (save-excursion
            (setq backword (buffer-substring (point) (progn (forward-word -1) (point)))))
          (setq ab/debug backword)
          (save-excursion
            (when (and backword          ;; when backword contains space
                       (s-contains? " " backword))
              (setq space-pos (ignore-errors (search-backward " ")))))
          (save-excursion
            (let* ((pos (ignore-errors (search-backward-regexp "\n")))
                   (substr (when pos (buffer-substring pos cp))))
              (when (or (and substr (s-blank? (s-trim substr)))
                        (s-contains? "\n" backword))
                (setq end pos))))
          (if end
              (kill-region cp end)
            (if space-pos
                (kill-region cp space-pos)
              (backward-kill-word 1))))
      (kill-region cp (- cp 1)))         ;; word is non-english word
    ))

(global-set-key  [C-backspace]
            'aborn/backward-kill-word)