Setting word document table cell margin programmatically using c#

*爱你&永不变心* 提交于 2019-12-24 07:36:00

问题


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.

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: I am adding tables in header and footer using Microsoft.Interop.Wordeverything gone perfect expect this :/


回答1:


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);


来源:https://stackoverflow.com/questions/24854178/setting-word-document-table-cell-margin-programmatically-using-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!