Word/OpenXML - 如何创建隐藏书签?

发布于 2024-10-28 05:48:24 字数 203 浏览 5 评论 0原文

我在 OpenXML API 或 Word/VSTO API 方面都没有找到创建或修改书签可见性的方法。即使在 Word 中手动添加书签,也没有复选框可以使书签隐藏。尽管“书签”对话框中一个复选框,可让您显示隐藏的书签。那么,隐藏书签是如何在 XML 中表示的?您可以使用 Open XML SDK 创建它们吗?或者它们是微软不再希望我们创造的遗留事物?

I've not had any luck in either the OpenXML API or Word/VSTO API, finding a way to create or modify a bookmark's visibility. Even adding bookmarks manually in Word, there's no box to check to make the bookmark hidden. Although there is a checkbox in the Bookmarks dialog that lets you show hidden bookmarks. So how are hidden bookmarks represented in the XML, and can you create them using the Open XML SDK? Or are they a legacy thing that MS no longer wants us to create?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹤仙姿 2024-11-04 05:48:24

好的,这比我想象的要容易......你只需在书签名称前面加上下划线即可。请注意,这只能通过编程来完成,而不能在 Word 中手动添加书签时完成。

Iiiiiiii有趣....

更新:我发现的另一件事 - 在您可以迭代或访问 书签 对象,您必须设置其 ShowHidden 属性设置为 true。

PS-所以,如果您对拼写字典有任何控制权,您可以添加“以编程方式”。除非我拼写错误。 :)

OK so this is easier than I thought...you just precede the bookmark name with an underscore. Note that this can only be done programmatically, not when adding bookmarks manually in Word.

Iiiiiiiiinteresting....

Update: Another thing I found--before you can iterate or access hidden bookmarks in a Bookmarks object, you must set its ShowHidden property to true.

PS - SO, if you have any control over your spelling dictionary, you might add "programmatically". Unless I'm spelling it wrong. :)

像极了他 2024-11-04 05:48:24

我在Word文件中创建了普通书签,然后以编程方式将它们转换为隐藏书签。如上所述,隐藏书签只能以编程方式创建,并且其名称前面带有“_”。迭代书签列表时,请确保将 Bookmarks.ShowHidden 设置为 true,否则隐藏的书签将不会显示在列表中。下面是我用来隐藏所有可见书签的代码。最后我还清除了撤消记录,以确保用户无法撤消我所做的更改。如果您愿意,您可以创建自定义撤消记录删除最后一个操作。

public static void hideAllBookmark(Document doc)
{
    String newName = null;
    Range newRange = null;
    bool backup = doc.Bookmarks.ShowHidden;
    doc.Bookmarks.ShowHidden = false;
            
    for (int i = doc.Bookmarks.Count; i > 0; i--)
    {
        if (!doc.Bookmarks[i].Name.Substring(0, 1).Equals("_", StringComparison.OrdinalIgnoreCase))
        {
            newName= "_" + doc.Bookmarks[i].Name;
            newRange = doc.Bookmarks[i].Range;
            doc.Bookmarks[i].Delete();
            doc.Bookmarks.Add(newName, newRange);
        }
    }
    doc.Bookmarks.ShowHidden = backup;
    doc.UndoClear();
}

I created normal bookmarks in word file and than converted them to hidden bookmark programmatically. As said above hidden bookmarks can only be created programmatically and their name precede by "_". When ever iterating the bookmarks list make sure Bookmarks.ShowHidden is set to true, otherwise hidden bookmarks will not show up in the list. Below is the code that I used to hide all the visible bookmarks. At the very end I also clear undo record to make sure user can not undo the changes I made. You may create the custom undo record delete the last action if you wish.

public static void hideAllBookmark(Document doc)
{
    String newName = null;
    Range newRange = null;
    bool backup = doc.Bookmarks.ShowHidden;
    doc.Bookmarks.ShowHidden = false;
            
    for (int i = doc.Bookmarks.Count; i > 0; i--)
    {
        if (!doc.Bookmarks[i].Name.Substring(0, 1).Equals("_", StringComparison.OrdinalIgnoreCase))
        {
            newName= "_" + doc.Bookmarks[i].Name;
            newRange = doc.Bookmarks[i].Range;
            doc.Bookmarks[i].Delete();
            doc.Bookmarks.Add(newName, newRange);
        }
    }
    doc.Bookmarks.ShowHidden = backup;
    doc.UndoClear();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文