简体   繁体   English

使用Open XML在C#中设置Word文档的页边距

[英]Setting the margin of word document in c# using Open XML

I have created a Word document using Open Xml. 我已经使用Open Xml创建了Word文档。 The document gets created when a button in a web part is created. 创建Web部件中的按钮时,将创建文档。 Currently I have created a table in the document to test that it works. 目前,我已经在文档中创建了一个表格来测试其是否有效。 What I want to do now is to be able to set the page margins for the newly created documnet. 我现在想做的是能够为新创建的documnet设置页边距。

I am not sure how to proceed. 我不确定如何进行。 What would be the simplest way to achieve this? 实现这一目标的最简单方法是什么?

(below is the current code I have that creates the word document with a table inside) (以下是我拥有的当前代码,该代码创建带有表的单词文档)


void GenerateBadges_Click(object sender, EventArgs e)
{
    //Creating a word document using the the Open XML SDK 2.0
    WordprocessingDocument document = WordprocessingDocument.Create(@"C:\Users\Daniel.Perez
    \Documents\sample-badges.docx", WordprocessingDocumentType.Document);

    //create a paragraph
    MainDocumentPart mainDocumenPart = document.AddMainDocumentPart();
    mainDocumenPart.Document = new Document();
    Body documentBody = new Body();
    mainDocumenPart.Document.Append(documentBody);

    //adding a table to the document
    Table table = new Table();
    TableProperties tblProps = new TableProperties();
    TableBorders tblBorders = new TableBorders();

    tblBorders.TopBorder = new TopBorder();
    tblBorders.TopBorder.Val = new EnumValue<BorderValues>(BorderValues.Single);

    tblBorders.BottomBorder = new BottomBorder();
    tblBorders.BottomBorder.Val = new EnumValue<BorderValues>(BorderValues.Single);

    tblBorders.RightBorder = new RightBorder();
    tblBorders.RightBorder.Val = new EnumValue<BorderValues>(BorderValues.Single);

    tblBorders.LeftBorder = new LeftBorder();
    tblBorders.LeftBorder.Val = new EnumValue<BorderValues>(BorderValues.Single);

    tblBorders.InsideHorizontalBorder = new InsideHorizontalBorder();
    tblBorders.InsideHorizontalBorder.Val = BorderValues.Single;

    tblBorders.InsideVerticalBorder = new InsideVerticalBorder();
    tblBorders.InsideVerticalBorder.Val = BorderValues.Single;

    tblProps.Append(tblBorders);
    table.Append(tblProps);

    TableRow row;
    TableCell cell;

    //first table row
    row = new TableRow();
    cell = new TableCell(new Paragraph(new Run(new Text("Table to hold the badges"))));

    TableCellProperties cellProp = new TableCellProperties();
    GridSpan gridSpan = new GridSpan();
    gridSpan.Val = 11;

    cellProp.Append(gridSpan);
    cell.Append(cellProp);
    row.Append(cell);
    table.Append(row);

    //second row
    row = new TableRow();
    cell = new TableCell();
    cell.Append(new Paragraph(new Run(new Text("Inner Table"))));
    row.Append(cell);

    for (int i = 1; i <= 10; i++)
    {
        row.Append(new TableCell(new Paragraph (new Run(new Text(i.ToString())))));
    }

    table.Append(row);
    for (int i = 1; i <= 10; i++)
    {
        row = new TableRow();
        row.Append(new TableCell(new Paragraph(new Run(new Text(i.ToString())))));

        for (int j = 1; j <= 10; j++)
        {
            row.Append(new TableCell(new Paragraph(new Run(new Text((i * j).ToString())))));
        }
        table.Append(row);
    }


    //add the table to the document - table needs to be wired into the for each loop above
    documentBody.Append(table);

    //Saving/Disposing of the created word Document
    document.MainDocumentPart.Document.Save();
    document.Dispose();
}

Any suggestions will be greatly appreciated. 任何建议将不胜感激。 Thanks in advance 提前致谢

Although the question is quite old and left unanswered, just to complete the thread and to help those who might land on this page later, I am posting the resolved code: 尽管这个问题已经很老了,但仍然没有答案,只是为了完成话题并为以后可能会登陆该页面的人们提供帮助,我将发布已解决的代码:

SectionProperties sectionProps = new SectionProperties();
PageMargin pageMargin = new PageMargin() { Top = 1008, Right = (UInt32Value)1008U, Bottom = 1008, Left = (UInt32Value)1008U, Header = (UInt32Value)720U, Footer = (UInt32Value)720U, Gutter = (UInt32Value)0U };
sectionProps.Append(pageMargin);
mainPart.Document.Body.Append(sectionProps);

Note: You may convert Open XML to C# code using "Open XML SDK 2.0 Productivity Tool". 注意:您可以使用“ Open XML SDK 2.0生产率工具”将Open XML转换为C#代码。 Just open any open XML format file (document/spreadsheet etc) in the tool and click "Reflect Code" toolbar button. 只需在工具中打开任何打开的XML格式文件(文档/电子表格等),然后单击“反映代码”工具栏按钮即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM