在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;