C# 更改Word文档的页边距

C# 更改Word文档的页边距,c#,.net,sharepoint-2010,openxml-sdk,C#,.net,Sharepoint 2010,Openxml Sdk,我创建了一个web部件,其中有一个按钮,单击该按钮会生成一个word文档,其中包含特定列表的列表项值。我希望能够更改文档的页边距(上、下页边距),但我不确定如何继续。有人能解释一下如何实现这一点吗 到目前为止,我掌握的代码如下: void generateBages_单击(对象发送方,事件参数e) { 字符串标题=null; 字符串jobTitle=null; WordprocessingDocument=WordprocessingDocument.Create(@“C:\sample- 徽章

我创建了一个web部件,其中有一个按钮,单击该按钮会生成一个word文档,其中包含特定列表的列表项值。我希望能够更改文档的页边距(上、下页边距),但我不确定如何继续。有人能解释一下如何实现这一点吗

到目前为止,我掌握的代码如下:
void generateBages_单击(对象发送方,事件参数e)
{
字符串标题=null;
字符串jobTitle=null;
WordprocessingDocument=WordprocessingDocument.Create(@“C:\sample-
徽章.docx”,文字处理文档类型.docx);
MainDocumentPart mainDocumenPart=document.AddMainDocumentPart();
mainDocumenPart.Document=新文档();
Body documentBody=新的Body();
mainDocumenPart.Document.Append(文档体);
SPWeb web=SPContext.Current.web;
SPList list=web.Lists[“SampleList”];
SPListItemCollection collListItems=列表项;
//获取列表的Title和JobTitle字段的内部名称
字符串jobTitleField=collListItems.Fields[“JobTitle”].InternalName;
字符串titleField=collListItems.Fields[“Title”].InternalName;
//向文档中添加表
//创建属性对象以向表中添加边框(需要wNo边框)
Table Table=新表();
TableProperties tblProps=新的TableProperties();
TableBorders tblBorders=新的TableBorders();
tblBorders.TopBorder=新的TopBorder();
tblBorders.TopBorder.Val=新的枚举值(BorderValues.Single);
tblBorders.BottomBorder=新的BottomBorder();
tblBorders.BottomBorder.Val=新的枚举值(BorderValues.Single);
tblBorders.RightBorder=新的RightBorder();
tblBorders.RightBorder.Val=新的枚举值(BorderValues.Single);
tblBorders.LeftBorder=新的LeftBorder();
tblBorders.LeftBorder.Val=新的枚举值(BorderValues.Single);
tblBorders.InsideHorizontalBorder=新的InsideHorizontalBorder();
tblBorders.InsideHorizontalBorder.Val=边界值。单个;
tblBorders.InsideverticalOrder=新的InsideverticalOrder();
tblBorders.InsideVerticalBorder.Val=BorderValues.Single;
tblProps.Append(tblBorders);
表.追加(tblProps);
INTX=collListItems.Count;
//创建表中的行/单元格
对于(int i=0;(i*2)


很难说清楚您到底想做什么-以下链接包含页边距、页眉、页脚等的详细信息和源代码:


如果以上不是您所要求的,请提供您实现的更多细节…

更改页面边距的关键部分是首先创建“PageMagrin”对象,然后创建“SectionProperties”对象。最后,我们需要将页边距对象附加到section properties对象。最后,将section properties对象附加到body对象

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

以下是实际使用的代码:




希望这对其他人有所帮助,我花了很长时间才弄明白您使用了什么代码来生成文档?我已经添加了上面的代码。我需要能够设置页面边距(顶部和底部)但不确定如何实现这一点。如果可能,请提供一些帮助。多谢。其目的是能够更改上、下、左、右页边距,以便能够将上、下页边距从“0.5”减少到类似“0.2”的程度。目前,该文档是使用默认的上、右、下和左页边距设置创建的。这正是我试图实现的。感谢您的帮助。虽然示例本身不是我可以使用的,但我能够根据我的要求对其进行调整。它帮助我确定了实际创建的关键部分先创建一个“PageMargin”对象,后跟一个“SectionProperties”对象,然后将页边距obejct附加到section属性,然后将section属性对象附加到文档正文对象。现在可以了!@DanielPerez请不要忘了将投票/标记为可接受的答案,这会有帮助(请参阅)好的,我已经投票/接受了你的建议答案。谢谢你的帮助。同样的原则也适用于我使用docx4j。
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();
//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);