Unplanned
Last Updated: 10 Jul 2024 05:38 by ADMIN
ADMIN
Deyan
Created on: 26 Apr 2016 10:20
Category: WordsProcessing
Type: Bug Report
5
WordsProcessing: ArgumentNullException is thrown when exporting table border without specified border color to PDF
When table with table border without color set is created (the color is null), and the document is exported to PDF, ArgumentNullException is thrown. 

Workaround: Explicitly set a color where the color is null.
private void PdfExport()
{
	var tables = this.document.EnumerateChildrenOfType<Table>();

	foreach (var table in tables)
	{
		TableBorders coloredClone = this.CopyTableBorders_SetColorWhenOmitted(table);
		table.Borders = coloredClone;
		
		using (Stream output = new FileStream(fileName, FileMode.OpenOrCreate))
		{
			provider.Export(this.document, output);
		}
	}
}

private TableBorders CopyTableBorders_SetColorWhenOmitted(Table table)
{
	var leftBorder = new Border(table.Borders.Left.Thickness,
				  table.Borders.Left.Style,
				  table.Borders.Left.Color ?? new ThemableColor(Colors.Transparent),
				  table.Borders.Left.Shadow,
				  table.Borders.Left.Frame,
				  table.Borders.Left.Spacing);

	var rightBorder = new Border(table.Borders.Right.Thickness,
				  table.Borders.Right.Style,
				  table.Borders.Right.Color ?? new ThemableColor(Colors.Transparent),
				  table.Borders.Right.Shadow,
				  table.Borders.Right.Frame,
				  table.Borders.Right.Spacing);

	var bottomBorder = new Border(table.Borders.Bottom.Thickness,
				table.Borders.Bottom.Style,
				table.Borders.Bottom.Color ?? new ThemableColor(Colors.Transparent),
				table.Borders.Bottom.Shadow,
				table.Borders.Bottom.Frame,
				table.Borders.Bottom.Spacing);

	var topBorder = new Border(table.Borders.Top.Thickness,
				table.Borders.Top.Style,
				table.Borders.Top.Color ?? new ThemableColor(Colors.Transparent),
				table.Borders.Top.Shadow,
				table.Borders.Top.Frame,
				table.Borders.Top.Spacing);

	var insideHorizontalBorder = new Border(table.Borders.InsideHorizontal.Thickness,
			 table.Borders.InsideHorizontal.Style,
			 table.Borders.InsideHorizontal.Color ?? new ThemableColor(Colors.Transparent),
			 table.Borders.InsideHorizontal.Shadow,
			 table.Borders.InsideHorizontal.Frame,
			 table.Borders.InsideHorizontal.Spacing);

	var insideVerticalBorder = new Border(table.Borders.InsideVertical.Thickness,
			table.Borders.InsideVertical.Style,
			table.Borders.InsideVertical.Color ?? new ThemableColor(Colors.Transparent),
			table.Borders.InsideVertical.Shadow,
			table.Borders.InsideVertical.Frame,
			table.Borders.InsideVertical.Spacing);

	var tableBorders = new TableBorders(leftBorder, topBorder, rightBorder, bottomBorder, insideHorizontalBorder, insideVerticalBorder);

	return tableBorders;
}
1 comment
ADMIN
Dess | Tech Support Engineer, Principal
Posted on: 10 Jul 2024 05:38

Hi,

I would like to add one more workaround in case the cells' border should be also specified:

        var stream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite);
        var formatProvider = new DocxFormatProvider();
        var document = formatProvider.Import(stream); 

                foreach (Section s in document.Sections)
                {
                    foreach (var b in s.Blocks)
                    {
                        Table t = b as Table;
                        if (t != null)
                        {
                            t.Borders = new TableBorders(new Border(1, BorderStyle.Single, new ThemableColor(Colors.Black)));
                            foreach (TableRow row in t.Rows)
                            {
                                foreach (TableCell cell in row.Cells)
                                {
                                    cell.Borders = new TableCellBorders(new Border(1, BorderStyle.Single, new ThemableColor(Colors.Black)));
                                }
                            }
                        }
                    }
                }

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.