Changing the margins of a Word Document

时光总嘲笑我的痴心妄想 提交于 2019-12-07 17:51:57

问题


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


回答1:


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.:

  • http://msdn.microsoft.com/en-us/library/ee355228%28office.12%29.aspx
  • http://sharepointweblog.blogspot.com/2009/08/wordprocessingml-insert-page-number-in.html

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




回答2:


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



来源:https://stackoverflow.com/questions/9467050/changing-the-margins-of-a-word-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!