Hi ,
I have added a watermark in my document but It got cut in some places of the text. I have added the image for reference.
//Get the table from Word document.
Table table = doc.Sections[0].Tables[0] as Table;
//Loop through table cell
foreach (TableRow row in table.Rows)
{
foreach (TableCell cell in row.Cells)
{
Color color = cell.CellFormat.BackColor;
if (color != null)
{
//Remove background
cell.CellFormat.ClearBackground();
}
}
} static void Main(string[] args)
{
Document doc = new Document();
doc.LoadFromFile("Sample.docx");
foreach (Section section in doc.Sections)
{
//Get the table from Word document.
foreach (Table table in section.Tables)
{
//Iterate through all the rows
foreach (TableRow row in table.Rows)
{
//Iterate over the cells in each row
foreach (TableCell cell in row.Cells)
{
//Whether there are nested tables
foreach (DocumentObject collection in cell.ChildObjects)
{
//A nested tables
if (collection is Table)
{
Table tables = (Table)collection;
foreach (TableRow rows in tables.Rows)
{
foreach (TableCell cells in rows.Cells)
{
ClearBackgroundColor(cells);
}
}
}
ClearBackgroundColor(cell);
}
}
}
}
InsertTextWatermark(section);
}
string filePath = "output.docx";
doc.SaveToFile(filePath,FileFormat.Docx2013);
}
private static void InsertTextWatermark(Section section)
{
TextWatermark txtWatermark = new TextWatermark();
txtWatermark.Text = "E-iceblue";
txtWatermark.FontSize = 95;
txtWatermark.Color = Color.Blue;
txtWatermark.Layout = WatermarkLayout.Diagonal;
txtWatermark.Semitransparent = true;
section.Document.Watermark = txtWatermark;
}
private static void ClearBackgroundColor(TableCell cell)
{
//Gets the cell background color
Color colors = cell.CellFormat.BackColor;
if (colors != null)
{
//Clear background color
cell.CellFormat.ClearBackground();
}
}