2016-12-06 922 views
0

我有一个非常基本的助手方法来设置书签的文本。Aspose.Words删除包含书签的行(行)

public static Bookmark SetBookmark(this Document doc, string bookmarkName, string value) 
{ 
     var bm = doc.Range.Bookmarks[bookmarkName]; 
     if(bm == null) 
      throw new NullReferenceException(string.Format("Bookmark {0} Not Found!", bookmarkName)); 
     bm.Text = value ?? string.Empty; 
     return bm; 
} 

我需要的是删除一个书签,删除一行文字包含它当满足特定条件,例如当时value == null。有什么建议么?

样品文件看起来像:
你好
[收藏]
再见

后去除

导致文档:
你好
再见

回答

0

请设置Bookmark.Text属性的空值字符串以删除其内容并使用Bookmark.Remove方法从文档中删除书签。 Bookmark.Remove方法不会删除书签中的文本。

我使用Aspose作为开发人员的传道者。

+0

Bookmark.Remove删除书签本身。我需要的是在书签所在的文档中取出整行。 – Sherlock

+0

请通过任何免费的文件共享服务器分享样本输入和期望输出文档,例如Dropbox的。然后我会分享代码示例。 - 我与Aspose一起作为开发者传道者。 –

+0

由于公司政策无法上传,因此请在此处提供抽象样本。 – Sherlock

0

请将Bookmark.Text属性的值设置为空字符串以删除其内容,如下面的代码示例所示。我在之前的回答中已经分享了这一点。

Document doc = new Document(MyDir + "Bookmark.doc"); 

// Use the indexer of the Bookmarks collection to obtain the desired bookmark. 
Bookmark bookmark = doc.Range.Bookmarks["MyBookmark"]; 

// Remove the contents of bookmark. 
bookmark.Text = ""; 

doc.Save(MyDir + @"Out.docx"); 

我使用Aspose作为Developer evangelist。