I found out that the root cause is because of Table.SetEdge(int clm, int row, int clms, int rows, Edge edge, BorderStyle style, Unit width).
The error below will prompt during save the document as pdf when Table.SetEdge(int clm, int row, int clms, int rows, Edge edge, BorderStyle style, Unit width) is executed after i add the table to the section. I am wondering why?
System.NullReferenceException HResult=0x80004003 Message=Object reference not set to an instance of an object. Source=MigraDoc.DocumentObjectModel
Error Code: private void DefineTableForCustomerInfo() { // Add an empty paragraph for spacing. section.AddParagraph();
//Add a paragraph for the Customer Information tableCustomerInfo = section.AddTable(); tableCustomerInfo.Format.Font.Name = ReportDocumentFontName; // Font name tableCustomerInfo.Format.Font.Size = 9; // Adjust the font size as needed
// Create and populate the table for Customer Information CreateAndPopulateTable(tableCustomerInfo, 5, 7, tableCustomerInfo.Format.Font.Size.Centimeter, ColumnWidthCM, false);
//Predefine the column width double[] columnWidths = { 3.75, 0.25, 5.25, 0.5, 3.75, 0.25, 5.25 };
for (int i = 0; i < columnWidths.Length; i++) { FormatColumn(tableCustomerInfo.Rows[0].Cells[i], columnWidths[i]); } tableCustomerInfo.Rows[0].Cells[0].MergeRight = 6; tableCustomerInfo.Rows[0].Format.Font.Bold = true; tableCustomerInfo.Rows[0].Format.Alignment = ParagraphAlignment.Center; tableCustomerInfo.Rows[0].Cells[0].Shading.Color = Colors.Silver; tableCustomerInfo.Rows[0].Cells[0].AddParagraph("CUSTOMER INFORMATION");
tableCustomerInfo.SetEdge(0, 0, 1, 1, Edge.Box, BorderStyle.Single, 1); tableCustomerInfo.SetEdge(0, 0, tableCustomerInfo.Columns.Count, tableCustomerInfo.Rows.Count, Edge.Box, BorderStyle.Single, 1); }
Pass Code: private void DefineTableForCustomerInfo() { // Add an empty paragraph for spacing. section.AddParagraph();
//tableCustomerInfo = section.AddTable();
tableCustomerInfo = new Table(); tableCustomerInfo.Format.Font.Name = ReportDocumentFontName; // Font name tableCustomerInfo.Format.Font.Size = 9; // Adjust the font size as needed
// Create and populate the table for Customer Information CreateAndPopulateTable(tableCustomerInfo, 5, 7, tableCustomerInfo.Format.Font.Size.Centimeter, ColumnWidthCM, false);
//Predefine the column width double[] columnWidths = { 3.75, 0.25, 5.25, 0.5, 3.75, 0.25, 5.25 };
for (int i = 0; i < columnWidths.Length; i++) { FormatColumn(tableCustomerInfo.Rows[0].Cells[i], columnWidths[i]); }
tableCustomerInfo.SetEdge(0, 0, 1, 1, Edge.Box, BorderStyle.Single, 1); tableCustomerInfo.SetEdge(0, 0, tableCustomerInfo.Columns.Count, tableCustomerInfo.Rows.Count, Edge.Box, BorderStyle.Single, 1);
tableCustomerInfo.Rows[0].Cells[0].MergeRight = 6; tableCustomerInfo.Rows[0].Format.Font.Bold = true; tableCustomerInfo.Rows[0].Format.Alignment = ParagraphAlignment.Center; tableCustomerInfo.Rows[0].Cells[0].Shading.Color = Colors.Silver; tableCustomerInfo.Rows[0].Cells[0].AddParagraph("CUSTOMER INFORMATION");
section.Add(tableCustomerInfo);
}
|