Aspose.words DocumentBuilder 插入图片、文字到Word
插入图片、文字到Word
以下是指定插入到段落内,所以每次需要移到指定的段落,每创建一个段落需要+1
黄色背景的分别是插入图片和文字
private string DownLoadStudyImg(List<Project> projects) { var projectIds = projects.Select(p => p.Id).ToList(); var faceImgs = new List<FaceImg>(); if (faceImgs.Count <= 0) { return ""; } var dic = Server.MapPath("/UploadFile/识别照片/"); var zipPath = "/UploadFile/识别照片.zip"; var zipName = Server.MapPath(zipPath); if (System.IO.Directory.Exists(dic)) { System.IO.Directory.Delete(dic, true); } if (System.IO.File.Exists(zipName)) { System.IO.File.Delete(zipName); }foreach (var item in projects) { Aspose.Words.Document doc; Aspose.Words.Tables.Table tableRecordCommon = null;//表格 using (var stream = System.IO.File.OpenRead(Server.MapPath("/Content/培训照片.docx"))) { doc = new Aspose.Words.Document(stream); } tableRecordCommon = (Aspose.Words.Tables.Table)doc.GetChild(Aspose.Words.NodeType.Table, 0, true); tableRecordCommon.Style.Font.Size = 10; FaceImage img = null; var pNum = 0;//段落数 for (int i = 0; i < projectImgs.Count(); i++) { //默认有一行,所以此处少克隆一行 if (i < projectImgs.Count() - 1) { //克隆指定行 Aspose.Words.Tables.Row clonedRow = (Aspose.Words.Tables.Row)tableRecordCommon.LastRow.Clone(true); //在指定位置插入克隆行 tableRecordCommon.AppendChild(clonedRow); } img = projectImgs[i]; var row = tableRecordCommon.Rows[i]; var cell0 = row.Cells[0]; Aspose.Words.DocumentBuilder builder = null; builder = new Aspose.Words.DocumentBuilder(doc); builder.MoveToParagraph(pNum, 0); Shape shape = builder.InsertImage(img.Url, 30, 30); Aspose.Words.Paragraph pg0 = builder.CurrentParagraph; cell0.RemoveAllChildren(); cell0.AppendChild(pg0); pNum++; var cell1 = row.Cells[1]; builder.MoveToParagraph(pNum, 0); builder.Write(img.Url.Contains("Small") ? "微信小程序" : "PC网站"); Aspose.Words.Paragraph pg1 = builder.CurrentParagraph; cell1.RemoveAllChildren(); cell1.AppendChild(pg1); pNum++; var cell2 = row.Cells[2]; builder.MoveToParagraph(pNum, 0); builder.Write(img.Time.ToString("yyyy-MM-dd HH:mm:ss")); Aspose.Words.Paragraph pg2 = builder.CurrentParagraph; cell2.RemoveAllChildren(); cell2.AppendChild(pg2); pNum++; var cell3 = row.Cells[3]; builder.MoveToParagraph(pNum, 0); builder.Write("检测通过"); Aspose.Words.Paragraph pg3 = builder.CurrentParagraph; cell3.RemoveAllChildren(); cell3.AppendChild(pg3); pNum++; } doc.Save(dic + item.Name + ".pdf"); } SharpZip.PackFiles(zipName, dic, ""); if (System.IO.File.Exists(dic)) { System.IO.File.Delete(dic); } return zipPath; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2019-06-14 C# 下载模板
2017-06-14 HTML基础