C # Aspose.Words word conversion and return to the specified multiple pictures

 

public ResultResponse<string[]> PrintStudyRecords([FromBody]StudyInfo info)
        {
            ResultResponse<string[]> ret = new ResultResponse<string[]>();
            if (info.ProjectId <= 0)
            {
                ret.Flag = false;
                return ret;
            }
            var project = ProjectContract.GetFilterProject(p => p.Id == info.ProjectId).FirstOrDefault();
            if (project == null)
            {
                ret.Flag = false;
                return ret;
            }
            var trainTarget = TrainTargetContract.GetFilterTrainTargets(t => t.Id == project.TrainTargetId).FirstOrDefault();
            var studyRecords = StudyRecordsContract.GetList(s => s.ProjectId == info.ProjectId && s.IsFinish && !s.IsDelete).ToList();
            Document doc;
            using (var stream = System.IO.File.OpenRead(HttpContext.Current.Server.MapPath("/Content/学时记录.docx")))
            {
                doc = new Document(stream);
            }
            doc.Range.Replace(string.Format("${0}$", "Name"), LoginUser.Name, false, false);
            doc.Range.Replace(string.Format("${0}$", "PersonalId"), LoginUser.PersonalId, false, false);
            doc.Range.Replace(string.Format("${0}$", "Sex"), string.IsNullOrWhiteSpace(LoginUser.Sex) ? "" : LoginUser.Sex, false, false);
            doc.Range.Replace(string.Format("${0}$", "PhoneNum"), string.IsNullOrWhiteSpace(LoginUser.Tel) ? "" : LoginUser.Tel, false, false);
            doc.Range.Replace(string.Format("${0}$", "Company"), string.IsNullOrWhiteSpace(project.Company) ? "" : project.Company, false, false);
            doc.Range.Replace(string.Format("${0}$", "TrainTargetName"), trainTarget != null ? trainTarget.TrainName : "", false, false);
            doc.Range.Replace(string.Format("${0}$", "AllHours"), studyRecords.Count().ToString(), false, false);
        // 2 refers to the index table, a total of three, I need to change the content of a third table Table tableRecord
= (Table)doc.GetChild(NodeType.Table, 2, true); if (tableRecord == null) { var op = new ImageSaveOptions(SaveFormat.Jpeg); op.PrettyFormat = true; ret.Result = new string[doc.PageCount]; for (var page = 0; page < doc.PageCount; page++) { op.PageIndex = page; using (var ms = new MemoryStream()) { doc.Save(ms, op); ret.Result[page] = "data:image/jpeg;base64," + Convert.ToBase64String(ms.GetBuffer()); } } ret.Flag = false; return ret; } if (studyRecords.Count() > 1) { for (int i = 0 ; I <studyRecords.Count - . 1 ; I ++ ) { // clone designated row Row = clonedRow (Row) tableRecord.LastRow.Clone ( to true ); // inserted into a cloning rows at the specified location tableRecord.AppendChild (clonedRow); } } var rowIndex = 0 ; ZhuJian.Entity.TrainManage.StudyRecord studyRecord = null ; for ( int I = 0 ; I <studyRecords.Count; I ++ ) { studyRecord = studyRecords[i]; rowIndex = i + 1; var row = tableRecord.Rows[rowIndex]; var cell0 = row.Cells[0]; Paragraph pg0 = new Paragraph(doc); pg0.AppendChild(new Run(doc, rowIndex + "")); cell0.RemoveAllChildren(); cell0.AppendChild(pg0); var cell1 = row.Cells[1]; Paragraph pg1 = new Paragraph(doc); pg1.AppendChild(new Run(doc, studyRecord.StartTime.ToString("yyyy/MM/dd") + "-" + (studyRecord.EndTime.HasValue ? studyRecord.EndTime.Value.ToString("yyyy/MM/dd") : ""))); cell1.RemoveAllChildren(); cell1.AppendChild(pg1); var cell2 = row.Cells[2]; Paragraph pg2 = new Paragraph(doc); pg2.AppendChild(new Run(doc, studyRecord.ResourceName)); cell2.RemoveAllChildren(); cell2.AppendChild(pg2); var cell3 = row.Cells[3]; Paragraph pg3 = new Paragraph(doc); pg3.AppendChild(new Run(doc, studyRecord.IsFinish ? "1" : "0")); cell3.RemoveAllChildren(); cell3.AppendChild(pg3); } var option = new ImageSaveOptions(SaveFormat.Jpeg); option.PrettyFormat = true; ret.Result = new string[doc.PageCount]; for (var page = 0; page < doc.PageCount; page++) { option.PageIndex = page; using (var ms = new MemoryStream()) { doc.Save(ms, option); ret.Result[page] = "data:image/jpeg;base64," + Convert.ToBase64String(ms.GetBuffer()); } } ret.Flag = true ; Return the right; }

 

 

Guess you like

Origin www.cnblogs.com/xiaonangua/p/11304504.html