更改Word文档的边距

时间:2012-02-27 14:53:25

标签: c# .net sharepoint-2010 openxml-sdk

我创建了一个带有按钮的Web部件,该按钮一旦点击就会生成一个包含特定列表的列表项值的word文档。我希望能够更改文档的边距(顶部,底部边距),但我不确定如何继续。任何人都可以阐明如何实现这一目标吗?

到目前为止,我的代码如下:

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

2 个答案:

答案 0 :(得分:4)

能够更改页边距的关键部分是首先创建一个“PageMagrin”对象,然后创建一个“SectionProperties”对象。最后,我们需要将页边距对象附加到节属性对象中。最后将section属性对象附加到body对象。

此外,创建word文档非常有用,然后将其另存为.xml。然后在记事本,记事本++或甚至visual studio 2010中打开它。这将显示构成单词文档的所有元素,然后您就可以确定需要修改的文档的哪些部分或元素。

以下是使用的实际代码:


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

希望这有助于其他人,我很难搞清楚这一点

答案 1 :(得分:3)

有点难以确定您想要做什么 - 以下链接包含页边距,页眉,页脚等的详细信息和源代码:

如果上述内容不符合您的要求,请提供您所取得的更多详细信息......

相关问题