2012-02-24 142 views
0

我已經使用Open Xml創建了一個Word文檔。在創建Web部件中的按鈕時創建文檔。目前我在文檔中創建了一個表來測試它的工作原理。我現在想要做的是能夠爲新創建的documnet設置頁邊距。使用Open XML在c#中設置word文檔的邊距

我不確定如何繼續。什麼是最簡單的方法來實現這一點?

(下面是當前代碼我具有與內部表創建word文檔)


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

任何建議,將不勝感激。在此先感謝

+0

你試過了PageMargin嗎? http://msdn.microsoft.com/en-us/library/documentformat.openxml.wordprocessing.pagemargin.aspx – mindandmedia 2012-02-24 11:14:11

+0

這顯示瞭如何在開放的Xml中做到這一點,我有興趣通過代碼使用c# – 2012-02-24 11:17:23

+0

http:///stackoverflow.com/questions/8430437/open-xml-sdk-page-margins – mindandmedia 2012-02-24 11:22:00

回答

8

雖然這個問題是很舊的和沒有回答,只是爲了完成線程,並幫助那些誰可能這個網頁後在陸地上,我張貼的解析的代碼:

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格式文件(文檔/電子表格等),然後單擊「反射代碼」工具欄按鈕。

+0

謝謝Ali.NET,這對我很有用。 – 2013-04-25 17:15:50

相關問題