I'm getting an ArgumentOutOfRangeException(Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index) exception when saving a document to PDF. It appears to be related to merged cells but it doesn't happen in all cases of merged cells.
The below code will reproduce the problem:
- Code: Select all
using (Document doc = new Document())
{
Section section = doc.AddSection();
Table table = section.AddTable(true);
table.ResetCells(2, 3);
table.PreferredWidth = new PreferredWidth(WidthType.Percentage, 100);
table.TableFormat.LayoutType = LayoutType.Fixed;
TableRow row = table.Rows[0];
row.Cells[0].AddParagraph().AppendText("0:0");
row.Cells[0].SetCellWidth(100, CellWidthType.Percentage);
table.ApplyHorizontalMerge(0, 0, 2);
row = table.Rows[1];
row.Cells[0].AddParagraph().AppendText("1:0");
row.Cells[0].SetCellWidth(50, CellWidthType.Percentage);
row.Cells[1].AddParagraph().AppendText("1:1");
row.Cells[1].SetCellWidth(25, CellWidthType.Percentage);
row.Cells[2].AddParagraph().AppendText("1:2");
row.Cells[2].SetCellWidth(25, CellWidthType.Percentage);
string filename = Path.Combine(Path.GetTempPath(), "PdfTest.docx");
doc.SaveToFile(filename, FileFormat.Docx2013);
using (Process process = new Process())
{
process.StartInfo.FileName = filename;
process.Start();
}
filename = Path.ChangeExtension(filename, ".pdf");
doc.SaveToFile(filename, FileFormat.PDF);
using (Process process = new Process())
{
process.StartInfo.FileName = filename;
process.Start();
}
}
If I only have two columns in the table there is no error:
- Code: Select all
using (Document doc = new Document())
{
Section section = doc.AddSection();
Table table = section.AddTable(true);
table.ResetCells(2, 2);
table.PreferredWidth = new PreferredWidth(WidthType.Percentage, 100);
table.TableFormat.LayoutType = LayoutType.Fixed;
TableRow row = table.Rows[0];
row.Cells[0].AddParagraph().AppendText("0:0");
row.Cells[0].SetCellWidth(100, CellWidthType.Percentage);
table.ApplyHorizontalMerge(0, 0, 1);
row = table.Rows[1];
row.Cells[0].AddParagraph().AppendText("1:0");
row.Cells[0].SetCellWidth(50, CellWidthType.Percentage);
row.Cells[1].AddParagraph().AppendText("1:1");
row.Cells[1].SetCellWidth(50, CellWidthType.Percentage);
string filename = Path.Combine(Path.GetTempPath(), "PdfTest.docx");
doc.SaveToFile(filename, FileFormat.Docx2013);
using (Process process = new Process())
{
process.StartInfo.FileName = filename;
process.Start();
}
filename = Path.ChangeExtension(filename, ".pdf");
doc.SaveToFile(filename, FileFormat.PDF);
using (Process process = new Process())
{
process.StartInfo.FileName = filename;
process.Start();
}
}
Thanks,
Mark