c# – 更改Word文档的边距
发布时间:2020-12-16 01:51:29 所属栏目:百科 来源:网络整理
导读:我创建了一个带有按钮的Web部件,该按钮一旦单击就会生成一个包含特定列表的列表项值的word文档.我希望能够更改文档的边距(顶部,底部边距),但我不确定如何继续.任何人都可以阐明如何实现这一目标吗? 到目前为止,我的代码如下: void GenerateBadges_Click(ob
我创建了一个带有按钮的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 如果以上不是您的要求,请提供您所取得的更多详细信息…… (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |