c# - 使用c#以编程方式设置word文档表格单元格边距

标签 c# ms-word office-interop

因为我们可以使用此步骤更改 MS WORD 中的默认表格单元格边距

Click the table.
On the Table menu, click Table Properties, and then click the Table tab.
Click Options.
Under Default cell margins, enter the new values you want.

确切地说,我想问的是如何以编程方式执行此操作,我已经尝试了 top 和 bottom padding 属性,但它没有用,而且我还尝试了 spacing 但我没有也可以使用 Microsoft.Interop.Word 库设置默认单元格边距,非常感谢

附言: 我正在使用 Microsoft.Interop.Word 在页眉和页脚中添加表格,一切都完美无缺:/

最佳答案

有Table padding,也有Cell padding。我猜 Table padding 包含应用于添加到表中的新单元格的默认值。可能使用单元格填充来更改现有单元格。例如

    Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
    Documents docs = app.Documents;
    Document doc = docs.Open("C:\\temp\\Test2.docx", ReadOnly:true);
    Table t = doc.Tables[1];
    double b1 = t.BottomPadding;
    double t1 = t.TopPadding;
    double r1 = t.RightPadding;
    double l1 = t.LeftPadding;

    Range r = t.Range;
    Cells cells = r.Cells;
    for (int i = 1; i <= cells.Count; i++) {
        Cell cell = cells[i];
        double b2 = cell.BottomPadding;
        double t2 = cell.TopPadding;
        double r2 = cell.RightPadding;
        double l2 = cell.LeftPadding;

        // e.g. Here is the edit:
        cell.TopPadding = 21.6f;
        cell.BottomPadding = 28.8f;

        Range r2b = cell.Range;
        String txt = r2b.Text;
        Marshal.ReleaseComObject(cell);
        Marshal.ReleaseComObject(r2b);
    }

    doc.Close(false);
    app.Quit(false);
    Marshal.ReleaseComObject(cells);
    Marshal.ReleaseComObject(r);
    Marshal.ReleaseComObject(t);
    Marshal.ReleaseComObject(doc);
    Marshal.ReleaseComObject(docs);
    Marshal.ReleaseComObject(app);

关于c# - 使用c#以编程方式设置word文档表格单元格边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24854178/

相关文章:

c# - 缺少 App1.Pages.SetPageDetail 的默认构造函数(位置 12 :18)

c# - 在发生未处理的异常后,我可以将执行返回到失败的方法吗?

excel - 单步执行代码(F8)时Excel无法关闭Word

c# - 从 Word 中删除不需要的分节符?

c# - 需要一个文档来使用 onenote Interop 从图像中提取文本?

c# - 获取 Azure BlockBlob 内容类型

c# - RightToLeft 标志和原子组

java - Apache poi 分页符

vb.net - 列出 Word 文档使用的字体(更快的方法)

.net - 在 VS2012 中找不到 Microsoft.Office.Interop 程序集