如何使用OpenXML創建從右到左的TOC

[英]How to create right-to-left TOC using OpenXML


I have created a TOC for my DOCX file using C# and OpenXML using this tutorial. The problem is I cannot manage list of my TOC to get right-to-left. I was able to change to change sample XML to make TOC's header RtL. But it seems nothing can change the list items. Here is the changed XML I'm using:

我使用本教程使用C#和OpenXML為我的DOCX文件創建了一個TOC。問題是我無法管理我的TOC列表從右到左。我能夠更改為更改示例XML以生成TOC的標頭RtL。但似乎沒有什么可以改變列表項。這是我正在使用的更改的XML:

<w:sdt xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>

  <w:sdtPr>
    <w:pPr>
      <w:jc w:val='right'/>
    </w:pPr>
    <w:rPr>
      <w:rFonts w:asciiTheme='minorHAnsi' w:eastAsiaTheme='minorHAnsi' w:hAnsiTheme='minorHAnsi'/>
      <w:bCs w:val='0'/>
      <w:i w:val='0'/>
      <w:color w:val='auto'/>
      <w:sz w:val='22'/>
      <w:szCs w:val='22'/>
      <w:rtl/>
      <w:lang w:eastAsia='en-US' w:bidi='fa-IR'/>
    </w:rPr>
    <w:docPartObj>
      <w:docPartGallery w:val='Table of Contents'/>
      <w:docPartUnique/>
    </w:docPartObj>
  </w:sdtPr>

  <w:sdtEndPr>
    <w:pPr>
      <w:jc w:val='right'/>
    </w:pPr>
    <w:rPr>
      <w:rFonts w:asciiTheme='minorHAnsi' w:cstheme='minorBidi' w:eastAsiaTheme='minorHAnsi' w:hAnsiTheme='minorHAnsi'/>
      <w:color w:val='auto'/>
      <w:sz w:val='22'/>
      <w:szCs w:val='22'/>
      <w:rFonts w:cstheme='minorBidi'/>
      <w:rtl w:val='1'/>
      <w:lang w:bidi='fa-IR'/>
    </w:rPr>
  </w:sdtEndPr>

  <w:sdtContent>

    <w:p>
      <w:pPr>
        <w:spacing w:line='50' w:lineRule='auto'/>
        <w:jc w:val='right'/>
      </w:pPr>
      <w:r>
        <w:rPr>
          <w:rtl w:val='1'/>
          <w:lang w:eastAsia='en-US' w:bidi='fa-IR'/>
        </w:rPr>
        <w:t>
          <!-- empty parag -->
        </w:t>
      </w:r>
    </w:p>

    <w:p>
      <w:pPr>
        <w:pStyle w:val='TOCHeading'/>
        <w:spacing w:line='500' w:lineRule='auto'/>
        <w:jc w:val='right'/>
      </w:pPr>
      <w:r>
        <w:rPr>
          <w:rtl w:val='1'/>
          <w:lang w:bidi='fa-IR'/>
        </w:rPr>
        <w:t>{0}</w:t>
      </w:r>
    </w:p>

    <w:p>
      <w:pPr>
        <w:pStyle w:val='TOC1'/>
        <w:jc w:val='right' />
        <w:rPr>
          <w:rFonts w:eastAsiaTheme='minorEastAsia' w:cstheme='minorBidi' w:hint='cs'/>
          <w:noProof/>
          <w:lang w:bidi='fa-IR'/>
          <w:rtl w:val='1'/>
        </w:rPr>
        <w:tabs>
          <w:tab w:val='left' w:leader='dot' w:pos='{1}'/>
        </w:tabs>
      </w:pPr>

      <w:jc w:val='right' />
      <w:lang w:bidi='fa-IR' />

      <w:r>
        <w:rPr>
          <w:rFonts w:ascii='Lucida Console'/>
          <w:i/>
          <w:sz w:val='24'/>
          <w:rtl w:val='1'/>
          <w:lang w:bidi='fa-IR'/>
        </w:rPr>
        <w:fldChar w:fldCharType='begin'/>
      </w:r>
      <w:r>
        <w:rPr>
          <w:rFonts w:ascii='Lucida Console'/>
          <w:i/>
          <w:sz w:val='24'/>
          <w:rtl w:val='1'/>
          <w:lang w:bidi='fa-IR'/>
        </w:rPr>
        <w:instrText xml:space='preserve'> {2} </w:instrText>
      </w:r>
      <w:r>
        <w:rPr>
          <w:rFonts w:ascii='Lucida Console'/>
          <w:i/>
          <w:sz w:val='24'/>
          <w:rtl w:val='1'/>
          <w:lang w:bidi='fa-IR'/>
        </w:rPr>
        <w:fldChar w:fldCharType='separate'/>
      </w:r>

    </w:p>

    <w:p>

      <w:pPr>
        <w:jc w:val='right' />
        <w:rPr>
          <w:lang w:bidi='fa-IR'/>
          <w:rtl w:val='1'/>
        </w:rPr>
      </w:pPr>

      <w:jc w:val='right' />
      <w:lang w:bidi='fa-IR' />

      <w:r>
        <w:rPr>
          <w:b/>
          <w:bCs/>
          <w:noProof/>
          <w:rFonts w:ascii='Lucida Console'/>
          <w:i/>
          <w:sz w:val='24'/>
          <w:rtl w:val='1'/>
          <w:lang w:bidi='fa-IR'/>
        </w:rPr>
        <w:fldChar w:fldCharType='end'/>
      </w:r>
    </w:p>
  </w:sdtContent>
