spire.doc的一些使用方法

在NuGet上安装’FreeSpire.Doc‘这个是免费版的,有一定限制

根据标签替换值:文档中的标签为’{下载时间}‘

//载入文档
var tagretFile="";//word文件路径
var pdf="";//pdf保存路径
Spire.Doc.Document document = new Spire.Doc.Document(tagretFile);
//下载时间
var dttime = DateTime.Now.ToLocalTime().ToString("yyyy/MM/dd HH:mm");
document.Replace("{下载时间}", dttime, false, false);

设置表格并向表格赋值:

设置单元格中的内容居中:(使用段落居中)

Spire.Doc.Documents.Paragraph paragraph = table[0, 0].AddParagraph();

//设置居中
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center;

//获取第一个节
Spire.Doc.Section section = document.Sections[0];
//获取第一个表格
Spire.Doc.Table table = section.Tables[0] as Spire.Doc.Table;
//插入一行到表格的第6行
table.Rows.Insert(5, table.AddRow());
//将第6行的第6个单元格拆分为3列2行      
table.Rows[5].Cells[5].SplitCell(3, 2);  
//设置行高
table.Rows[5].Height = 20;
//添加第1行第1个单元格
Spire.Doc.Documents.Paragraph paragraph = table[0, 0].AddParagraph();
TextRange range = paragraph.AppendText("文本内容");
//字体类型
range.CharacterFormat.FontName = "Century Gothic";
//字体大小
range.CharacterFormat.FontSize = 8;
//粗体
range.CharacterFormat.Bold = bl;
//设置居中
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center;
//设置表格列宽适应窗体,还有适应内容,列宽的。
table.AutoFit(AutoFitBehaviorType.AutoFitToWindow);
//保存文档
document.SaveToFile(tagretFile, FileFormat.Docx2013);
document.Close();


转pdf 


Spire.Doc.Document documentpdf = new Spire.Doc.Document();
documentpdf.LoadFromFile(tagretFile);
documentpdf.SaveToFile(pdf, FileFormat.PDF);
System.Diagnostics.Process.Start(pdf);

转base64方便下载 

//转base64
FileStream fsForRead = new FileStream(pdf, FileMode.Open);//文件路径
string base64Str = "";
fsForRead.Seek(0, SeekOrigin.Begin);
byte[] bs = new byte[fsForRead.Length];
int log = Convert.ToInt32(fsForRead.Length);
fsForRead.Read(bs, 0, log);
base64Str = Convert.ToBase64String(bs);
fsForRead.Close();
return base64Str;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值