4

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?

So far the code I have is a as follows:

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

int x = collListItems.Count;

//creatin the table rows/cells
for (int i = 0; (i * 2) < 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 < 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();

Dev P
  • 1,157
  • 5
  • 32
  • 54
  • What code did you use to generate the document? – John Koerner Feb 27 '12 at 15:13
  • I have added the code above. I need to be able to set the page margins (top and bottom) but not sure how to achieve this. Please provide some assistance if possible. Many Thanks – Dev P Feb 27 '12 at 15:22

2 Answers2

4

The key part to be able to change the page margins is to firs t create a "PageMagrin" object and then a "SectionProperties" object. Lastly we need to append the page margin object in to the section properties object. Lastly append the section properties object to a body object.

Additionally it is very useful to create a word document, then save it as .xml. Then open it in notepad, notepad++ or even visual studio 2010. This displays all of th elements that make up the word document with this you are then able to determine which parts or elements of the document you need to modify.

Below is the actual code used:


//setting the page margins go here
PageMargin pageMargins = new PageMargin();
pageMargins.Left = 600;
pageMargins.Right = 600;
pageMargins.Bottom = 500;
pageMargins.Top = 500;
//pageMargins.Header = 1500; //not needed for now
//pageMargins.Footer = 1500; //not needed for now

//Important needed to access properties (sections) to set values for all elements.
SectionProperties sectionProps = new SectionProperties(); 
sectionProps.Append(pageMargins);
documentBody.Append(sectionProps);

Hope this helps others, I had a tough time figuring this out

Dev P
  • 1,157
  • 5
  • 32
  • 54
3

It is a bit hard to tell what exactly you want to do - the following links contain details and source code for page margins, headers, footers etc.:

IF the above is not what you asked please provide more details on what you achieve...

Yahia
  • 69,653
  • 9
  • 115
  • 144
  • The aim is to be able to change the top, bottom, left and right page margins to be able reduce both the top and bottom margins from the defualts "0.5" to something like "0.2". Currently the Document gets created with the default settings for the top, right, bottom and left margins. That's pretty much what I am trying to achieve. – Dev P Feb 27 '12 at 16:36
  • Thanks for your assistance. Although the example itselft was not something I could use, I was able to adapt it to my requirements. It helped me to identify the key part which is to actually create a "PageMargin" object followed by a "SectionProperties" object and the append the page margins obejct to the section properties, then append the section properties object to a document body object. This now works! – Dev P Feb 28 '12 at 09:37
  • Ok I have upvoted/accepted your suggested answer. Thanks for the assistance – Dev P Feb 28 '12 at 10:28