Office Open XML 打印Word文档目录
2010-06-28 03:06
549 查看
public partial class DemoTOC : Form { public DemoTOC() { InitializeComponent(); } Random rand = new Random(); private void DemoTOC_Load(object sender, EventArgs e) { GenerateContent(); } private void GenerateContent() { TreeNode root = new TreeNode("文档"); int count = rand.Next(5, 25); GenerateSubNode(root, count,true); trvContent.Nodes.Add(root); } private void GenerateSubNode(TreeNode parentNode, int count,bool isRoot) { for (int i = 1; i < count; i++) { TreeNode subNode = new TreeNode(); subNode.Text = string.Format(isRoot ? "第{0}章" : "第{0}节", i.ToString()); parentNode.Nodes.Add(subNode); if (isRoot) { int val = rand.Next(5,15); GenerateSubNode(subNode, val, false); } } } private void btnGenerate_Click(object sender, EventArgs e) { if(trvContent.Nodes.Count ==0 ) return; TreeNode root = trvContent.Nodes[0]; SaveFileDialog dialog = new SaveFileDialog(); dialog.Filter = "Word document(*.docx)|*.docx"; if (dialog.ShowDialog() != DialogResult.OK) return; string fileName = dialog.FileName; using (WordprocessingDocument wdDoc = WordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document)) { MainDocumentPart mainPart = wdDoc.AddMainDocumentPart(); StyleDefinitionsPart stylePart = mainPart.AddNewPart<StyleDefinitionsPart>(); Styles styles = new Styles(); string styleId = "TOCStyle"; Style style = styles.AppendChild(new Style() { Type = StyleValues.Paragraph, StyleId = styleId }); StyleName styleName = new StyleName(){ Val = "TOC样式"}; style.Append(styleName); // 段属性 ParagraphProperties pPr = new ParagraphProperties(); // 间距:段前、段后 SpacingBetweenLines spaceing = new SpacingBetweenLines() { Before = "6300", After = "200" }; // 对齐方式 Justification jc = new Justification() { Val = JustificationValues.Center }; pPr.Append(spaceing); pPr.Append(jc); // 字体样式 StyleRunProperties rPr = new StyleRunProperties() { Bold = new Bold(), FontSize = new FontSize() { Val = "40" } }; style.Append(pPr); style.Append(rPr); // 多级编号 NumberingDefinitionsPart numPart = mainPart.AddNewPart<NumberingDefinitionsPart>(); Numbering numbering = new Numbering(); NumberingInstance num = new NumberingInstance(); AbstractNumId abstractNumId = new AbstractNumId() { Val = 1 }; num.Append(abstractNumId); numbering.Append(num); // pPr.NumberingProperties.NumberingId = "1"; numPart.Numbering = numbering; stylePart.Styles = styles; Document doc = new Document(); Body body =doc.AppendChild(new Body()); GenerateTOC(body,root,styleId); mainPart.Document = doc; } } private void GenerateTOC(Body body, TreeNode parentNode,string styleId) { foreach (TreeNode subNode in parentNode.Nodes) { // 添加一空白段 body.AppendChild(new Paragraph(new Run(new Text()))); // 添加段及应用样式 Paragraph p = body.AppendChild(new Paragraph()); ParagraphProperties newpPr = p.PrependChild(new ParagraphProperties()); newpPr.ParagraphStyleId = new ParagraphStyleId() { Val = styleId }; Run r = p.AppendChild(new Run()); r.AppendChild(new Text(subNode.Text)); // 插入分页符 body.InsertAfter(new Paragraph(new Run(new Break() { Type = BreakValues.Page })), p); GenerateTOC(body, subNode, styleId); } } }
相关文章推荐
- OpenXml SDK 2.0 创建Word 2010文档(不带[Compatibility])最简代码
- Office Open XML 文档格式 (转)
- 使用 Open XML SDK 2.0 从 Word 2010 文档中提取样式
- Office Open XML WordprocessingML
- 使用OpenXml SDK 给Word文档添加样式并应用于文本
- 基于Office Open XML构建Office文档应用
- Office Open XML 文档格式(转)
- [转]how to programatically access built-in properties of open xml word doc(如何读取open xml格式文档属性)
- Word 导出带目录书签的PDF文档 Office
- 如何操作 Office Open XML 格式文档
- [Office 2010 易宝典]什么是OpenDocument格式?如何将Word文档保存为该格式?
- java创建word打开后弹出无法打开office open xml文件....因为内容有错误提示
- Office Open XML学习(1)-创建excel文档,并向单元格中插入字符串
- Office 如何双面打印Word文档
- Office 如何双面打印Word文档
- 用OpenXml SDK 2.0 创建一个页脚带页码的Word文档
- 如何:修改 Office Open XML 文档【转载】
- 处理 OpenOffice.org 基于 XML 的文档格式
- office excel WORD文档不能打印 提示未安装打印机
- Office Open XML 文档格式