簡體   English   中英

使用 C# 中的 aspose.words 從 word 文檔中提取項目符號

[英]Extract bullets from word document using aspose.words in C#

我需要從 C# 中的 word 文檔中提取帶有項目符號樣式的文本。 我正在使用 aspose.words 庫,但也歡迎使用不同庫的解決方案。 我已經可以上傳文檔並使用 header1 樣式提取文本。 但是當我嘗試使用子彈樣式時,我什么也沒得到。

我正在使用下面的代碼來獲取帶有 Heading1 樣式的文本,並且可以正常工作。

var heading1 = doc
    .GetChildNodes(NodeType.Paragraph, true)
    .Cast<Aspose.Words.Paragraph>()
    .ToArray()
    .Where(p => p.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading1);
    
foreach (var head1 in heading1)
{
    listBox11.Items.Add(head1.gettext()tostring());
}

我正在嘗試使用下面的代碼來獲取帶有項目符號樣式的文本,但這不起作用。

var bullets = doc
    .GetChildNodes(NodeType.Paragraph, true)
    .Cast<Aspose.Words.Paragraph>()
    .ToArray()
    .Where(p => p.ParagraphFormat.StyleIdentifier == StyleIdentifier.ListBullet);
    
foreach (var bullet in bullets)
{
    listBox19.Items.Add(bullet.GetText().ToString());
}
    
listBox19.Items.Add(bullet1.GetText().ToString());

我也嘗試使用 listbullet1,2,3,4 和 5 styleIdentifiers 但這也不能解決問題。

很可能您的代碼不起作用,因為項目符號不是通過樣式應用的。 在 MS Word 文檔中有幾個級別可以應用格式:文檔默認值、主題、樣式和直接格式。 我認為,就您而言,最好的方法是使用ListFormat.IsListItem屬性。

我現在正在使用它成功地從 word 文件中提取列表項並將它們放入列表框中。

       string fileName = listBox1.Items.Cast<string>().FirstOrDefault();
                // Open the document.
                Document doc = new Document(fileName);

                doc.UpdateListLabels();

                NodeCollection paras = doc.GetChildNodes(NodeType.Paragraph, true);

                // Find if we have the paragraph list. In our document, our list uses plain Arabic numbers,
                // which start at three and ends at six.
                foreach (Aspose.Words.Paragraph paragraph in paras.OfType<Aspose.Words.Paragraph>().Where(p => p.ListFormat.IsListItem))
                {
                    //listBox19.Items.Add($"List item paragraph #{paras.IndexOf(paragraph)}");

                    // This is the text we get when getting when we output this node to text format.
                    // This text output will omit list labels. Trim any paragraph formatting characters. 
                    string paragraphText = paragraph.ToString(SaveFormat.Text).Trim();
                    //remove the dot in front of the bullet
                    string bullet = paragraphText.Remove(0, 2);

                    listBox19.Items.Add(bullet);

                    ListLabel label = paragraph.ListLabel;
                }

暫無
暫無

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

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