Note that there are some explanatory texts on larger screens.

plurals
  1. POChanging the margins of a Word Document
    text
    copied!<p>I have created a web part with a button that once clicked generates a word document containing the list item values of particular list. I want to be able to change the margins for the document (top, bottom margins), but I am unsure as to how to proceed. Can anyone shed some light on how to achieve this?</p> <h2>So far the code I have is a as follows:</h2> <pre><code>void GenerateBadges_Click(object sender, EventArgs e) { string Title = null; string jobTitle = null; WordprocessingDocument document = WordprocessingDocument.Create(@"C:\sample- badges.docx", WordprocessingDocumentType.Document); MainDocumentPart mainDocumenPart = document.AddMainDocumentPart(); mainDocumenPart.Document = new Document(); Body documentBody = new Body(); mainDocumenPart.Document.Append(documentBody); SPWeb web = SPContext.Current.Web; SPList list = web.Lists["SampleList"]; SPListItemCollection collListItems = list.Items; //getting the internal name for the Title and JobTitle fields of the list string jobTitleField = collListItems.Fields["JobTitle"].InternalName; string titleField = collListItems.Fields["Title"].InternalName; //adding a table to the document //creating a properties object to add border to the table (wNo border will be required) Table table = new Table(); TableProperties tblProps = new TableProperties(); TableBorders tblBorders = new TableBorders(); tblBorders.TopBorder = new TopBorder(); tblBorders.TopBorder.Val = new EnumValue&lt;BorderValues&gt;(BorderValues.Single); tblBorders.BottomBorder = new BottomBorder(); tblBorders.BottomBorder.Val = new EnumValue&lt;BorderValues&gt;(BorderValues.Single); tblBorders.RightBorder = new RightBorder(); tblBorders.RightBorder.Val = new EnumValue&lt;BorderValues&gt;(BorderValues.Single); tblBorders.LeftBorder = new LeftBorder(); tblBorders.LeftBorder.Val = new EnumValue&lt;BorderValues&gt;(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); int x = collListItems.Count; //creatin the table rows/cells for (int i = 0; (i * 2) &lt; x; i++) { TableRow row = new TableRow(); // get the indexes for left and right cells as pairs (i.e. 0 + 1, 2 + 3, 4 + 5 etc) int leftIndexer = i * 2; int rightIndexer = (i * 2) + 1; if (leftIndexer == x) { break; } //getting the values from the list for the left table cell Title = collListItems[leftIndexer][titleField].ToString(); jobTitle = collListItems[leftIndexer][jobTitleField].ToString(); // attach content to row as cell row.Append(new TableCell(new Paragraph(new Run(new Text(Title))))); // get right cell contents, if there is a value for this index if (rightIndexer &lt; x) { //getting the values from the list for the right cell Title = collListItems[rightIndexer][titleField].ToString(); jobTitle = collListItems[rightIndexer][jobTitleField].ToString(); // attach to table row as right cell row.Append(new TableCell(new Paragraph(new Run(new Text(Title))))); } // attach row to table 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(); </code></pre> <hr>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload