DocX开源WORD操作组件的学习系列三
DocX学习系列
DocX开源WORD操作组件的学习系列一 : http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_sharp_001_docx1.html
DocX开源WORD操作组件的学习系列二 : http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_csharp_005_docx2.html
DocX开源WORD操作组件的学习系列三: http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_csharp_006_docx3.html
DocX开源WORD操作组件的学习系列四: http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_csharp_006_docx4.html
替换文本
private static void ReplaceText()
{
Console.WriteLine("ReplaceText()");
File.Copy(@"docs\Lists.docx", @"docs\ReplaceText.docx", true);
using (var document = DocX.Load(@"docs\ReplaceText.docx"))
{
//全局替换
document.ReplaceText("zhao", "zhang");
//段落定位后替换
var p1= document.Paragraphs.FirstOrDefault(r => r.Text == "赵杰迪");
if (p1 == null)
{
return;
}
p1.ReplaceText("zhaojiedi","zhaojiedi2");
//书签定位后替换
document.Bookmarks["书签1"].Paragraph.ReplaceText("zhaojiedi","zhaojiedi2");
//基于正则表达式的替换 用途:我们事先做一个模板文件, 然后基于正则表达式查找,然后替换。
List<Regex> regexList = new List<Regex>()
{
new Regex("[name](.*)[/name]"),
new Regex("[age](.*)[/age]"),
new Regex("[age](.*)[/age]")
};
foreach (var item in regexList)
{
Match match = item.Match(document.Text);
if (match.Success && match.Groups.Count >= )
{
document.ReplaceText(match.Groups[].ToString(),match.Groups[].ToString());
}
}
document.Save();
Console.WriteLine("\tCreated: docs\\ReplaceText.docx");
}
}
编号
private static void AddList()
{
Console.WriteLine("\tAddList()"); using (var document = DocX.Create(@"docs\Lists.docx"))
{
var numberedList = document.AddList("First List Item.", , ListItemType.Numbered);
//Add a numbered list starting at 2
document.AddListItem(numberedList, "Second List Item.");
document.AddListItem(numberedList, "Third list item.");
document.AddListItem(numberedList, "First sub list item", ); document.AddListItem(numberedList, "Nested item.", );
document.AddListItem(numberedList, "Fourth nested item."); var bulletedList = document.AddList("First Bulleted Item.", , ListItemType.Bulleted);
document.AddListItem(bulletedList, "Second bullet item");
document.AddListItem(bulletedList, "Sub bullet item", );
document.AddListItem(bulletedList, "Second sub bullet item", );
document.AddListItem(bulletedList, "Third bullet item");
document.InsertList(numberedList);
document.InsertList(bulletedList);
document.Save();
Console.WriteLine("\tCreated: docs\\Lists.docx");
}
}
3创建图形
3.1效果图
private class ChartData
{
public String Mounth { get; set; }
public Double Money { get; set; } public static List<ChartData> CreateCompanyList1()
{
List<ChartData> company1 = new List<ChartData>();
company1.Add(new ChartData() { Mounth = "January", Money = });
company1.Add(new ChartData() { Mounth = "February", Money = });
company1.Add(new ChartData() { Mounth = "March", Money = });
return company1;
} public static List<ChartData> CreateCompanyList2()
{
List<ChartData> company2 = new List<ChartData>();
company2.Add(new ChartData() { Mounth = "January", Money = });
company2.Add(new ChartData() { Mounth = "February", Money = });
company2.Add(new ChartData() { Mounth = "March", Money = });
return company2;
}
} private static void BarChart()
{
Console.WriteLine("\tBarChart()");
// Create new document.
using (DocX document = DocX.Create(@"docs\BarChart.docx"))
{
// Create chart.
BarChart c = new BarChart();
c.BarDirection = BarDirection.Column;
c.BarGrouping = BarGrouping.Standard;
c.GapWidth = ;
c.AddLegend(ChartLegendPosition.Bottom, false); // Create data.
List<ChartData> company1 = ChartData.CreateCompanyList1();
List<ChartData> company2 = ChartData.CreateCompanyList2(); // Create and add series
Series s1 = new Series("Microsoft");
s1.Color = WindowsColor.GreenYellow;
s1.Bind(company1, "Mounth", "Money");
c.AddSeries(s1);
Series s2 = new Series("Apple");
s2.Bind(company2, "Mounth", "Money");
c.AddSeries(s2); // Insert chart into document
document.InsertParagraph("Diagram").FontSize();
document.InsertChart(c);
document.Save();
}
Console.WriteLine("\tCreated: docs\\BarChart.docx\n");
} private static void PieChart()
{
Console.WriteLine("\tPieChart()");
// Create new document.
using (DocX document = DocX.Create(@"docs\PieChart.docx"))
{
// Create chart.
PieChart c = new PieChart();
c.AddLegend(ChartLegendPosition.Bottom, false); // Create data.
List<ChartData> company2 = ChartData.CreateCompanyList2(); // Create and add series
Series s = new Series("Apple");
s.Bind(company2, "Mounth", "Money");
c.AddSeries(s); // Insert chart into document
document.InsertParagraph("Diagram").FontSize();
document.InsertChart(c);
document.Save();
}
Console.WriteLine("\tCreated: docs\\PieChart.docx\n");
} private static void LineChart()
{
Console.WriteLine("\tLineChart()");
// Create new document.
using (DocX document = DocX.Create(@"docs\LineChart.docx"))
{
// Create chart.
LineChart c = new LineChart();
c.AddLegend(ChartLegendPosition.Bottom, false); // Create data.
List<ChartData> company1 = ChartData.CreateCompanyList1();
List<ChartData> company2 = ChartData.CreateCompanyList2(); // Create and add series
Series s1 = new Series("Microsoft");
s1.Color = WindowsColor.GreenYellow;
s1.Bind(company1, "Mounth", "Money");
c.AddSeries(s1);
Series s2 = new Series("Apple");
s2.Bind(company2, "Mounth", "Money");
c.AddSeries(s2); // Insert chart into document
document.InsertParagraph("Diagram").FontSize();
document.InsertChart(c);
document.Save();
}
Console.WriteLine("\tCreated: docs\\LineChart.docx\n");
} private static void Chart3D()
{
Console.WriteLine("\tChart3D()");
// Create new document.
using (DocX document = DocX.Create(@"docs\3DChart.docx"))
{
// Create chart.
BarChart c = new BarChart();
c.View3D = true; // Create data.
List<ChartData> company1 = ChartData.CreateCompanyList1(); // Create and add series
Series s = new Series("Microsoft");
s.Color = WindowsColor.GreenYellow;
s.Bind(company1, "Mounth", "Money");
c.AddSeries(s); // Insert chart into document
document.InsertParagraph("3D Diagram").FontSize();
document.InsertChart(c);
document.Save();
}
Console.WriteLine("\tCreated: docs\\3DChart.docx\n");
}
5添加图片
static void HelloWorldAddPictureToWord()
{
Console.WriteLine("\tHelloWorldAddPictureToWord()"); // Create a document.
using (DocX document = DocX.Create(@"docs\HelloWorldAddPictureToWord.docx"))
{
// Add an image into the document.
RelativeDirectory rd = new RelativeDirectory(); // prepares the files for testing
rd.Up();
Image image = document.AddImage(rd.Path + @"\images\logo_template.png"); // Create a picture (A custom view of an Image).
Picture picture = image.CreatePicture();
picture.Rotation = ;//旋转10度
picture.SetPictureShape(BasicShapes.cube); // Insert a new Paragraph into the document.
Paragraph title = document.InsertParagraph().Append("This is a test for a picture").FontSize().Font(new Font("Comic Sans MS"));
title.Alignment = Alignment.center; // Insert a new Paragraph into the document.
Paragraph p1 = document.InsertParagraph(); // Append content to the Paragraph
p1.AppendLine("Just below there should be a picture ").Append("picture").Bold().Append(" inserted in a non-conventional way.");
p1.AppendLine();
p1.AppendLine("Check out this picture ").AppendPicture(picture).Append(" its funky don't you think?");
p1.AppendLine(); // Insert a new Paragraph into the document.
Paragraph p2 = document.InsertParagraph();
// Append content to the Paragraph. p2.AppendLine("Is it correct?");
p2.AppendLine(); // Lets add another picture (without the fancy stuff)
Picture pictureNormal = image.CreatePicture(); Paragraph p3 = document.InsertParagraph();
p3.AppendLine("Lets add another picture (without the fancy rotation stuff)");
p3.AppendLine();
p3.AppendPicture(pictureNormal); // Save this document.
document.Save();
Console.WriteLine("\tCreated: docs\\HelloWorldAddPictureToWord.docx\n");
}
}
6.替换模板中的图片
public bool ReplaceImage(string oldImageName, string newImageNmae)
{ Paragraph paragraph = null;
int width = ;
int height = ;
foreach (Paragraph item in Document.Paragraphs){
if (item.Pictures.Count > && item.Pictures[].FileName == oldImageName)
{
paragraph = item;
break;
}
}
if (paragraph == null)
{
return false;
}
width = paragraph.Pictures[].Width;
//height = paragraph.Pictures[0].Height;
paragraph.Pictures[].Remove();
using (System.Drawing.Image tmpImage = new Bitmap(newImageNmae))
{
int tmpWidth = tmpImage.Width;
int tmpHeight = tmpImage.Height;
float radio = tmpHeight * 1.0f /tmpWidth;
height = (int)(width*radio);
}
Novacode.Image newImage = this.Document.AddImage(newImageNmae); // Insert the extracted logo into the paragraph
paragraph.InsertPicture(newImage.CreatePicture(height, width));//注意createpicture的构造函数
//paragraph.InsertPicture(newImage.CreatePicture());//注意createpicture的构造函数
return true;
}
DocX开源WORD操作组件的学习系列三的更多相关文章
- DocX开源WORD操作组件的学习系列四
DocX学习系列 DocX开源WORD操作组件的学习系列一 : http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_sharp_001_docx1.htm ...
- DocX开源WORD操作组件的学习系列二
DocX学习系列 DocX开源WORD操作组件的学习系列一 : http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_sharp_001_docx1.htm ...
- DocX开源WORD操作组件的学习系列一
DocX学习系列 DocX开源WORD操作组件的学习系列一 : http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_sharp_001_docx1.htm ...
- 开源word操作组件DocX的记录
开源word操作组件DocX的记录 使用开源word操作组件DocX的记录 1.DocX简介 1.1 简介 DocX是一个在不需要安装word的情况下对word进行操作的开源轻量级.net组件,是由爱 ...
- 使用开源word操作组件DocX的记录
1.DocX简介 1.1 简介 DocX是一个在不需要安装word的情况下对word进行操作的开源轻量级.net组件,是由爱尔兰的一个叫Cathal Coffey的博士生开发出来的.DocX使得操作w ...
- 开源Word读写组件DocX 的深入研究和问题总结
一. 前言 前两天看到了asxinyu大神的[原创]开源Word读写组件DocX介绍与入门,正好我也有类似的自动生成word文档得需求,于是便仔细的研究了这个DocX. 我也把它融入到我的项目当中并进 ...
- [.NET] 开头不讲"Hello Word",读尽诗书也枉然 : Word 操作组件介绍 - Spire.Doc
开头不讲"Hello Word",读尽诗书也枉然 : Word 操作组件介绍 - Spire.Doc [博主]反骨仔 [原文地址]http://www.cnblogs.com/li ...
- 开源RabbitMQ操作组件
开源RabbitMQ操作组件 对于目前大多的.NET项目,其实使用的技术栈都是差不多,估计现在很少用控件开发项目的了,毕竟一大堆问题.对.NET的项目,目前比较适合的架构ASP.NET MVC,ASP ...
- .net reactor 学习系列(三)---.net reactor代码自动操作相关保护功能
原文:.net reactor 学习系列(三)---.net reactor代码自动操作相关保护功能 接上篇,上篇已经学习了界面的各种功能以及各种配置,这篇准备学习下代码控制许可证. ...
随机推荐
- Android 动画 (1) 基础
背景 坑, 最近打算在recyclerview item上加一个带动画的button,结果button无法连续点击,还以为是动画是同步的,必须要结束之后才能开始另一个动画,后来去掉recylervie ...
- Rabin-Karp ACM训练
求解问题 寻找S中T出现的位置或次数.假设S的长度为n, T的长度为m, 通过枚举S长度为m的字串的hash值与T的hash值比较.此时使用滚动hash的优化使复杂度不为O(mn). 算法说明 滚动h ...
- 创建一个git仓库
1.git init 使用git init命令初始化一个git仓库,git仓库会生成一个.git目录 git init 1.使用指定的目录作为我们的git仓库 git init newrepo 2.初 ...
- sql server 查询当前月份日期列表数据
SELECT ), ,) AS every_time, --日期 ,getdate())) ) AS Weekd --星期几 FROM master..spt_values n WHERE n.typ ...
- 使用IDEA时跳转到.class的解决办法
项目背景:jdk1.8 软件环境:IDEA 问题: 1. 两个不同的项目,在A项目中写了一个实体类.B项目中引用.在B项目中CTRL+鼠标左键点击进入,正常情况下是进入了源码文件,也就是.JAVA文件 ...
- SpringBoot报错:Failed to load ApplicationContext( Failed to bind properties under 'logging.level')
引起条件: SpringBoot2.0下yml文件配置SLF4j日志输出日志级别 logging: level: debug 解决方法: 指定系统包路径 logging: root: debug 指定 ...
- react-router路由地址变了页面却没有跳转的解决办法
最近,自己在摸索react的时候,遇到一个很奇葩的问题,大概是这样的: 我从列表页使用Link跳转到详情页面,列表页面的路由是'/list',详情页面的路由是'/list/detail',由于详情页面 ...
- 4.21Linux(2)
2019-4-21 22:46:55 今天买了阿里云服务器1年的 116大洋!!! 但是有个服务器感觉很爽!!!!Linux系统还是很有意思的!!!! 直接贴上笔记! 越努力,越幸运!永远不要高估自己 ...
- Springboot搭建SSM+JSP的web项目
Springboot搭建SSM+JSP的web项目 一:创建项目结构: 项目结构分为三个部分: 1 后端项目开发文件: 包: Util 工具包 Mapper db层 Serv ...
- win10上使用Xshell通过ssh连接Linux
Windows 10上现在能安装Linux子系统了,正好最近.Net Core也逐渐发展起来了,我也就在自己电脑上搞了一下 在Windows 10上安装Ubuntu的过程就不用说了,都是流程性的东西 ...