在WPF应用中实现Word文档文本选择及上下文菜单的免费最优方案咨询
Great question—ditching the MS Office dependency for WPF Word viewing is totally doable with free libraries, and I’ve got a few solid options that support text selection and custom context menus:
1. GemBox.Document (Free Version)
GemBox.Document’s free tier is a fantastic choice here. It lets you load .docx/.doc files and convert them directly to a WPF FlowDocument—which natively supports text selection and plays nicely with context menus. No messy interop required.
Quick implementation steps:
- Grab the NuGet package
GemBox.Document - Load your document and convert it to a FlowDocument:
var doc = DocumentModel.Load("YourFile.docx"); var flowDoc = doc.ToFlowDocument(); - Stick that
FlowDocumentinto aFlowDocumentScrollViewerorFlowDocumentReader(both built-in WPF controls). These controls handle text selection automatically—you don’t have to write any extra code for that. - Add your custom context menu directly to the viewer:
<FlowDocumentScrollViewer x:Name="WordViewer"> <FlowDocumentScrollViewer.ContextMenu> <ContextMenu> <MenuItem Header="Copy Selection" Click="CopySelectedText_Click"/> <MenuItem Header="Highlight Text" Click="HighlightSelected_Click"/> <!-- Add your own operation items here --> </ContextMenu> </FlowDocumentScrollViewer.ContextMenu> </FlowDocumentScrollViewer>
The free version has a 20-page limit and adds a small watermark, but it’s more than enough for most applications.
2. OpenXML SDK + WPF FlowDocument
If you want full control without any third-party dependencies, Microsoft’s OpenXML SDK is completely free and open-source. It lets you parse Word documents at the raw XML level, but you’ll need to manually convert the content into a FlowDocument (since it doesn’t handle rendering for you).
Pros & cons to consider:
- ✅ Pros: No licensing limits, fully customizable, works with all .docx files.
- ❌ Cons: More work—you’ll have to handle styles, tables, images, and formatting yourself (OpenXML just gives you the document structure, not rendered content).
- Quick snippet to get started extracting text (you’ll expand this to build FlowDocument elements):
using (var wordDoc = WordprocessingDocument.Open("YourFile.docx", false)) { var body = wordDoc.MainDocumentPart.Document.Body; // Loop through paragraphs/runs and build FlowDocument elements here string rawText = body.InnerText; }
Once you’ve built the FlowDocument, you can use the same WPF viewer controls and context menu setup as above.
3. DocX (Novacode.DocX) + Custom WPF Rendering
DocX is another free, open-source library for working with .docx files. It has a simpler API than OpenXML, making it easier to extract paragraphs, text runs, and formatting details. Like OpenXML, you’ll need to map that content to a WPF FlowDocument manually, but the API is more developer-friendly.
Pro tip for context menus
Whichever library you pick, remember that WPF’s FlowDocument controls expose the selected text via their Selection property. For example, in your code-behind, you can grab the selected text with:
var selectedText = WordViewer.Selection.Text;
If you’re using MVVM, you can bind context menu items to commands instead of using code-behind clicks for a cleaner setup.
内容的提问来源于stack exchange,提问作者FetFrumos

