C#   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c# – 更改Word文档的边距大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我创建了一个带有按钮的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();

解决方法

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

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

如果以上不是您的要求,请提供您所取得的更多详细信息……

大佬总结

以上是大佬教程为你收集整理的c# – 更改Word文档的边距全部内容,希望文章能够帮你解决c# – 更改Word文档的边距所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: