0

我需要将包含一些数据的表格添加到现有的 Word 模板中,并且我正在使用 novacode-docx Lib。但是,有一个我找不到的问题。对于某些模板,一切正常,但对于其他模板,我遇到了错误。所以这是我的代码

//copying template file
File.Copy(Info.reportTemplatePath+reportTemplateName+".docx",(path + fileName));
var doc = DocX.Load(path + fileName);
Table tb = doc.AddTable(3,3);
//Table tb = doc.InsertTable(0, 3,3); 
Font font = new Font(new FontFamily("Times New Roman"), 12f,FontStyle.Bold);
tb.Alignment = Alignment.both;
tb.Paragraphs[0].Font(new FontFamily("Times New Roman"));

tb.Design = TableDesign.TableNormal;
tb.SetBorder(TableBorderType.Bottom, new Border(BorderStyle.Tcbs_none, 0,1,Color.Transparent));
tb.Rows[0].Cells[0].Paragraphs[0].Append("Patient Name: "+mirrorText(patientName) ).Alignment=Alignment.left; 
tb.Rows[0].Cells[1].Paragraphs[0].Append("DOB: " +DOB  );
tb.Rows[0].Cells[2].Paragraphs[0].Append("Visit Date: "+visitDate  ).Alignment = Alignment.right;
tb.Rows[1].Cells[0].Paragraphs[0].Append("Patient ID: " + patientID).Alignment=Alignment.left;
tb.Rows[1].Cells[2].Paragraphs[0].Append(" ");
tb.Rows[2].Cells[2].Paragraphs[0].Append("المحترم "+mirrorText(referredDoctor) + " الدكتور " + " :المرسل " + "الطبيب").Alignment = Alignment.right;
tb.Rows[0].Cells[0].Width = 330d;
tb.Rows[0].Cells[1].Width = 190d;
tb.Rows[0].Cells[2].Width = 220d;
tb.Rows[0].Cells[2].Paragraphs[0].FontSize(12);
tb.Rows[0].Cells[1].Paragraphs[0].FontSize(12);
tb.Rows[0].Cells[0].Paragraphs[0].FontSize(12);
tb.Rows[1].Cells[0].Paragraphs[0].FontSize(12);
tb.Rows[2].Cells[2].Paragraphs[0].FontSize(14);
tb.Rows[0].Cells[2].Paragraphs[0].Bold();
tb.Rows[0].Cells[1].Paragraphs[0].Bold();
tb.Rows[0].Cells[0].Paragraphs[0].Bold();
tb.Rows[1].Cells[0].Paragraphs[0].Bold();
tb.Rows[2].Cells[2].Paragraphs[0].Bold();
tb.Rows[0].Cells[2].Paragraphs[0].Font(new FontFamily("Times New Roman"));
tb.Rows[0].Cells[1].Paragraphs[0].Font(new FontFamily("Times New Roman"));
tb.Rows[0].Cells[0].Paragraphs[0].Font(new FontFamily("Times New Roman"));
tb.Rows[1].Cells[0].Paragraphs[0].Font(new FontFamily("Times New Roman"));
tb.Rows[2].Cells[2].Paragraphs[0].Font(new FontFamily("Times New Roman"));
tb.Rows[2].MergeCells(0, 2);
tb.Rows[2].Cells[0].RemoveParagraphAt(0);
tb.Rows[2].Cells[0].RemoveParagraphAt(0);

doc.InsertTable(0,tb); // I am getting the error here which is {"Object reference not set to an instance of an object."}
//   doc.InsertParagraph("المحترم " + mirrorText(referredDoctor) + " الدكتور " + " :المرسل " + "الطبيب", false, patientFormat).Alignment = Alignment.right;
doc.InsertParagraph(string.Empty, false);
doc.AddFooters();
doc.MarginBottom = 0.1f;
Footer footer = doc.Footers.odd;
Bitmap tempFoImg = Info.generateFooterImage();
//  System.Drawing.Image tempImg=(System.Drawing.Image)tempFoImg;

MemoryStream ms2 = new MemoryStream();
tempFoImg.Save(ms2, System.Drawing.Imaging.ImageFormat.Png);
ms2.Seek(0, SeekOrigin.Begin);
Novacode.Image img2 = doc.AddImage(ms2);
Picture footerImage = img2.CreatePicture();
Paragraph p4 = footer.InsertParagraph("", false).InsertPicture(footerImage, 0);
p4.Alignment = Novacode.Alignment.center;
doc.Save();
Process.Start(path + fileName);

错误位于此处: doc.InsertTable(0,tb); // {"Object reference not set to an instance of an object."} 我使用 0 将表格附加到文档的顶部,当我删除该位置时,它工作正常,有时我愿意使用不同的库。如果它解决了问题,并提前谢谢你

4

0 回答 0