更改了Word文档的页边距 [英] Changing the margins of a Word Document
问题描述
我已经创建了一个按钮,一旦点击生成包含特定列表的列表项值的word文档Web部件。我希望能够改变利润率文件(上,下边距),但我不能确定如何进行。 ?任何人都可以阐明如何实现这一一些轻
到目前为止,我的代码是如下:
无效GenerateBadges_Click(对象发件人,EventArgs五)
{
字符串标题= NULL;
字符串JOBTITLE = NULL;
WordprocessingDocument文件= WordprocessingDocument.Create(@C:\sample-
badges.docx,WordprocessingDocumentType.Document);
MainDocumentPart mainDocumenPart = document.AddMainDocumentPart();
mainDocumenPart.Document =新的文件();
体documentBody =新的机构();
mainDocumenPart.Document.Append(documentBody);
的SPWeb网站= SPContext.Current.Web;
SPList列表= web.Lists [SampleList];
SPListItemCollection collListItems = list.Items;
//为列表
串jobTitleField = collListItems.Fields [JOBTITLE] INTERNALNAME的标题和JOBTITLE领域获得的内部名称;
串titleField = collListItems.Fields [标题] INTERNALNAME。
//将表添加到文档
//创建一个对象的属性来添加边框表(WNO边框将被要求)
表表=新表();
TableProperties tblProps =新TableProperties();
TableBorders tblBorders =新TableBorders();
tblBorders.TopBorder =新TopBorder();
tblBorders.TopBorder.Val =新EnumValue< BorderValues>(BorderValues.Single);
tblBorders.BottomBorder =新BottomBorder();
tblBorders.BottomBorder.Val =新EnumValue< BorderValues>(BorderValues.Single);
tblBorders.RightBorder =新RightBorder();
tblBorders.RightBorder.Val =新EnumValue< BorderValues>(BorderValues.Single);
tblBorders.LeftBorder =新LeftBorder();
tblBorders.LeftBorder.Val =新EnumValue< BorderValues>(BorderValues.Single);
tblBorders.InsideHorizontalBorder =新InsideHorizontalBorder();
tblBorders.InsideHorizontalBorder.Val = BorderValues.Single;
tblBorders.InsideVerticalBorder =新InsideVerticalBorder();
tblBorders.InsideVerticalBorder.Val = BorderValues.Single;
tblProps.Append(tblBorders);
table.Append(tblProps);
INT X = collListItems.Count;
//肌酸为表中的行/单元格
(INT I = 0;(我* 2)< X;我++)
{
的TableRow行=新的TableRow();
//得到左和右细胞作为对索引(即0 + 1,2 + 3,4 + 5等)
INT leftIndexer = I * 2;
INT rightIndexer =(I * 2)+ 1;
如果(leftIndexer == X)
{
中断;
}
//获取从列表中的值为左表单元
名称= collListItems [leftIndexer] [titleField]的ToString();
JOBTITLE = collListItems [leftIndexer] [jobTitleField]的ToString();
//附加内容,以行作为细胞
row.Append(新的TableCell(新段(新润(新文本(标题)))));
//得到正确的单元格内容,如果有此索引
的值。如果(rightIndexer< X)
{
//获取从列表右边的单元格
标题值= collListItems [rightIndexer] [titleField]的ToString();
JOBTITLE = collListItems [rightIndexer] [jobTitleField]的ToString();
//附加到表行作为右边的单元格
row.Append(新的TableCell(新段(新润(新文本(标题)))));
}
//附加排表
table.Append(行);
}
$ B $B∥加上表格文档 - 表需要被接线到以上
documentBody.Append每个循环(表);
创建的Word文档
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
如果上面是不是你问什么,请提供你达到什么样的...
更多详细信息
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();
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...
这篇关于更改了Word文档的页边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!