</w:sdt>

I almost tried every property on every node! But nothing changed. Do you have any idea how to do that? Thanks in advance.

我幾乎嘗試了每個節點上的每個屬性!但沒有改變。你知道怎么做嗎?提前致謝。

2 个解决方案

#1


0  

Maybe remove spaces and return in your file with regex? I tested with small piece of your xml.

也許刪除空格並使用正則表達式在文件中返回?我測試了一小塊你的xml。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input =
                "<w:sdt xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>\n" +
                  "<w:sdtPr>\n" +
                    "<w:pPr>\n" +
                      "<w:jc w:val='right'/>\n" +
                    "</w:pPr>\n" +
                    "<w:rPr>\n" +
                      "<w:rFonts w:asciiTheme='minorHAnsi' w:eastAsiaTheme='minorHAnsi' w:hAnsiTheme='minorHAnsi'/>\n" +
                      "<w:bCs w:val='0'/>\n" +
                      "<w:i w:val='0'/>\n" +
                      "<w:color w:val='auto'/>\n";

            //remove spaces at beginning of line
            string pattern1 = @"^\s+<";
            input = Regex.Replace(input, pattern1, "<");
            //remove spaces and return at end of line
            string pattern2 = ">\\s*\n";
            input = Regex.Replace(input, pattern2, ">",RegexOptions.Singleline);

        }
    }
}
​

#2


0  

the trick is to use "Bidi" inside your paragraph (such a cute name). If you manually create a word with a "right to left" TOC and look at a differences inside the xml you'll notice only one tag is added

訣竅是在你的段落中使用“Bidi”(這么可愛的名字)。如果您手動創建一個帶有“從右到左”TOC的單詞並查看xml內部的差異,您會發現只添加了一個標記

<w:p w:rsidP="00A63AAE" w:rsidRDefault="00A63AAE" w:rsidR="000E5A53">
  <w:pPr>
    <w:pStyle w:val="TM2"/>
    <w:bidi/>

So, on each paragraphProperties of your TOC, do a pPr.Append(new BiDi()) and it should do the work.

所以,在你的TOC的每個段落屬性上,做一個pPr.Append(新的BiDi()),它應該做的工作。

To have access to bidirection in Word, you have to add a Language (like Arabic) that use bidirection. Maybe you'll have to add a language like this in your word to enable the use of Bidi (there is a chance word delete bidi tag after a save if it don't find a bidirectional language in the word setting)

要在Word中訪問雙向,您必須添加使用雙向的語言(如阿拉伯語)。也許你必須在你的單詞中添加這樣的語言才能使用Bidi(如果在單詞設置中沒有找到雙向語言,則在保存后有可能刪除bidi標簽)


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/07/29/84b37bed92e64fe9cd4befd3f85aeaa.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com