c# - 如何使用 C# 在 Microsoft Word 的文本框中将特定单词加粗?
问题描述
我有一个应用程序将打印一个 .docx 文件,该文件有一个带有文本的文本框形状。其中,有些词需要加粗。
例如,部分文本是“...representando a empresa M&A, o presente...”,只有“M&A”需要加粗。
但是,我不确定我该怎么做。我在 SO 以及 MSDN 等其他网站中进行了搜索,但没有一个提供解决方案。有人可以帮我吗?
编辑:我正在使用互操作来实现这一点。到目前为止我所做的代码如下:
// opens word app
wordApp = new Microsoft.Office.Interop.Word.Application();
wordApp.Visible = false;
// print dialog for settings input
PrintDialog pd = new PrintDialog();
if (pd.ShowDialog() == DialogResult.OK)
{
wordApp.ActivePrinter = pd.PrinterSettings.PrinterName;
// opens the document
Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(filePath);
// iterates through each shape in the file
foreach (Microsoft.Office.Interop.Word.Shape shape in doc.Shapes)
{
string shapeName = shape.Name;
// checks whether the shape is a Text Box
if (shapeName.StartsWith("Text Box"))
{
string shapeText = shape.TextFrame.ContainingRange.Text;
// checks whether the shape is the one I want to modify
// side note: there are more shapes in the file, so I specify it
if (shapeText.StartsWith("Concedemos"))
{
// erases the text, and sets it to a string
shape.TextFrame.ContainingRange.Text = "";
shape.TextFrame.ContainingRange.Text = "textSample";
}
}
}
// prints the file
wordApp.ActiveDocument.PrintOut();
}
// quits the app
wordApp.Quit();
wordApp = null;
提前致谢!
解决方案
下面的代码摘录演示了如何遍历 Word 文档中的所有形状,检查它是否是以所需字符串开头的文本框(绘图元素),然后在文本框中搜索要加粗的术语并将其加粗。
可以检查
Shape.Type
以确定它是否是文本框。这Shape.Type
是一个Office枚举(因为该Shape
对象对许多 Office 应用程序都是通用的)。所以if (shapeType == Office.MsoShapeType.msoTextBox)
要挑选文本并对其进行格式化,Word 的
Range.Find
功能通常是最好的方法。它可以查找和替换格式以及字符串值。(提示:在着手进行需要此类操作的项目时,通常最好在 Word UI 中的对话框 (Ctrl+H) 中进行测试,以找出正确的参数组合。)要了解所有参数的
Find.Execute
用途,请查阅帮助或查看 Intellisense(或两者的组合)。
对于这个例子,重要的是要注意,为了格式化目标文本,没有Replacement.Text
指定。
foreach (Microsoft.Office.Interop.Word.Shape shape in doc.Shapes)
{
Office.MsoShapeType shapeType = shape.Type;
// checks whether the shape is a Text Box
if (shapeType == Office.MsoShapeType.msoTextBox)
{
// checks whether the shape is the one I want to modify
// side note: there are more shapes in the file, so I specify it
if (shapeText.StartsWith("Concedemos"))
{
string textToBold = "M&A";
Word.Range rngTextBox = shape.TextFrame.TextRange;
Word.Find rngFind = rngTextBox.Find;
rngFind.Text = textToBold;
rngFind.Replacement.Font.Bold = -1;
object oFindStop = Word.WdFindWrap.wdFindStop;
object oTrue = true;
object oReplaceAll = Word.WdReplace.wdReplaceAll;
object missing = System.Type.Missing;
rngFind.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref oFindStop, ref oTrue, ref missing, ref oReplaceAll,
ref missing, ref missing, ref missing, ref missing);
}
}
推荐阅读
- python - 如何确保绘图的 x 轴和 y 轴大小相等?
- vba - 在 Access 2016 中计算对数趋势线端点值
- java - 处理迭代多个集合所需的 lambda 集合问题帮助
- neo4j - Neo4j-admin 转储失败
- python - 将多个 MetaGraphDef 导入单个图形并恢复变量
- php - 两个模型的通用 model_id - Laravel 5 - Eloquent
- python-3.x - ZONE_RESOURCE_POOL_EXHAUSTED 是 googleapiclient.errors.httpError 吗?
- node.js - (节点:11254)UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝 id:1):TypeError:无法读取 null 的属性“长度”
- c# - 当 Response.ContentType 为“audio/wav”时,如何修复谷歌浏览器加载页面两次?
- node.js - 如何正确使用 Nest.js 的 @Headers?