簡體   English   中英

使用Open XML在C#中設置Word文檔的頁邊距

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

我已經使用Open Xml創建了Word文檔。 創建Web部件中的按鈕時,將創建文檔。 目前,我已經在文檔中創建了一個表格來測試其是否有效。 我現在想做的是能夠為新創建的documnet設置頁邊距。

我不確定如何進行。 實現這一目標的最簡單方法是什么?

(以下是我擁有的當前代碼,該代碼創建帶有表的單詞文檔)


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

任何建議將不勝感激。 提前致謝

盡管這個問題已經很老了,但仍然沒有答案,只是為了完成話題並為以后可能會登陸該頁面的人們提供幫助,我將發布已解決的代碼:

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

注意:您可以使用“ Open XML SDK 2.0生產率工具”將Open XML轉換為C#代碼。 只需在工具中打開任何打開的XML格式文件(文檔/電子表格等),然后單擊“反映代碼”工具欄按鈕即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM