项目场景:
提示:C# 使用DocX插件插入图片方法总结
最近做一个生成报告的项目,使用word生成报告,踩过很多坑后总结,还有不完善的地方以后补充。这里只是记录一下解决思路和内容。
使用DocX类库,没有文字替换为图片的功能,这里记录一下解决方案。
解决方案:
1、使用基本方法
/// <summary>
/// 创建一个具有超链接、图像和表的文档。
/// </summary>
/// <param name="path">文档保存路径</param>
/// <param name="imagePath">加载的图片路径</param>
public static void HyperlinksImagesTables(string path, string imagePath)
{
// 创建一个文档
using (var document = DocX.Create(path))
{
// 在文档中添加超链接。
var link = document.AddHyperlink("link", new Uri("http://www.google.com"));
// 在文档中添加一个表。
var table = document.AddTable(2, 2);
table.Design = TableDesign.ColorfulGridAccent2;
table.Alignment = Alignment.center;
table.Rows[0].Cells[0].Paragraphs[0].Append("1");
table.Rows[0].Cells[1].Paragraphs[0].Append("2");
table.Rows[1].Cells[0].Paragraphs[0].Append("3");
table.Rows[1].Cells[1].Paragraphs[0].Append("4");
var newRow = table.InsertRow(table.Rows[1]);
newRow.ReplaceText("4", "5");
// 将图像添加到文档中。
var image = document.AddImage(imagePath);
//创建一个图片(一个自定义视图的图像)。
var picture = image.CreatePicture();
picture.Rotation = 10;
picture.SetPictureShape(BasicShapes.cube);
// 在文档中插入一个新段落。
var title = document.InsertParagraph().Append("Test").FontSize(20).Font(new FontFamily("Comic Sans MS"));
title.Alignment = Alignment.center;
// 在文档中插入一个新段落。
var p1 = document.InsertParagraph();
// 附加内容到段落
p1.AppendLine("This line contains a ").Append("bold").Bold().Append(" word.");
p1.AppendLine("Here is a cool ").AppendHyperlink(link).Append(".");
p1.AppendLine();
p1.AppendLine("Check out this picture ").AppendPicture(picture).Append(" its funky don't you think?");
p1.AppendLine();
p1.AppendLine("Can you check this Table of figures for me?");
p1.AppendLine();
// 在第1段后插入表格。
p1.InsertTableAfterSelf(table);
// 在文档中插入一个新段落。
Paragraph p2 = document.InsertParagraph();
// 附加内容到段落。
p2.AppendLine("Is it correct?");
// 保存当前文档
document.Save();
}
}
2、表格定位插入图片
文档编辑成表格样式,然后通过表格的单元格定位,然后插入图片
document.ReplaceText("QRCode", "");
//加载替换的图片
System.Drawing.Image image = QRCode.GetQRCodeByZXingNet(mod.ReportCode, 100, 100);
var imagePath = tempFilePath + "\\" + mod.ReportCode + ".jpg";
image.Save(imagePath);
var images = document.AddImage(imagePath);
//创建一个图片(一个自定义视图的图像)。
var picture = images.CreatePicture();
Table tb = document.Tables[0];// 表示文档里的第一个表格
//tb.Rows[0].Cells[2].Paragraphs[0].Append("第一行第一列");
tb.Rows[0].Cells[2].Paragraphs[0].AppendPicture(picture);
File.Delete(imagePath);
3、定位段落添加图片
首先查找文档关键字,定位替换位置,然后插入图片。
document.ReplaceText("QRCode", "");
//加载替换的图片
System.Drawing.Image image = QRCode.GetQRCodeByZXingNet(mod.ReportCode, 100, 100);
var imagePath = tempFilePath + "\\" + mod.ReportCode + ".jpg";
image.Save(imagePath);
var images = document.AddImage(imagePath);
//创建一个图片(一个自定义视图的图像)。
var picture = images.CreatePicture();
var select = document.FindAll("QRCode");
//document.InsertParagraph().AppendPicture(picture);
Paragraph paragraph = document.Paragraphs[0];
var Position = paragraph.Position(document.FindAll("QRCode")[0]);
Position.Alignment = Alignment.right;
Position.AppendPicture(picture);
File.Delete(imagePath);