簡體   English   中英

如何在C#中使用openxml查找與段落相關的選項卡數量?

[英]how to find the numbers of tabs related to paragraphs with openxml in c#?

如何在C#中使用openxml查找與段落相關的選項卡數量?

例如:

     test paragraph1
              test paragraph2

想象第一段有一個制表符空間,第二段有兩個制表符空間。 我想在C#中使用Open xml找到類似的東西,但是如何?

這個帖子有關系嗎?

c-sharp-tab-轉義字符

轉義序列

Open XMl將使用下面的xml來在段落中包含選項卡

<w:r>
  <w:tab />
  <w:t>Paragraph one</w:t>
</w:r>

以下代碼將滿足您在C#中的要求:

XNamespace W = @"http://schemas.openxmlformats.org/wordprocessingml/2006/main";
using (
    WordprocessingDocument document =
        WordprocessingDocument.Open(@"YourDocPath\tabs_in_text.docx", true))
{
    var body = document.MainDocumentPart.Document.GetFirstChild<Body>();
    var paras = body.Elements<Paragraph>();

    foreach (var para in paras)
    {
        //Ver. 1
        //var xml = XElement.Parse(para.OuterXml);
        // var count = xml.Descendants(W + "tab").Count();

        //Ver. 2
        var tabElements = para.Descendants<TabChar>();
        var count = tabElements.Count(); // Collect the counts into array or dictionary for your usage.
    }
}

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM