ASPOSE.Word 开发资料整理
1.总体说明:操作主要涉及两个对象Document及DocumentBuilder
Document主要用来获取文档中的节点,DocumentBuilder主要用于实现文档内容的写入
doc_Operate = new Document(blankTemplatePth);
doc_template = new Document(ToCopytemplatePth);
builder_template = new DocumentBuilder(doc_template);
builder_operate = new DocumentBuilder(doc_Operate);
2.内容写入,样式设置
builder_operate.ParagraphFormat.Style = doc_Operate.Styles["标题"];
builder_operate.Writeln("XXX报告");
builder_operate.ParagraphFormat.Style = doc_Operate.Styles["正文"];
builder_operate.Writeln("(征求意见稿)");
3.关于分节,分节之后,需要将当前插入光标移动至新插入的Section中,否则容易出错
builder_operate.InsertBreak(BreakType.SectionBreakNewPage);
Section lstSection2 = doc_Operate.LastSection;
int idx2 = doc_Operate.Sections.IndexOf(lstSection2);
builder_operate.MoveToSection(idx2);
builder_operate.MoveToDocumentEnd();
4.复制模板文档中的表格,插入到当前文档中
//获取Table对象
Table tbN=doc.ImportNode(tb, true, ImportFormatMode.KeepSourceFormatting) as Table;
doc.LastSection.Body.AppendChild(tbN);
Paragraph p = new Paragraph(doc);
tbN.ParentNode.AppendChild(p); //添加paragraph,以打断表格
5.当前表格插入新行
Row rN = table_N.Rows[].Clone(true) as Row;
table_N.InsertAfter(rN, table_N.LastRow);
6.单元格插入图片
builder_operate.MoveToCell(tableindex, , , );
Aspose.Words.Drawing.Shape spa = builder_operate.InsertImage(jietuA);
此处插入图片时,容易出现异常,提示tableindex超出索引,TableIndex是通过indexOf对象获取到的.
查阅大量资料发现,如果一个word文档中出现了多个Section,需要采用本篇第3小节的内容,移动当前的光标
7.设置单元格内容
public static void setCellText(this Cell cell, string txt)
{
Run run;
if (cell.FirstParagraph.Runs.Count == )
{
run = new Run(cell.Document);
}
else
{
run = (Run)cell.FirstParagraph.Runs[].Clone(true);
}
run.Text = txt;
for (int i = ; i < cell.Paragraphs.Count; i++)
{
Node np=cell.GetChild(NodeType.Paragraph, i, false);
cell.RemoveChild(np);
} cell.FirstParagraph.RemoveAllChildren();
cell.EnsureMinimum();
if(cell.Paragraphs.Count!=)
cell.Paragraphs[].AppendChild(run);
}
8.合并单元格
public static void HorizontallyMergeCells(Cell c1, Cell c2,bool SaveAllVal=false)
{
c1.CellFormat.HorizontalMerge = CellMerge.First; //Move all content from next cell to previous
if (SaveAllVal)
{
foreach (Node child in c2.ChildNodes)
c1.AppendChild(child);
} c2.CellFormat.HorizontalMerge = CellMerge.Previous;
} public static void VerticallyMergeCells(Cell c1, Cell c2,bool SaveAllVal)
{
c1.CellFormat.VerticalMerge = CellMerge.First; //Move all content from bottom cell to top
if (SaveAllVal)
{
foreach (Node child in c2.ChildNodes)
c1.AppendChild(child);
} c2.CellFormat.VerticalMerge = CellMerge.Previous;
} public static void MergeCell(this Table tb, int startrowid, int endrowid, int startColId, int endColId)
{
for (int i = startrowid; i <= endrowid; i++)
{
for (int j = startColId+; j <= endColId; j++)
{
//每行进行横向合并
HorizontallyMergeCells(tb.Rows[i].Cells[startColId], tb.Rows[i].Cells[j]);
}
} //首行进行纵向合并
for (int i = startrowid+; i <= endrowid; i++)
{
VerticallyMergeCells(tb.Rows[startrowid].Cells[startColId], tb.Rows[i].Cells[startColId], false);
}
}
9.插入另一个Word文档
Document docShuoMing = new Document(summaryTemplatePth);
//docShuoMing.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;
//docShuoMing.FirstSection.PageSetup.RestartPageNumbering = true;
doc_Operate.LastSection.AppendContent(docShuoMing.FirstSection);
//doc_Operate.AppendDocument(docShuoMing, ImportFormatMode.KeepSourceFormatting);
builder_operate.MoveToDocumentEnd();
10.插入页码
public static void InsertHeaderFooter(Section sect, HeaderFooterType headerType)
{
HeaderFooter header = sect.HeadersFooters[headerType]; if (header == null)
{
header = new HeaderFooter(sect.Document, headerType);
sect.HeadersFooters.Add(header);
}
} public static void CancelHeaderFotter(Section sect)
{
for (int i = sect.HeadersFooters.Count-; i >=; i--)
{
sect.HeadersFooters.RemoveAt(i);
}
}
/// <summary>
/// 插入页码
/// </summary>
/// <param name="builder_operate"></param>
/// <param name="sec"></param>
/// <param name="startNumber"></param>
public static void InsertYeMa(DocumentBuilder builder_operate,Section sec,NumberStyle ns=NumberStyle.Arabic,int startNumber=)
{
//添加页码
ASPWHelper.InsertHeaderFooter(sec, HeaderFooterType.HeaderPrimary);
builder_operate.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
//设置开始页码
builder_operate.PageSetup.PageStartingNumber = startNumber;
builder_operate.PageSetup.PageNumberStyle = ns;
//页码在每个section会重新开始
builder_operate.PageSetup.RestartPageNumbering = true;
//页码位置
builder_operate.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder_operate.InsertField("PAGE", "");
builder_operate.MoveToDocumentEnd();
}
11.移除最后的分页符
public static void RemoveLastPageBreak(Document doc)
{
NodeCollection runs = doc.LastSection.GetChildNodes(NodeType.Run, true); for (int i = runs.Count - ; i >= ; i--)
{
Run run = (Run)runs[i];
if (run.Text.IndexOf(ControlChar.PageBreakChar) >= )
{
run.Text = run.Text.Remove(run.Text.IndexOf(ControlChar.PageBreakChar), );
break;
}
}
}
12.插入目录
/// <summary>
/// 插入目录
/// </summary>
/// <param name="bulider_blank"></param>
public static void InsertTOC(DocumentBuilder bulider_blank)
{
//设置"目录"格式
bulider_blank.ParagraphFormat.Alignment = ParagraphAlignment.Center;
bulider_blank.Bold = true;
bulider_blank.Font.Name = "SONG";
bulider_blank.Writeln("目录");
bulider_blank.ParagraphFormat.ClearFormatting();//清除所有样式
bulider_blank.InsertTableOfContents("\\o\"1-3\"\\h\\z\\u");
bulider_blank.InsertBreak(BreakType.SectionBreakNewPage);
}
获取某一Section的页数
public static int getLastSectionPageCount(Document doc)
{
int idx = doc.Sections.IndexOf(doc.LastSection);
Document tmp = doc.Clone();
for (int i = tmp.Sections.Count - ; i >= ; i--)
{
if (i != idx)
{
tmp.Sections.RemoveAt(i);
}
}
return tmp.PageCount;
}
ASPOSE.Word 开发资料整理的更多相关文章
- Word 开发资料集合
Word 对象模型概述 https://msdn.microsoft.com/zh-cn/library/kw65a0we.aspx DSOframer微软官方API的查阅方法 http://sh ...
- iOS app开发资料整理
Objective C快速入门: http://blog.csdn.net/totogo2010/article/details/7632384 http://www.cocoachina.com/i ...
- android插件式开发资料整理
1.DL : Apk动态载入框架 2.android中的动态载入机制
- iOS 开发学习资料整理(持续更新)
“如果说我看得比别人远些,那是因为我站在巨人们的肩膀上.” ---牛顿 iOS及Mac开源项目和学习资料[超级全面] http://www.kancloud.cn/digest/ios-mac ...
- 【Android开发资料分享】自己整理的Android开发资料,非常全面
学习Android以来,不知不觉中收集了大量非常优秀的Android开发资料,一直没有系统的整理,最近抽时间把收藏夹中的资料做了一下整理,现在分享给大家,希望能够帮助到需要的人.这份资料我还会不断的更 ...
- iOS 学习资料整理
iOS学习资料整理 https://github.com/NunchakusHuang/trip-to-iOS 很好的个人博客 http://www.cnblogs.com/ygm900/ 开发笔记 ...
- 如何查找STM32开发资料
Ⅰ.概述 该文写给那些处于初学ST芯片开发.英文不好而又想偷懒的人. 该文主要的目的是提醒大家:学习一门技术是需要舍得花功夫,捷径是你在起点与终点之间不断的探索,最终总结出来的一条适合自己的路. 下面 ...
- 推荐资料——最受网友力荐的30份HTML前端开发资料
w3cmark最近会新增一个栏目,专注于搜集前端资源的下载,包括和前端相关的电子书.文档PPT.手册.视频教程等等.而下载的媒介是用微博的微盘,至于为什么选择微盘的,那是因为和微博关联起来,通过微盘上 ...
- 转:基于IOS上MDM技术相关资料整理及汇总
一.MDM相关知识: MDM (Mobile Device Management ),即移动设备管理.在21世纪的今天,数据是企业宝贵的资产,安全问题更是重中之重,在移动互联网时代,员工个人的设备接入 ...
随机推荐
- javaScript事件机制深入学习(事件冒泡,事件捕获,事件绑定方式,移除事件方式,阻止浏览器默认行为,事件委托,模拟浏览器事件,自定义事件)
前言 JavaScript与HTML之间的交互是通过事件实现的.事件,就是文档或浏览器窗口中发生的一些特定的交互瞬间.可以使用侦听器(或处理程序)来预订事件,以便事件发生时执行相应的代码.这种在传统软 ...
- 拆系数FFT及其部分优化
模拟考某题一开始由于校内OJ太慢直接拆系数FFT跑不过 后来被神仙婊了一顿之后发现复杂度写炸了改了改随便过 模版题:任意模数NTT 三模数NTT 常数巨大,跑的极慢 拆系数FFT 原理是对于两个多项式 ...
- eclipse+tomcat出现警告警告: [SetPropertiesRule]...
启动tomcat出现警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to ' ...
- Django组件-用户认证
用户认证 一.auth模块 from django.contrib import auth django.contrib.auth中提供了许多方法,这里主要介绍其中的三个: 1.1 .authenti ...
- Xpath初了解
如下一段html: <html> <body> <form id="loginForm"> <input name="usern ...
- jquery .map() 和 .each()函数结合使用
需求:页面动态添加的html元素(如div),保存时组装div中的数据为一个json对象. 思路:遍历每个div,再遍历div中每个输入元素,把所有先把数据放到一个对象中,再添加进数组,Json.st ...
- Mysql -- 外键的变种 三种关系
一.介绍 因为有foreign key的约束, 使得两张表形成了三种关系 多对一 多对多 一对一 二.如果找出两张表之间的关系 #.先站在左表的角度去找 是否左表的多条记录可以对应右 ...
- django中sqlite迁移mysql
sqlite数据迁移 1 数据备份 django中打开terminalpython manage.py dumpdata authorization > authorization_data.j ...
- 有道云笔记Markdown上传本地图片的方法
有道云笔记截图&保存 方法有多种,例如:开通有道云笔记VIP会员.先将图片文件上传到有道云笔记后使用图片的分享链接.说到底还是使用的 Markdown 的图片功能 ![图片名称](图片链接 ...
- IntelliJ IDEA运行eclipse的web项目报错的问题
用IDEA已经有一段时间了, 由于之前的IDEA版本不支持Tomcat服务器, 所以很长一段时间web项目都是由eclipse开发调试. 今天闲来无事下载了一个最新版的IDEA, 按网上的教程, 尝试 ...