使用C#以编程方式设置Word文档表单元格边距 [英] Setting word document table cell margin programmatically using c#

查看:270
本文介绍了使用C#以编程方式设置Word文档表单元格边距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以按照以下步骤更改MS WORD中的默认表格单元格边距

As we can change default table cells margin in MS WORD using this steps

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 and bottom padding属性,但是它没有用,我也尝试了spacing,但是我也没有用,所以有什么方法可以使用Microsoft.Interop.Word库非常感谢您

and exactly I want to ask how to do this programmatically I have tried top and bottom padding properties but it did not work and also I tried spacing but I did not work too so is there any way to set default cell margin using Microsoft.Interop.Word libraries thank you so much

PS: 我正在使用Microsoft.Interop.Word在页眉和页脚中添加表格,一切都很完美,这是期望的:/

PS: I am adding tables in header and footer using Microsoft.Interop.Wordeverything gone perfect expect this :/

推荐答案

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

There is Table padding, and there is also Cell padding. I'm guessing that Table padding contains the default values to apply to new cells added to the table. Probably use Cell padding to change the existing cells. E.g.

    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#以编程方式设置Word文档表单元格边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